mobile/android/base/prompts/PromptListItem.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 package org.mozilla.gecko.prompts;
     3 import org.mozilla.gecko.gfx.BitmapUtils;
     4 import org.mozilla.gecko.GeckoAppShell;
     5 import org.mozilla.gecko.widget.GeckoActionProvider;
     7 import org.json.JSONArray;
     8 import org.json.JSONObject;
     9 import org.json.JSONException;
    11 import android.content.Context;
    12 import android.content.Intent;
    13 import android.graphics.drawable.Drawable;
    14 import android.util.Log;
    16 import java.util.List;
    17 import java.util.ArrayList;
    19 // This class should die and be replaced with normal menu items
    20 public class PromptListItem {
    21     private static final String LOGTAG = "GeckoPromptListItem";
    22     public final String label;
    23     public final boolean isGroup;
    24     public final boolean inGroup;
    25     public final boolean disabled;
    26     public final int id;
    27     public final boolean showAsActions;
    28     public final boolean isParent;
    30     public Intent mIntent;
    31     public boolean mSelected;
    32     public Drawable mIcon;
    34     PromptListItem(JSONObject aObject) {
    35         Context context = GeckoAppShell.getContext();
    36         label = aObject.isNull("label") ? "" : aObject.optString("label");
    37         isGroup = aObject.optBoolean("isGroup");
    38         inGroup = aObject.optBoolean("inGroup");
    39         disabled = aObject.optBoolean("disabled");
    40         id = aObject.optInt("id");
    41         mSelected = aObject.optBoolean("selected");
    43         JSONObject obj = aObject.optJSONObject("showAsActions");
    44         if (obj != null) {
    45             showAsActions = true;
    46             String uri = obj.isNull("uri") ? "" : obj.optString("uri");
    47             String type = obj.isNull("type") ? GeckoActionProvider.DEFAULT_MIME_TYPE :
    48                                                obj.optString("type", GeckoActionProvider.DEFAULT_MIME_TYPE);
    50             mIntent = GeckoAppShell.getShareIntent(context, uri, type, "");
    51             isParent = true;
    52         } else {
    53             mIntent = null;
    54             showAsActions = false;
    55             // Support both "isParent" (backwards compat for older consumers), and "menu" for the new Tabbed prompt ui.
    56             isParent = aObject.optBoolean("isParent") || aObject.optBoolean("menu");
    57         }
    59         final String iconStr = aObject.optString("icon");
    60         if (iconStr != null) {
    61             BitmapUtils.getDrawable(context, iconStr, new BitmapUtils.BitmapLoader() {
    62                     @Override
    63                     public void onBitmapFound(Drawable d) {
    64                         mIcon = d;
    65                     }
    66                 });
    67         }
    68     }
    70     public void setIntent(Intent i) {
    71         mIntent = i;
    72     }
    74     public Intent getIntent() {
    75         return mIntent;
    76     }
    78     public void setIcon(Drawable icon) {
    79         mIcon = icon;
    80     }
    82     public Drawable getIcon() {
    83         return mIcon;
    84     }
    86     public void setSelected(boolean selected) {
    87         mSelected = selected;
    88     }
    90     public boolean getSelected() {
    91         return mSelected;
    92     }
    94     public PromptListItem(String aLabel) {
    95         label = aLabel;
    96         isGroup = false;
    97         inGroup = false;
    98         isParent = false;
    99         disabled = false;
   100         id = 0;
   101         showAsActions = false;
   102     }
   104     static PromptListItem[] getArray(JSONArray items) {
   105         if (items == null) {
   106             return new PromptListItem[0];
   107         }
   109         int length = items.length();
   110         List<PromptListItem> list = new ArrayList<PromptListItem>(length);
   111         for (int i = 0; i < length; i++) {
   112             try {
   113                 PromptListItem item = new PromptListItem(items.getJSONObject(i));
   114                 list.add(item);
   115             } catch(Exception ex) { }
   116         }
   118         PromptListItem[] arrays = new PromptListItem[length];
   119         list.toArray(arrays);
   120         return arrays;
   121     }
   122 }

mercurial