From 9faa2740a58d4bd9d7b6c4cb1ecc8c261c5444af Mon Sep 17 00:00:00 2001 From: Steve Kondik Date: Wed, 14 Sep 2016 01:37:29 -0700 Subject: [PATCH] cmsdk: Add ListPreference helpers backed by CMSettings Change-Id: Ide607a4ba3b220d82aff5019f61ecdc7afe0a523 --- api/cm_current.txt | 18 ++++++ .../CMSecureSettingListPreference.java | 64 +++++++++++++++++++ .../CMSystemSettingListPreference.java | 64 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 sdk/src/java/cyanogenmod/preference/CMSecureSettingListPreference.java create mode 100644 sdk/src/java/cyanogenmod/preference/CMSystemSettingListPreference.java diff --git a/api/cm_current.txt b/api/cm_current.txt index 4a968d1b..d8910560 100644 --- a/api/cm_current.txt +++ b/api/cm_current.txt @@ -791,6 +791,15 @@ package cyanogenmod.preference { method protected boolean persistBoolean(boolean); } + public class CMSecureSettingListPreference extends ListPreference { + ctor public CMSecureSettingListPreference(android.content.Context, android.util.AttributeSet, int); + ctor public CMSecureSettingListPreference(android.content.Context, android.util.AttributeSet); + method public int getIntValue(int); + method protected java.lang.String getPersistedString(java.lang.String); + method protected boolean isPersisted(); + method protected boolean persistString(java.lang.String); + } + public class CMSecureSettingSwitchPreference extends SwitchPreference { ctor public CMSecureSettingSwitchPreference(android.content.Context, android.util.AttributeSet, int); ctor public CMSecureSettingSwitchPreference(android.content.Context, android.util.AttributeSet); @@ -800,6 +809,15 @@ package cyanogenmod.preference { method protected boolean persistBoolean(boolean); } + public class CMSystemSettingListPreference extends ListPreference { + ctor public CMSystemSettingListPreference(android.content.Context, android.util.AttributeSet, int); + ctor public CMSystemSettingListPreference(android.content.Context, android.util.AttributeSet); + method public int getIntValue(int); + method protected java.lang.String getPersistedString(java.lang.String); + method protected boolean isPersisted(); + method protected boolean persistString(java.lang.String); + } + public class CMSystemSettingSwitchPreference extends SwitchPreference { ctor public CMSystemSettingSwitchPreference(android.content.Context, android.util.AttributeSet, int); ctor public CMSystemSettingSwitchPreference(android.content.Context, android.util.AttributeSet); diff --git a/sdk/src/java/cyanogenmod/preference/CMSecureSettingListPreference.java b/sdk/src/java/cyanogenmod/preference/CMSecureSettingListPreference.java new file mode 100644 index 00000000..307a821d --- /dev/null +++ b/sdk/src/java/cyanogenmod/preference/CMSecureSettingListPreference.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2016 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 cyanogenmod.preference; + +import android.content.Context; +import android.support.v7.preference.ListPreference; +import android.util.AttributeSet; + +import cyanogenmod.providers.CMSettings; + + +public class CMSecureSettingListPreference extends ListPreference { + public CMSecureSettingListPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public CMSecureSettingListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected boolean persistString(String value) { + if (shouldPersist()) { + if (value == getPersistedString(null)) { + // It's already there, so the same as persisting + return true; + } + CMSettings.Secure.putString(getContext().getContentResolver(), getKey(), value); + return true; + } + return false; + } + + @Override + protected String getPersistedString(String defaultReturnValue) { + if (!shouldPersist()) { + return defaultReturnValue; + } + String value = CMSettings.Secure.getString(getContext().getContentResolver(), getKey()); + return value == null ? defaultReturnValue : value; + } + + @Override + protected boolean isPersisted() { + return CMSettings.Secure.getString(getContext().getContentResolver(), getKey()) != null; + } + + public int getIntValue(int defValue) { + return getValue() == null ? defValue : Integer.valueOf(getValue()); + } +} diff --git a/sdk/src/java/cyanogenmod/preference/CMSystemSettingListPreference.java b/sdk/src/java/cyanogenmod/preference/CMSystemSettingListPreference.java new file mode 100644 index 00000000..ff19b04d --- /dev/null +++ b/sdk/src/java/cyanogenmod/preference/CMSystemSettingListPreference.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2016 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 cyanogenmod.preference; + +import android.content.Context; +import android.support.v7.preference.ListPreference; +import android.util.AttributeSet; + +import cyanogenmod.providers.CMSettings; + + +public class CMSystemSettingListPreference extends ListPreference { + public CMSystemSettingListPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public CMSystemSettingListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected boolean persistString(String value) { + if (shouldPersist()) { + if (value == getPersistedString(null)) { + // It's already there, so the same as persisting + return true; + } + CMSettings.System.putString(getContext().getContentResolver(), getKey(), value); + return true; + } + return false; + } + + @Override + protected String getPersistedString(String defaultReturnValue) { + if (!shouldPersist()) { + return defaultReturnValue; + } + String value = CMSettings.System.getString(getContext().getContentResolver(), getKey()); + return value == null ? defaultReturnValue : value; + } + + @Override + protected boolean isPersisted() { + return CMSettings.System.getString(getContext().getContentResolver(), getKey()) != null; + } + + public int getIntValue(int defValue) { + return getValue() == null ? defValue : Integer.valueOf(getValue()); + } +}