MotoActions: Bring doze settings up-to-date
authordianlujitao <dianlujitao@lineageos.org>
Tue, 1 Feb 2022 15:22:46 +0000 (23:22 +0800)
committerdianlujitao <dianlujitao@lineageos.org>
Sat, 12 Feb 2022 14:31:20 +0000 (22:31 +0800)
Based off oneplus3 doze app and integrated with MotoActions.

Change-Id: Ie5e87ecd1cf33631b71186a95e33dcddd70acb11

MotoActions/AndroidManifest.xml
MotoActions/res/layout/doze.xml [new file with mode: 0644]
MotoActions/res/xml/doze_panel.xml
MotoActions/src/org/lineageos/settings/device/DozePreferenceActivity.java
MotoActions/src/org/lineageos/settings/device/DozePreferenceFragment.java [new file with mode: 0644]
MotoActions/src/org/lineageos/settings/device/MotoActionsSettings.java

index 5064b6255efeb1b40da8cc3e50067ef18950c1ef..ddf89b99358e5765e44a08b25ca58a7d0a07dfa9 100644 (file)
             android:label="@string/ambient_display_gestures_title"
             android:theme="@style/Theme.Main">
             <intent-filter>
-                <action android:name="com.android.settings.action.IA_SETTINGS" />
+                <action android:name="org.lineageos.settings.device.DOZE_SETTINGS" />
+                <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
-            <meta-data
-                android:name="com.android.settings.category"
-                android:value="com.android.settings.category.ia.display" />
-            <meta-data
-                android:name="com.android.settings.summary"
-                android:resource="@string/ambient_display_gestures_summary" />
         </activity>
 
         <activity
diff --git a/MotoActions/res/layout/doze.xml b/MotoActions/res/layout/doze.xml
new file mode 100644 (file)
index 0000000..e9f3c0f
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2014, The Android Open Source 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.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <include layout="@layout/switch_bar" />
+
+</LinearLayout>
+
index 656c182d54a618bf12771a041191ce012eb57923..9ecb5f82492fef13f935770b727c2edce142a11e 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-     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.
 -->
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 
+    <SwitchPreference
+        android:key="always_on_display"
+        android:disableDependentsState="true"
+        android:title="@string/ambient_display_always_on_title"
+        android:summary="@string/ambient_display_always_on_summary"
+        android:persistent="false" />
+
     <PreferenceCategory
         android:key="ambient_display_key"
         android:title="@string/ambient_display_title">
 
-       <SwitchPreference
-           android:key="gesture_pick_up"
-           android:defaultValue="true"
-           android:title="@string/pick_up_gesture_title"
-           android:summary="@string/pick_up_gesture_summary"
-           android:icon="@drawable/ic_pickup" />
-
-       <SwitchPreference
-           android:key="gesture_hand_wave"
-           android:defaultValue="true"
-           android:title="@string/hand_wave_gesture_title"
-           android:summary="@string/hand_wave_gesture_summary"
-           android:icon="@drawable/ic_hand" />
+        <SwitchPreference
+            android:key="gesture_pick_up"
+            android:defaultValue="true"
+            android:title="@string/pick_up_gesture_title"
+            android:summary="@string/pick_up_gesture_summary"
+            android:icon="@drawable/ic_pickup" />
+
+        <SwitchPreference
+            android:key="gesture_hand_wave"
+            android:defaultValue="true"
+            android:title="@string/hand_wave_gesture_title"
+            android:summary="@string/hand_wave_gesture_summary"
+            android:icon="@drawable/ic_hand" />
+
     </PreferenceCategory>
 </PreferenceScreen>
index 05d45761ae9ea93238d9c19daa9069fa208c5c2d..aad612606aa1b0e67582d9d1c5dac901433556e6 100644 (file)
@@ -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 (file)
index 0000000..59bb626
--- /dev/null
@@ -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");
+    }
+}
index b6b3a4e44403b31ba8fc27d62ac5e8766b07133e..863e2754a9e5378e7204acfce4310b92e6112e1f 100644 (file)
@@ -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() {