1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.work.impl.utils; 18 19 import android.arch.lifecycle.LiveData; 20 import android.arch.lifecycle.MutableLiveData; 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.support.annotation.RestrictTo; 24 25 /** 26 * Preferences for WorkManager. 27 * 28 * TODO: Migrate all preferences, including IdGenerator, to this file. 29 * 30 * @hide 31 */ 32 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 33 public class Preferences { 34 35 private static final String PREFERENCES_FILE_NAME = "androidx.work.util.preferences"; 36 37 private static final String KEY_LAST_CANCEL_ALL_TIME_MS = "last_cancel_all_time_ms"; 38 private static final String KEY_MIGRATE_PERSISTED_JOBS = "migrate_persisted_jobs"; 39 40 private SharedPreferences mSharedPreferences; 41 Preferences(Context context)42 public Preferences(Context context) { 43 mSharedPreferences = 44 context.getSharedPreferences(PREFERENCES_FILE_NAME, Context.MODE_PRIVATE); 45 } 46 47 /** 48 * @return The last time (in milliseconds) a {@code cancelAll} method was called 49 */ getLastCancelAllTimeMillis()50 public long getLastCancelAllTimeMillis() { 51 return mSharedPreferences.getLong(KEY_LAST_CANCEL_ALL_TIME_MS, 0L); 52 } 53 54 /** 55 * @return A {@link LiveData} of the last time (in milliseconds) a {@code cancelAll} method was 56 * called 57 */ getLastCancelAllTimeMillisLiveData()58 public LiveData<Long> getLastCancelAllTimeMillisLiveData() { 59 return new LastCancelAllLiveData(mSharedPreferences); 60 } 61 62 /** 63 * Sets the last time a {@code cancelAll} method was called 64 * 65 * @param timeMillis The time a {@code cancelAll} method was called (in milliseconds) 66 */ setLastCancelAllTimeMillis(long timeMillis)67 public void setLastCancelAllTimeMillis(long timeMillis) { 68 mSharedPreferences.edit().putLong(KEY_LAST_CANCEL_ALL_TIME_MS, timeMillis).apply(); 69 } 70 71 /** 72 * @return {@code true} When we should migrate from persisted jobs to non-persisted jobs in 73 * {@link android.app.job.JobScheduler} 74 */ shouldMigratePersistedJobs()75 public boolean shouldMigratePersistedJobs() { 76 // TODO Remove this before WorkManager 1.0 beta. 77 return mSharedPreferences.getBoolean(KEY_MIGRATE_PERSISTED_JOBS, true); 78 } 79 80 /** 81 * Updates the key which indicates that we have migrated all our persisted jobs in 82 * {@link android.app.job.JobScheduler}. 83 */ setMigratedPersistedJobs()84 public void setMigratedPersistedJobs() { 85 mSharedPreferences.edit().putBoolean(KEY_MIGRATE_PERSISTED_JOBS, true).apply(); 86 } 87 88 /** 89 * A {@link android.arch.lifecycle.LiveData} that responds to changes in 90 * {@link SharedPreferences} for the {@code lastCancelAllTime} value. 91 */ 92 private static class LastCancelAllLiveData extends MutableLiveData<Long> 93 implements SharedPreferences.OnSharedPreferenceChangeListener { 94 95 private SharedPreferences mSharedPreferences; 96 private long mLastCancelAllTimeMillis; 97 LastCancelAllLiveData(SharedPreferences sharedPreferences)98 LastCancelAllLiveData(SharedPreferences sharedPreferences) { 99 mSharedPreferences = sharedPreferences; 100 mLastCancelAllTimeMillis = mSharedPreferences.getLong(KEY_LAST_CANCEL_ALL_TIME_MS, 0L); 101 postValue(mLastCancelAllTimeMillis); 102 } 103 104 @Override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)105 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 106 if (KEY_LAST_CANCEL_ALL_TIME_MS.equals(key)) { 107 long lastCancelAllTimeMillis = sharedPreferences.getLong(key, 0L); 108 if (mLastCancelAllTimeMillis != lastCancelAllTimeMillis) { 109 mLastCancelAllTimeMillis = lastCancelAllTimeMillis; 110 setValue(mLastCancelAllTimeMillis); 111 } 112 } 113 } 114 115 @Override onActive()116 protected void onActive() { 117 super.onActive(); 118 mSharedPreferences.registerOnSharedPreferenceChangeListener(this); 119 } 120 121 @Override onInactive()122 protected void onInactive() { 123 super.onInactive(); 124 mSharedPreferences.unregisterOnSharedPreferenceChangeListener(this); 125 } 126 } 127 } 128