Example #1
0
  @Override
  public void onBackPressed() {
    final Tabs tabs = Tabs.getInstance();
    final Tab tab = tabs.getSelectedTab();

    // Give Gecko a chance to handle the back press first, then fallback to the Java UI.
    GeckoAppShell.sendRequestToGecko(
        new GeckoRequest("Browser:OnBackPressed", null) {
          @Override
          public void onResponse(NativeJSObject nativeJSObject) {
            if (!nativeJSObject.getBoolean("handled")) {
              // Default behavior is Gecko didn't prevent.
              onDefault();
            }
          }

          @Override
          public void onError(NativeJSObject error) {
            // Default behavior is Gecko didn't prevent, via failure.
            onDefault();
          }

          // Return from Gecko thread, then back-press through the Java UI.
          private void onDefault() {
            ThreadUtils.postToUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    if (tab.doBack()) {
                      return;
                    }

                    finish();
                  }
                });
          }
        });
  }
 @Override
 public void sendRequestToGecko(
     final String event,
     final JSONObject message,
     final JavaAddonInterfaceV1.RequestCallback callback) {
   final String prefixedEvent = guid + ":" + event;
   GeckoAppShell.sendRequestToGecko(
       new GeckoRequest(prefixedEvent, message) {
         @Override
         public void onResponse(NativeJSObject nativeJSObject) {
           if (callback == null) {
             // Nothing to do.
             return;
           }
           try {
             final JSONObject json = new JSONObject(nativeJSObject.toString());
             callback.onResponse(GeckoAppShell.getContext(), json);
           } catch (JSONException e) {
             // No way to report failure.
             Log.e(LOGTAG, "Exception handling response to request [" + event + "]:", e);
           }
         }
       });
 }