Bug 917942 - Create a JS interface to Sync configuration. r=margaret

This commit is contained in:
Richard Newman 2014-02-18 19:20:27 -08:00
parent fcdbfe136b
commit 2701729b53
6 changed files with 161 additions and 0 deletions

View File

@ -13,6 +13,8 @@ import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
import org.mozilla.gecko.favicons.LoadFaviconTask;
import org.mozilla.gecko.favicons.decoders.IconDirectoryEntry;
import org.mozilla.gecko.fxa.activities.FxAccountGetStartedActivity;
import org.mozilla.gecko.fxa.FirefoxAccounts;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.gfx.GeckoLayerClient;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
@ -28,6 +30,7 @@ import org.mozilla.gecko.home.SearchEngine;
import org.mozilla.gecko.menu.GeckoMenu;
import org.mozilla.gecko.preferences.GeckoPreferences;
import org.mozilla.gecko.prompts.Prompt;
import org.mozilla.gecko.sync.setup.SyncAccounts;
import org.mozilla.gecko.toolbar.AutocompleteHandler;
import org.mozilla.gecko.toolbar.BrowserToolbar;
import org.mozilla.gecko.util.Clipboard;
@ -545,6 +548,8 @@ abstract public class BrowserApp extends GeckoApp
registerEventListener("Menu:Add");
registerEventListener("Menu:Remove");
registerEventListener("Menu:Update");
registerEventListener("Accounts:Create");
registerEventListener("Accounts:Exist");
Distribution.init(this);
JavaAddonManager.getInstance().init(getApplicationContext());
@ -862,6 +867,8 @@ abstract public class BrowserApp extends GeckoApp
unregisterEventListener("Menu:Add");
unregisterEventListener("Menu:Remove");
unregisterEventListener("Menu:Update");
unregisterEventListener("Accounts:Create");
unregisterEventListener("Accounts:Exist");
if (AppConstants.MOZ_ANDROID_BEAM && Build.VERSION.SDK_INT >= 14) {
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
@ -1246,6 +1253,30 @@ abstract public class BrowserApp extends GeckoApp
bringToFrontIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.BROWSER_INTENT_CLASS);
bringToFrontIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(bringToFrontIntent);
} else if (event.equals("Accounts:Create")) {
// Do exactly the same thing as if you tapped 'Sync'
// in Settings.
final Intent intent = new Intent(getContext(), FxAccountGetStartedActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
} else if (event.equals("Accounts:Exist")) {
final String kind = message.getString("kind");
final JSONObject response = new JSONObject();
if ("any".equals(kind)) {
response.put("exists", SyncAccounts.syncAccountsExist(getContext()) ||
FirefoxAccounts.firefoxAccountsExist(getContext()));
EventDispatcher.sendResponse(message, response);
} else if ("fxa".equals(kind)) {
response.put("exists", FirefoxAccounts.firefoxAccountsExist(getContext()));
EventDispatcher.sendResponse(message, response);
} else if ("sync11".equals(kind)) {
response.put("exists", SyncAccounts.syncAccountsExist(getContext()));
EventDispatcher.sendResponse(message, response);
} else {
response.put("error", "Unknown kind");
EventDispatcher.sendError(message, response);
}
} else {
super.handleMessage(event, message);
}

View File

@ -77,6 +77,7 @@ skip-if = processor == "x86"
# [testVkbOverlap] # see bug 907274
# Using JavascriptTest
[testAccounts]
[testBrowserDiscovery]
[testDeviceSearchEngine]
[testJNI]

View File

@ -0,0 +1,28 @@
/* 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.gecko.tests;
import org.mozilla.gecko.*;
import org.mozilla.gecko.fxa.activities.FxAccountGetStartedActivity;
public class testAccounts extends JavascriptTest {
public testAccounts() {
super("testAccounts.js");
}
@Override
public void testJavascript() throws Exception {
super.testJavascript();
// Rather than waiting for the JS call to message
// Java and wait for the Activity to launch, we just
// don't test these.
/*
android.app.Activity activity = mSolo.getCurrentActivity();
System.out.println("Current activity: " + activity);
mAsserter.ok(activity instanceof FxAccountGetStartedActivity, "checking activity", "setup activity launched");
*/
}
}

View File

@ -0,0 +1,24 @@
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
/* 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/. */
Components.utils.import("resource://gre/modules/Accounts.jsm");
add_task(function test_Accounts() {
let syncExists = yield Accounts.syncAccountsExist();
dump("Sync account exists? " + syncExists + "\n");
let firefoxExists = yield Accounts.firefoxAccountsExist();
dump("Firefox account exists? " + firefoxExists + "\n");
let anyExists = yield Accounts.anySyncAccountsExist();
dump("Any accounts exist? " + anyExists + "\n");
// Only one account should exist.
do_check_true(!syncExists || !firefoxExists);
do_check_eq(anyExists, firefoxExists || syncExists);
dump("Launching setup.\n");
Accounts.launchSetup();
});
run_next_test();

View File

@ -0,0 +1,76 @@
/* 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";
this.EXPORTED_SYMBOLS = ["Accounts"];
const { utils: Cu } = Components;
Cu.import("resource://gre/modules/Messaging.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Promise.jsm");
/**
* A promise-based API for querying the existence of Sync accounts,
* and accessing the Sync setup wizard.
*
* Usage:
*
* Cu.import("resource://gre/modules/Accounts.jsm");
* Accounts.anySyncAccountsExist().then(
* (exist) => {
* console.log("Accounts exist? " + exist);
* if (!exist) {
* Accounts.launchSetup();
* }
* },
* (err) => {
* console.log("We failed so hard.");
* }
* );
*/
let Accounts = Object.freeze({
_accountsExist: function (kind) {
let deferred = Promise.defer();
sendMessageToJava({
type: "Accounts:Exist",
kind: kind,
}, (data, error) => {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(JSON.parse(data).exists);
}
});
return deferred.promise;
},
firefoxAccountsExist: function () {
return this._accountsExist("fxa");
},
syncAccountsExist: function () {
return this._accountsExist("sync11");
},
anySyncAccountsExist: function () {
return this._accountsExist("any");
},
/**
* Fire-and-forget: open the Firefox accounts activity, which
* will be the Getting Started screen if FxA isn't yet set up.
*
* There is no return value from this method.
*/
launchSetup: function () {
sendMessageToJava({
type: "Accounts:Create",
});
},
});

View File

@ -5,6 +5,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
EXTRA_JS_MODULES += [
'Accounts.jsm',
'ContactService.jsm',
'HelperApps.jsm',
'Home.jsm',