diff options
| author | Dirk Rettschlag <dirk.rettschlag@gmail.com> | 2014-06-27 22:44:23 +0200 |
|---|---|---|
| committer | LorDClockaN <davor@losinj.com> | 2014-07-08 12:56:40 +0200 |
| commit | e00b42a3019a068ff0a69c04e92af30b1f0127e9 (patch) | |
| tree | 76cf2f9d6312f834e79e67731b96ffe1c593bd5a | |
| parent | cc0892202f75a97492babf037b45dedfd5f235e9 (diff) | |
Add SwitchSetting
alternative to the CheckboxSetting
Change-Id: Ia596401c6865a82c27c2f192be9c8453d92895bf
Signed-off-by: Dirk Rettschlag <dirk.rettschlag@gmail.com>
| -rw-r--r-- | res/layout/setting_switch.xml | 18 | ||||
| -rw-r--r-- | src/com/aokp/romcontrol/settings/SwitchSetting.java | 110 |
2 files changed, 128 insertions, 0 deletions
diff --git a/res/layout/setting_switch.xml b/res/layout/setting_switch.xml new file mode 100644 index 0000000..7c03408 --- /dev/null +++ b/res/layout/setting_switch.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2006 The Android Open Source Project Licensed under the + Apache License, Version 2.0 (the "License"); you may not use this file except + in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed + under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + OR CONDITIONS OF ANY KIND, either express or implied. See the License for + the specific language governing permissions and limitations under the License. --> +<merge xmlns:android="http://schemas.android.com/apk/res/android"> + + <Switch + android:id="@+id/the_switch" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:focusable="false" + android:clickable="false" + /> +</merge> diff --git a/src/com/aokp/romcontrol/settings/SwitchSetting.java b/src/com/aokp/romcontrol/settings/SwitchSetting.java new file mode 100644 index 0000000..ad0170b --- /dev/null +++ b/src/com/aokp/romcontrol/settings/SwitchSetting.java @@ -0,0 +1,110 @@ +package com.aokp.romcontrol.settings; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Switch; +import com.aokp.romcontrol.R; + +/** + * Setting toggle which represents a boolean value + * <p/> + * <ul><b>Supported attributes (in addition to {@link BaseSetting} attributes)</b> + * <li>aokp:descriptionOn - a @string reference, which will be set as the summary when enabled. + * <li>aokp:descriptionOff - a @string reference, which will be set as the summary when disabled. + * </ul> + */ +public class SwitchSetting extends BaseSetting implements OnClickListener { + + Switch mSwitch; + + String aDescriptionOn, aDescriptionOff; + + Boolean mChecked = false; + + + public SwitchSetting(Context context) { + this(context, null); + } + + public SwitchSetting(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SwitchSetting(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + if (attrs != null) { + + TypedArray typedArray = null; + try { + typedArray = context.obtainStyledAttributes(attrs, R.styleable.CheckboxSetting); + + mChecked = Boolean.parseBoolean(getDefaultValue()); + aDescriptionOn = typedArray.getString(R.styleable.CheckboxSetting_descriptionOn); + aDescriptionOff = typedArray.getString(R.styleable.CheckboxSetting_descriptionOff); + } finally { + if (typedArray != null) { + typedArray.recycle(); + } + } + } + + /** + * Inflate Views + */ + addView(View.inflate(context, R.layout.setting_switch, mRootView)); + mSwitch = (Switch) findViewById(R.id.the_switch); + + + /** + * Setup initial logic + */ + updateSummary(); + + // The default value of a boolean setting is usually stored as 1 or 0, but support "true" and "false" values + if (getValue() == null && getDefaultValue() != null) { + + // if this key is not present in the table, try to use the defualt value supplied + mChecked = Boolean.valueOf(getDefaultValue()) || getDefaultValue().equals("1"); + + } else if (getValue() != null) { + + mChecked = getValue().equals("1"); + + } + mSwitch.setChecked(mChecked); + + setOnClickListener(this); + setFocusable(true); + } + + private void updateSummary() { + if (getCurrentSummary() == null) { + // no summary is set, so let's use the descriptions if we can + if (aDescriptionOff != null || aDescriptionOn != null) { + setSummary(isChecked() ? aDescriptionOn : aDescriptionOff); + } else { + setSummary(null); + } + } + } + + @Override + public void onClick(View v) { + setChecked(!isChecked()); + updateSummary(); + } + + public void setChecked(boolean checked) { + mChecked = checked; + mSwitch.setChecked(checked); + setValue(checked ? "1" : "0"); + } + + public boolean isChecked() { + return mChecked; + } +} |
