<commit_before>/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfpush;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.onepf.opfpush.model.State;

import static org.onepf.opfpush.model.State.UNREGISTERED;

/**
 * @author <NAME>
 * @since 01.10.14.
 */
//TODO Add timeout for storing state
class Settings {

    private static final String KEY_LAST_PROVIDER_NAME = "last_provider_name";
    private static final String KEY_STATE = "state";
    private static final String KEY_LAST_ANDROID_ID = "android_id";

    private static final String PREF_NAME = "org.onepf.openpush";

    @NonNull
    private final SharedPreferences mPreferences;

    public Settings(@NonNull Context context) {
        mPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    @NonNull
    public State getState() {
        final int stateValue = mPreferences.getInt(KEY_STATE, UNREGISTERED.getValue());
        final State state = State.fromValue(stateValue);
        return state == null ? UNREGISTERED : state;
    }

    public void saveState(@NonNull final State state) {
        mPreferences.edit().putInt(KEY_STATE, state.getValue()).apply();
    }

    public void clear() {
        mPreferences.edit().clear().apply();
    }

    @Nullable
    public String getLastProviderName() {
        return mPreferences.getString(KEY_LAST_PROVIDER_NAME, null);
    }

    public void saveLastProvider(@Nullable PushProvider provider) {
        SharedPreferences.Editor editor = mPreferences.edit();
        if (provider == null) {
            editor.remove(KEY_LAST_PROVIDER_NAME);
        } else {
            editor.putString(KEY_LAST_PROVIDER_NAME, provider.getName());
        }
        editor.apply();
    }

    @Nullable
    public String getLastAndroidId() {
        return mPreferences.getString(KEY_LAST_ANDROID_ID, null);
    }

    public void saveLastAndroidId(@Nullable String androidId) {
        SharedPreferences.Editor editor = mPreferences.edit();
        if (androidId == null) {
            editor.remove(KEY_LAST_ANDROID_ID);
        } else {
            editor.putString(KEY_LAST_ANDROID_ID, androidId);
        }
        editor.apply();
    }
}<commit_msg>Add timeout of storing REGISTERING and UNREGISTERING states.<commit_after>/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfpush;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.onepf.opfpush.model.State;

import java.util.concurrent.TimeUnit;

import static org.onepf.opfpush.model.State.REGISTERED;
import static org.onepf.opfpush.model.State.REGISTERING;
import static org.onepf.opfpush.model.State.UNREGISTERED;
import static org.onepf.opfpush.model.State.UNREGISTERING;

/**
 * @author <NAME>
 * @author <NAME>
 * @since 01.10.14.
 */
//TODO: Config timeout via options.
class Settings {

    private static final long STATE_INFINITY_TIMESTAMP = -1L;

    private static final String KEY_LAST_PROVIDER_NAME = "last_provider_name";
    private static final String KEY_STATE = "state";
    private static final String KEY_LAST_ANDROID_ID = "android_id";
    private static final String KEY_STATE_TIMESTAMP = "state_timestamp";

    private static final String PREF_NAME = "org.onepf.openpush";

    @NonNull
    private final SharedPreferences preferences;

    public Settings(@NonNull Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    @NonNull
    public State getState() {
        final int stateValue = preferences.getInt(KEY_STATE, UNREGISTERED.getValue());
        State state = State.fromValue(stateValue);
        if (state == null) {
            state = UNREGISTERED;
            saveState(state);
        }

        final long stateTimestamp = preferences.getLong(KEY_STATE_TIMESTAMP, STATE_INFINITY_TIMESTAMP);
        if (stateTimestamp > TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)) {
            if (state == REGISTERING) {
                state = UNREGISTERED;
                saveState(state);
            } else if (state == UNREGISTERING) {
                state = REGISTERED;
                saveState(state);
            }
        }

        return state;
    }

    public void saveState(@NonNull final State state) {
        final long stateTimestamp = isInfinityState(state) ? STATE_INFINITY_TIMESTAMP
                : System.currentTimeMillis();

        preferences.edit()
                .putInt(KEY_STATE, state.getValue())
                .putLong(KEY_STATE_TIMESTAMP, stateTimestamp)
                .apply();
    }

    public void clear() {
        preferences.edit().clear().apply();
    }

    @Nullable
    public String getLastProviderName() {
        return preferences.getString(KEY_LAST_PROVIDER_NAME, null);
    }

    public void saveLastProvider(@Nullable PushProvider provider) {
        final SharedPreferences.Editor editor = preferences.edit();
        if (provider == null) {
            editor.remove(KEY_LAST_PROVIDER_NAME);
        } else {
            editor.putString(KEY_LAST_PROVIDER_NAME, provider.getName());
        }
        editor.apply();
    }

    @Nullable
    public String getLastAndroidId() {
        return preferences.getString(KEY_LAST_ANDROID_ID, null);
    }

    public void saveLastAndroidId(@Nullable String androidId) {
        final SharedPreferences.Editor editor = preferences.edit();
        if (androidId == null) {
            editor.remove(KEY_LAST_ANDROID_ID);
        } else {
            editor.putString(KEY_LAST_ANDROID_ID, androidId);
        }
        editor.apply();
    }

    private boolean isInfinityState(@NonNull final State state) {
        return state == REGISTERED || state == UNREGISTERED;
    }
}
{'fraction_non_alphanumeric': 0.09058790276992651, 'fraction_numerical': 0.005370265686828717, 'mean_word_length': 3.0324786324786324, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763567', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1920, 'n_words': 524}