From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../examples/messaging_example/app/build.gradle | 55 +++++++ .../app/src/main/AndroidManifest.xml | 28 ++++ .../app/src/main/assets/messaging/.eslintrc.js | 11 ++ .../app/src/main/assets/messaging/manifest.json | 22 +++ .../app/src/main/assets/messaging/messaging.js | 13 ++ .../geckoview/example/messaging/MainActivity.java | 103 ++++++++++++ .../res/drawable-v24/ic_launcher_foreground.xml | 38 +++++ .../main/res/drawable/ic_launcher_background.xml | 174 +++++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 18 +++ .../src/main/res/mipmap-anydpi-v26/ic_launcher.xml | 9 ++ .../res/mipmap-anydpi-v26/ic_launcher_round.xml | 9 ++ .../app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../src/main/res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes .../app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../src/main/res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes .../app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../main/res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../app/src/main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../main/res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../src/main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../main/res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes .../app/src/main/res/values/colors.xml | 10 ++ .../app/src/main/res/values/strings.xml | 7 + .../app/src/main/res/values/styles.xml | 15 ++ .../port_messaging_example/app/build.gradle | 54 +++++++ .../app/src/main/AndroidManifest.xml | 28 ++++ .../app/src/main/assets/messaging/.eslintrc.js | 11 ++ .../app/src/main/assets/messaging/background.js | 11 ++ .../app/src/main/assets/messaging/manifest.json | 15 ++ .../geckoview/example/messaging/MainActivity.java | 120 ++++++++++++++ .../res/drawable-v24/ic_launcher_foreground.xml | 38 +++++ .../main/res/drawable/ic_launcher_background.xml | 174 +++++++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 18 +++ .../src/main/res/mipmap-anydpi-v26/ic_launcher.xml | 9 ++ .../res/mipmap-anydpi-v26/ic_launcher_round.xml | 9 ++ .../app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../src/main/res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes .../app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../src/main/res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes .../app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../main/res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../app/src/main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../main/res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../src/main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../main/res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes .../app/src/main/res/values/colors.xml | 10 ++ .../app/src/main/res/values/strings.xml | 7 + .../app/src/main/res/values/styles.xml | 15 ++ 48 files changed, 1031 insertions(+) create mode 100644 mobile/android/examples/messaging_example/app/build.gradle create mode 100644 mobile/android/examples/messaging_example/app/src/main/AndroidManifest.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/assets/messaging/.eslintrc.js create mode 100644 mobile/android/examples/messaging_example/app/src/main/assets/messaging/manifest.json create mode 100644 mobile/android/examples/messaging_example/app/src/main/assets/messaging/messaging.js create mode 100644 mobile/android/examples/messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/layout/activity_main.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/values/colors.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/values/strings.xml create mode 100644 mobile/android/examples/messaging_example/app/src/main/res/values/styles.xml create mode 100644 mobile/android/examples/port_messaging_example/app/build.gradle create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/AndroidManifest.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/.eslintrc.js create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/background.js create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/manifest.json create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/layout/activity_main.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/values/colors.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/values/strings.xml create mode 100644 mobile/android/examples/port_messaging_example/app/src/main/res/values/styles.xml (limited to 'mobile/android/examples') diff --git a/mobile/android/examples/messaging_example/app/build.gradle b/mobile/android/examples/messaging_example/app/build.gradle new file mode 100644 index 0000000000..1fbc89b457 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/build.gradle @@ -0,0 +1,55 @@ +buildDir "${topobjdir}/gradle/build/mobile/android/examples/messaging_example" + +apply plugin: 'com.android.application' + +apply from: "${topsrcdir}/mobile/android/gradle/product_flavors.gradle" + +android { + buildToolsVersion project.ext.buildToolsVersion + compileSdkVersion project.ext.compileSdkVersion + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + defaultConfig { + applicationId "org.mozilla.geckoview.example.messaging" + targetSdkVersion project.ext.targetSdkVersion + minSdkVersion project.ext.minSdkVersion + versionCode 1 + versionName "1.0" + multiDexEnabled true + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + // By default the android plugins ignores folders that start with `_`, but + // we need those in web extensions. + // See also: + // - https://issuetracker.google.com/issues/36911326 + // - https://stackoverflow.com/questions/9206117/how-to-workaround-autoomitting-fiiles-folders-starting-with-underscore-in + aaptOptions { + ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' + noCompress 'ja' + } + + project.configureProductFlavors.delegate = it + project.configureProductFlavors() + + namespace 'org.mozilla.geckoview.example.messaging' +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "androidx.annotation:annotation:1.6.0" + implementation "androidx.appcompat:appcompat:1.6.1" + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.multidex:multidex:2.0.1' + testImplementation 'junit:junit:4.13.2' + // Replace this with implementation "org.mozilla.geckoview:geckoview-${geckoviewChannel}:${geckoviewVersion}" + implementation project(path: ':geckoview') +} diff --git a/mobile/android/examples/messaging_example/app/src/main/AndroidManifest.xml b/mobile/android/examples/messaging_example/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..a4bab45e72 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + diff --git a/mobile/android/examples/messaging_example/app/src/main/assets/messaging/.eslintrc.js b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/.eslintrc.js new file mode 100644 index 0000000000..c5fda00676 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/.eslintrc.js @@ -0,0 +1,11 @@ +/* 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/. */ + +"use strict"; + +module.exports = { + env: { + webextensions: true, + }, +}; diff --git a/mobile/android/examples/messaging_example/app/src/main/assets/messaging/manifest.json b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/manifest.json new file mode 100644 index 0000000000..28e02ff816 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/manifest.json @@ -0,0 +1,22 @@ +{ + "manifest_version": 2, + "name": "messaging", + "version": "1.0", + "description": "Example messaging web extension.", + "browser_specific_settings": { + "gecko": { + "id": "messaging@example.com" + } + }, + "content_scripts": [ + { + "matches": ["*://*.twitter.com/*"], + "js": ["messaging.js"] + } + ], + "permissions": [ + "nativeMessaging", + "nativeMessagingFromContent", + "geckoViewAddons" + ] +} diff --git a/mobile/android/examples/messaging_example/app/src/main/assets/messaging/messaging.js b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/messaging.js new file mode 100644 index 0000000000..f092f2096d --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/assets/messaging/messaging.js @@ -0,0 +1,13 @@ +/* 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/. */ + +const manifest = document.querySelector("head > link[rel=manifest]"); +if (manifest) { + fetch(manifest.href) + .then(response => response.json()) + .then(json => { + const message = { type: "WPAManifest", manifest: json }; + browser.runtime.sendNativeMessage("browser", message); + }); +} diff --git a/mobile/android/examples/messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java b/mobile/android/examples/messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java new file mode 100644 index 0000000000..1b0b13d0ad --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java @@ -0,0 +1,103 @@ +/* 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.gecko.util.ThreadUtils; +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; + +// The apk file for this example can be build using the following mach command: +// +// mach gradle messaging_example:build +// +// Formatting error reported by Spotless as part of the build can be autofixed +// can be fixed using the mach command: +// +// mach gradle messaging_example:spotlessApply +// +// After the gradle task messaging_example:build runs successfully, an apk +// file will be stored in the OBJDIR/gradle/build/mobile/android/examples/messaging_example. +// subdirectories + +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 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 -> + ThreadUtils.runOnUiThread( + new Runnable() { + @Override + public void run() { + 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"); + } +} diff --git a/mobile/android/examples/messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/mobile/android/examples/messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000000..6e4009f3e7 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + diff --git a/mobile/android/examples/messaging_example/app/src/main/res/drawable/ic_launcher_background.xml b/mobile/android/examples/messaging_example/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000000..bcab90d6ae --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,174 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mobile/android/examples/messaging_example/app/src/main/res/layout/activity_main.xml b/mobile/android/examples/messaging_example/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000000..87e7c0ae72 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000000..f39d507313 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000000..f39d507313 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000000..898f3ed59a Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000000..dffca3601e Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000000..64ba76f75e Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000000..dae5e08234 Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000000..e5ed46597e Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..14ed0af350 Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000..b0907cac3b Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..d8ae031549 Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000000..2c18de9e66 Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..beed3cdd2c Binary files /dev/null and b/mobile/android/examples/messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/messaging_example/app/src/main/res/values/colors.xml b/mobile/android/examples/messaging_example/app/src/main/res/values/colors.xml new file mode 100644 index 0000000000..8c84b9a3fc --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + + + #008577 + #00574B + #D81B60 + diff --git a/mobile/android/examples/messaging_example/app/src/main/res/values/strings.xml b/mobile/android/examples/messaging_example/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..88982ec38c --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/values/strings.xml @@ -0,0 +1,7 @@ + + + + MessagingExample + diff --git a/mobile/android/examples/messaging_example/app/src/main/res/values/styles.xml b/mobile/android/examples/messaging_example/app/src/main/res/values/styles.xml new file mode 100644 index 0000000000..436c5ab087 --- /dev/null +++ b/mobile/android/examples/messaging_example/app/src/main/res/values/styles.xml @@ -0,0 +1,15 @@ + + + + + + + + diff --git a/mobile/android/examples/port_messaging_example/app/build.gradle b/mobile/android/examples/port_messaging_example/app/build.gradle new file mode 100644 index 0000000000..b9eab053fd --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/build.gradle @@ -0,0 +1,54 @@ +buildDir "${topobjdir}/gradle/build/mobile/android/examples/port_messaging_example" + +apply plugin: 'com.android.application' + +apply from: "${topsrcdir}/mobile/android/gradle/product_flavors.gradle" + +android { + buildToolsVersion project.ext.buildToolsVersion + compileSdkVersion project.ext.compileSdkVersion + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + defaultConfig { + applicationId "org.mozilla.geckoview.example.messaging" + targetSdkVersion project.ext.targetSdkVersion + minSdkVersion project.ext.minSdkVersion + versionCode 1 + versionName "1.0" + multiDexEnabled true + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + // By default the android plugins ignores folders that start with `_`, but + // we need those in web extensions. + // See also: + // - https://issuetracker.google.com/issues/36911326 + // - https://stackoverflow.com/questions/9206117/how-to-workaround-autoomitting-fiiles-folders-starting-with-underscore-in + aaptOptions { + ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' + noCompress 'ja' + } + + project.configureProductFlavors.delegate = it + project.configureProductFlavors() + + namespace 'org.mozilla.geckoview.example.messaging' +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "androidx.annotation:annotation:1.6.0" + implementation "androidx.appcompat:appcompat:1.6.1" + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.multidex:multidex:2.0.1' + testImplementation 'junit:junit:4.13.2' + implementation project(path: ':geckoview') +} diff --git a/mobile/android/examples/port_messaging_example/app/src/main/AndroidManifest.xml b/mobile/android/examples/port_messaging_example/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..a4bab45e72 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + diff --git a/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/.eslintrc.js b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/.eslintrc.js new file mode 100644 index 0000000000..c5fda00676 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/.eslintrc.js @@ -0,0 +1,11 @@ +/* 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/. */ + +"use strict"; + +module.exports = { + env: { + webextensions: true, + }, +}; diff --git a/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/background.js b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/background.js new file mode 100644 index 0000000000..0929ee35a5 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/background.js @@ -0,0 +1,11 @@ +/* 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/. */ + +// Establish connection with app +const port = browser.runtime.connectNative("browser"); +port.onMessage.addListener(response => { + // Let's just echo the message back + port.postMessage(`Received: ${JSON.stringify(response)}`); +}); +port.postMessage("Hello from WebExtension!"); diff --git a/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/manifest.json b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/manifest.json new file mode 100644 index 0000000000..c7deb57a94 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/assets/messaging/manifest.json @@ -0,0 +1,15 @@ +{ + "manifest_version": 2, + "name": "messaging", + "version": "1.0", + "description": "Example messaging web extension.", + "browser_specific_settings": { + "gecko": { + "id": "messaging@example.com" + } + }, + "background": { + "scripts": ["background.js"] + }, + "permissions": ["nativeMessaging", "geckoViewAddons"] +} diff --git a/mobile/android/examples/port_messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java b/mobile/android/examples/port_messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java new file mode 100644 index 0000000000..e2f31eea70 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/java/org/mozilla/geckoview/example/messaging/MainActivity.java @@ -0,0 +1,120 @@ +/* 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 android.view.KeyEvent; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import org.json.JSONException; +import org.json.JSONObject; +import org.mozilla.gecko.util.ThreadUtils; +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; + +// +// The apk file for this example can be build using the following mach command: +// +// mach gradle port_messaging_example:build +// +// Formatting error reported by Spotless as part of the build can be autofixed +// can be fixed using the mach command: +// +// mach gradle port_messaging_example:spotlessApply +// +// After the gradle task messaging_example:build runs successfully, an apk +// file will be stored in the OBJDIR/gradle/build/mobile/android/examples/port_messaging_example. +// subdirectories + +public class MainActivity extends AppCompatActivity { + private static GeckoRuntime sRuntime; + + private WebExtension.Port mPort; + + @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.PortDelegate portDelegate = + new WebExtension.PortDelegate() { + @Override + public void onPortMessage( + final @NonNull Object message, final @NonNull WebExtension.Port port) { + Log.d("PortDelegate", "Received message from extension: " + message); + } + + @Override + public void onDisconnect(final @NonNull WebExtension.Port port) { + // This port is not usable anymore. + if (port == mPort) { + mPort = null; + } + } + }; + + WebExtension.MessageDelegate messageDelegate = + new WebExtension.MessageDelegate() { + @Override + @Nullable + public void onConnect(final @NonNull WebExtension.Port port) { + mPort = port; + mPort.setDelegate(portDelegate); + } + }; + + sRuntime + .getWebExtensionController() + .ensureBuiltIn("resource://android/assets/messaging/", "messaging@example.com") + .accept( + // Register message delegate for background script + extension -> + ThreadUtils.runOnUiThread( + new Runnable() { + @Override + public void run() { + extension.setMessageDelegate(messageDelegate, "browser"); + } + }), + e -> Log.e("MessageDelegate", "Error registering WebExtension", e)); + + session.open(sRuntime); + view.setSession(session); + session.loadUri("https://mobile.twitter.com"); + } + + @Override + public boolean onKeyLongPress(int keyCode, KeyEvent event) { + if (mPort == null) { + // No extension registered yet, let's ignore this message + return false; + } + + JSONObject message = new JSONObject(); + try { + message.put("keyCode", keyCode); + message.put("event", KeyEvent.keyCodeToString(event.getKeyCode())); + } catch (JSONException ex) { + throw new RuntimeException(ex); + } + + mPort.postMessage(message); + return true; + } +} diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000000..6e4009f3e7 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/drawable/ic_launcher_background.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000000..bcab90d6ae --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,174 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/layout/activity_main.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000000..87e7c0ae72 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000000..f39d507313 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000000..f39d507313 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000000..898f3ed59a Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000000..dffca3601e Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000000..64ba76f75e Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000000..dae5e08234 Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000000..e5ed46597e Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..14ed0af350 Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000..b0907cac3b Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..d8ae031549 Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000000..2c18de9e66 Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000000..beed3cdd2c Binary files /dev/null and b/mobile/android/examples/port_messaging_example/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/values/colors.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/values/colors.xml new file mode 100644 index 0000000000..8c84b9a3fc --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + + + #008577 + #00574B + #D81B60 + diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/values/strings.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/values/strings.xml new file mode 100644 index 0000000000..88982ec38c --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/values/strings.xml @@ -0,0 +1,7 @@ + + + + MessagingExample + diff --git a/mobile/android/examples/port_messaging_example/app/src/main/res/values/styles.xml b/mobile/android/examples/port_messaging_example/app/src/main/res/values/styles.xml new file mode 100644 index 0000000000..436c5ab087 --- /dev/null +++ b/mobile/android/examples/port_messaging_example/app/src/main/res/values/styles.xml @@ -0,0 +1,15 @@ + + + + + + + + -- cgit v1.2.3