From f9f9c448c90ad039fcc94cd38ffed47d43faf516 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Wed, 6 Oct 2021 07:25:13 +0000 Subject: msm8996-common: doze: Adapt to S style * Use Theme.SubSettingsBase for theme * Replace PreferenceActivity with CollapsingToolbarBaseActivity * Add Titles to prefernce screen * Remove onOptionsItemSelected and where neccessary move to activity Change-Id: Ic879ce61d83a33da12829008a4336c8a23230d63 Signed-off-by: Davide Garberi --- .../settings/doze/BootCompletedReceiver.java | 2 +- .../org/lineageos/settings/doze/DozeService.java | 17 ++-- .../settings/doze/DozeSettingsActivity.java | 10 +- .../settings/doze/DozeSettingsFragment.java | 83 ++++----------- .../src/org/lineageos/settings/doze/DozeUtils.java | 111 +++++++++++++++++++++ .../lineageos/settings/doze/ProximitySensor.java | 10 +- .../org/lineageos/settings/doze/TiltSensor.java | 6 +- doze/src/org/lineageos/settings/doze/Utils.java | 111 --------------------- 8 files changed, 153 insertions(+), 197 deletions(-) create mode 100644 doze/src/org/lineageos/settings/doze/DozeUtils.java delete mode 100644 doze/src/org/lineageos/settings/doze/Utils.java (limited to 'doze/src/org') diff --git a/doze/src/org/lineageos/settings/doze/BootCompletedReceiver.java b/doze/src/org/lineageos/settings/doze/BootCompletedReceiver.java index 20a27c2..f894a9b 100644 --- a/doze/src/org/lineageos/settings/doze/BootCompletedReceiver.java +++ b/doze/src/org/lineageos/settings/doze/BootCompletedReceiver.java @@ -30,6 +30,6 @@ public class BootCompletedReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { if (DEBUG) Log.d(TAG, "Received boot completed intent"); - Utils.checkDozeService(context); + DozeUtils.checkDozeService(context); } } diff --git a/doze/src/org/lineageos/settings/doze/DozeService.java b/doze/src/org/lineageos/settings/doze/DozeService.java index 5b161e2..581397f 100644 --- a/doze/src/org/lineageos/settings/doze/DozeService.java +++ b/doze/src/org/lineageos/settings/doze/DozeService.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2015 The CyanogenMod Project - * 2017-2018 The LineageOS Project + * 2017-2018,2021 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. @@ -38,7 +38,8 @@ public class DozeService extends Service { mProximitySensor = new ProximitySensor(this); mTiltSensor = new TiltSensor(this); - IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); + IntentFilter screenStateFilter = new IntentFilter(); + screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mScreenStateReceiver, screenStateFilter); } @@ -65,22 +66,22 @@ public class DozeService extends Service { private void onDisplayOn() { if (DEBUG) Log.d(TAG, "Display on"); - if (Utils.isPickUpEnabled(this)) { + if (DozeUtils.isPickUpEnabled(this)) { mTiltSensor.disable(); } - if (Utils.isHandwaveGestureEnabled(this) || - Utils.isPocketGestureEnabled(this)) { + if (DozeUtils.isHandwaveGestureEnabled(this) || + DozeUtils.isPocketGestureEnabled(this)) { mProximitySensor.disable(); } } private void onDisplayOff() { if (DEBUG) Log.d(TAG, "Display off"); - if (Utils.isPickUpEnabled(this)) { + if (DozeUtils.isPickUpEnabled(this)) { mTiltSensor.enable(); } - if (Utils.isHandwaveGestureEnabled(this) || - Utils.isPocketGestureEnabled(this)) { + if (DozeUtils.isHandwaveGestureEnabled(this) || + DozeUtils.isPocketGestureEnabled(this)) { mProximitySensor.enable(); } } diff --git a/doze/src/org/lineageos/settings/doze/DozeSettingsActivity.java b/doze/src/org/lineageos/settings/doze/DozeSettingsActivity.java index 1591b2c..213f0b4 100644 --- a/doze/src/org/lineageos/settings/doze/DozeSettingsActivity.java +++ b/doze/src/org/lineageos/settings/doze/DozeSettingsActivity.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2015-2016 The CyanogenMod Project - * 2017 The LineageOS Project + * 2017,2021 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. @@ -18,9 +18,11 @@ package org.lineageos.settings.doze; import android.os.Bundle; -import android.preference.PreferenceActivity; -public class DozeSettingsActivity extends PreferenceActivity { +import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity; +import com.android.settingslib.collapsingtoolbar.R; + +public class DozeSettingsActivity extends CollapsingToolbarBaseActivity { private static final String TAG_DOZE = "doze"; @@ -28,7 +30,7 @@ public class DozeSettingsActivity extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getFragmentManager().beginTransaction().replace(android.R.id.content, + getFragmentManager().beginTransaction().replace(R.id.content_frame, new DozeSettingsFragment(), TAG_DOZE).commit(); } } diff --git a/doze/src/org/lineageos/settings/doze/DozeSettingsFragment.java b/doze/src/org/lineageos/settings/doze/DozeSettingsFragment.java index 4701eff..c02aee8 100644 --- a/doze/src/org/lineageos/settings/doze/DozeSettingsFragment.java +++ b/doze/src/org/lineageos/settings/doze/DozeSettingsFragment.java @@ -1,6 +1,6 @@ /* * Copyright (C) 2015 The CyanogenMod Project - * 2017-2019 The LineageOS Project + * 2017-2022 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 org.lineageos.settings.doze; -import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -27,24 +26,19 @@ import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.CompoundButton; import android.widget.Switch; -import android.widget.TextView; import androidx.preference.Preference; -import androidx.preference.Preference.OnPreferenceChangeListener; import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceFragment; import androidx.preference.SwitchPreference; -public class DozeSettingsFragment extends PreferenceFragment implements OnPreferenceChangeListener, - CompoundButton.OnCheckedChangeListener { +import com.android.settingslib.widget.MainSwitchPreference; +import com.android.settingslib.widget.OnMainSwitchChangeListener; - private TextView mTextView; - private View mSwitchBar; +public class DozeSettingsFragment extends PreferenceFragment implements + Preference.OnPreferenceChangeListener, OnMainSwitchChangeListener { + + private MainSwitchPreference mSwitchBar; private SwitchPreference mPickUpPreference; private SwitchPreference mHandwavePreference; @@ -55,8 +49,6 @@ public class DozeSettingsFragment extends PreferenceFragment implements OnPrefer @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.doze_settings); - final ActionBar actionBar = getActivity().getActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); SharedPreferences prefs = getActivity().getSharedPreferences("doze_settings", Activity.MODE_PRIVATE); @@ -64,88 +56,49 @@ public class DozeSettingsFragment extends PreferenceFragment implements OnPrefer showHelp(); } - boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + boolean dozeEnabled = DozeUtils.isDozeEnabled(getActivity()); PreferenceCategory proximitySensorCategory = - (PreferenceCategory) getPreferenceScreen().findPreference(Utils.CATEG_PROX_SENSOR); + (PreferenceCategory) getPreferenceScreen().findPreference(DozeUtils.CATEG_PROX_SENSOR); - mPickUpPreference = (SwitchPreference) findPreference(Utils.GESTURE_PICK_UP_KEY); + mPickUpPreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_PICK_UP_KEY); mPickUpPreference.setEnabled(dozeEnabled); mPickUpPreference.setOnPreferenceChangeListener(this); - mHandwavePreference = (SwitchPreference) findPreference(Utils.GESTURE_HAND_WAVE_KEY); + mHandwavePreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_HAND_WAVE_KEY); mHandwavePreference.setEnabled(dozeEnabled); mHandwavePreference.setOnPreferenceChangeListener(this); - mPocketPreference = (SwitchPreference) findPreference(Utils.GESTURE_POCKET_KEY); + mPocketPreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_POCKET_KEY); mPocketPreference.setEnabled(dozeEnabled); mPocketPreference.setOnPreferenceChangeListener(this); // Hide proximity sensor related features if the device doesn't support them - if (!Utils.getProxCheckBeforePulse(getActivity())) { + if (!DozeUtils.getProxCheckBeforePulse(getActivity())) { getPreferenceScreen().removePreference(proximitySensorCategory); } } - @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 onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); - - mTextView = view.findViewById(R.id.switch_text); - mTextView.setText(getString(dozeEnabled ? - R.string.switch_bar_on : R.string.switch_bar_off)); - - mSwitchBar = view.findViewById(R.id.switch_bar); - Switch switchWidget = mSwitchBar.findViewById(android.R.id.switch_widget); - switchWidget.setChecked(dozeEnabled); - switchWidget.setOnCheckedChangeListener(this); - mSwitchBar.setActivated(dozeEnabled); - mSwitchBar.setOnClickListener(v -> { - switchWidget.setChecked(!switchWidget.isChecked()); - mSwitchBar.setActivated(switchWidget.isChecked()); - }); - } - @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - mHandler.post(() -> Utils.checkDozeService(getActivity())); + mHandler.post(() -> DozeUtils.checkDozeService(getActivity())); return true; } @Override - public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { - Utils.enableDoze(getActivity(), isChecked); - Utils.checkDozeService(getActivity()); + public void onSwitchChanged(Switch switchView, boolean isChecked) { + DozeUtils.enableDoze(getActivity(), isChecked); + DozeUtils.checkDozeService(getActivity()); - mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off)); - mSwitchBar.setActivated(isChecked); + mSwitchBar.setChecked(isChecked); mPickUpPreference.setEnabled(isChecked); mHandwavePreference.setEnabled(isChecked); mPocketPreference.setEnabled(isChecked); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - getActivity().onBackPressed(); - return true; - } - return false; - } - private static class HelpDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { diff --git a/doze/src/org/lineageos/settings/doze/DozeUtils.java b/doze/src/org/lineageos/settings/doze/DozeUtils.java new file mode 100644 index 0000000..62dce44 --- /dev/null +++ b/doze/src/org/lineageos/settings/doze/DozeUtils.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2022 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 org.lineageos.settings.doze; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.UserHandle; +import android.provider.Settings; +import android.util.Log; +import androidx.preference.PreferenceManager; + +import static android.provider.Settings.Secure.DOZE_ENABLED; + +public final class DozeUtils { + + private static final String TAG = "DozeUtils"; + private static final boolean DEBUG = false; + + private static final String DOZE_INTENT = "com.android.systemui.doze.pulse"; + + protected static final String CATEG_PROX_SENSOR = "proximity_sensor"; + + protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; + protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave"; + protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; + + protected static void startService(Context context) { + if (DEBUG) Log.d(TAG, "Starting service"); + context.startServiceAsUser(new Intent(context, DozeService.class), + UserHandle.CURRENT); + } + + protected static void stopService(Context context) { + if (DEBUG) Log.d(TAG, "Stopping service"); + context.stopServiceAsUser(new Intent(context, DozeService.class), + UserHandle.CURRENT); + } + + protected static void checkDozeService(Context context) { + if (isDozeEnabled(context) && sensorsEnabled(context)) { + startService(context); + } else { + stopService(context); + } + } + + protected static boolean getProxCheckBeforePulse(Context context) { + try { + Context con = context.createPackageContext("com.android.systemui", 0); + int id = con.getResources().getIdentifier("doze_proximity_check_before_pulse", + "bool", "com.android.systemui"); + return con.getResources().getBoolean(id); + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } + + protected static boolean isDozeEnabled(Context context) { + return Settings.Secure.getInt(context.getContentResolver(), + DOZE_ENABLED, 1) != 0; + } + + protected static boolean enableDoze(Context context, boolean enable) { + return Settings.Secure.putInt(context.getContentResolver(), + DOZE_ENABLED, enable ? 1 : 0); + } + + protected static void launchDozePulse(Context context) { + if (DEBUG) Log.d(TAG, "Launch doze pulse"); + context.sendBroadcastAsUser(new Intent(DOZE_INTENT), + new UserHandle(UserHandle.USER_CURRENT)); + } + + protected static boolean isGestureEnabled(Context context, String gesture) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(gesture, false); + } + + protected static boolean isPickUpEnabled(Context context) { + return isGestureEnabled(context, GESTURE_PICK_UP_KEY); + } + + protected static boolean isHandwaveGestureEnabled(Context context) { + return isGestureEnabled(context, GESTURE_HAND_WAVE_KEY); + } + + protected static boolean isPocketGestureEnabled(Context context) { + return isGestureEnabled(context, GESTURE_POCKET_KEY); + } + + protected static boolean sensorsEnabled(Context context) { + return isPickUpEnabled(context) || isHandwaveGestureEnabled(context) + || isPocketGestureEnabled(context); + } +} diff --git a/doze/src/org/lineageos/settings/doze/ProximitySensor.java b/doze/src/org/lineageos/settings/doze/ProximitySensor.java index 71594ff..b1de348 100644 --- a/doze/src/org/lineageos/settings/doze/ProximitySensor.java +++ b/doze/src/org/lineageos/settings/doze/ProximitySensor.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * 2017-2018 The LineageOS Project + * 2017-2018,2021 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. @@ -63,7 +63,7 @@ public class ProximitySensor implements SensorEventListener { boolean isNear = event.values[0] < mSensor.getMaximumRange(); if (mSawNear && !isNear) { if (shouldPulse(event.timestamp)) { - Utils.launchDozePulse(mContext); + DozeUtils.launchDozePulse(mContext); } } else { mInPocketTime = event.timestamp; @@ -74,11 +74,11 @@ public class ProximitySensor implements SensorEventListener { private boolean shouldPulse(long timestamp) { long delta = timestamp - mInPocketTime; - if (Utils.isHandwaveGestureEnabled(mContext) && Utils.isPocketGestureEnabled(mContext)) { + if (DozeUtils.isHandwaveGestureEnabled(mContext) && DozeUtils.isPocketGestureEnabled(mContext)) { return true; - } else if (Utils.isHandwaveGestureEnabled(mContext)) { + } else if (DozeUtils.isHandwaveGestureEnabled(mContext)) { return delta < HANDWAVE_MAX_DELTA_NS; - } else if (Utils.isPocketGestureEnabled(mContext)) { + } else if (DozeUtils.isPocketGestureEnabled(mContext)) { return delta >= POCKET_MIN_DELTA_NS; } return false; diff --git a/doze/src/org/lineageos/settings/doze/TiltSensor.java b/doze/src/org/lineageos/settings/doze/TiltSensor.java index 6598127..768eb92 100644 --- a/doze/src/org/lineageos/settings/doze/TiltSensor.java +++ b/doze/src/org/lineageos/settings/doze/TiltSensor.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2015 The CyanogenMod Project - * 2017-2018 The LineageOS Project + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2018,2021 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. @@ -67,7 +67,7 @@ public class TiltSensor implements SensorEventListener { } if (event.values[0] == 1) { - Utils.launchDozePulse(mContext); + DozeUtils.launchDozePulse(mContext); } } diff --git a/doze/src/org/lineageos/settings/doze/Utils.java b/doze/src/org/lineageos/settings/doze/Utils.java deleted file mode 100644 index 987e06a..0000000 --- a/doze/src/org/lineageos/settings/doze/Utils.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2015 The CyanogenMod Project - * 2017-2019 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 org.lineageos.settings.doze; - -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.UserHandle; -import android.provider.Settings; -import android.util.Log; -import androidx.preference.PreferenceManager; - -import static android.provider.Settings.Secure.DOZE_ENABLED; - -public final class Utils { - - private static final String TAG = "DozeUtils"; - private static final boolean DEBUG = false; - - private static final String DOZE_INTENT = "com.android.systemui.doze.pulse"; - - protected static final String CATEG_PROX_SENSOR = "proximity_sensor"; - - protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; - protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave"; - protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; - - protected static void startService(Context context) { - if (DEBUG) Log.d(TAG, "Starting service"); - context.startServiceAsUser(new Intent(context, DozeService.class), - UserHandle.CURRENT); - } - - protected static void stopService(Context context) { - if (DEBUG) Log.d(TAG, "Stopping service"); - context.stopServiceAsUser(new Intent(context, DozeService.class), - UserHandle.CURRENT); - } - - protected static void checkDozeService(Context context) { - if (isDozeEnabled(context) && sensorsEnabled(context)) { - startService(context); - } else { - stopService(context); - } - } - - protected static boolean getProxCheckBeforePulse(Context context) { - try { - Context con = context.createPackageContext("com.android.systemui", 0); - int id = con.getResources().getIdentifier("doze_proximity_check_before_pulse", - "bool", "com.android.systemui"); - return con.getResources().getBoolean(id); - } catch (PackageManager.NameNotFoundException e) { - return false; - } - } - - protected static boolean isDozeEnabled(Context context) { - return Settings.Secure.getInt(context.getContentResolver(), - DOZE_ENABLED, 1) != 0; - } - - protected static boolean enableDoze(Context context, boolean enable) { - return Settings.Secure.putInt(context.getContentResolver(), - DOZE_ENABLED, enable ? 1 : 0); - } - - protected static void launchDozePulse(Context context) { - if (DEBUG) Log.d(TAG, "Launch doze pulse"); - context.sendBroadcastAsUser(new Intent(DOZE_INTENT), - new UserHandle(UserHandle.USER_CURRENT)); - } - - protected static boolean isGestureEnabled(Context context, String gesture) { - return PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(gesture, false); - } - - protected static boolean isPickUpEnabled(Context context) { - return isGestureEnabled(context, GESTURE_PICK_UP_KEY); - } - - protected static boolean isHandwaveGestureEnabled(Context context) { - return isGestureEnabled(context, GESTURE_HAND_WAVE_KEY); - } - - protected static boolean isPocketGestureEnabled(Context context) { - return isGestureEnabled(context, GESTURE_POCKET_KEY); - } - - protected static boolean sensorsEnabled(Context context) { - return isPickUpEnabled(context) || isHandwaveGestureEnabled(context) - || isPocketGestureEnabled(context); - } -} -- cgit v1.2.3