mobile/android/base/prompts/PromptListItem.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial