<commit_before>/* 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.sync.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import org.mozilla.gecko.fxa.login.State;
import org.mozilla.gecko.fxa.login.State.StateLabel;
import org.mozilla.gecko.fxa.login.StateFactory;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;
import org.mozilla.sync.impl.FirefoxAccountShared;
import org.mozilla.sync.impl.FirefoxAccount;
import org.mozilla.sync.impl.FirefoxAccountEndpointConfig;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * A store for a {@link FirefoxAccount}.
 *
 * This is a quick, temporary implementation to use for development. The real implementation should encrypt personal user
 * data, such as email, uid, and tokens. Also, the way the account is used may change the store & its API so I'm delaying
 * those decisions for now.
 *
 * TODO: replace me. See rules ^.
 * Note: iOS is encrypted with iOS keystore (uses pin from phone login) but Android is not.
 */
class FirefoxAccountDevelopmentStore { //TODO: Maybe FirefoxAccountSession. Or FirefoxSyncSession.

    private static final String LOGTAG = FirefoxAccountShared.LOGTAG;

    private static final String DEFAULT_STORE_NAME = "FirefoxAccountDevelopmentStore";
    private static final String PREFS_BRANCH_PREFIX = "org.mozilla.accounts.";

    private final SharedPreferences sharedPrefs;

    /** Create a FirefoxAccountDevelopmentStore with the default name. */
    FirefoxAccountDevelopmentStore(final Context context) {
        this(context, DEFAULT_STORE_NAME);
    }

    // Untested, but in theory this should allow support for multiple accounts.
    private FirefoxAccountDevelopmentStore(final Context context, final String storeName) {
        this.sharedPrefs = context.getSharedPreferences(getPrefsBranch(storeName), 0);
    }

    void saveFirefoxAccount(final FirefoxAccount account) {
        sharedPrefs.edit()
                .putString("email", account.email)
                .putString("uid", account.uid)
                .putString("state-label", account.accountState.getStateLabel().name())
                .putString("state-json", account.accountState.toJSONObject().toJSONString())
                .putString("config-label", account.endpointConfig.label)
                .apply();
    }

    // TODO: docs, return null when empty.
    FirefoxAccount loadFirefoxAccount() {<commit_msg>Add versioning to development store.<commit_after>/* 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.sync.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import org.mozilla.gecko.fxa.login.State;
import org.mozilla.gecko.fxa.login.State.StateLabel;
import org.mozilla.gecko.fxa.login.StateFactory;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;
import org.mozilla.sync.impl.FirefoxAccountShared;
import org.mozilla.sync.impl.FirefoxAccount;
import org.mozilla.sync.impl.FirefoxAccountEndpointConfig;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * A store for a {@link FirefoxAccount}.
 *
 * This is a quick, temporary implementation to use for development. The real implementation should encrypt personal user
 * data, such as email, uid, and tokens. Also, the way the account is used may change the store & its API so I'm delaying
 * those decisions for now.
 *
 * TODO: replace me. See rules ^.
 * Note: iOS is encrypted with iOS keystore (uses pin from phone login) but Android is not.
 */
class FirefoxAccountDevelopmentStore { //TODO: Maybe FirefoxAccountSession. Or FirefoxSyncSession.

    private static final String LOGTAG = FirefoxAccountShared.LOGTAG;

    private static final String DEFAULT_STORE_NAME = "FirefoxAccountDevelopmentStore";
    private static final String PREFS_BRANCH_PREFIX = "org.mozilla.accounts.";
    private static final int STORE_VERSION = 1; // for wiggle room with potential future revisions.

    private final SharedPreferences sharedPrefs;

    /** Create a FirefoxAccountDevelopmentStore with the default name. */
    FirefoxAccountDevelopmentStore(final Context context) {
        this(context, DEFAULT_STORE_NAME);
    }

    // Untested, but in theory this should allow support for multiple accounts.
    private FirefoxAccountDevelopmentStore(final Context context, final String storeName) {
        this.sharedPrefs = context.getSharedPreferences(getPrefsBranch(storeName), 0);
    }

    void saveFirefoxAccount(final FirefoxAccount account) {
        sharedPrefs.edit()
                .putInt("version", STORE_VERSION)
                .putString("email", account.email)
                .putString("uid", account.uid)
                .putString("state-label", account.accountState.getStateLabel().name())
                .putString("state-json", account.accountState.toJSONObject().toJSONString())
                .putString("config-label", account.endpointConfig.label)
                .apply();
    }

    // TODO: docs, return null when empty.
    FirefoxAccount loadFirefoxAccount() {
{'fraction_non_alphanumeric': 0.09006807470762786, 'fraction_numerical': 0.0019200558561703613, 'mean_word_length': 3.8436179205409973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6081419', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1493, 'n_words': 467}