Active Edge: allow to choose a specific app activity [2/4]

Change-Id: I54fb88f588c192620708a43341a317caa1d77fd7
diff --git a/res/values/strings.xml b/res/values/strings.xml
index e91b668..88a82eb 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -42,4 +42,6 @@
     <string name="set">Set</string>
     <string name="color_default">Default</string>
 
+    <string name="active_edge_app_select_title">Select app</string>
+    <string name="active_edge_activity_select_title">Select activity</string>
 </resources>
diff --git a/src/com/bliss/support/preferences/AppPicker.java b/src/com/bliss/support/preferences/AppPicker.java
index 72e71dd..ed57e8a 100644
--- a/src/com/bliss/support/preferences/AppPicker.java
+++ b/src/com/bliss/support/preferences/AppPicker.java
@@ -18,13 +18,16 @@
 
 import android.app.ListActivity;
 import android.content.Context;
+import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.ListView;
@@ -43,21 +46,43 @@
     protected List<ApplicationInfo> applist = null;
     Adapter listadapter = null;
 
+    protected List<ActivityInfo> mActivitiesList = null;
+    protected boolean mIsActivitiesList = false;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(android.R.layout.list_content);
+        setTitle(R.string.active_edge_app_select_title);
 
         packageManager = getPackageManager();
         new LoadApplications().execute();
     }
 
     protected void onListItemClick(ListView l, View v, int position, long id) {
-        super.onListItemClick(l, v, position, id);
+        if (!mIsActivitiesList) {
+            // we are in the Apps list
+        } else if (mIsActivitiesList) {
+            // we are in the Activities list
+        }
+
+        mIsActivitiesList = false;
 
         finish();
     }
 
+    @Override
+    public void onBackPressed() {
+        if (mIsActivitiesList) {
+            // Reset the dialog again
+            setListAdapter(listadapter);
+            setTitle(R.string.active_edge_app_select_title);
+            mIsActivitiesList = false;
+        } else {
+            finish();
+        }
+    }
+
     private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
         ArrayList<ApplicationInfo> applist = new ArrayList<>();
 
@@ -95,7 +120,7 @@
             applist = checkForLaunchIntent(packageManager.getInstalledApplications(
                     PackageManager.GET_META_DATA));
             listadapter = new Adapter(AppPicker.this,
-                    R.layout.app_list_item, applist);
+                    R.layout.app_list_item, applist, packageManager);
             return null;
         }
 
@@ -103,6 +128,16 @@
         protected void onPostExecute(Void result) {
             super.onPostExecute(result);
             setListAdapter(listadapter);
+
+            getListView().setLongClickable(true);
+            getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
+                @Override
+                public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
+                        int pos, long id) {
+                    onLongClick(pos);
+                    return true;
+                }
+            });
         }
 
         @Override
@@ -111,18 +146,50 @@
         }
     }
 
+    protected void onLongClick(int pos) {
+        /*if (mIsActivitiesList) return;
+        String packageName = applist.get(position).packageName;
+        showActivitiesDialog(packageName);*/
+    }
+
+    protected void showActivitiesDialog(String packageName) {
+        mIsActivitiesList = true;
+        ArrayList<ActivityInfo> list = null;
+        try {
+            PackageInfo pi = packageManager.getPackageInfo (
+                    packageName, PackageManager.GET_ACTIVITIES);
+
+            list = new ArrayList<>(Arrays.asList(pi.activities));
+
+        } catch (PackageManager.NameNotFoundException e) {
+        }
+
+        mActivitiesList = list;
+
+        if (list == null) {
+            // no activities to show, let's stay in the Apps list
+            mIsActivitiesList = false;
+            return;
+        }
+
+        setTitle(R.string.active_edge_activity_select_title);
+        // switch to a new adapter to show app activities
+        ActivitiesAdapter adapter = new ActivitiesAdapter(this, R.layout.app_list_item, list, packageManager);
+        setListAdapter(adapter);
+    }
+
     class Adapter extends ArrayAdapter<ApplicationInfo> {
 
         private List<ApplicationInfo> appList;
         private Context context;
         private PackageManager packageManager;
 
-        private Adapter(Context context, int resource, List<ApplicationInfo> objects) {
+        private Adapter(Context context, int resource, List<ApplicationInfo> objects, PackageManager pm) {
             super(context, resource, objects);
 
             this.context = context;
             this.appList = objects;
-            packageManager = context.getPackageManager();
+            packageManager = pm;
         }
 
         @Override
@@ -162,4 +229,59 @@
             return view;
         }
     }
+
+    class ActivitiesAdapter extends ArrayAdapter<ActivityInfo> {
+
+        private List<ActivityInfo> appList;
+        private Context context;
+        private PackageManager packageManager;
+
+        private ActivitiesAdapter(Context context, int resource, List<ActivityInfo> objects, PackageManager pm) {
+            super(context, resource, objects);
+
+            this.context = context;
+            this.appList = objects;
+            this.packageManager = pm;
+        }
+
+        @Override
+        public int getCount() {
+            return ((null != appList) ? appList.size() : 0);
+        }
+
+        @Override
+        public ActivityInfo getItem(int position) {
+            return ((null != appList) ? appList.get(position) : null);
+        }
+
+        @Override
+        public long getItemId(int position) {
+            return position;
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            View view = convertView;
+
+            ActivityInfo data = appList.get(position);
+
+            if (view == null) {
+                LayoutInflater layoutInflater = (LayoutInflater) context
+                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+                view = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
+            }
+
+            if (data != null) {
+                TextView appName = view.findViewById(android.R.id.text1);
+
+                String name = /*data.loadLabel(packageManager).toString();
+                if (name == null) {
+                    name = */data.name;
+                //}
+                appName.setText(name);
+            }
+
+            return view;
+        }
+    }
 }