Commit 6364098a authored by Emily Toop's avatar Emily Toop
Browse files

Bug 1598010 - Move examples into mozilla-central r=agi

Examples are currently in github. They should be kept alongside the documentation and code and built along with other projects in Android Studio.

Differential Revision: https://phabricator.services.mozilla.com/D53978

--HG--
extra : moz-landing-system : lando
parent 2b779548
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
buildDir "${topobjdir}/gradle/build/mobile/android/examples/messaging_example"

apply plugin: 'com.android.application'

apply from: "${topsrcdir}/mobile/android/gradle/product_flavors.gradle"

android {
    compileSdkVersion project.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "org.mozilla.geckoview.example.messaging"
        targetSdkVersion project.ext.targetSdkVersion
        minSdkVersion project.ext.minSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    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:!*~'
    }

    project.configureProductFlavors.delegate = it
    project.configureProductFlavors()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:support-annotations:$support_library_version"
    implementation "com.android.support:appcompat-v7:$support_library_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(path: ':geckoview')
}
 No newline at end of file
+25 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.mozilla.geckoview.example.messaging">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 No newline at end of file
+16 −0
Original line number Diff line number Diff line
{
  "manifest_version": 2,
  "name": "messaging",
  "version": "1.0",
  "description": "Example messaging web extension.",
  "content_scripts": [
    {
      "matches": ["*://*.twitter.com/*"],
      "js": ["messaging.js"]
    }
  ],
  "permissions": [
    "nativeMessaging",
    "geckoViewAddons"
  ]
}
+13 −0
Original line number Diff line number Diff line
/* 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/. */

let manifest = document.querySelector("head > link[rel=manifest]");
if (manifest) {
  fetch(manifest.href)
    .then(response => response.json())
    .then(json => {
      let message = { type: "WPAManifest", manifest: json };
      window.browser.runtime.sendNativeMessage("browser", message);
    });
}
+67 −0
Original line number Diff line number Diff line
package org.mozilla.geckoview.example.messaging;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;
import org.mozilla.geckoview.WebExtension;

public class MainActivity extends AppCompatActivity {
    private static GeckoRuntime sRuntime;

    @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) {
            sRuntime = GeckoRuntime.create(this);
        }

        WebExtension.MessageDelegate messageDelegate = new WebExtension.MessageDelegate() {
            @Nullable
            public GeckoResult<Object> onMessage(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;
            }
        };

        WebExtension extension = new WebExtension(
                "resource://android/assets/messaging/",
                "myextension",
                WebExtension.Flags.ALLOW_CONTENT_MESSAGING);

        sRuntime.registerWebExtension(extension).exceptionally(e -> {
            Log.e("MessageDelegate", "Error registering WebExtension", e);
            return null;
        });

        session.setMessageDelegate(extension, messageDelegate, "browser");

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