/* * Copyright 2014 sonaive.com. All rights reserved. * * 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 com.sonaive.v2ex.util; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import static com.sonaive.v2ex.util.LogUtils.makeLogTag; /** * Created by liutao on 12/6/14. */ public class PrefUtils { private static final String TAG = makeLogTag("PrefUtils"); /** * Boolean indicating whether we should attempt to sign in on startup (default true). */ public static final String PREF_USER_REFUSED_SIGN_IN = "pref_user_refused_sign_in"; /** Long indicating when a sync was last ATTEMPTED (not necessarily succeeded) */ public static final String PREF_LAST_SYNC_ATTEMPTED = "pref_last_sync_attempted"; /** Long indicating when a sync last SUCCEEDED */ public static final String PREF_LAST_SYNC_SUCCEEDED = "pref_last_sync_succeeded"; /** Sync interval that's currently configured */ public static final String PREF_CUR_SYNC_INTERVAL = "pref_cur_sync_interval"; public static long getLastSyncAttemptedTime(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getLong(PREF_LAST_SYNC_ATTEMPTED, 0L); } public static void markSyncAttemptedNow(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putLong(PREF_LAST_SYNC_ATTEMPTED, UIUtils.getCurrentTime(context)).commit(); } public static long getLastSyncSucceededTime(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getLong(PREF_LAST_SYNC_SUCCEEDED, 0L); } public static void markSyncSucceededNow(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putLong(PREF_LAST_SYNC_SUCCEEDED, UIUtils.getCurrentTime(context)).commit(); } public static long getCurSyncInterval(final Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getLong(PREF_CUR_SYNC_INTERVAL, 0L); } public static void setCurSyncInterval(final Context context, long interval) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putLong(PREF_CUR_SYNC_INTERVAL, interval).commit(); } public static void registerOnSharedPreferenceChangeListener(final Context context, SharedPreferences.OnSharedPreferenceChangeListener listener) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.registerOnSharedPreferenceChangeListener(listener); } public static void unregisterOnSharedPreferenceChangeListener(final Context context, SharedPreferences.OnSharedPreferenceChangeListener listener) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.unregisterOnSharedPreferenceChangeListener(listener); } }