[2/3] OmniGears: power menu config

to configure what power menu actions are shown
TODO: we could also make the order of the actions
configurable in the future

Change-Id: I47718a025ab311027be223e50446227928ab96af
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index d55fd24..e7fe400 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -283,4 +283,17 @@
     <string name="dashboard_colums_resize_title">Orientation adjustment</string>
     <string name="dashboard_colums_resize_summary">Automatically adjust settings columns based on orientation.</string>
 
+    <!-- global acions config -->
+    <string name="global_actions_power_title">Power</string>
+    <string name="global_actions_reboot_title">Reboot</string>
+    <string name="global_actions_lockdown_title">Lock</string>
+    <string name="global_actions_settings_title">Settings</string>
+    <string name="global_actions_airplane_title">Airplane mode</string>
+    <string name="global_actions_users_title">Users</string>
+    <string name="global_actions_voiceassist_title">Voiceassist</string>
+    <string name="global_actions_silent_title">Silent mode</string>
+    <string name="global_actions_dnd_title">Do not disturb</string>
+    <string name="global_actions_title">Power menu</string>
+    <string name="global_actions_summary">Actions shown in power menu</string>
+    <string name="global_actions_header">Some actions are only visible depending on the setup or only if the device is unlocked</string>
 </resources>
diff --git a/res/xml/global_actions.xml b/res/xml/global_actions.xml
new file mode 100644
index 0000000..4514c0a
--- /dev/null
+++ b/res/xml/global_actions.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--  Copyright (C) 2016 The OmniROM Project
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ -->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    android:title="@string/global_actions_title">
+
+        <Preference
+            android:summary="@string/global_actions_header"
+            android:selectable="false" />
+
+        <SwitchPreference
+            android:key="power"
+            android:title="@string/global_actions_power_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="reboot"
+            android:title="@string/global_actions_reboot_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="lockdown"
+            android:title="@string/global_actions_lockdown_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="settings"
+            android:title="@string/global_actions_settings_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="airplane"
+            android:title="@string/global_actions_airplane_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="users"
+            android:title="@string/global_actions_users_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="voiceassist"
+            android:title="@string/global_actions_voiceassist_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="silent"
+            android:title="@string/global_actions_silent_title"
+            android:persistent="false" />
+        <SwitchPreference
+            android:key="dnd"
+            android:title="@string/global_actions_dnd_title"
+            android:persistent="false" />
+</PreferenceScreen>
diff --git a/res/xml/more_interface_settings.xml b/res/xml/more_interface_settings.xml
index 9c99362..cead945 100644
--- a/res/xml/more_interface_settings.xml
+++ b/res/xml/more_interface_settings.xml
@@ -76,6 +76,12 @@
                 android:summary="@string/dashboard_colums_resize_summary"
                 android:defaultValue="true" />
 
+            <PreferenceScreen
+                android:key="global_actions"
+                android:title="@string/global_actions_title"
+                android:summary="@string/global_actions_summary"
+                android:fragment="org.omnirom.omnigears.interfacesettings.GlobalActionsSettings" />
+
         </PreferenceCategory>
 </PreferenceScreen>
 
diff --git a/src/org/omnirom/omnigears/interfacesettings/GlobalActionsSettings.java b/src/org/omnirom/omnigears/interfacesettings/GlobalActionsSettings.java
new file mode 100644
index 0000000..3cffc44
--- /dev/null
+++ b/src/org/omnirom/omnigears/interfacesettings/GlobalActionsSettings.java
@@ -0,0 +1,161 @@
+/*
+ *  Copyright (C) 2016 The OmniROM Project
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package org.omnirom.omnigears.interfacesettings;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.os.Bundle;
+import android.os.UserManager;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceManager;
+import android.preference.PreferenceScreen;
+import android.preference.SwitchPreference;
+import android.provider.SearchIndexableResource;
+import android.provider.Settings;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.android.internal.logging.MetricsLogger;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+
+import java.util.List;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+
+public class GlobalActionsSettings extends SettingsPreferenceFragment implements
+        Preference.OnPreferenceChangeListener, Indexable {
+    private static final String TAG = "GlobalActionsSettings";
+
+    private LinkedHashMap<String, Boolean> mGlobalActionsMap;
+
+    @Override
+    protected int getMetricsCategory() {
+        return MetricsLogger.OMNI_SETTINGS;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        addPreferencesFromResource(R.xml.global_actions);
+
+        final PreferenceScreen prefScreen = getPreferenceScreen();
+        final ContentResolver contentResolver = getContext().getContentResolver();
+
+        final String[] defaultActions = getContext().getResources().getStringArray(
+                com.android.internal.R.array.config_globalActionsList);
+        final List<String> defaultActionsList = Arrays.asList(defaultActions);
+
+        final String[] allActions = getContext().getResources().getStringArray(
+                com.android.internal.R.array.values_globalActionsList);
+
+        final String enabledActions = Settings.System.getString(contentResolver,
+                Settings.System.GLOBAL_ACTIONS_LIST);
+
+        List<String> enabledActionsList = null;
+        if (enabledActions != null) {
+            enabledActionsList = Arrays.asList(enabledActions.split(","));
+        }
+
+        mGlobalActionsMap = new LinkedHashMap<String, Boolean>();
+        for (String actionKey : allActions) {
+            if (enabledActionsList != null) {
+                mGlobalActionsMap.put(actionKey, enabledActionsList.contains(actionKey));
+            } else {
+                mGlobalActionsMap.put(actionKey, defaultActionsList.contains(actionKey));
+            }
+        }
+        final UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
+        boolean multiUser = um.isUserSwitcherEnabled();
+        Preference userPref = null;
+        int count = prefScreen.getPreferenceCount();
+        for (int i = 0; i < count; i++) {
+            Preference p = prefScreen.getPreference(i);
+            if (p instanceof SwitchPreference) {
+                SwitchPreference action = (SwitchPreference) p;
+                String key = action.getKey();
+                if (key.equals("users") && !multiUser) {
+                    userPref = action;
+                }
+                action.setChecked(mGlobalActionsMap.get(key));
+            }
+        }
+        if (userPref != null) {
+            prefScreen.removePreference(userPref);
+        }
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
+        if (preference instanceof SwitchPreference) {
+            SwitchPreference action = (SwitchPreference) preference;
+            mGlobalActionsMap.put(action.getKey(), action.isChecked());
+
+            List<String> enabledActionsList = new ArrayList<String>();
+            for (String actionKey : mGlobalActionsMap.keySet()) {
+                Boolean checked = mGlobalActionsMap.get(actionKey);
+                if (checked) {
+                    enabledActionsList.add(actionKey);
+                }
+            }
+            setList(enabledActionsList);
+            return true;
+        }
+        return super.onPreferenceTreeClick(preferenceScreen, preference);
+    }
+
+    private void setList(List<String> actionList) {
+        final ContentResolver contentResolver = getContext().getContentResolver();
+        Settings.System.putString(contentResolver, Settings.System.GLOBAL_ACTIONS_LIST,
+                TextUtils.join(",", actionList));
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object objValue) {
+        return false;
+    }
+
+    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+                        boolean enabled) {
+                    ArrayList<SearchIndexableResource> result =
+                            new ArrayList<SearchIndexableResource>();
+
+                    SearchIndexableResource sir = new SearchIndexableResource(context);
+                    sir.xmlResId = R.xml.global_actions;
+                    result.add(sir);
+
+                    return result;
+                }
+
+                @Override
+                public List<String> getNonIndexableKeys(Context context) {
+                    ArrayList<String> result = new ArrayList<String>();
+                    return result;
+                }
+            };
+}
+