Revert "LineageParts: Drop power menu actions settings"

This reverts commit cfe52298b243689d7bbf8ac133401e2f4a20dbf1.

Signed-off-by: Gegham Zakaryan <zakaryan.2004@outlook.com>
Change-Id: Ia56cb761dd83539b136ba33d7caf5c66d156228a
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1e7c183..f40fd0d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -252,6 +252,16 @@
     <string name="navigation_bar_app_switch_long_press_title">Recents long press action</string>
     <string name="navigation_bar_edge_long_swipe_title">Edge long swipe action</string>
 
+    <!-- Power menu -->
+    <string name="power_menu_title">Power menu</string>
+    <string name="power_menu_screenshot_title">Screenshot</string>
+    <string name="power_menu_screenshot_summary">Long-press for partial screenshot</string>
+    <string name="power_menu_airplane_title">Airplane mode</string>
+    <string name="power_menu_users_title">User switcher</string>
+    <string name="power_menu_bug_report_title">Bug report</string>
+    <string name="power_menu_bug_report_disabled">Bug reporting is disabled in development settings</string>
+    <string name="power_menu_lockdown_title">Lockdown</string>
+
     <string name="camera_double_tap_power_gesture_title">Press power button twice for camera</string>
 
     <!-- Description of setting that controls gesture to open camera by double tapping the power button [CHAR LIMIT=NONE] -->
diff --git a/res/xml/button_settings.xml b/res/xml/button_settings.xml
index 31dfbf9..5234a96 100644
--- a/res/xml/button_settings.xml
+++ b/res/xml/button_settings.xml
@@ -99,6 +99,11 @@
         android:key="power_key"
         android:title="@string/hardware_keys_power_key_title" >
 
+        <PreferenceScreen
+            android:key="power_menu"
+            android:title="@string/power_menu_title"
+            android:fragment="org.lineageos.lineageparts.input.PowerMenuActions" />
+
         <SwitchPreference
             android:key="power_end_call"
             android:title="@string/power_end_call_title"
diff --git a/res/xml/parts_catalog.xml b/res/xml/parts_catalog.xml
index a6f5355..a4346c7 100644
--- a/res/xml/parts_catalog.xml
+++ b/res/xml/parts_catalog.xml
@@ -75,6 +75,11 @@
           android:fragment="org.lineageos.lineageparts.lineagestats.AnonymousStats"
           lineage:xmlRes="@xml/anonymous_stats" />
 
+    <part android:key="power_menu"
+          android:title="@string/power_menu_title"
+          android:fragment="org.lineageos.lineageparts.input.PowerMenuActions"
+          lineage:xmlRes="@xml/power_menu_settings" />
+
     <part android:key="weather_settings"
           android:title="@string/weather_settings_title"
           android:fragment="org.lineageos.lineageparts.weather.WeatherServiceSettings"
diff --git a/res/xml/power_menu_settings.xml b/res/xml/power_menu_settings.xml
new file mode 100644
index 0000000..e947f31
--- /dev/null
+++ b/res/xml/power_menu_settings.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2014-2015 The CyanogenMod 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.
+-->
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:key="power_menu"
+    android:title="@string/power_menu_title">
+
+    <CheckBoxPreference
+        android:key="screenshot"
+        android:title="@string/power_menu_screenshot_title"
+        android:summary="@string/power_menu_screenshot_summary"
+        android:defaultValue="false" />
+
+    <CheckBoxPreference
+        android:key="airplane"
+        android:title="@string/power_menu_airplane_title"
+        android:defaultValue="false" />
+
+    <CheckBoxPreference
+        android:key="users"
+        android:title="@string/power_menu_users_title"
+        android:defaultValue="false" />
+
+    <CheckBoxPreference
+        android:key="bugreport"
+        android:title="@string/power_menu_bug_report_title"
+        android:defaultValue="false" />
+
+    <CheckBoxPreference
+        android:key="lockdown"
+        android:title="@string/power_menu_lockdown_title"
+        android:defaultValue="false" />
+</PreferenceScreen>
diff --git a/src/org/lineageos/lineageparts/input/PowerMenuActions.java b/src/org/lineageos/lineageparts/input/PowerMenuActions.java
new file mode 100644
index 0000000..f119769
--- /dev/null
+++ b/src/org/lineageos/lineageparts/input/PowerMenuActions.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright (C) 2014-2015 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 org.lineageos.lineageparts.input;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.UserInfo;
+import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.provider.Settings;
+
+import androidx.preference.CheckBoxPreference;
+import androidx.preference.Preference;
+
+import org.lineageos.internal.util.PowerMenuConstants;
+import org.lineageos.lineageparts.R;
+import org.lineageos.lineageparts.SettingsPreferenceFragment;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import lineageos.providers.LineageSettings;
+
+import static org.lineageos.internal.util.PowerMenuConstants.*;
+
+public class PowerMenuActions extends SettingsPreferenceFragment {
+    final static String TAG = "PowerMenuActions";
+
+    private CheckBoxPreference mScreenshotPref;
+    private CheckBoxPreference mAirplanePref;
+    private CheckBoxPreference mUsersPref;
+    private CheckBoxPreference mBugReportPref;
+    private CheckBoxPreference mLockDownPref;
+
+    Context mContext;
+    private ArrayList<String> mLocalUserConfig = new ArrayList<String>();
+    private String[] mAllActions;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        addPreferencesFromResource(R.xml.power_menu_settings);
+        mContext = getActivity().getApplicationContext();
+
+        mAllActions = PowerMenuConstants.getAllActions();
+
+        for (String action : mAllActions) {
+            if (action.equals(GLOBAL_ACTION_KEY_SCREENSHOT)) {
+                mScreenshotPref = findPreference(GLOBAL_ACTION_KEY_SCREENSHOT);
+            } else if (action.equals(GLOBAL_ACTION_KEY_AIRPLANE)) {
+                mAirplanePref = findPreference(GLOBAL_ACTION_KEY_AIRPLANE);
+            } else if (action.equals(GLOBAL_ACTION_KEY_USERS)) {
+                mUsersPref = findPreference(GLOBAL_ACTION_KEY_USERS);
+            } else if (action.equals(GLOBAL_ACTION_KEY_BUGREPORT)) {
+                mBugReportPref = findPreference(GLOBAL_ACTION_KEY_BUGREPORT);
+            } else if (action.equals(GLOBAL_ACTION_KEY_LOCKDOWN)) {
+                mLockDownPref = findPreference(GLOBAL_ACTION_KEY_LOCKDOWN);
+            }
+        }
+
+        getUserConfig();
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+
+        if (mScreenshotPref != null) {
+            mScreenshotPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_SCREENSHOT));
+        }
+
+        if (mAirplanePref != null) {
+            mAirplanePref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_AIRPLANE));
+        }
+
+        if (mUsersPref != null) {
+            if (!UserHandle.MU_ENABLED || !UserManager.supportsMultipleUsers()) {
+                getPreferenceScreen().removePreference(findPreference(GLOBAL_ACTION_KEY_USERS));
+                mUsersPref = null;
+            } else {
+                List<UserInfo> users = ((UserManager) mContext.getSystemService(
+                        Context.USER_SERVICE)).getUsers();
+                boolean enabled = (users.size() > 1);
+                mUsersPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_USERS) && enabled);
+                mUsersPref.setEnabled(enabled);
+            }
+        }
+
+        if (mBugReportPref != null) {
+            mBugReportPref.setChecked(settingsArrayContains(GLOBAL_ACTION_KEY_BUGREPORT));
+        }
+
+        updatePreferences();
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        updatePreferences();
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(Preference preference) {
+        boolean value;
+
+        if (preference == mScreenshotPref) {
+            value = mScreenshotPref.isChecked();
+            updateUserConfig(value, GLOBAL_ACTION_KEY_SCREENSHOT);
+
+        } else if (preference == mAirplanePref) {
+            value = mAirplanePref.isChecked();
+            updateUserConfig(value, GLOBAL_ACTION_KEY_AIRPLANE);
+
+        } else if (preference == mUsersPref) {
+            value = mUsersPref.isChecked();
+            updateUserConfig(value, GLOBAL_ACTION_KEY_USERS);
+
+        } else if (preference == mBugReportPref) {
+            value = mBugReportPref.isChecked();
+            updateUserConfig(value, GLOBAL_ACTION_KEY_BUGREPORT);
+
+        } else if (preference == mLockDownPref) {
+            value = mLockDownPref.isChecked();
+            updateUserConfig(value, GLOBAL_ACTION_KEY_LOCKDOWN);
+            Settings.Secure.putInt(getContentResolver(),
+                    Settings.Secure.LOCKDOWN_IN_POWER_MENU, value ? 1 : 0);
+
+        } else {
+            return super.onPreferenceTreeClick(preference);
+        }
+        return true;
+    }
+
+    private boolean settingsArrayContains(String preference) {
+        return mLocalUserConfig.contains(preference);
+    }
+
+    private void updateUserConfig(boolean enabled, String action) {
+        if (enabled) {
+            if (!settingsArrayContains(action)) {
+                mLocalUserConfig.add(action);
+            }
+        } else {
+            if (settingsArrayContains(action)) {
+                mLocalUserConfig.remove(action);
+            }
+        }
+        saveUserConfig();
+    }
+
+    private void updatePreferences() {
+        boolean bugreport = Settings.Global.getInt(getContentResolver(),
+                Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0;
+
+        if (mBugReportPref != null) {
+            mBugReportPref.setEnabled(bugreport);
+            if (bugreport) {
+                mBugReportPref.setSummary(null);
+            } else {
+                mBugReportPref.setSummary(R.string.power_menu_bug_report_disabled);
+            }
+        }
+    }
+
+    private void getUserConfig() {
+        mLocalUserConfig.clear();
+        String[] defaultActions;
+        String savedActions = LineageSettings.Secure.getStringForUser(mContext.getContentResolver(),
+                LineageSettings.Secure.POWER_MENU_ACTIONS, UserHandle.USER_CURRENT);
+
+        if (savedActions == null) {
+            defaultActions = mContext.getResources().getStringArray(
+                    com.android.internal.R.array.config_globalActionsList);
+            for (String action : defaultActions) {
+                mLocalUserConfig.add(action);
+            }
+        } else {
+            for (String action : savedActions.split("\\|")) {
+                mLocalUserConfig.add(action);
+            }
+        }
+    }
+
+    private void saveUserConfig() {
+        StringBuilder s = new StringBuilder();
+
+        ArrayList<String> setactions = new ArrayList<String>();
+        for (String action : mAllActions) {
+            if (settingsArrayContains(action)) {
+                setactions.add(action);
+            } else {
+                continue;
+            }
+        }
+
+        for (int i = 0; i < setactions.size(); i++) {
+            s.append(setactions.get(i).toString());
+            if (i != setactions.size() - 1) {
+                s.append("|");
+            }
+        }
+
+        LineageSettings.Secure.putStringForUser(getContentResolver(),
+                LineageSettings.Secure.POWER_MENU_ACTIONS, s.toString(), UserHandle.USER_CURRENT);
+        updatePowerMenuDialog();
+    }
+
+    private void updatePowerMenuDialog() {
+        Intent u = new Intent();
+        u.setAction(lineageos.content.Intent.ACTION_UPDATE_POWER_MENU);
+        mContext.sendBroadcastAsUser(u, UserHandle.ALL);
+    }
+}