From 83358debe4826aabca5e535e1635b6b2086d7e64 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Thu, 30 Mar 2017 22:45:48 +0100 Subject: 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 --- doze/AndroidManifest.xml | 32 ++++--- doze/res/layout/switch_bar.xml | 3 +- doze/res/values/styles.xml | 14 +-- .../cyanogenmod/settings/doze/DozeReceiver.java | 55 ----------- .../cyanogenmod/settings/doze/DozeSettings.java | 47 --------- .../settings/doze/DozeSettingsActivity.java | 48 ++++++++++ .../settings/doze/DozeSettingsFragment.java | 106 +++++++-------------- doze/src/com/cyanogenmod/settings/doze/Utils.java | 21 ++-- 8 files changed, 116 insertions(+), 210 deletions(-) delete mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeReceiver.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeSettings.java create mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java (limited to 'doze') 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 @@ + - @@ -19,32 +34,25 @@ android:label="@string/device_settings_app_name" android:persistent="true"> - + - - + android:theme="@style/Theme.Main"> - - 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" /> - - - - - 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/DozeSettings.java deleted file mode 100644 index 603e5ef..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java +++ /dev/null @@ -1,47 +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.app.Activity; -import android.os.Bundle; -import android.view.MenuItem; - -import com.android.settingslib.drawer.SettingsDrawerActivity; - -public class DozeSettings extends SettingsDrawerActivity { - - private static final String TAG_DOZE = "doze"; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.doze); - - 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; - } - return false; - } -} diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java new file mode 100644 index 0000000..7e3620a --- /dev/null +++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java @@ -0,0 +1,48 @@ +/* + * 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. + */ + +package com.cyanogenmod.settings.doze; + +import android.os.Bundle; +import android.view.MenuItem; + +import com.android.settingslib.drawer.SettingsDrawerActivity; + +public class DozeSettingsActivity extends SettingsDrawerActivity { + + private static final String TAG_DOZE = "doze"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + 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) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; + } + 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) { -- cgit v1.2.3