mobile/android/base/prompts/PromptListItem.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/prompts/PromptListItem.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +package org.mozilla.gecko.prompts;
     1.5 +
     1.6 +import org.mozilla.gecko.gfx.BitmapUtils;
     1.7 +import org.mozilla.gecko.GeckoAppShell;
     1.8 +import org.mozilla.gecko.widget.GeckoActionProvider;
     1.9 +
    1.10 +import org.json.JSONArray;
    1.11 +import org.json.JSONObject;
    1.12 +import org.json.JSONException;
    1.13 +
    1.14 +import android.content.Context;
    1.15 +import android.content.Intent;
    1.16 +import android.graphics.drawable.Drawable;
    1.17 +import android.util.Log;
    1.18 +
    1.19 +import java.util.List;
    1.20 +import java.util.ArrayList;
    1.21 +
    1.22 +// This class should die and be replaced with normal menu items
    1.23 +public class PromptListItem {
    1.24 +    private static final String LOGTAG = "GeckoPromptListItem";
    1.25 +    public final String label;
    1.26 +    public final boolean isGroup;
    1.27 +    public final boolean inGroup;
    1.28 +    public final boolean disabled;
    1.29 +    public final int id;
    1.30 +    public final boolean showAsActions;
    1.31 +    public final boolean isParent;
    1.32 +
    1.33 +    public Intent mIntent;
    1.34 +    public boolean mSelected;
    1.35 +    public Drawable mIcon;
    1.36 +
    1.37 +    PromptListItem(JSONObject aObject) {
    1.38 +        Context context = GeckoAppShell.getContext();
    1.39 +        label = aObject.isNull("label") ? "" : aObject.optString("label");
    1.40 +        isGroup = aObject.optBoolean("isGroup");
    1.41 +        inGroup = aObject.optBoolean("inGroup");
    1.42 +        disabled = aObject.optBoolean("disabled");
    1.43 +        id = aObject.optInt("id");
    1.44 +        mSelected = aObject.optBoolean("selected");
    1.45 +
    1.46 +        JSONObject obj = aObject.optJSONObject("showAsActions");
    1.47 +        if (obj != null) {
    1.48 +            showAsActions = true;
    1.49 +            String uri = obj.isNull("uri") ? "" : obj.optString("uri");
    1.50 +            String type = obj.isNull("type") ? GeckoActionProvider.DEFAULT_MIME_TYPE :
    1.51 +                                               obj.optString("type", GeckoActionProvider.DEFAULT_MIME_TYPE);
    1.52 +
    1.53 +            mIntent = GeckoAppShell.getShareIntent(context, uri, type, "");
    1.54 +            isParent = true;
    1.55 +        } else {
    1.56 +            mIntent = null;
    1.57 +            showAsActions = false;
    1.58 +            // Support both "isParent" (backwards compat for older consumers), and "menu" for the new Tabbed prompt ui.
    1.59 +            isParent = aObject.optBoolean("isParent") || aObject.optBoolean("menu");
    1.60 +        }
    1.61 +
    1.62 +        final String iconStr = aObject.optString("icon");
    1.63 +        if (iconStr != null) {
    1.64 +            BitmapUtils.getDrawable(context, iconStr, new BitmapUtils.BitmapLoader() {
    1.65 +                    @Override
    1.66 +                    public void onBitmapFound(Drawable d) {
    1.67 +                        mIcon = d;
    1.68 +                    }
    1.69 +                });
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    public void setIntent(Intent i) {
    1.74 +        mIntent = i;
    1.75 +    }
    1.76 +
    1.77 +    public Intent getIntent() {
    1.78 +        return mIntent;
    1.79 +    }
    1.80 +
    1.81 +    public void setIcon(Drawable icon) {
    1.82 +        mIcon = icon;
    1.83 +    }
    1.84 +
    1.85 +    public Drawable getIcon() {
    1.86 +        return mIcon;
    1.87 +    }
    1.88 +
    1.89 +    public void setSelected(boolean selected) {
    1.90 +        mSelected = selected;
    1.91 +    }
    1.92 +
    1.93 +    public boolean getSelected() {
    1.94 +        return mSelected;
    1.95 +    }
    1.96 +
    1.97 +    public PromptListItem(String aLabel) {
    1.98 +        label = aLabel;
    1.99 +        isGroup = false;
   1.100 +        inGroup = false;
   1.101 +        isParent = false;
   1.102 +        disabled = false;
   1.103 +        id = 0;
   1.104 +        showAsActions = false;
   1.105 +    }
   1.106 +
   1.107 +    static PromptListItem[] getArray(JSONArray items) {
   1.108 +        if (items == null) {
   1.109 +            return new PromptListItem[0];
   1.110 +        }
   1.111 +
   1.112 +        int length = items.length();
   1.113 +        List<PromptListItem> list = new ArrayList<PromptListItem>(length);
   1.114 +        for (int i = 0; i < length; i++) {
   1.115 +            try {
   1.116 +                PromptListItem item = new PromptListItem(items.getJSONObject(i));
   1.117 +                list.add(item);
   1.118 +            } catch(Exception ex) { }
   1.119 +        }
   1.120 +
   1.121 +        PromptListItem[] arrays = new PromptListItem[length];
   1.122 +        list.toArray(arrays);
   1.123 +        return arrays;
   1.124 +    }
   1.125 +}

mercurial