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.config; michael@0: michael@0: import java.io.FileOutputStream; michael@0: import java.io.PrintStream; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.setup.Constants; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; michael@0: michael@0: import android.accounts.Account; michael@0: import android.content.Context; michael@0: michael@0: /** michael@0: * Bug 768102: Android deletes Account objects when the Authenticator that owns michael@0: * the Account disappears. This happens when an App is installed to the SD card michael@0: * and the SD card is un-mounted or the device is rebooted. michael@0: *
michael@0: * Bug 769745: Work around this by pickling the current Sync account data every michael@0: * sync. michael@0: *
michael@0: * Bug 735842: Work around this by un-pickling when we check if Sync accounts michael@0: * exist (called from Fennec). michael@0: *
michael@0: * Android just doesn't support installing Apps that define long-lived Services michael@0: * and/or own Account types onto the SD card. The documentation says not to do michael@0: * it. There are hordes of developers who want to do it, and have tried to michael@0: * register for almost every "package installation changed" broadcast intent michael@0: * that Android supports. They all explicitly state that the package that has michael@0: * changed does *not* receive the broadcast intent, thereby preventing an App michael@0: * from re-establishing its state. michael@0: *
michael@0: * Reference. michael@0: *
michael@0: * Quote: Your AbstractThreadedSyncAdapter and all its sync functionality michael@0: * will not work until external storage is remounted. michael@0: *
michael@0: * Quote: Your running Service will be killed and will not be restarted michael@0: * when external storage is remounted. You can, however, register for the michael@0: * ACTION_EXTERNAL_APPLICATIONS_AVAILABLE broadcast Intent, which will notify michael@0: * your application when applications installed on external storage have become michael@0: * available to the system again. At which time, you can restart your Service. michael@0: *
michael@0: * Problem: that intent doesn't work!
michael@0: */
michael@0: public class AccountPickler {
michael@0: public static final String LOG_TAG = "AccountPickler";
michael@0:
michael@0: public static final long VERSION = 1;
michael@0:
michael@0: /**
michael@0: * Remove Sync account persisted to disk.
michael@0: *
michael@0: * @param context Android context.
michael@0: * @param filename name of persisted pickle file; must not contain path separators.
michael@0: * @return true
if given pickle existed and was successfully deleted.
michael@0: */
michael@0: public static boolean deletePickle(final Context context, final String filename) {
michael@0: return context.deleteFile(filename);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Persist Sync account to disk as a JSON object.
michael@0: *
michael@0: * JSON object has keys: michael@0: *
Constants.JSON_KEY_ACCOUNT
: the Sync account's un-encoded username,
michael@0: * like "test@mozilla.com".Constants.JSON_KEY_PASSWORD
: the Sync account's password;Constants.JSON_KEY_SERVER
: the Sync account's server;Constants.JSON_KEY_SYNCKEY
: the Sync account's sync key;Constants.JSON_KEY_CLUSTER
: the Sync account's cluster (may be null);Constants.JSON_KEY_CLIENT_NAME
: the Sync account's client name (may be null);Constants.JSON_KEY_CLIENT_GUID
: the Sync account's client GUID (may be null);Constants.JSON_KEY_SYNC_AUTOMATICALLY
: true if the Android Account is syncing automically;Constants.JSON_KEY_VERSION
: version of this file;Constants.JSON_KEY_TIMESTAMP
: when this file was written.