michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.setup; michael@0: michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.security.NoSuchAlgorithmException; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SyncConstants; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity; michael@0: michael@0: import android.accounts.AbstractAccountAuthenticator; michael@0: import android.accounts.Account; michael@0: import android.accounts.AccountAuthenticatorResponse; michael@0: import android.accounts.AccountManager; michael@0: import android.accounts.NetworkErrorException; michael@0: import android.app.Service; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.os.Bundle; michael@0: import android.os.IBinder; michael@0: michael@0: public class SyncAuthenticatorService extends Service { michael@0: private static final String LOG_TAG = "SyncAuthService"; michael@0: michael@0: private SyncAccountAuthenticator sAccountAuthenticator = null; michael@0: michael@0: @Override michael@0: public void onCreate() { michael@0: Logger.debug(LOG_TAG, "onCreate"); michael@0: sAccountAuthenticator = getAuthenticator(); michael@0: } michael@0: michael@0: @Override michael@0: public IBinder onBind(Intent intent) { michael@0: if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) { michael@0: return getAuthenticator().getIBinder(); michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: private SyncAccountAuthenticator getAuthenticator() { michael@0: if (sAccountAuthenticator == null) { michael@0: sAccountAuthenticator = new SyncAccountAuthenticator(this); michael@0: } michael@0: return sAccountAuthenticator; michael@0: } michael@0: michael@0: /** michael@0: * Generate a "plain" auth token. michael@0: *
michael@0: * Android caches only the value of the key
michael@0: * AccountManager.KEY_AUTHTOKEN
, so if a caller needs the other
michael@0: * keys in this bundle, it needs to invalidate the token (so that the bundle
michael@0: * is re-generated).
michael@0: *
michael@0: * @param context
michael@0: * Android context.
michael@0: * @param account
michael@0: * Android account.
michael@0: * @return a Bundle
instance containing a subset of the following
michael@0: * keys: (caller's must check for missing keys)
michael@0: *
AccountManager.KEY_ACCOUNT_TYPE
: the Android
michael@0: * Account's typeAccountManager.KEY_ACCOUNT_NAME
: the Android
michael@0: * Account's nameAccountManager.KEY_AUTHTOKEN
: the Sync account's
michael@0: * password Constants.OPTION_USERNAME
: the Sync account's
michael@0: * hashed usernameConstants.OPTION_SERVER
: the Sync account's
michael@0: * server Constants.OPTION_SYNCKEY
: the Sync account's
michael@0: * sync keymichael@0: * This is not called when an Android Account is blown away due to michael@0: * the SD card being unmounted. michael@0: *
michael@0: * Broadcasting a Firefox intent to version sharing this Android Account is michael@0: * a terrible hack, but it's better than the catching the generic michael@0: * "accounts changed" broadcast intent and trying to figure out whether our michael@0: * Account disappeared. michael@0: */ michael@0: @Override michael@0: public Bundle getAccountRemovalAllowed(final AccountAuthenticatorResponse response, Account account) michael@0: throws NetworkErrorException { michael@0: Bundle result = super.getAccountRemovalAllowed(response, account); michael@0: michael@0: if (result == null || michael@0: !result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) || michael@0: result.containsKey(AccountManager.KEY_INTENT)) { michael@0: return result; michael@0: } michael@0: michael@0: final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); michael@0: if (!removalAllowed) { michael@0: return result; michael@0: } michael@0: michael@0: // Bug 790931: Broadcast a message to all Firefox versions sharing this michael@0: // Android Account type telling that this Sync Account has been deleted. michael@0: // michael@0: // We would really prefer to receive Android's michael@0: // LOGIN_ACCOUNTS_CHANGED_ACTION broadcast, but that michael@0: // doesn't include enough information about which Accounts changed to michael@0: // correctly identify whether a Sync account has been removed (when some michael@0: // Firefox versions are installed on the SD card). michael@0: // michael@0: // Broadcast intents protected with permissions are secure, so it's okay michael@0: // to include password and sync key, etc. michael@0: final Intent intent = SyncAccounts.makeSyncAccountDeletedIntent(mContext, AccountManager.get(mContext), account); michael@0: Logger.info(LOG_TAG, "Account named " + account.name + " being removed; " + michael@0: "broadcasting secure intent " + intent.getAction() + "."); michael@0: mContext.sendBroadcast(intent, SyncConstants.PER_ACCOUNT_TYPE_PERMISSION); michael@0: michael@0: return result; michael@0: } michael@0: } michael@0: }