summaryrefslogtreecommitdiffstats
path: root/mobile/android/examples/messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java
blob: 759951c81929a3724ef08b6cb6e6a304be4bd365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview.example.messaging;

import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoRuntimeSettings;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;
import org.mozilla.geckoview.WebExtension;

public class MainActivity extends AppCompatActivity {
  private static GeckoRuntime sRuntime;

  private static final String EXTENSION_LOCATION = "resource://android/assets/messaging/";
  private static final String EXTENSION_ID = "messaging@example.com";
  // If you make changes to the extension you need to update this
  private static final String EXTENSION_VERSION = "1.0";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GeckoView view = findViewById(R.id.geckoview);
    GeckoSession session = new GeckoSession();

    if (sRuntime == null) {
      GeckoRuntimeSettings settings =
          new GeckoRuntimeSettings.Builder().remoteDebuggingEnabled(true).build();
      sRuntime = GeckoRuntime.create(this, settings);
    }

    WebExtension.MessageDelegate messageDelegate =
        new WebExtension.MessageDelegate() {
          @Nullable
          @Override
          public GeckoResult<Object> onMessage(
              final @NonNull String nativeApp,
              final @NonNull Object message,
              final @NonNull WebExtension.MessageSender sender) {
            if (message instanceof JSONObject) {
              JSONObject json = (JSONObject) message;
              try {
                if (json.has("type") && "WPAManifest".equals(json.getString("type"))) {
                  JSONObject manifest = json.getJSONObject("manifest");
                  Log.d("MessageDelegate", "Found WPA manifest: " + manifest);
                }
              } catch (JSONException ex) {
                Log.e("MessageDelegate", "Invalid manifest", ex);
              }
            }
            return null;
          }
        };

    // Let's make sure the extension is installed
    sRuntime
        .getWebExtensionController()
        .ensureBuiltIn(EXTENSION_LOCATION, "messaging@example.com")
        .accept(
            // Set delegate that will receive messages coming from this extension.
            extension ->
                session
                    .getWebExtensionController()
                    .setMessageDelegate(extension, messageDelegate, "browser"),
            // Something bad happened, let's log an error
            e -> Log.e("MessageDelegate", "Error registering extension", e));

    session.open(sRuntime);
    view.setSession(session);
    session.loadUri("https://mobile.twitter.com");
  }
}