OmniGears: expanded desktop: add only launcher filter

Change-Id: I3f75f0b9bb84cd953eab9aa80eb407bec5820cde
diff --git a/res/menu/expanded_desktop_menu.xml b/res/menu/expanded_desktop_menu.xml
new file mode 100644
index 0000000..0c3705e
--- /dev/null
+++ b/res/menu/expanded_desktop_menu.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/show_all_apps"
+        android:title="@string/show_all_apps_menu" />
+</menu>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 51b96f5..2ea865a 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -726,4 +726,6 @@
     <string name="expanded_desktop_state">Expanded state</string>
     <string name="expanded_enabled_for_all">Enabled for all</string>
     <string name="expanded_user_configurable">User configurable</string>
+    <string name="show_only_launcher_menu">Only launcher apps</string>
+    <string name="show_all_apps_menu">All apps</string>
 </resources>
diff --git a/src/org/omnirom/omnigears/interfacesettings/ExpandedDesktop.java b/src/org/omnirom/omnigears/interfacesettings/ExpandedDesktop.java
index ab4a58d..ada5ff2 100644
--- a/src/org/omnirom/omnigears/interfacesettings/ExpandedDesktop.java
+++ b/src/org/omnirom/omnigears/interfacesettings/ExpandedDesktop.java
@@ -30,6 +30,9 @@
 import android.provider.Settings.SettingNotFoundException;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManagerGlobal;
@@ -84,6 +87,8 @@
             new HashMap<String, ApplicationsState.AppEntry>();
     private int mExpandedDesktopState;
     private SwitchBar mSwitchBar;
+    private boolean mOnlyLauncher = true;
+    private MenuItem mMenuItem;
 
     private int getExpandedDesktopState(ContentResolver cr) {
         String value = Settings.Global.getString(cr, Settings.Global.POLICY_CONTROL);
@@ -107,6 +112,8 @@
                     Settings.Global.POLICY_CONTROL);
         }
         mAllPackagesAdapter = new AllPackagesAdapter(getActivity());
+
+        setHasOptionsMenu(true);
     }
 
     @Override
@@ -304,6 +311,33 @@
         }
     }
 
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        inflater.inflate(R.menu.expanded_desktop_menu, menu);
+        mMenuItem = menu.findItem(R.id.show_all_apps);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+        case R.id.show_all_apps:
+            mOnlyLauncher = !mOnlyLauncher;
+            mActivityFilter.updateLauncherInfoList();
+            rebuild();
+            break;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
+    @Override
+    public void onPrepareOptionsMenu (Menu menu) {
+        super.onPrepareOptionsMenu(menu);
+        if (mMenuItem != null) {
+            mMenuItem.setTitle(mOnlyLauncher ? getResources().getString(R.string.show_all_apps_menu)
+                    : getResources().getString(R.string.show_only_launcher_menu));
+        }
+    }
+
     private class AllPackagesAdapter extends BaseAdapter
             implements AdapterView.OnItemSelectedListener, SectionIndexer {
 
@@ -507,7 +541,6 @@
 
         private final PackageManager mPackageManager;
         private final List<String> launcherResolveInfoList = new ArrayList<String>();
-        private boolean onlyLauncher = false;
 
         private ActivityFilter(PackageManager packageManager) {
             this.mPackageManager = packageManager;
@@ -535,7 +568,7 @@
         @Override
         public boolean filterApp(AppEntry info) {
             boolean show = !mAllPackagesAdapter.entries.contains(info.info.packageName);
-            if (show && onlyLauncher) {
+            if (show && mOnlyLauncher) {
                 synchronized (launcherResolveInfoList) {
                     show = launcherResolveInfoList.contains(info.info.packageName);
                 }