Bug 973013 - Pull PromptListItem into its own class. r=bnicholson
authorWesley Johnston <wjohnston>
Thu, 06 Feb 2014 16:13:00 -0800
changeset 170449 b92e36eba34932089a1e205070838104ef7037e4
parent 170448 f3aa442c84e46237de05da61f4725caf5a34f009
child 170450 6c1982ae44001e40561649062a3c70e1f6940f9e
push id26288
push userryanvm@gmail.com
push dateTue, 25 Feb 2014 20:20:43 +0000
treeherdermozilla-central@22650589a724 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs973013
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 973013 - Pull PromptListItem into its own class. r=bnicholson
mobile/android/base/BrowserApp.java
mobile/android/base/moz.build
mobile/android/base/prompts/Prompt.java
mobile/android/base/prompts/PromptListItem.java
mobile/android/base/prompts/PromptService.java
--- a/mobile/android/base/BrowserApp.java
+++ b/mobile/android/base/BrowserApp.java
@@ -36,16 +36,17 @@ import org.mozilla.gecko.home.BrowserSea
 import org.mozilla.gecko.home.HomeBanner;
 import org.mozilla.gecko.home.HomeConfigInvalidator;
 import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.SearchEngine;
 import org.mozilla.gecko.menu.GeckoMenu;
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.prompts.Prompt;
+import org.mozilla.gecko.prompts.PromptListItem;
 import org.mozilla.gecko.sync.setup.SyncAccounts;
 import org.mozilla.gecko.toolbar.AutocompleteHandler;
 import org.mozilla.gecko.toolbar.BrowserToolbar;
 import org.mozilla.gecko.util.Clipboard;
 import org.mozilla.gecko.util.GamepadUtils;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.MenuUtils;
 import org.mozilla.gecko.util.StringUtils;
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -293,16 +293,17 @@ gbjar.sources += [
     'preferences/SearchPreferenceCategory.java',
     'preferences/SyncPreference.java',
     'PrefsHelper.java',
     'PrivateTab.java',
     'prompts/ColorPickerInput.java',
     'prompts/IconGridInput.java',
     'prompts/Prompt.java',
     'prompts/PromptInput.java',
+    'prompts/PromptListItem.java',
     'prompts/PromptService.java',
     'ReaderModeUtils.java',
     'ReferrerReceiver.java',
     'RemoteTabs.java',
     'Restarter.java',
     'ScrollAnimator.java',
     'ServiceNotificationClient.java',
     'SessionParser.java',
--- a/mobile/android/base/prompts/Prompt.java
+++ b/mobile/android/base/prompts/Prompt.java
@@ -428,17 +428,17 @@ public class Prompt implements OnClickLi
         JSONArray inputs = getSafeArray(geckoObject, "inputs");
         mInputs = new PromptInput[inputs.length()];
         for (int i = 0; i < mInputs.length; i++) {
             try {
                 mInputs[i] = PromptInput.getInput(inputs.getJSONObject(i));
             } catch(Exception ex) { }
         }
 
-        PromptListItem[] menuitems = getListItemArray(geckoObject, "listitems");
+        PromptListItem[] menuitems = PromptListItem.getArray(geckoObject.optJSONArray("listitems"));
         mSelected = getBooleanArray(geckoObject, "selected");
         boolean multiple = geckoObject.optBoolean("multiple");
         show(title, text, menuitems, multiple);
     }
 
     private static JSONArray getSafeArray(JSONObject json, String key) {
         try {
             return json.getJSONArray(key);
@@ -469,58 +469,16 @@ public class Prompt implements OnClickLi
         for (int i = 0; i < length; i++) {
             try {
                 list[i] = items.getBoolean(i);
             } catch(Exception ex) { }
         }
         return list;
     }
 
-    private PromptListItem[] getListItemArray(JSONObject aObject, String aName) {
-        JSONArray items = getSafeArray(aObject, aName);
-        int length = items.length();
-        PromptListItem[] list = new PromptListItem[length];
-        for (int i = 0; i < length; i++) {
-            try {
-                list[i] = new PromptListItem(items.getJSONObject(i));
-            } catch(Exception ex) { }
-        }
-        return list;
-    }
-
-    public static class PromptListItem {
-        public final String label;
-        public final boolean isGroup;
-        public final boolean inGroup;
-        public final boolean disabled;
-        public final int id;
-        public final boolean isParent;
-
-        // This member can't be accessible from JS, see bug 733749.
-        public Drawable icon;
-
-        PromptListItem(JSONObject aObject) {
-            label = aObject.optString("label");
-            isGroup = aObject.optBoolean("isGroup");
-            inGroup = aObject.optBoolean("inGroup");
-            disabled = aObject.optBoolean("disabled");
-            id = aObject.optInt("id");
-            isParent = aObject.optBoolean("isParent");
-        }
-
-        public PromptListItem(String aLabel) {
-            label = aLabel;
-            isGroup = false;
-            inGroup = false;
-            disabled = false;
-            id = 0;
-            isParent = false;
-        }
-    }
-
     public interface PromptCallback {
         public void onPromptFinished(String jsonResult);
     }
 
     public class PromptListAdapter extends ArrayAdapter<PromptListItem> {
         private static final int VIEW_TYPE_ITEM = 0;
         private static final int VIEW_TYPE_GROUP = 1;
         private static final int VIEW_TYPE_COUNT = 2;
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/prompts/PromptListItem.java
@@ -0,0 +1,59 @@
+package org.mozilla.gecko.prompts;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.JSONException;
+
+import android.graphics.drawable.Drawable;
+import java.util.List;
+import java.util.ArrayList;
+
+// This class should die and be replaced with normal menu items
+public class PromptListItem {
+    private static final String LOGTAG = "GeckoPromptListItem";
+    public final String label;
+    public final boolean isGroup;
+    public final boolean inGroup;
+    public final boolean disabled;
+    public final int id;
+    public boolean isParent;
+
+    public Drawable icon;
+
+    PromptListItem(JSONObject aObject) {
+        label = aObject.optString("label");
+        isGroup = aObject.optBoolean("isGroup");
+        inGroup = aObject.optBoolean("inGroup");
+        disabled = aObject.optBoolean("disabled");
+        id = aObject.optInt("id");
+        isParent = aObject.optBoolean("isParent");
+    }
+
+    public PromptListItem(String aLabel) {
+        label = aLabel;
+        isGroup = false;
+        inGroup = false;
+        disabled = false;
+        id = 0;
+        isParent = false;
+    }
+
+    static PromptListItem[] getArray(JSONArray items) {
+        if (items == null) {
+            return new PromptListItem[0];
+        }
+
+        int length = items.length();
+        List<PromptListItem> list = new ArrayList<PromptListItem>(length);
+        for (int i = 0; i < length; i++) {
+            try {
+                PromptListItem item = new PromptListItem(items.getJSONObject(i));
+                list.add(item);
+            } catch(Exception ex) { }
+        }
+
+        PromptListItem[] arrays = new PromptListItem[length];
+        list.toArray(arrays);
+        return arrays;
+    }
+}
--- a/mobile/android/base/prompts/PromptService.java
+++ b/mobile/android/base/prompts/PromptService.java
@@ -26,17 +26,17 @@ public class PromptService implements Ge
         mContext = context;
     }
 
     public void destroy() {
         GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:Show", this);
         GeckoAppShell.getEventDispatcher().unregisterEventListener("Prompt:ShowTop", this);
     }
 
-    public void show(final String aTitle, final String aText, final Prompt.PromptListItem[] aMenuList,
+    public void show(final String aTitle, final String aText, final PromptListItem[] aMenuList,
                      final boolean aMultipleSelection, final Prompt.PromptCallback callback) {
         // The dialog must be created on the UI thread.
         ThreadUtils.postToUiThread(new Runnable() {
             @Override
             public void run() {
                 Prompt p;
                 p = new Prompt(mContext, callback);
                 p.show(aTitle, aText, aMenuList, aMultipleSelection);