diff options
author | Bruno Martins <bgcngm@gmail.com> | 2017-03-30 22:45:48 +0100 |
---|---|---|
committer | Davide Garberi <dade.garberi@gmail.com> | 2017-12-04 09:01:04 +0100 |
commit | 83358debe4826aabca5e535e1635b6b2086d7e64 (patch) | |
tree | 2f3b3e73477f774ab43565be155dd36fb46127df /doze | |
parent | ed1f601ff074e80c50fc64df939a921fa649bf16 (diff) |
msm8996-common: Doze: Refactor
* Fix Ambient Display's switchbar on/off text.
* Get rid of unnecessary receiver and all its related code,
permissions and meta-data.
* Keep the service running only if Doze is enabled system-wide along
with at least one of the additional Ambient Display features.
Change-Id: Id1f7f5e099f289a20f5757b7869e297e0ce74f7a
Signed-off-by: Davide Garberi <dade.garberi@gmail.com>
Diffstat (limited to 'doze')
-rw-r--r-- | doze/AndroidManifest.xml | 32 | ||||
-rw-r--r-- | doze/res/layout/switch_bar.xml | 3 | ||||
-rw-r--r-- | doze/res/values/styles.xml | 14 | ||||
-rw-r--r-- | doze/src/com/cyanogenmod/settings/doze/DozeReceiver.java | 55 | ||||
-rw-r--r-- | doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java (renamed from doze/src/com/cyanogenmod/settings/doze/DozeSettings.java) | 19 | ||||
-rw-r--r-- | doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java | 106 | ||||
-rw-r--r-- | doze/src/com/cyanogenmod/settings/doze/Utils.java | 21 |
7 files changed, 78 insertions, 172 deletions
diff --git a/doze/AndroidManifest.xml b/doze/AndroidManifest.xml index 4470e1b..5dfde01 100644 --- a/doze/AndroidManifest.xml +++ b/doze/AndroidManifest.xml @@ -1,4 +1,20 @@ <?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2015-2016 The CyanogenMod Project + 2017 The LineageOS 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. +--> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cyanogenmod.settings.doze" android:versionCode="1" @@ -7,7 +23,6 @@ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.WAKE_LOCK" /> - <uses-permission android:name="cyanogenmod.permission.MANAGE_REMOTE_PREFERENCES" /> <protected-broadcast android:name="com.android.systemui.doze.pulse" /> @@ -19,32 +34,25 @@ android:label="@string/device_settings_app_name" android:persistent="true"> - <receiver android:name="com.cyanogenmod.settings.doze.DozeReceiver"> + <receiver android:name=".BootCompletedReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> - <action android:name="cyanogenmod.intent.action.UPDATE_PREFERENCE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> - <service android:name="com.cyanogenmod.settings.doze.DozeService" + <service android:name=".DozeService" android:permission="ZukDozeService"> </service> <activity - android:name=".DozeSettings" + android:name=".DozeSettingsActivity" android:label="@string/ambient_display_title" - android:theme="@style/DozeSettings"> + android:theme="@style/Theme.Main"> <intent-filter> <action android:name="org.cyanogenmod.settings.device.DOZE_SETTINGS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> - <meta-data - android:name="org.cyanogenmod.settings.summary.receiver" - android:value="com.cyanogenmod.settings.doze.DozeReceiver" /> - <meta-data - android:name="org.cyanogenmod.settings.summary.key" - android:value="doze_device_settings" /> </activity> </application> diff --git a/doze/res/layout/switch_bar.xml b/doze/res/layout/switch_bar.xml index f98de96..d25948b 100644 --- a/doze/res/layout/switch_bar.xml +++ b/doze/res/layout/switch_bar.xml @@ -34,8 +34,7 @@ android:ellipsize="end" android:textAppearance="@android:style/TextAppearance.Material.Title" android:textColor="?android:attr/textColorPrimaryInverse" - android:textAlignment="viewStart" - android:text="@string/switch_bar_on" /> + android:textAlignment="viewStart" /> <Switch android:id="@android:id/switch_widget" diff --git a/doze/res/values/styles.xml b/doze/res/values/styles.xml index 78d2d59..f64a2b8 100644 --- a/doze/res/values/styles.xml +++ b/doze/res/values/styles.xml @@ -1,5 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> -<!-- Copyright (C) 2016 The CyanogenMod Project +<!-- + Copyright (C) 2015-2016 The CyanogenMod Project + 2017 The LineageOS Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,13 +16,8 @@ limitations under the License. --> <resources xmlns:android="http://schemas.android.com/apk/res/android"> - <style name="DozeSettings" parent="@android:style/Theme.Material.Settings"> - <item name="android:windowActionBar">false</item> - <item name="preferenceTheme">@style/DozePreferenceTheme</item> - </style> - - <style name="DozePreferenceTheme" parent="@android:style/Theme.Material.Settings"> - <item name="dropdownPreferenceStyle">@style/Preference.DropDown.Material</item> + <style name="Theme.Main" parent="@android:style/Theme.Material.Settings"> + <item name="preferenceTheme">@android:style/Theme.Material.Settings</item> </style> <style name="ThemeOverlay.SwitchBar" parent="@android:style/ThemeOverlay"> @@ -30,5 +27,4 @@ <style name="ThemeOverlay.SwitchBar.Secondary" parent="@android:style/ThemeOverlay"> <item name="android:colorAccent">@color/system_secondary_color</item> </style> - </resources> diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeReceiver.java b/doze/src/com/cyanogenmod/settings/doze/DozeReceiver.java deleted file mode 100644 index ee92ab4..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/DozeReceiver.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2016 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS 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 com.cyanogenmod.settings.doze; - -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import cyanogenmod.preference.RemotePreferenceUpdater; - -public class DozeReceiver extends RemotePreferenceUpdater { - - private static final boolean DEBUG = false; - private static final String TAG = "XiaomiDoze"; - - private static final String DOZE_CATEGORY_KEY = "doze_device_settings"; - - @Override - public void onReceive(Context context, Intent intent) { - super.onReceive(context, intent); - - if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - if (Utils.isDozeEnabled(context) && Utils.sensorsEnabled(context)) { - if (DEBUG) Log.d(TAG, "Starting service"); - Utils.startService(context); - } - } - } - - @Override - public String getSummary(Context context, String key) { - if (DOZE_CATEGORY_KEY.equals(key)) { - return DozeSettingsFragment.getDozeSummary(context); - } - return null; - } - - static void notifyChanged(Context context) { - notifyChanged(context, DOZE_CATEGORY_KEY); - } -} diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java index 603e5ef..7e3620a 100644 --- a/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java +++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java @@ -1,6 +1,6 @@ /* - * Copyright (C) 2016 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS Project + * Copyright (C) 2015-2016 The CyanogenMod Project + * 2017 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,15 +14,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.cyanogenmod.settings.doze; -import android.app.Activity; import android.os.Bundle; import android.view.MenuItem; import com.android.settingslib.drawer.SettingsDrawerActivity; -public class DozeSettings extends SettingsDrawerActivity { +public class DozeSettingsActivity extends SettingsDrawerActivity { private static final String TAG_DOZE = "doze"; @@ -31,17 +31,18 @@ public class DozeSettings extends SettingsDrawerActivity { super.onCreate(savedInstanceState); setContentView(R.layout.doze); - + getActionBar().setDisplayHomeAsUpEnabled(true); getFragmentManager().beginTransaction().replace(R.id.content_frame, new DozeSettingsFragment(), TAG_DOZE).commit(); } @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - onBackPressed(); - return true; + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; } - return false; + return super.onOptionsItemSelected(item); } } diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java index 8b56e67..6aff232 100644 --- a/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java +++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2015 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS Project + * 2017 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package com.cyanogenmod.settings.doze; -import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -37,95 +36,60 @@ import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.Switch; +import android.widget.TextView; public class DozeSettingsFragment extends PreferenceFragment implements OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener { - private SharedPreferences mPreferences; + private TextView mTextView; private SwitchPreference mPickUpPreference; private SwitchPreference mHandwavePreference; private SwitchPreference mPocketPreference; - private ContentObserver mDozeObserver = new ContentObserver(new Handler()) { - @Override - public void onChange(boolean selfChange) { - super.onChange(selfChange); - - updateSwitches(Utils.isDozeEnabled(getActivity())); - DozeReceiver.notifyChanged(getActivity()); - } - }; - - static String getDozeSummary(Context context) { - if (Utils.isDozeEnabled(context)) { - return context.getString(R.string.ambient_display_summary_on); - } - return context.getString(R.string.ambient_display_summary_off); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - final View view = LayoutInflater.from(getContext()).inflate(R.layout.doze, container, false); - ((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState)); - return view; - } - @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.doze_settings); - // get shared preference - mPreferences = getActivity().getSharedPreferences("doze_settings", Activity.MODE_PRIVATE); - - if (savedInstanceState == null && !mPreferences.getBoolean("first_help_shown", false)) { + SharedPreferences prefs = getActivity().getSharedPreferences("doze_settings", + Activity.MODE_PRIVATE); + if (savedInstanceState == null && !prefs.getBoolean("first_help_shown", false)) { showHelp(); } + boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + mPickUpPreference = (SwitchPreference) findPreference(Utils.GESTURE_PICK_UP_KEY); - mPickUpPreference.setOnPreferenceChangeListener(this); + mPickUpPreference.setEnabled(dozeEnabled); mHandwavePreference = (SwitchPreference) findPreference(Utils.GESTURE_HAND_WAVE_KEY); - mHandwavePreference.setOnPreferenceChangeListener(this); + mHandwavePreference.setEnabled(dozeEnabled); mPocketPreference = (SwitchPreference) findPreference(Utils.GESTURE_POCKET_KEY); - mPocketPreference.setOnPreferenceChangeListener(this); + mPocketPreference.setEnabled(dozeEnabled); } @Override - public void onResume() { - super.onResume(); - getActivity().getContentResolver().registerContentObserver( - Utils.DOZE_ENABLED_URI, false, mDozeObserver); - updateSwitches(Utils.isDozeEnabled(getActivity())); - } - - @Override - public void onPause() { - super.onPause(); - getActivity().getContentResolver().unregisterContentObserver(mDozeObserver); - } - - private void updateSwitches(boolean enabled) { - mPickUpPreference.setEnabled(enabled); - mHandwavePreference.setEnabled(enabled); - mPocketPreference.setEnabled(enabled); + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + final View view = LayoutInflater.from(getContext()).inflate(R.layout.doze, container, false); + ((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState)); + return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + + mTextView = (TextView) view.findViewById(R.id.switch_text); + mTextView.setText(getString(dozeEnabled ? + R.string.switch_bar_on : R.string.switch_bar_off)); + View switchBar = view.findViewById(R.id.switch_bar); Switch switchWidget = (Switch) switchBar.findViewById(android.R.id.switch_widget); - switchWidget.setChecked(Utils.isDozeEnabled(getActivity())); + switchWidget.setChecked(dozeEnabled); switchWidget.setOnCheckedChangeListener(this); switchBar.setOnClickListener(new View.OnClickListener() { @@ -136,31 +100,25 @@ public class DozeSettingsFragment extends PreferenceFragment implements OnPrefer }); } - @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - final String key = preference.getKey(); - final boolean value = (Boolean) newValue; - if (Utils.GESTURE_PICK_UP_KEY.equals(key)) { - mPickUpPreference.setChecked(value); - } else if (Utils.GESTURE_HAND_WAVE_KEY.equals(key)) { - mHandwavePreference.setChecked(value); - } else if (Utils.GESTURE_POCKET_KEY.equals(key)) { - mPocketPreference.setChecked(value); - } else { - return false; - } - - Utils.startService(getActivity()); + Utils.checkDozeService(getActivity()); return true; } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { Utils.enableDoze(b, getActivity()); + Utils.checkDozeService(getActivity()); + + mTextView.setText(getString(b ? R.string.switch_bar_on : R.string.switch_bar_off)); + + mPickUpPreference.setEnabled(b); + mHandwavePreference.setEnabled(b); + mPocketPreference.setEnabled(b); } - public static class HelpDialogFragment extends DialogFragment { + private static class HelpDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) diff --git a/doze/src/com/cyanogenmod/settings/doze/Utils.java b/doze/src/com/cyanogenmod/settings/doze/Utils.java index 7e13eb1..65f1d99 100644 --- a/doze/src/com/cyanogenmod/settings/doze/Utils.java +++ b/doze/src/com/cyanogenmod/settings/doze/Utils.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (C) 2017 The LineageOS Project + * 2017 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ package com.cyanogenmod.settings.doze; import android.content.Context; import android.content.Intent; -import android.net.Uri; import android.os.UserHandle; import android.support.v7.preference.PreferenceManager; import android.provider.Settings; @@ -38,8 +37,6 @@ public final class Utils { protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave"; protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; - public static final Uri DOZE_ENABLED_URI = Settings.Secure.getUriFor(DOZE_ENABLED); - protected static void startService(Context context) { if (DEBUG) Log.d(TAG, "Starting service"); context.startServiceAsUser(new Intent(context, DozeService.class), @@ -52,20 +49,22 @@ public final class Utils { UserHandle.CURRENT); } + protected static void checkDozeService(Context context) { + if (isDozeEnabled(context) && sensorsEnabled(context)) { + startService(context); + } else { + stopService(context); + } + } + protected static boolean isDozeEnabled(Context context) { return Settings.Secure.getInt(context.getContentResolver(), DOZE_ENABLED, 1) != 0; } protected static boolean enableDoze(boolean enable, Context context) { - boolean dozeEnabled = Settings.Secure.putInt(context.getContentResolver(), + return Settings.Secure.putInt(context.getContentResolver(), DOZE_ENABLED, enable ? 1 : 0); - if (enable) { - startService(context); - } else { - stopService(context); - } - return dozeEnabled; } protected static void launchDozePulse(Context context) { |