michael@0: package org.mozilla.gecko.prompts; michael@0: michael@0: import org.mozilla.gecko.gfx.BitmapUtils; michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.widget.GeckoActionProvider; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONObject; michael@0: import org.json.JSONException; michael@0: michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.Log; michael@0: michael@0: import java.util.List; michael@0: import java.util.ArrayList; michael@0: michael@0: // This class should die and be replaced with normal menu items michael@0: public class PromptListItem { michael@0: private static final String LOGTAG = "GeckoPromptListItem"; michael@0: public final String label; michael@0: public final boolean isGroup; michael@0: public final boolean inGroup; michael@0: public final boolean disabled; michael@0: public final int id; michael@0: public final boolean showAsActions; michael@0: public final boolean isParent; michael@0: michael@0: public Intent mIntent; michael@0: public boolean mSelected; michael@0: public Drawable mIcon; michael@0: michael@0: PromptListItem(JSONObject aObject) { michael@0: Context context = GeckoAppShell.getContext(); michael@0: label = aObject.isNull("label") ? "" : aObject.optString("label"); michael@0: isGroup = aObject.optBoolean("isGroup"); michael@0: inGroup = aObject.optBoolean("inGroup"); michael@0: disabled = aObject.optBoolean("disabled"); michael@0: id = aObject.optInt("id"); michael@0: mSelected = aObject.optBoolean("selected"); michael@0: michael@0: JSONObject obj = aObject.optJSONObject("showAsActions"); michael@0: if (obj != null) { michael@0: showAsActions = true; michael@0: String uri = obj.isNull("uri") ? "" : obj.optString("uri"); michael@0: String type = obj.isNull("type") ? GeckoActionProvider.DEFAULT_MIME_TYPE : michael@0: obj.optString("type", GeckoActionProvider.DEFAULT_MIME_TYPE); michael@0: michael@0: mIntent = GeckoAppShell.getShareIntent(context, uri, type, ""); michael@0: isParent = true; michael@0: } else { michael@0: mIntent = null; michael@0: showAsActions = false; michael@0: // Support both "isParent" (backwards compat for older consumers), and "menu" for the new Tabbed prompt ui. michael@0: isParent = aObject.optBoolean("isParent") || aObject.optBoolean("menu"); michael@0: } michael@0: michael@0: final String iconStr = aObject.optString("icon"); michael@0: if (iconStr != null) { michael@0: BitmapUtils.getDrawable(context, iconStr, new BitmapUtils.BitmapLoader() { michael@0: @Override michael@0: public void onBitmapFound(Drawable d) { michael@0: mIcon = d; michael@0: } michael@0: }); michael@0: } michael@0: } michael@0: michael@0: public void setIntent(Intent i) { michael@0: mIntent = i; michael@0: } michael@0: michael@0: public Intent getIntent() { michael@0: return mIntent; michael@0: } michael@0: michael@0: public void setIcon(Drawable icon) { michael@0: mIcon = icon; michael@0: } michael@0: michael@0: public Drawable getIcon() { michael@0: return mIcon; michael@0: } michael@0: michael@0: public void setSelected(boolean selected) { michael@0: mSelected = selected; michael@0: } michael@0: michael@0: public boolean getSelected() { michael@0: return mSelected; michael@0: } michael@0: michael@0: public PromptListItem(String aLabel) { michael@0: label = aLabel; michael@0: isGroup = false; michael@0: inGroup = false; michael@0: isParent = false; michael@0: disabled = false; michael@0: id = 0; michael@0: showAsActions = false; michael@0: } michael@0: michael@0: static PromptListItem[] getArray(JSONArray items) { michael@0: if (items == null) { michael@0: return new PromptListItem[0]; michael@0: } michael@0: michael@0: int length = items.length(); michael@0: List list = new ArrayList(length); michael@0: for (int i = 0; i < length; i++) { michael@0: try { michael@0: PromptListItem item = new PromptListItem(items.getJSONObject(i)); michael@0: list.add(item); michael@0: } catch(Exception ex) { } michael@0: } michael@0: michael@0: PromptListItem[] arrays = new PromptListItem[length]; michael@0: list.toArray(arrays); michael@0: return arrays; michael@0: } michael@0: }