Bug 1441279 - 3. Add selection action handler in GeckoSession; r?esawin
Add a handler for selection action delegate in GeckoSession that calls
the callback methods.
MozReview-Commit-ID: C2mMHHheTJT
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
@@ -333,21 +333,73 @@ public class GeckoSession extends LayerS
delegate.onMediaPermissionRequest(
GeckoSession.this, message.getString("uri"),
videos, audios, new PermissionCallback("media", callback));
}
}
};
+ private final GeckoSessionHandler<SelectionActionDelegate> mSelectionActionDelegate =
+ new GeckoSessionHandler<SelectionActionDelegate>(
+ "GeckoViewSelectionAction", this,
+ new String[] {
+ "GeckoView:HideSelectionAction",
+ "GeckoView:ShowSelectionAction",
+ }
+ ) {
+ @Override
+ public void handleMessage(final SelectionActionDelegate delegate,
+ final String event,
+ final GeckoBundle message,
+ final EventCallback callback) {
+ if ("GeckoView:ShowSelectionAction".equals(event)) {
+ final SelectionActionDelegate.Selection selection =
+ new SelectionActionDelegate.Selection(message);
+
+ final String[] actions = message.getStringArray("actions");
+ final int seqNo = message.getInt("seqNo");
+ final Response<String> response = new Response<String>() {
+ @Override
+ public void respond(final String action) {
+ final GeckoBundle response = new GeckoBundle(2);
+ response.putString("id", action);
+ response.putInt("seqNo", seqNo);
+ callback.sendSuccess(response);
+ }
+ };
+
+ delegate.onShowActionRequest(GeckoSession.this, selection,
+ actions, response);
+
+ } else if ("GeckoView:HideSelectionAction".equals(event)) {
+ final String reasonString = message.getString("reason");
+ final int reason;
+ if ("invisibleselection".equals(reasonString)) {
+ reason = SelectionActionDelegate.HIDE_REASON_INVISIBLE_SELECTION;
+ } else if ("presscaret".equals(reasonString)) {
+ reason = SelectionActionDelegate.HIDE_REASON_ACTIVE_SELECTION;
+ } else if ("scroll".equals(reasonString)) {
+ reason = SelectionActionDelegate.HIDE_REASON_ACTIVE_SCROLL;
+ } else if ("visibilitychange".equals(reasonString)) {
+ reason = SelectionActionDelegate.HIDE_REASON_NO_SELECTION;
+ } else {
+ throw new IllegalArgumentException();
+ }
+
+ delegate.onHideAction(GeckoSession.this, reason);
+ }
+ }
+ };
+
/* package */ int handlersCount;
private final GeckoSessionHandler<?>[] mSessionHandlers = new GeckoSessionHandler<?>[] {
mContentHandler, mNavigationHandler, mProgressHandler, mScrollHandler,
- mTrackingProtectionHandler, mPermissionHandler
+ mTrackingProtectionHandler, mPermissionHandler, mSelectionActionDelegate
};
private static class PermissionCallback implements
PermissionDelegate.Callback, PermissionDelegate.MediaCallback {
private final String mType;
private EventCallback mCallback;
@@ -1130,16 +1182,34 @@ public class GeckoSession extends LayerS
/**
* Get the current prompt delegate for this GeckoSession.
* @return PromptDelegate instance or null if using built-in delegate.
*/
public PromptDelegate getPromptDelegate() {
return mPromptDelegate;
}
+ /**
+ * Set the current selection action delegate for this GeckoSession.
+ *
+ * @param delegate SelectionActionDelegate instance or null to unset.
+ */
+ public void setSelectionActionDelegate(@Nullable SelectionActionDelegate delegate) {
+ mSelectionActionDelegate.setDelegate(delegate, this);
+ }
+
+ /**
+ * Get the current selection action delegate for this GeckoSession.
+ *
+ * @return SelectionActionDelegate instance or null if not set.
+ */
+ public @Nullable SelectionActionDelegate getSelectionActionDelegate() {
+ return mSelectionActionDelegate.getDelegate();
+ }
+
private static class PromptCallback implements
PromptDelegate.AlertCallback, PromptDelegate.ButtonCallback,
PromptDelegate.TextCallback, PromptDelegate.AuthCallback,
PromptDelegate.ChoiceCallback, PromptDelegate.FileCallback {
private final String mType;
private final String mMode;
private final boolean mHasCheckbox;