Bug 752905 - Move the Prompt:Show handler out of handleGeckoMessage. r=margaret
authorKartikaya Gupta <kgupta@mozilla.com>
Wed, 13 Jun 2012 17:12:15 -0400
changeset 101436 fc8d0eefddb4b9cca287f68e79784d816b0e9788
parent 101435 87dbb95cde7ddacd65d46ed3f39b39cfe1e1560a
child 101437 eaab1f7507f737c0e59e3d00d8d07461ea5ca255
push id1316
push userakeybl@mozilla.com
push dateMon, 27 Aug 2012 22:37:00 +0000
treeherdermozilla-beta@db4b09302ee2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs752905
milestone16.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 752905 - Move the Prompt:Show handler out of handleGeckoMessage. r=margaret
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoAppShell.java
mobile/android/base/PromptService.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -97,16 +97,17 @@ abstract public class GeckoApp
     private static GeckoThread sGeckoThread = null;
     public Handler mMainHandler;
     private GeckoProfile mProfile;
     public static boolean sIsGeckoReady = false;
     public static int mOrientation;
 
     private GeckoConnectivityReceiver mConnectivityReceiver;
     private GeckoBatteryManager mBatteryReceiver;
+    private PromptService mPromptService;
 
     public static DoorHangerPopup mDoorHangerPopup;
     public static FormAssistPopup mFormAssistPopup;
     public TabsPanel mTabsPanel;
     public Favicons mFavicons;
 
     private static LayerController mLayerController;
     private static GeckoLayerClient mLayerClient;
@@ -1850,16 +1851,18 @@ abstract public class GeckoApp
         }
 
         mBatteryReceiver = new GeckoBatteryManager();
         mBatteryReceiver.registerFor(mAppContext);
 
         mConnectivityReceiver = new GeckoConnectivityReceiver();
         mConnectivityReceiver.registerFor(mAppContext);
 
+        mPromptService = new PromptService();
+
         GeckoNetworkManager.getInstance().init();
         GeckoNetworkManager.getInstance().start();
 
         GeckoScreenOrientationListener.getInstance().start();
 
         final GeckoApp self = this;
 
         GeckoAppShell.getHandler().postDelayed(new Runnable() {
@@ -2450,17 +2453,17 @@ abstract public class GeckoApp
     private class FilePickerPromptRunnable implements Runnable {
         public FilePickerPromptRunnable(String aTitle, PromptService.PromptListItem[] aItems) {
             super();
             mTitle = aTitle;
             mItems = aItems;
         }
 
         public void run() {
-            GeckoAppShell.getPromptService().Show(mTitle, "", null, mItems, false);
+            mPromptService.Show(mTitle, "", null, mItems, false);
         }
 
         private String mTitle;
         private PromptService.PromptListItem[] mItems;
     }
 
     private int addIntentActivitiesToList(Intent intent, ArrayList<PromptService.PromptListItem> items, ArrayList<Intent> aIntents) {
         PackageManager pm = mAppContext.getPackageManager();
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -59,17 +59,16 @@ public class GeckoAppShell
 
     // static members only
     private GeckoAppShell() { }
 
     static private LinkedList<GeckoEvent> gPendingEvents =
         new LinkedList<GeckoEvent>();
 
     static private boolean gRestartScheduled = false;
-    static private PromptService gPromptService = null;
 
     static private GeckoInputConnection mInputConnection = null;
 
     static private final HashMap<Integer, AlertNotification>
         mAlertNotifications = new HashMap<Integer, AlertNotification>();
 
     /* Keep in sync with constants found here:
       http://mxr.mozilla.org/mozilla-central/source/uriloader/base/nsIWebProgressListener.idl
@@ -1828,32 +1827,16 @@ public class GeckoAppShell
         //                "type": "value",
         //                "event_specific": "value",
         //                ....
         try {
             JSONObject json = new JSONObject(message);
             final JSONObject geckoObject = json.getJSONObject("gecko");
             String type = geckoObject.getString("type");
             
-            if (type.equals("Prompt:Show")) {
-                getHandler().post(new Runnable() {
-                    public void run() {
-                        getPromptService().processMessage(geckoObject);
-                    }
-                });
-
-                String promptServiceResult = "";
-                try {
-                    promptServiceResult = PromptService.waitForReturn();
-                } catch (InterruptedException e) {
-                    Log.i(LOGTAG, "showing prompt ",  e);
-                }
-                return promptServiceResult;
-            }
-
             CopyOnWriteArrayList<GeckoEventListener> listeners;
             synchronized (mEventListeners) {
                 listeners = mEventListeners.get(type);
             }
 
             if (listeners == null)
                 return "";
 
@@ -1879,23 +1862,16 @@ public class GeckoAppShell
 
         return "";
     }
 
     public static void disableBatteryNotifications() {
         GeckoBatteryManager.disableNotifications();
     }
 
-    public static PromptService getPromptService() {
-        if (gPromptService == null) {
-            gPromptService = new PromptService();
-        }
-        return gPromptService;
-    }
-
     public static double[] getCurrentBatteryInformation() {
         return GeckoBatteryManager.getCurrentInformation();
     }
 
     static void checkUriVisited(String uri) {   // invoked from native JNI code
         GlobalHistory.getInstance().checkUriVisited(uri);
     }
 
--- a/mobile/android/base/PromptService.java
+++ b/mobile/android/base/PromptService.java
@@ -36,17 +36,17 @@ import android.widget.EditText;
 import android.widget.LinearLayout;
 import android.widget.Spinner;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import org.json.JSONArray;
 import org.json.JSONObject;
 import android.text.InputType;
 
-public class PromptService implements OnClickListener, OnCancelListener, OnItemClickListener {
+public class PromptService implements OnClickListener, OnCancelListener, OnItemClickListener, GeckoEventResponder {
     private static final String LOGTAG = "GeckoPromptService";
 
     private PromptInput[] mInputs;
     private AlertDialog mDialog = null;
     private static LayoutInflater mInflater;
 
     private final static int GROUP_PADDING_SIZE = 32; // in dip units
     private static int mGroupPaddingSize = 0; // calculated from GROUP_PADDING_SIZE. In pixel units
@@ -76,16 +76,18 @@ public class PromptService implements On
                                                                        TOP_BOTTOM_TEXT_WITH_ICON_PADDING,
                                                                        res.getDisplayMetrics());
         mIconTextPadding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                           ICON_TEXT_PADDING,
                                                           res.getDisplayMetrics());
         mIconSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                    ICON_SIZE,
                                                    res.getDisplayMetrics());
+
+        GeckoAppShell.registerGeckoEventListener("Prompt:Show", this);
     }
 
     private class PromptButton {
         public String label = "";
         PromptButton(JSONObject aJSONButton) {
             try {
                 label = aJSONButton.getString("label");
             } catch(Exception ex) { }
@@ -167,16 +169,38 @@ public class PromptService implements On
             } else if (type.equals("menulist")) {
                 Spinner spinner = (Spinner)view;
                 return Integer.toString(spinner.getSelectedItemPosition());
             }
             return "";
         }
     }
 
+    // GeckoEventListener implementation
+    public void handleMessage(String event, final JSONObject message) {
+        GeckoAppShell.getHandler().post(new Runnable() {
+            public void run() {
+                processMessage(message);
+            }
+        });
+    }
+
+    // GeckoEventResponder implementation
+    public String getResponse() {
+        // we only handle one kind of message in handleMessage, and this is the
+        // response we provide for that message
+        String promptServiceResult = "";
+        try {
+            promptServiceResult = waitForReturn();
+        } catch (InterruptedException e) {
+            Log.i(LOGTAG, "showing prompt ",  e);
+        }
+        return promptServiceResult;
+    }
+
     public void Show(String aTitle, String aText, PromptButton[] aButtons, PromptListItem[] aMenuList, boolean aMultipleSelection) {
         AlertDialog.Builder builder = new AlertDialog.Builder(GeckoApp.mAppContext);
         if (!aTitle.equals("")) {
             builder.setTitle(aTitle);
         }
 
         if (!aText.equals("")) {
             builder.setMessage(aText);
@@ -313,17 +337,17 @@ public class PromptService implements On
         mInputs = null;
         mDialog = null;
         mSelected = null;
         try {
             mPromptQueue.put(aReturn);
         } catch(Exception ex) { }
     }
 
-    public void processMessage(JSONObject geckoObject) {
+    private void processMessage(JSONObject geckoObject) {
         String title = "";
         try {
             title = geckoObject.getString("title");
         } catch(Exception ex) { }
         String text = "";
         try {
             text = geckoObject.getString("text");
         } catch(Exception ex) { }