|
1 package org.mozilla.gecko.prompts; |
|
2 |
|
3 import org.mozilla.gecko.gfx.BitmapUtils; |
|
4 import org.mozilla.gecko.GeckoAppShell; |
|
5 import org.mozilla.gecko.widget.GeckoActionProvider; |
|
6 |
|
7 import org.json.JSONArray; |
|
8 import org.json.JSONObject; |
|
9 import org.json.JSONException; |
|
10 |
|
11 import android.content.Context; |
|
12 import android.content.Intent; |
|
13 import android.graphics.drawable.Drawable; |
|
14 import android.util.Log; |
|
15 |
|
16 import java.util.List; |
|
17 import java.util.ArrayList; |
|
18 |
|
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; |
|
29 |
|
30 public Intent mIntent; |
|
31 public boolean mSelected; |
|
32 public Drawable mIcon; |
|
33 |
|
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"); |
|
42 |
|
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); |
|
49 |
|
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 } |
|
58 |
|
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 } |
|
69 |
|
70 public void setIntent(Intent i) { |
|
71 mIntent = i; |
|
72 } |
|
73 |
|
74 public Intent getIntent() { |
|
75 return mIntent; |
|
76 } |
|
77 |
|
78 public void setIcon(Drawable icon) { |
|
79 mIcon = icon; |
|
80 } |
|
81 |
|
82 public Drawable getIcon() { |
|
83 return mIcon; |
|
84 } |
|
85 |
|
86 public void setSelected(boolean selected) { |
|
87 mSelected = selected; |
|
88 } |
|
89 |
|
90 public boolean getSelected() { |
|
91 return mSelected; |
|
92 } |
|
93 |
|
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 } |
|
103 |
|
104 static PromptListItem[] getArray(JSONArray items) { |
|
105 if (items == null) { |
|
106 return new PromptListItem[0]; |
|
107 } |
|
108 |
|
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 } |
|
117 |
|
118 PromptListItem[] arrays = new PromptListItem[length]; |
|
119 list.toArray(arrays); |
|
120 return arrays; |
|
121 } |
|
122 } |