From 79505d555e40e64c72ab72dc60d1cdc9814cac8f Mon Sep 17 00:00:00 2001 From: dianlujitao Date: Tue, 1 Feb 2022 23:22:46 +0800 Subject: [PATCH] MotoActions: Bring doze settings up-to-date Based off oneplus3 doze app and integrated with MotoActions. Change-Id: Ie5e87ecd1cf33631b71186a95e33dcddd70acb11 --- MotoActions/AndroidManifest.xml | 9 +- MotoActions/res/layout/doze.xml | 28 +++ MotoActions/res/xml/doze_panel.xml | 36 ++-- .../device/DozePreferenceActivity.java | 40 +--- .../device/DozePreferenceFragment.java | 179 ++++++++++++++++++ .../settings/device/MotoActionsSettings.java | 43 ++++- 6 files changed, 272 insertions(+), 63 deletions(-) create mode 100644 MotoActions/res/layout/doze.xml create mode 100644 MotoActions/src/org/lineageos/settings/device/DozePreferenceFragment.java diff --git a/MotoActions/AndroidManifest.xml b/MotoActions/AndroidManifest.xml index 7737d90..c4510df 100644 --- a/MotoActions/AndroidManifest.xml +++ b/MotoActions/AndroidManifest.xml @@ -42,14 +42,9 @@ android:label="@string/ambient_display_gestures_title" android:theme="@style/Theme.Main"> - + + - - + + + + + + + + diff --git a/MotoActions/res/xml/doze_panel.xml b/MotoActions/res/xml/doze_panel.xml index 656c182..9ecb5f8 100644 --- a/MotoActions/res/xml/doze_panel.xml +++ b/MotoActions/res/xml/doze_panel.xml @@ -1,6 +1,6 @@ + + - - - + + + + diff --git a/MotoActions/src/org/lineageos/settings/device/DozePreferenceActivity.java b/MotoActions/src/org/lineageos/settings/device/DozePreferenceActivity.java index 05d4576..aad6126 100644 --- a/MotoActions/src/org/lineageos/settings/device/DozePreferenceActivity.java +++ b/MotoActions/src/org/lineageos/settings/device/DozePreferenceActivity.java @@ -1,5 +1,6 @@ /* - * 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. @@ -18,42 +19,17 @@ package org.lineageos.settings.device; import android.os.Bundle; import android.preference.PreferenceActivity; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceFragment; -import android.view.MenuItem; public class DozePreferenceActivity extends PreferenceActivity { + private static final String TAG_DOZE = "doze"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - getActionBar().setDisplayHomeAsUpEnabled(true); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, new DozePreferenceFragment()).commit(); - } - - public static class DozePreferenceFragment extends PreferenceFragment { - private static final String CATEGORY_AMBIENT_DISPLAY = "ambient_display_key"; - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - addPreferencesFromResource(R.xml.doze_panel); - boolean dozeEnabled = MotoActionsSettings.isDozeEnabled(getActivity()); - boolean aodEnabled = MotoActionsSettings.isAODEnabled(getActivity()); - PreferenceCategory ambientDisplayCat = (PreferenceCategory) - findPreference(CATEGORY_AMBIENT_DISPLAY); - if (ambientDisplayCat != null) { - ambientDisplayCat.setEnabled(dozeEnabled && !aodEnabled); - } - } - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - onBackPressed(); - return true; - } - return super.onOptionsItemSelected(item); + getFragmentManager() + .beginTransaction() + .replace(android.R.id.content, new DozePreferenceFragment(), TAG_DOZE) + .commit(); } } diff --git a/MotoActions/src/org/lineageos/settings/device/DozePreferenceFragment.java b/MotoActions/src/org/lineageos/settings/device/DozePreferenceFragment.java new file mode 100644 index 0000000..59bb626 --- /dev/null +++ b/MotoActions/src/org/lineageos/settings/device/DozePreferenceFragment.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * Copyright (C) 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.device; + +import android.app.ActionBar; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.os.Bundle; +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 DozePreferenceFragment extends PreferenceFragment + implements OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener { + + private TextView mTextView; + private View mSwitchBar; + + private SwitchPreference mAlwaysOnDisplayPreference; + + private SwitchPreference mHandwavePreference; + private SwitchPreference mPickUpPreference; + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + addPreferencesFromResource(R.xml.doze_panel); + final ActionBar actionBar = getActivity().getActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + + SharedPreferences prefs = + getActivity().getSharedPreferences("doze_panel", Activity.MODE_PRIVATE); + if (savedInstanceState == null && !prefs.getBoolean("first_help_shown", false)) { + showHelp(); + } + + boolean dozeEnabled = MotoActionsSettings.isDozeEnabled(getActivity()); + + mAlwaysOnDisplayPreference = findPreference(MotoActionsSettings.ALWAYS_ON_DISPLAY); + mAlwaysOnDisplayPreference.setEnabled(dozeEnabled); + mAlwaysOnDisplayPreference.setChecked(MotoActionsSettings.isAlwaysOnEnabled(getActivity())); + mAlwaysOnDisplayPreference.setOnPreferenceChangeListener(this); + + mHandwavePreference = findPreference(MotoActionsSettings.GESTURE_IR_WAKEUP_KEY); + mHandwavePreference.setEnabled(dozeEnabled); + mHandwavePreference.setOnPreferenceChangeListener(this); + + mPickUpPreference = findPreference(MotoActionsSettings.GESTURE_PICK_UP_KEY); + mPickUpPreference.setEnabled(dozeEnabled); + mPickUpPreference.setOnPreferenceChangeListener(this); + + // Hide AOD if not supported and set all its dependents otherwise + if (!MotoActionsSettings.alwaysOnDisplayAvailable(getActivity())) { + getPreferenceScreen().removePreference(mAlwaysOnDisplayPreference); + } else { + PreferenceCategory ambientDisplayCategory = findPreference("ambient_display_key"); + ambientDisplayCategory.setDependency(MotoActionsSettings.ALWAYS_ON_DISPLAY); + } + } + + @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 = MotoActionsSettings.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) { + if (MotoActionsSettings.ALWAYS_ON_DISPLAY.equals(preference.getKey())) { + MotoActionsSettings.enableAlwaysOn(getActivity(), (Boolean) newValue); + } + + return true; + } + + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { + MotoActionsSettings.enableDoze(getActivity(), isChecked); + + mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off)); + mSwitchBar.setActivated(isChecked); + + if (!isChecked) { + MotoActionsSettings.enableAlwaysOn(getActivity(), false); + mAlwaysOnDisplayPreference.setChecked(false); + } + mAlwaysOnDisplayPreference.setEnabled(isChecked); + + mHandwavePreference.setEnabled(isChecked); + mPickUpPreference.setEnabled(isChecked); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + getActivity().onBackPressed(); + return true; + } + return false; + } + + public static class HelpDialogFragment extends DialogFragment { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + return new AlertDialog.Builder(getActivity()) + .setTitle(R.string.doze_settings_help_title) + .setMessage(R.string.doze_settings_help_text) + .setNegativeButton(R.string.dialog_ok, (dialog, which) -> dialog.cancel()) + .create(); + } + + @Override + public void onCancel(DialogInterface dialog) { + getActivity() + .getSharedPreferences("doze_panel", Activity.MODE_PRIVATE) + .edit() + .putBoolean("first_help_shown", true) + .apply(); + } + } + + private void showHelp() { + HelpDialogFragment fragment = new HelpDialogFragment(); + fragment.show(getFragmentManager(), "help_dialog"); + } +} diff --git a/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java b/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java index b6b3a4e..863e275 100644 --- a/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java +++ b/MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 The CyanogenMod Project - * Copyright (c) 2017 The LineageOS Project + * Copyright (c) 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. @@ -22,22 +22,29 @@ import android.content.SharedPreferences; import android.hardware.display.AmbientDisplayConfiguration; import android.os.UserHandle; import android.preference.PreferenceManager; +import android.provider.Settings; import org.lineageos.settings.device.actions.UpdatedStateNotifier; import org.lineageos.settings.device.actions.CameraActivationAction; import org.lineageos.settings.device.actions.TorchAction; +import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; +import static android.provider.Settings.Secure.DOZE_ENABLED; + public class MotoActionsSettings { private static final String TAG = "MotoActions"; private static final String GESTURE_CAMERA_ACTION_KEY = "gesture_camera_action"; private static final String GESTURE_CHOP_CHOP_KEY = "gesture_chop_chop"; - private static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; - private static final String GESTURE_IR_WAKEUP_KEY = "gesture_hand_wave"; private static final String GESTURE_IR_SILENCER_KEY = "gesture_ir_silencer"; private static final String GESTURE_FLIP_TO_MUTE_KEY = "gesture_flip_to_mute"; private static final String GESTURE_LIFT_TO_SILENCE_KEY = "gesture_lift_to_silence"; + static final String GESTURE_IR_WAKEUP_KEY = "gesture_hand_wave"; + static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; + + static final String ALWAYS_ON_DISPLAY = "always_on_display"; + private final Context mContext; private final UpdatedStateNotifier mUpdatedStateNotifier; @@ -65,16 +72,32 @@ public class MotoActionsSettings { return mChopChopEnabled; } - public static boolean isAODEnabled(Context context) { - return new AmbientDisplayConfiguration(context).alwaysOnEnabled(UserHandle.USER_CURRENT); + public static boolean isAlwaysOnEnabled(Context context) { + return Settings.Secure.getIntForUser(context.getContentResolver(), + DOZE_ALWAYS_ON, 0, UserHandle.USER_CURRENT) != 0; + } + + public static boolean alwaysOnDisplayAvailable(Context context) { + return new AmbientDisplayConfiguration(context).alwaysOnAvailable(); + } + + public static boolean enableAlwaysOn(Context context, boolean enable) { + return Settings.Secure.putIntForUser(context.getContentResolver(), + DOZE_ALWAYS_ON, enable ? 1 : 0, UserHandle.USER_CURRENT); + } + + public static boolean enableDoze(Context context, boolean enable) { + return Settings.Secure.putInt(context.getContentResolver(), + DOZE_ENABLED, enable ? 1 : 0); } public static boolean isDozeEnabled(Context context) { - return new AmbientDisplayConfiguration(context).pulseOnNotificationEnabled(UserHandle.USER_CURRENT); + return Settings.Secure.getInt(context.getContentResolver(), + DOZE_ENABLED, 1) != 0; } - public boolean isAODEnabled() { - return isAODEnabled(mContext); + public boolean isAlwaysOnEnabled() { + return isAlwaysOnEnabled(mContext); } public boolean isDozeEnabled() { @@ -82,11 +105,11 @@ public class MotoActionsSettings { } public boolean isIrWakeupEnabled() { - return isDozeEnabled() && !isAODEnabled() && mIrWakeUpEnabled; + return isDozeEnabled() && !isAlwaysOnEnabled() && mIrWakeUpEnabled; } public boolean isPickUpEnabled() { - return isDozeEnabled() && !isAODEnabled() && mPickUpGestureEnabled; + return isDozeEnabled() && !isAlwaysOnEnabled() && mPickUpGestureEnabled; } public boolean isIrSilencerEnabled() { -- 2.20.1