OmniGears: make easy to add a checkbox preference
based from Cyanogenmod, but im adding more preferences :D
use SystemCheckBoxPreference if you want adding option for Settings.System
use SecureCheckBoxPreference if you want adding option for Settings.Secure
use GlobalCheckBoxPreference if you want adding option for Settings.Global
example:
we always using like this
<CheckBoxPreference
android:key="soft_back_kill_app"
android:title="@string/soft_back_kill_app_title"
android:summary="@string/soft_back_kill_app_summary"
android:persistent="false"/>
and put preference into java code
but with this patch, we can use like this
<org.omnirom.omnigears.preference.SystemCheckBoxPreference
android:key="soft_back_kill_app_enable" << put key from android.provider.Settings
android:title="@string/soft_back_kill_app_title"
android:summary="@string/soft_back_kill_app_summary" />
the preference will always use presistent command
no need to add this preference into java code again :)
and say goodbye to bad java code style xD
will make all checkbox preference using this, if merged :)
depends on https://gerrit.omnirom.org/#/c/11977/ :)
Change-Id: Ied880d49ccc60c1e02615eccc6d01ddbd30bddf7
(cherry picked from commit bf86799feaebf524c637816e727cc2f9b2932cfb)
diff --git a/src/org/omnirom/omnigears/preference/GlobalCheckBoxPreference.java b/src/org/omnirom/omnigears/preference/GlobalCheckBoxPreference.java
new file mode 100644
index 0000000..0ba460f
--- /dev/null
+++ b/src/org/omnirom/omnigears/preference/GlobalCheckBoxPreference.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 The CyanogenMod 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.
+ */
+
+package org.omnirom.omnigears.preference;
+
+import android.content.Context;
+import android.preference.CheckBoxPreference;
+import android.provider.Settings;
+import android.util.AttributeSet;
+
+public class GlobalCheckBoxPreference extends CheckBoxPreference {
+ public GlobalCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public GlobalCheckBoxPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public GlobalCheckBoxPreference(Context context) {
+ super(context, null);
+ }
+
+ @Override
+ protected boolean persistBoolean(boolean value) {
+ if (shouldPersist()) {
+ if (value == getPersistedBoolean(!value)) {
+ // It's already there, so the same as persisting
+ return true;
+ }
+
+ Settings.Global.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected boolean getPersistedBoolean(boolean defaultReturnValue) {
+ if (!shouldPersist()) {
+ return defaultReturnValue;
+ }
+
+ return Settings.Global.getInt(getContext().getContentResolver(),
+ getKey(), defaultReturnValue ? 1 : 0) != 0;
+ }
+
+ @Override
+ protected boolean isPersisted() {
+ // Using getString instead of getInt so we can simply check for null
+ // instead of catching an exception. (All values are stored as strings.)
+ return Settings.Global.getString(getContext().getContentResolver(), getKey()) != null;
+ }
+}
diff --git a/src/org/omnirom/omnigears/preference/SecureCheckBoxPreference.java b/src/org/omnirom/omnigears/preference/SecureCheckBoxPreference.java
new file mode 100644
index 0000000..8f4e3f9
--- /dev/null
+++ b/src/org/omnirom/omnigears/preference/SecureCheckBoxPreference.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 The CyanogenMod 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.
+ */
+
+package org.omnirom.omnigears.preference;
+
+import android.content.Context;
+import android.preference.CheckBoxPreference;
+import android.provider.Settings;
+import android.util.AttributeSet;
+
+public class SecureCheckBoxPreference extends CheckBoxPreference {
+ public SecureCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public SecureCheckBoxPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public SecureCheckBoxPreference(Context context) {
+ super(context, null);
+ }
+
+ @Override
+ protected boolean persistBoolean(boolean value) {
+ if (shouldPersist()) {
+ if (value == getPersistedBoolean(!value)) {
+ // It's already there, so the same as persisting
+ return true;
+ }
+
+ Settings.Secure.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected boolean getPersistedBoolean(boolean defaultReturnValue) {
+ if (!shouldPersist()) {
+ return defaultReturnValue;
+ }
+
+ return Settings.Secure.getInt(getContext().getContentResolver(),
+ getKey(), defaultReturnValue ? 1 : 0) != 0;
+ }
+
+ @Override
+ protected boolean isPersisted() {
+ // Using getString instead of getInt so we can simply check for null
+ // instead of catching an exception. (All values are stored as strings.)
+ return Settings.Secure.getString(getContext().getContentResolver(), getKey()) != null;
+ }
+}
diff --git a/src/org/omnirom/omnigears/preference/SystemCheckBoxPreference.java b/src/org/omnirom/omnigears/preference/SystemCheckBoxPreference.java
new file mode 100644
index 0000000..8a099e7
--- /dev/null
+++ b/src/org/omnirom/omnigears/preference/SystemCheckBoxPreference.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2013 The CyanogenMod 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.
+ */
+
+package org.omnirom.omnigears.preference;
+
+import android.content.Context;
+import android.preference.CheckBoxPreference;
+import android.provider.Settings;
+import android.util.AttributeSet;
+
+public class SystemCheckBoxPreference extends CheckBoxPreference {
+ public SystemCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public SystemCheckBoxPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public SystemCheckBoxPreference(Context context) {
+ super(context, null);
+ }
+
+ @Override
+ protected boolean persistBoolean(boolean value) {
+ if (shouldPersist()) {
+ if (value == getPersistedBoolean(!value)) {
+ // It's already there, so the same as persisting
+ return true;
+ }
+
+ Settings.System.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected boolean getPersistedBoolean(boolean defaultReturnValue) {
+ if (!shouldPersist()) {
+ return defaultReturnValue;
+ }
+
+ return Settings.System.getInt(getContext().getContentResolver(),
+ getKey(), defaultReturnValue ? 1 : 0) != 0;
+ }
+
+ @Override
+ protected boolean isPersisted() {
+ // Using getString instead of getInt so we can simply check for null
+ // instead of catching an exception. (All values are stored as strings.)
+ return Settings.System.getString(getContext().getContentResolver(), getKey()) != null;
+ }
+}