Bug 942270 - Add intents to PromptListItems. r=bnicholson
authorWes Johnston <wjohnston@mozilla.com>
Fri, 14 Mar 2014 14:41:10 -0700
changeset 191810 6838701222096aa2d37a6b3afe71dd23820fe6cd
parent 191809 d234b1080a13b2103983666139170c9a31810251
child 191811 4c8b77b42f5534d3fbc91a6803b1436ea254f5e0
push id474
push userasasaki@mozilla.com
push dateMon, 02 Jun 2014 21:01:02 +0000
treeherdermozilla-release@967f4cf1b31c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs942270
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 942270 - Add intents to PromptListItems. r=bnicholson
mobile/android/base/GeckoAppShell.java
mobile/android/base/menu/MenuItemActionView.java
mobile/android/base/menu/MenuItemDefault.java
mobile/android/base/prompts/IntentChooserPrompt.java
mobile/android/base/prompts/PromptListAdapter.java
mobile/android/base/prompts/PromptListItem.java
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -1192,20 +1192,20 @@ public class GeckoAppShell
      *
      * @param context a <code>Context</code> instance.
      * @param targetURI the string spec of the URI to open.
      * @param mimeType an optional MIME type string.
      * @param title the title to use in <code>ACTION_SEND</code> intents.
      * @return an <code>Intent</code>, or <code>null</code> if none could be
      *         produced.
      */
-    static Intent getShareIntent(final Context context,
-                                 final String targetURI,
-                                 final String mimeType,
-                                 final String title) {
+    public static Intent getShareIntent(final Context context,
+                                        final String targetURI,
+                                        final String mimeType,
+                                        final String title) {
         Intent shareIntent = getIntentForActionString(Intent.ACTION_SEND);
         shareIntent.putExtra(Intent.EXTRA_TEXT, targetURI);
         shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
 
         // Note that EXTRA_TITLE is intended to be used for share dialog
         // titles. Common usage (e.g., Pocket) suggests that it's sometimes
         // interpreted as an alternate to EXTRA_SUBJECT, so we include it.
         shareIntent.putExtra(Intent.EXTRA_TITLE, title);
--- a/mobile/android/base/menu/MenuItemActionView.java
+++ b/mobile/android/base/menu/MenuItemActionView.java
@@ -97,16 +97,35 @@ public class MenuItemActionView extends 
         }
     }
 
     @Override
     public void setShowIcon(boolean show) {
         mMenuItem.setShowIcon(show);
     }
 
+    public void setIcon(Drawable icon) {
+        mMenuItem.setIcon(icon);
+        mMenuButton.setIcon(icon);
+    }
+
+    public void setIcon(int icon) {
+        mMenuItem.setIcon(icon);
+        mMenuButton.setIcon(icon);
+    }
+
+    public void setTitle(CharSequence title) {
+        mMenuItem.setTitle(title);
+        mMenuButton.setContentDescription(title);
+    }
+
+    public void setSubMenuIndicator(boolean hasSubMenu) {
+        mMenuItem.setSubMenuIndicator(hasSubMenu);
+    }
+
     public void addActionButton(Drawable drawable) {
         // If this is the first icon, retain the text.
         // If not, make the menu item an icon.
         final int count = mActionButtons.size();
         mMenuItem.setVisibility(View.GONE);
         mMenuButton.setVisibility(View.VISIBLE);
 
         if (drawable != null) {
--- a/mobile/android/base/menu/MenuItemDefault.java
+++ b/mobile/android/base/menu/MenuItemDefault.java
@@ -137,15 +137,15 @@ public class MenuItemDefault extends Tex
     @Override
     public void setShowIcon(boolean show) {
         if (mShowIcon != show) {
             mShowIcon = show;
             refreshIcon();
         }
     }
 
-    private void setSubMenuIndicator(boolean hasSubMenu) {
+    void setSubMenuIndicator(boolean hasSubMenu) {
         if (mHasSubMenu != hasSubMenu) {
             mHasSubMenu = hasSubMenu;
             refreshDrawableState();
         }
     }
 }
--- a/mobile/android/base/prompts/IntentChooserPrompt.java
+++ b/mobile/android/base/prompts/IntentChooserPrompt.java
@@ -56,17 +56,17 @@ public class IntentChooserPrompt {
         if (mItems.isEmpty()) {
             Log.i(LOGTAG, "No activities for the intent chooser!");
             handler.onCancelled();
             return;
         }
 
         // If there's only one item in the intent list, just return it
         if (mItems.size() == 1) {
-            handler.onIntentSelected(mItems.get(0).intent, 0);
+            handler.onIntentSelected(mItems.get(0).getIntent(), 0);
             return;
         }
 
         final Prompt prompt = new Prompt(context, new Prompt.PromptCallback() {
             @Override
             public void onPromptFinished(String promptServiceResult) {
                 if (handler == null) {
                     return;
@@ -77,17 +77,17 @@ public class IntentChooserPrompt {
                     itemId = new JSONObject(promptServiceResult).getInt("button");
                 } catch (JSONException e) {
                     Log.e(LOGTAG, "result from promptservice was invalid: ", e);
                 }
 
                 if (itemId == -1) {
                     handler.onCancelled();
                 } else {
-                    handler.onIntentSelected(mItems.get(itemId).intent, itemId);
+                    handler.onIntentSelected(mItems.get(itemId).getIntent(), itemId);
                 }
             }
         });
 
         PromptListItem[] arrays = new PromptListItem[mItems.size()];
         mItems.toArray(arrays);
         prompt.show(title, "", arrays, ListView.CHOICE_MODE_NONE);
 
@@ -123,22 +123,24 @@ public class IntentChooserPrompt {
             items.add(getItemForResolveInfo(info, packageManager, provider.getIntent()));
         }
 
         return items;
     }
 
     private PromptListItem getItemForResolveInfo(ResolveInfo info, PackageManager pm, Intent intent) {
         PromptListItem item = new PromptListItem(info.loadLabel(pm).toString());
-        item.icon = info.loadIcon(pm);
-        item.intent = new Intent(intent);
+        item.setIcon(info.loadIcon(pm));
 
+        Intent i = new Intent(intent);
         // These intents should be implicit.
-        item.intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
-                                                   info.activityInfo.name));
+        i.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
+                                         info.activityInfo.name));
+        item.setIntent(new Intent(i));
+
         return item;
     }
 
     private ArrayList<PromptListItem> getItemsForIntent(Context context, Intent intent) {
         ArrayList<PromptListItem> items = new ArrayList<PromptListItem>();
         PackageManager pm = context.getPackageManager();
         List<ResolveInfo> lri = pm.queryIntentActivityOptions(GeckoAppShell.getGeckoInterface().getActivity().getComponentName(), null, intent, 0);
 
--- a/mobile/android/base/prompts/PromptListAdapter.java
+++ b/mobile/android/base/prompts/PromptListAdapter.java
@@ -85,33 +85,33 @@ public class PromptListAdapter extends A
         if (mBlankDrawable == null) {
             mBlankDrawable = res.getDrawable(R.drawable.blank);
         }
         return mBlankDrawable;
     }
 
     public void toggleSelected(int position) {
         PromptListItem item = getItem(position);
-        item.selected = !item.selected;
+        item.setSelected(!item.getSelected());
     }
 
     private void maybeUpdateIcon(PromptListItem item, TextView t) {
-        if (item.icon == null && !item.inGroup && !item.isParent) {
+        if (item.getIcon() == null && !item.inGroup && !item.isParent) {
             t.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
             return;
         }
 
         Drawable d = null;
         Resources res = getContext().getResources();
         // Set the padding between the icon and the text.
         t.setCompoundDrawablePadding(mIconTextPadding);
-        if (item.icon != null) {
+        if (item.getIcon() != null) {
             // We want the icon to be of a specific size. Some do not
             // follow this rule so we have to resize them.
-            Bitmap bitmap = ((BitmapDrawable) item.icon).getBitmap();
+            Bitmap bitmap = ((BitmapDrawable) item.getIcon()).getBitmap();
             d = new BitmapDrawable(res, Bitmap.createScaledBitmap(bitmap, mIconSize, mIconSize, true));
         } else if (item.inGroup) {
             // We don't currently support "indenting" items with icons
             d = getBlankDrawable(res);
         }
 
         Drawable moreDrawable = null;
         if (item.isParent) {
@@ -125,22 +125,22 @@ public class PromptListAdapter extends A
 
     private void maybeUpdateCheckedState(ListView list, int position, PromptListItem item, ViewHolder viewHolder) {
         viewHolder.textView.setEnabled(!item.disabled && !item.isGroup);
         viewHolder.textView.setClickable(item.isGroup || item.disabled);
         if (viewHolder.textView instanceof CheckedTextView) {
             // Apparently just using ct.setChecked(true) doesn't work, so this
             // is stolen from the android source code as a way to set the checked
             // state of these items
-            list.setItemChecked(position, item.selected);
+            list.setItemChecked(position, item.getSelected());
         }
     }
 
     boolean isSelected(int position){
-        return getItem(position).selected;
+        return getItem(position).getSelected();
     }
 
     ArrayList<Integer> getSelected() {
         int length = getCount();
 
         ArrayList<Integer> selected = new ArrayList<Integer>();
         for (int i = 0; i< length; i++) {
             if (isSelected(i)) {
--- a/mobile/android/base/prompts/PromptListItem.java
+++ b/mobile/android/base/prompts/PromptListItem.java
@@ -1,50 +1,100 @@
 package org.mozilla.gecko.prompts;
 
+import org.mozilla.gecko.gfx.BitmapUtils;
+import org.mozilla.gecko.GeckoAppShell;
+
 import org.json.JSONArray;
 import org.json.JSONObject;
 import org.json.JSONException;
 
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
+import android.util.Log;
 
 import java.util.List;
 import java.util.ArrayList;
 
 // This class should die and be replaced with normal menu items
 public class PromptListItem {
     private static final String LOGTAG = "GeckoPromptListItem";
     public final String label;
     public final boolean isGroup;
     public final boolean inGroup;
     public final boolean disabled;
     public final int id;
-    public boolean selected;
-    public Intent intent;
+    public final boolean showAsActions;
+    public final boolean isParent;
 
-    public boolean isParent;
-    public Drawable icon;
+    public Intent mIntent;
+    public boolean mSelected;
+    public Drawable mIcon;
 
     PromptListItem(JSONObject aObject) {
-        label = aObject.optString("label");
+        label = aObject.isNull("label") ? "" : aObject.optString("label");
         isGroup = aObject.optBoolean("isGroup");
         inGroup = aObject.optBoolean("inGroup");
         disabled = aObject.optBoolean("disabled");
         id = aObject.optInt("id");
-        isParent = aObject.optBoolean("isParent");
-        selected = aObject.optBoolean("selected");
+        mSelected = aObject.optBoolean("selected");
+
+        JSONObject obj = aObject.optJSONObject("shareData");
+        if (obj != null) {
+            showAsActions = true;
+            String uri = obj.isNull("uri") ? "" : obj.optString("uri");
+            String type = obj.isNull("type") ? "" : obj.optString("type");
+            mIntent = GeckoAppShell.getShareIntent(GeckoAppShell.getContext(), uri, type, "");
+            isParent = true;
+        } else {
+            mIntent = null;
+            showAsActions = false;
+            isParent = aObject.optBoolean("isParent");
+        }
+
+        BitmapUtils.getDrawable(GeckoAppShell.getContext(), aObject.optString("icon"), new BitmapUtils.BitmapLoader() {
+            @Override
+            public void onBitmapFound(Drawable d) {
+                mIcon = d;
+            }
+        });
+    }
+
+    public void setIntent(Intent i) {
+        mIntent = i;
+    }
+
+    public Intent getIntent() {
+        return mIntent;
+    }
+
+    public void setIcon(Drawable icon) {
+        mIcon = icon;
+    }
+
+    public Drawable getIcon() {
+        return mIcon;
+    }
+
+    public void setSelected(boolean selected) {
+        mSelected = selected;
+    }
+
+    public boolean getSelected() {
+        return mSelected;
     }
 
     public PromptListItem(String aLabel) {
         label = aLabel;
         isGroup = false;
         inGroup = false;
+        isParent = false;
         disabled = false;
         id = 0;
+        showAsActions = false;
     }
 
     static PromptListItem[] getArray(JSONArray items) {
         if (items == null) {
             return new PromptListItem[0];
         }
 
         int length = items.length();