Bug 795045 - Part 1: Cleanup for TextSelection/TextSelectionHandle. r=wesj
authorBrian Nicholson <bnicholson@mozilla.com>
Tue, 18 Dec 2012 22:57:54 -0800
changeset 125610 ffe29f6052f2
parent 125609 f9e5c189ae6b
child 125611 9af5a57f905a
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs795045
milestone20.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 795045 - Part 1: Cleanup for TextSelection/TextSelectionHandle. r=wesj
mobile/android/base/GeckoApp.java
mobile/android/base/TextSelection.java
mobile/android/base/TextSelectionHandle.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -1715,17 +1715,18 @@ abstract public class GeckoApp
           SmsManager.getInstance().start();
         }
 
         mPromptService = new PromptService();
 
         mTextSelection = new TextSelection((TextSelectionHandle) findViewById(R.id.start_handle),
                                            (TextSelectionHandle) findViewById(R.id.middle_handle),
                                            (TextSelectionHandle) findViewById(R.id.end_handle),
-                                           GeckoAppShell.getEventDispatcher());
+                                           GeckoAppShell.getEventDispatcher(),
+                                           this);
 
         PrefsHelper.getPref("app.update.autodownload", new PrefsHelper.PrefHandlerBase() {
             @Override public void prefValue(String pref, String value) {
                 UpdateServiceHelper.registerForUpdates(GeckoApp.this, value);
             }
         });
 
         final GeckoApp self = this;
--- a/mobile/android/base/TextSelection.java
+++ b/mobile/android/base/TextSelection.java
@@ -7,16 +7,17 @@ package org.mozilla.gecko;
 import org.mozilla.gecko.gfx.Layer;
 import org.mozilla.gecko.gfx.Layer.RenderContext;
 import org.mozilla.gecko.gfx.LayerView;
 import org.mozilla.gecko.util.EventDispatcher;
 import org.mozilla.gecko.util.FloatUtils;
 import org.mozilla.gecko.util.GeckoEventListener;
 
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.util.Log;
 import android.view.View;
 
 class TextSelection extends Layer implements GeckoEventListener {
     private static final String LOGTAG = "GeckoTextSelection";
 
@@ -24,24 +25,28 @@ class TextSelection extends Layer implem
     private final TextSelectionHandle mMiddleHandle;
     private final TextSelectionHandle mEndHandle;
     private final EventDispatcher mEventDispatcher;
 
     private float mViewLeft;
     private float mViewTop;
     private float mViewZoom;
 
+    private GeckoApp mActivity;
+
     TextSelection(TextSelectionHandle startHandle,
                   TextSelectionHandle middleHandle,
                   TextSelectionHandle endHandle,
-                  EventDispatcher eventDispatcher) {
+                  EventDispatcher eventDispatcher,
+                  GeckoApp activity) {
         mStartHandle = startHandle;
         mMiddleHandle = middleHandle;
         mEndHandle = endHandle;
         mEventDispatcher = eventDispatcher;
+        mActivity = activity;
 
         // Only register listeners if we have valid start/middle/end handles
         if (mStartHandle == null || mMiddleHandle == null || mEndHandle == null) {
             Log.e(LOGTAG, "Failed to initialize text selection because at least one handle is null");
         } else {
             registerEventListener("TextSelection:ShowHandles");
             registerEventListener("TextSelection:HideHandles");
             registerEventListener("TextSelection:PositionHandles");
@@ -59,95 +64,80 @@ class TextSelection extends Layer implem
             return mStartHandle;
         } else if (name.equals("MIDDLE")) {
             return mMiddleHandle;
         } else {
             return mEndHandle;
         }
     }
 
-    public void handleMessage(String event, JSONObject message) {
-        try {
-            if (event.equals("TextSelection:ShowHandles")) {
-                final JSONArray handles = message.getJSONArray("handles");
-                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
-                    public void run() {
-                        try {
-                            for (int i=0; i < handles.length(); i++) {
-                                String handle = handles.getString(i);
-                                getHandle(handle).setVisibility(View.VISIBLE);
-                            }
+    public void handleMessage(final String event, final JSONObject message) {
+        mActivity.runOnUiThread(new Runnable() {
+            public void run() {
+                try {
+                    if (event.equals("TextSelection:ShowHandles")) {
+                        final JSONArray handles = message.getJSONArray("handles");
+                        for (int i=0; i < handles.length(); i++) {
+                            String handle = handles.getString(i);
+                            getHandle(handle).setVisibility(View.VISIBLE);
+                        }
 
-                            mViewLeft = 0.0f;
-                            mViewTop = 0.0f;
-                            mViewZoom = 0.0f;
-                            LayerView layerView = GeckoApp.mAppContext.getLayerView();
-                            if (layerView != null) {
-                                layerView.addLayer(TextSelection.this);
-                            }
-                        } catch(Exception e) {}
+                        mViewLeft = 0.0f;
+                        mViewTop = 0.0f;
+                        mViewZoom = 0.0f;
+                        LayerView layerView = mActivity.getLayerView();
+                        if (layerView != null) {
+                            layerView.addLayer(TextSelection.this);
+                        }
+                    } else if (event.equals("TextSelection:HideHandles")) {
+                        final JSONArray handles = message.getJSONArray("handles");
+                        LayerView layerView = mActivity.getLayerView();
+                        if (layerView != null) {
+                            layerView.removeLayer(TextSelection.this);
+                        }
+
+                        for (int i=0; i < handles.length(); i++) {
+                            String handle = handles.getString(i);
+                            getHandle(handle).setVisibility(View.GONE);
+                        }
+                    } else if (event.equals("TextSelection:PositionHandles")) {
+                        final boolean rtl = message.getBoolean("rtl");
+                        final JSONArray positions = message.getJSONArray("positions");
+                        for (int i=0; i < positions.length(); i++) {
+                            JSONObject position = positions.getJSONObject(i);
+                            int left = position.getInt("left");
+                            int top = position.getInt("top");
+
+                            TextSelectionHandle handle = getHandle(position.getString("handle"));
+                            handle.setVisibility(position.getBoolean("hidden") ? View.GONE : View.VISIBLE);
+                            handle.positionFromGecko(left, top, rtl);
+                        }
                     }
-                });
-            } else if (event.equals("TextSelection:HideHandles")) {
-                final JSONArray handles = message.getJSONArray("handles");
-                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
-                    public void run() {
-                        try {
-                            LayerView layerView = GeckoApp.mAppContext.getLayerView();
-                            if (layerView != null) {
-                                layerView.removeLayer(TextSelection.this);
-                            }
-
-                            for (int i=0; i < handles.length(); i++) {
-                                String handle = handles.getString(i);
-                                getHandle(handle).setVisibility(View.GONE);
-                            }
-
-                        } catch(Exception e) {}
-                    }
-                });
-            } else if (event.equals("TextSelection:PositionHandles")) {
-                final boolean rtl = message.getBoolean("rtl");
-                final JSONArray positions = message.getJSONArray("positions");
-                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
-                    public void run() {
-                        try {
-                            for (int i=0; i < positions.length(); i++) {
-                                JSONObject position = positions.getJSONObject(i);
-                                int left = position.getInt("left");
-                                int top = position.getInt("top");
-
-                                TextSelectionHandle handle = getHandle(position.getString("handle"));
-                                handle.setVisibility(position.getBoolean("hidden") ? View.GONE : View.VISIBLE);
-                                handle.positionFromGecko(left, top, rtl);
-                            }
-                        } catch (Exception e) { }
-                    }
-                });
+                } catch (JSONException e) {
+                    Log.e(LOGTAG, "JSON exception", e);
+                }
             }
-        } catch (Exception e) {
-            Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
-        }
+        });
     }
 
     @Override
     public void draw(final RenderContext context) {
         // cache the relevant values from the context and bail out if they are the same. we do this
         // because this draw function gets called a lot (once per compositor frame) and we want to
         // avoid doing a lot of extra work in cases where it's not needed.
         if (FloatUtils.fuzzyEquals(mViewLeft, context.viewport.left)
                 && FloatUtils.fuzzyEquals(mViewTop, context.viewport.top)
                 && FloatUtils.fuzzyEquals(mViewZoom, context.zoomFactor)) {
             return;
         }
         mViewLeft = context.viewport.left;
         mViewTop = context.viewport.top;
         mViewZoom = context.zoomFactor;
 
-        GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
+        mActivity.runOnUiThread(new Runnable() {
             public void run() {
                 mStartHandle.repositionWithViewport(context.viewport.left, context.viewport.top, context.zoomFactor);
                 mMiddleHandle.repositionWithViewport(context.viewport.left, context.viewport.top, context.zoomFactor);
                 mEndHandle.repositionWithViewport(context.viewport.left, context.viewport.top, context.zoomFactor);
             }
         });
     }
 
--- a/mobile/android/base/TextSelectionHandle.java
+++ b/mobile/android/base/TextSelectionHandle.java
@@ -36,19 +36,22 @@ class TextSelectionHandle extends ImageV
     private int mTouchStartX;
     private int mTouchStartY;
 
     private RelativeLayout.LayoutParams mLayoutParams;
 
     private static final int IMAGE_LEVEL_LTR = 0;
     private static final int IMAGE_LEVEL_RTL = 1;
 
+    private GeckoApp mActivity;
+
     TextSelectionHandle(Context context, AttributeSet attrs) {
         super(context, attrs);
         setOnTouchListener(this);
+        mActivity = (GeckoApp) context;
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextSelectionHandle);
         int handleType = a.getInt(R.styleable.TextSelectionHandle_handleType, 0x01);
 
         if (handleType == 0x01)
             mHandleType = HandleType.START;
         else if (handleType == 0x02)
             mHandleType = HandleType.MIDDLE;
@@ -91,17 +94,17 @@ class TextSelectionHandle extends ImageV
         }
         return true;
     }
 
     private void move(int newX, int newY) {
         mLeft = mLeft + newX - mTouchStartX;
         mTop = mTop + newY - mTouchStartY;
 
-        LayerView layerView = GeckoApp.mAppContext.getLayerView();
+        LayerView layerView = mActivity.getLayerView();
         if (layerView == null) {
             Log.e(LOGTAG, "Can't move selection because layerView is null");
             return;
         }
         // Send x coordinate on the right side of the start handle, left side of the end handle.
         float left = (float) mLeft + adjustLeftForHandle();
 
         PointF geckoPoint = new PointF(left, (float) mTop);
@@ -116,17 +119,17 @@ class TextSelectionHandle extends ImageV
             Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Move");
         }
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Move", args.toString()));
 
         setLayoutPosition();
     }
 
     void positionFromGecko(int left, int top, boolean rtl) {
-        LayerView layerView = GeckoApp.mAppContext.getLayerView();
+        LayerView layerView = mActivity.getLayerView();
         if (layerView == null) {
             Log.e(LOGTAG, "Can't position handle because layerView is null");
             return;
         }
 
         mGeckoPoint = new PointF((float) left, (float) top);
         if (mIsRTL != rtl) {
             mIsRTL = rtl;