[2/2] OmniGears: add navbar recents style

allow switching between default AOSP recents and
OmniSwitch

Change-Id: I0aab41c04e39f6f1598199ea415634730aae9f01
diff --git a/res/values/custom_arrays.xml b/res/values/custom_arrays.xml
index 764d4c7..8cd1433 100644
--- a/res/values/custom_arrays.xml
+++ b/res/values/custom_arrays.xml
@@ -147,4 +147,14 @@
         <item>1</item>
         <item>2</item>
     </string-array>
+
+    <string-array name="navbar_recents_style_entries" translatable="false">
+        <item>@string/keys_action_app_switch</item>
+        <item>@string/keys_action_omniswitch</item>
+    </string-array>
+
+    <string-array name="navbar_recents_style_values" translatable="false">
+        <item>0</item>
+        <item>1</item>
+    </string-array>
 </resources>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 6faf074..88282d3 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -41,7 +41,7 @@
     <string name="keys_app_switch_long_press_title">App switch key (long press)</string>
     <string name="keys_action_nothing">No action</string>
     <string name="keys_action_menu">Open/close menu</string>
-    <string name="keys_action_app_switch">Recent apps switcher</string>
+    <string name="keys_action_app_switch">AOSP recents</string>
     <string name="keys_action_search">Search assistant</string>
     <string name="keys_action_voice_search">Voice search</string>
     <string name="keys_action_in_app_search">In-app search</string>
@@ -169,4 +169,11 @@
     <string name="navigation_bar_show_summary">Force enable navigation bar</string>
     <string name="hardware_keys_disable_title">Disable keys</string>
     <string name="hardware_keys_disable_summary">Force disable hardware keys</string>
+
+    <string name="navbar_title">Navigation bar</string>
+    <string name="navbar_recents_style_title">Recents style</string>
+    <string name="omniswitch_settings">Settings</string>
+    <string name="omniswitch_dialog_running">OmniSwitch is not running. Please make sure to enable it before enabling it as recents.</string>
+    <string name="omniswitch_dialog_unavail">OmniSwitch is not installed.</string>
+    <string name="omniswitch_title">OmniSwitch</string>
 </resources>
diff --git a/res/xml/bars_settings.xml b/res/xml/bars_settings.xml
index 99b7b28..514cf80 100644
--- a/res/xml/bars_settings.xml
+++ b/res/xml/bars_settings.xml
@@ -34,7 +34,18 @@
                 android:entries="@array/entries_battery_percent"
                 android:entryValues="@array/values_battery_percent"
                 android:persistent="false"/>
+        </PreferenceCategory>
 
+        <PreferenceCategory
+            android:key="navbar_category"
+            android:title="@string/navbar_title" >
+
+            <ListPreference
+                android:key="navbar_recents_style"
+                android:title="@string/navbar_recents_style_title"
+                android:entries="@array/navbar_recents_style_entries"
+                android:entryValues="@array/navbar_recents_style_values"
+                android:persistent="false"/>
         </PreferenceCategory>
 
 </PreferenceScreen>
diff --git a/src/org/omnirom/omnigears/ButtonSettings.java b/src/org/omnirom/omnigears/ButtonSettings.java
index 204e4cf..491986e 100644
--- a/src/org/omnirom/omnigears/ButtonSettings.java
+++ b/src/org/omnirom/omnigears/ButtonSettings.java
@@ -64,6 +64,8 @@
 import com.android.settings.search.Indexable;
 import com.android.settings.preference.SystemCheckBoxPreference;
 
+import com.android.internal.util.omni.OmniSwitchConstants;
+import com.android.internal.util.omni.PackageUtils;
 import com.android.internal.util.omni.DeviceUtils;
 
 public class ButtonSettings extends SettingsPreferenceFragment implements OnPreferenceChangeListener, Indexable {
@@ -562,6 +564,7 @@
                     Settings.System.KEY_BACK_ACTION, value);
             mKeySettings.put(Settings.System.KEY_BACK_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mBackLongPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -572,6 +575,7 @@
                     Settings.System.KEY_BACK_LONG_PRESS_ACTION, value);
             mKeySettings.put(Settings.System.KEY_BACK_LONG_PRESS_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mHomePressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -582,6 +586,7 @@
                     Settings.System.KEY_HOME_ACTION, value);
             mKeySettings.put(Settings.System.KEY_HOME_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mHomeLongPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -592,6 +597,7 @@
                     Settings.System.KEY_HOME_LONG_PRESS_ACTION, value);
             mKeySettings.put(Settings.System.KEY_HOME_LONG_PRESS_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mHomeDoubleTapAction) {
             int value = Integer.valueOf((String) newValue);
@@ -602,6 +608,7 @@
                     Settings.System.KEY_HOME_DOUBLE_TAP_ACTION, value);
             mKeySettings.put(Settings.System.KEY_HOME_DOUBLE_TAP_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mMenuPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -612,6 +619,7 @@
                     Settings.System.KEY_MENU_ACTION, value);
             mKeySettings.put(Settings.System.KEY_MENU_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mMenuLongPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -622,6 +630,7 @@
                     Settings.System.KEY_MENU_LONG_PRESS_ACTION, value);
             mKeySettings.put(Settings.System.KEY_MENU_LONG_PRESS_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mAssistPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -632,6 +641,7 @@
                     Settings.System.KEY_ASSIST_ACTION, value);
             mKeySettings.put(Settings.System.KEY_ASSIST_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mAssistLongPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -642,6 +652,7 @@
                     Settings.System.KEY_ASSIST_LONG_PRESS_ACTION, value);
             mKeySettings.put(Settings.System.KEY_ASSIST_LONG_PRESS_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mAppSwitchPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -652,6 +663,7 @@
                     Settings.System.KEY_APP_SWITCH_ACTION, value);
             mKeySettings.put(Settings.System.KEY_APP_SWITCH_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
         } else if (preference == mAppSwitchLongPressAction) {
             int value = Integer.valueOf((String) newValue);
@@ -662,6 +674,7 @@
                     Settings.System.KEY_APP_SWITCH_LONG_PRESS_ACTION, value);
             mKeySettings.put(Settings.System.KEY_APP_SWITCH_LONG_PRESS_ACTION, value);
             checkForHomeKey();
+            checkForOmniSwitchRecents();
             return true;
 //        } else if (preference == mVolumeDefault) {
 //            int value = Integer.valueOf((String) newValue);
@@ -686,6 +699,17 @@
         return false;
     }
 
+    private boolean hasOmniSwitchKey() {
+        Iterator<Integer> nextAction = mKeySettings.values().iterator();
+        while (nextAction.hasNext()) {
+            int action = nextAction.next();
+            if (action == ACTION_OMNISWITCH) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private void checkForHomeKey() {
         if (!hasHomeKey()) {
             AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
@@ -702,6 +726,16 @@
         }
     }
 
+    private void checkForOmniSwitchRecents() {
+        if (hasOmniSwitchKey()) {
+            if (!isOmniSwitchInstalled()){
+                doOmniSwitchUnavail();
+            } else if (!OmniSwitchConstants.isOmniSwitchRunning(getActivity())) {
+                doOmniSwitchConfig();
+            }
+        }
+    }
+
 //    public void removeListEntry(ListPreference list, String valuetoRemove) {
 //        ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
 //        ArrayList<CharSequence> values = new ArrayList<CharSequence>();
@@ -734,6 +768,31 @@
         mKeysAssistCategory.setEnabled(!harwareKeysDisable && enableHWKeyRebinding);
     }
 
+    private void doOmniSwitchConfig() {
+        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
+        alertDialogBuilder.setTitle(R.string.omniswitch_title);
+        alertDialogBuilder.setMessage(R.string.omniswitch_dialog_running)
+            .setPositiveButton(R.string.omniswitch_settings, new DialogInterface.OnClickListener() {
+                public void onClick(DialogInterface dialog,int id) {
+                    startActivity(OmniSwitchConstants.INTENT_LAUNCH_APP);
+                }
+            });
+        AlertDialog alertDialog = alertDialogBuilder.create();
+        alertDialog.show();
+    }
+
+    private void doOmniSwitchUnavail() {
+        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
+        alertDialogBuilder.setTitle(R.string.omniswitch_title);
+        alertDialogBuilder.setMessage(R.string.omniswitch_dialog_unavail);
+        AlertDialog alertDialog = alertDialogBuilder.create();
+        alertDialog.show();
+    }
+
+    private boolean isOmniSwitchInstalled() {
+        return PackageUtils.isAvailableApp(OmniSwitchConstants.APP_PACKAGE_NAME, getActivity());
+    }
+
     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
             new BaseSearchIndexProvider() {
                 @Override
diff --git a/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java b/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
index 72ed3be..b1b0653 100644
--- a/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
+++ b/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
@@ -18,8 +18,10 @@
 
 package org.omnirom.omnigears.interfacesettings;
 
+import android.app.AlertDialog;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.os.RemoteException;
@@ -39,6 +41,9 @@
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
 
+import com.android.internal.util.omni.OmniSwitchConstants;
+import com.android.internal.util.omni.PackageUtils;
+
 import java.util.List;
 import java.util.ArrayList;
 
@@ -48,16 +53,19 @@
 
     private static final String STATUSBAR_BATTERY_STYLE = "statusbar_battery_style";
     private static final String STATUSBAR_BATTERY_PERCENT = "statusbar_battery_percent";
+    private static final String NAVIGATION_BAR_CATEGORY = "navbar_category";
+    private static final String NAVIGATION_BAR_RECENTS_STYLE = "navbar_recents_style";
 
     private ListPreference mBatteryStyle;
     private ListPreference mBatteryPercent;
+    private ListPreference mNavbarRecentsStyle;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.bars_settings);
 
-        PreferenceScreen prefSet = getPreferenceScreen();
+        PreferenceScreen prefScreen = getPreferenceScreen();
         ContentResolver resolver = getActivity().getContentResolver();
 
         mBatteryStyle = (ListPreference) findPreference(STATUSBAR_BATTERY_STYLE);
@@ -75,6 +83,18 @@
         mBatteryPercent.setValue(Integer.toString(batteryPercent));
         mBatteryPercent.setSummary(mBatteryPercent.getEntry());
         mBatteryPercent.setOnPreferenceChangeListener(this);
+
+        final boolean showNavBar = DeviceUtils.deviceSupportNavigationBar(getActivity());
+        final PreferenceCategory navbarCategory =
+                (PreferenceCategory) prefScreen.findPreference(NAVIGATION_BAR_CATEGORY);
+
+        mNavbarRecentsStyle = (ListPreference) findPreference(NAVIGATION_BAR_RECENTS_STYLE);
+        int recentsStyle = Settings.System.getInt(resolver,
+                Settings.System.NAVIGATION_BAR_RECENTS, 0);
+
+        mNavbarRecentsStyle.setValue(Integer.toString(recentsStyle));
+        mNavbarRecentsStyle.setSummary(mNavbarRecentsStyle.getEntry());
+        mNavbarRecentsStyle.setOnPreferenceChangeListener(this);
     }
 
     @Override
@@ -100,11 +120,50 @@
                     mBatteryPercent.getEntries()[index]);
             Settings.System.putInt(getContentResolver(),
                     Settings.System.STATUSBAR_BATTERY_PERCENT, value);
+        } else if (preference == mNavbarRecentsStyle) {
+            int value = Integer.valueOf((String) newValue);
+            if (value == 1) {
+                if (!isOmniSwitchInstalled()){
+                    doOmniSwitchUnavail();
+                } else if (!OmniSwitchConstants.isOmniSwitchRunning(getActivity())) {
+                    doOmniSwitchConfig();
+                }
+            }
+            int index = mNavbarRecentsStyle.findIndexOfValue((String) newValue);
+            mNavbarRecentsStyle.setSummary(
+                    mNavbarRecentsStyle.getEntries()[index]);
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.NAVIGATION_BAR_RECENTS, value);
         }
 
         return true;
     }
 
+    private void doOmniSwitchConfig() {
+        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
+        alertDialogBuilder.setTitle(R.string.omniswitch_title);
+        alertDialogBuilder.setMessage(R.string.omniswitch_dialog_running)
+            .setPositiveButton(R.string.omniswitch_settings,new DialogInterface.OnClickListener() {
+                public void onClick(DialogInterface dialog,int id) {
+                    startActivity(OmniSwitchConstants.INTENT_LAUNCH_APP);
+                }
+            });
+        AlertDialog alertDialog = alertDialogBuilder.create();
+        alertDialog.show();
+    }
+
+    private void doOmniSwitchUnavail() {
+        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
+        alertDialogBuilder.setTitle(R.string.omniswitch_title);
+        alertDialogBuilder.setMessage(R.string.omniswitch_dialog_unavail);
+        AlertDialog alertDialog = alertDialogBuilder.create();
+        alertDialog.show();
+    }
+
+    private boolean isOmniSwitchInstalled() {
+        return PackageUtils.isAvailableApp(OmniSwitchConstants.APP_PACKAGE_NAME, getActivity());
+    }
+
     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
             new BaseSearchIndexProvider() {
                 @Override