Exemplo n.º 1
0
    private static UnpickleParams fromJSON(final ExtendedJSONObject json)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NonObjectJSONException {
      final UnpickleParams params = new UnpickleParams();
      params.pickleVersion = json.getLong(KEY_PICKLE_VERSION);
      if (params.pickleVersion == null) {
        throw new IllegalStateException("Pickle version not found.");
      }

      switch (params.pickleVersion.intValue()) {
        case 1:
          params.unpickleV1(json);
          break;

        default:
          throw new IllegalStateException("Unknown pickle version, " + params.pickleVersion + ".");
      }

      return params;
    }
Exemplo n.º 2
0
  /**
   * Create Android account from saved JSON object. Assumes that an account does not exist.
   *
   * @param context Android context.
   * @param filename name of file to read from; must not contain path separators.
   * @return created Android account, or null on error.
   */
  public static AndroidFxAccount unpickle(final Context context, final String filename) {
    final String jsonString = Utils.readFile(context, filename);
    if (jsonString == null) {
      Logger.info(LOG_TAG, "Pickle file '" + filename + "' not found; aborting.");
      return null;
    }

    ExtendedJSONObject json = null;
    try {
      json = ExtendedJSONObject.parseJSONObject(jsonString);
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Got exception reading pickle file '" + filename + "'; aborting.", e);
      return null;
    }

    final UnpickleParams params;
    try {
      params = UnpickleParams.fromJSON(json);
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Got exception extracting unpickle json; aborting.", e);
      return null;
    }

    final AndroidFxAccount account;
    try {
      account =
          AndroidFxAccount.addAndroidAccount(
              context,
              params.email,
              params.profile,
              params.idpServerURI,
              params.tokenServerURI,
              params.state,
              params.accountVersion,
              params.isSyncingEnabled,
              true,
              params.bundle);
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Exception when adding Android Account; aborting.", e);
      return null;
    }

    if (account == null) {
      Logger.warn(LOG_TAG, "Failed to add Android Account; aborting.");
      return null;
    }

    Long timestamp = json.getLong(KEY_PICKLE_TIMESTAMP);
    if (timestamp == null) {
      Logger.warn(LOG_TAG, "Did not find timestamp in pickle file; ignoring.");
      timestamp = Long.valueOf(-1);
    }

    Logger.info(
        LOG_TAG,
        "Un-pickled Android account named "
            + params.email
            + " (version "
            + params.pickleVersion
            + ", pickled at "
            + timestamp
            + ").");

    return account;
  }