The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.preference; |
| 18 | |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 21 | import android.annotation.UnsupportedAppUsage; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 22 | import android.annotation.XmlRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
| 26 | import android.content.Intent; |
| 27 | import android.content.SharedPreferences; |
| 28 | import android.content.pm.ActivityInfo; |
| 29 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 31 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.res.XmlResourceParser; |
| 33 | import android.os.Bundle; |
| 34 | import android.util.Log; |
| 35 | |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.HashSet; |
| 38 | import java.util.List; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Used to help create {@link Preference} hierarchies |
| 42 | * from activities or XML. |
| 43 | * <p> |
| 44 | * In most cases, clients should use |
| 45 | * {@link PreferenceActivity#addPreferencesFromIntent} or |
| 46 | * {@link PreferenceActivity#addPreferencesFromResource(int)}. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 47 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * @see PreferenceActivity |
| 49 | */ |
| 50 | public class PreferenceManager { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | private static final String TAG = "PreferenceManager"; |
| 53 | |
| 54 | /** |
| 55 | * The Activity meta-data key for its XML preference hierarchy. |
| 56 | */ |
| 57 | public static final String METADATA_KEY_PREFERENCES = "android.preference"; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | public static final String KEY_HAS_SET_DEFAULT_VALUES = "_has_set_default_values"; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | /** |
| 62 | * @see #getActivity() |
| 63 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 64 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | private Activity mActivity; |
| 66 | |
| 67 | /** |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 68 | * Fragment that owns this instance. |
| 69 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 70 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 71 | @UnsupportedAppUsage |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 72 | private PreferenceFragment mFragment; |
| 73 | |
| 74 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * The context to use. This should always be set. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 76 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | * @see #mActivity |
| 78 | */ |
| 79 | private Context mContext; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | /** |
| 82 | * The counter for unique IDs. |
| 83 | */ |
| 84 | private long mNextId = 0; |
| 85 | |
| 86 | /** |
| 87 | * The counter for unique request codes. |
| 88 | */ |
| 89 | private int mNextRequestCode; |
| 90 | |
| 91 | /** |
| 92 | * Cached shared preferences. |
| 93 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 94 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 95 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private SharedPreferences mSharedPreferences; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 99 | * Data store to be used by the Preferences or {@code null} if |
| 100 | * {@link android.content.SharedPreferences} should be used. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 101 | */ |
| 102 | @Nullable |
| 103 | private PreferenceDataStore mPreferenceDataStore; |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | /** |
| 106 | * If in no-commit mode, the shared editor to give out (which will be |
| 107 | * committed when exiting no-commit mode). |
| 108 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 109 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private SharedPreferences.Editor mEditor; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Blocks commits from happening on the shared editor. This is used when |
| 114 | * inflating the hierarchy. Do not set this directly, use {@link #setNoCommit(boolean)} |
| 115 | */ |
| 116 | private boolean mNoCommit; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | /** |
| 119 | * The SharedPreferences name that will be used for all {@link Preference}s |
| 120 | * managed by this instance. |
| 121 | */ |
| 122 | private String mSharedPreferencesName; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | /** |
| 125 | * The SharedPreferences mode that will be used for all {@link Preference}s |
| 126 | * managed by this instance. |
| 127 | */ |
| 128 | private int mSharedPreferencesMode; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 129 | |
| 130 | private static final int STORAGE_DEFAULT = 0; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 131 | private static final int STORAGE_DEVICE_PROTECTED = 1; |
| 132 | private static final int STORAGE_CREDENTIAL_PROTECTED = 2; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 133 | |
| 134 | private int mStorage = STORAGE_DEFAULT; |
| 135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | /** |
| 137 | * The {@link PreferenceScreen} at the root of the preference hierarchy. |
| 138 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 139 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | private PreferenceScreen mPreferenceScreen; |
| 141 | |
| 142 | /** |
| 143 | * List of activity result listeners. |
| 144 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 145 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | private List<OnActivityResultListener> mActivityResultListeners; |
| 147 | |
| 148 | /** |
| 149 | * List of activity stop listeners. |
| 150 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 151 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private List<OnActivityStopListener> mActivityStopListeners; |
| 153 | |
| 154 | /** |
| 155 | * List of activity destroy listeners. |
| 156 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 157 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 158 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | private List<OnActivityDestroyListener> mActivityDestroyListeners; |
| 160 | |
| 161 | /** |
| 162 | * List of dialogs that should be dismissed when we receive onNewIntent in |
| 163 | * our PreferenceActivity. |
| 164 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 165 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | private List<DialogInterface> mPreferencesScreens; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 167 | |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 168 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | private OnPreferenceTreeClickListener mOnPreferenceTreeClickListener; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 170 | |
Justin Koh | 37ae558 | 2012-10-25 15:26:36 -0700 | [diff] [blame] | 171 | /** |
| 172 | * @hide |
| 173 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 174 | @UnsupportedAppUsage |
Justin Koh | 37ae558 | 2012-10-25 15:26:36 -0700 | [diff] [blame] | 175 | public PreferenceManager(Activity activity, int firstRequestCode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | mActivity = activity; |
| 177 | mNextRequestCode = firstRequestCode; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | init(activity); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * This constructor should ONLY be used when getting default values from |
| 184 | * an XML preference hierarchy. |
| 185 | * <p> |
| 186 | * The {@link PreferenceManager#PreferenceManager(Activity)} |
| 187 | * should be used ANY time a preference will be displayed, since some preference |
| 188 | * types need an Activity for managed queries. |
| 189 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 190 | @UnsupportedAppUsage |
Deepanshu Gupta | 10bb137 | 2014-10-05 14:41:17 -0700 | [diff] [blame] | 191 | /*package*/ PreferenceManager(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | init(context); |
| 193 | } |
| 194 | |
| 195 | private void init(Context context) { |
| 196 | mContext = context; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | setSharedPreferencesName(getDefaultSharedPreferencesName(context)); |
| 199 | } |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Sets the owning preference fragment |
| 203 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 204 | @UnsupportedAppUsage |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 205 | void setFragment(PreferenceFragment fragment) { |
| 206 | mFragment = fragment; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the owning preference fragment, if any. |
| 211 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 212 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 213 | @UnsupportedAppUsage |
Amith Yamasani | 82e7bc1 | 2010-09-23 15:07:58 -0700 | [diff] [blame] | 214 | PreferenceFragment getFragment() { |
| 215 | return mFragment; |
| 216 | } |
| 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | /** |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 219 | * Sets a {@link PreferenceDataStore} to be used by all Preferences associated with this manager |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 220 | * that don't have a custom {@link PreferenceDataStore} assigned via |
| 221 | * {@link Preference#setPreferenceDataStore(PreferenceDataStore)}. Also if the data store is |
| 222 | * set, the child preferences won't use {@link android.content.SharedPreferences} as long as |
| 223 | * they are assigned to this manager. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 224 | * |
| 225 | * @param dataStore The {@link PreferenceDataStore} to be used by this manager. |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 226 | * @see Preference#setPreferenceDataStore(PreferenceDataStore) |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 227 | */ |
| 228 | public void setPreferenceDataStore(PreferenceDataStore dataStore) { |
| 229 | mPreferenceDataStore = dataStore; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns the {@link PreferenceDataStore} associated with this manager or {@code null} if |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 234 | * the default {@link android.content.SharedPreferences} are used instead. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 235 | * |
| 236 | * @return The {@link PreferenceDataStore} associated with this manager or {@code null} if none. |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 237 | * @see #setPreferenceDataStore(PreferenceDataStore) |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 238 | */ |
| 239 | @Nullable |
| 240 | public PreferenceDataStore getPreferenceDataStore() { |
| 241 | return mPreferenceDataStore; |
| 242 | } |
| 243 | |
| 244 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | * Returns a list of {@link Activity} (indirectly) that match a given |
| 246 | * {@link Intent}. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 247 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * @param queryIntent The Intent to match. |
| 249 | * @return The list of {@link ResolveInfo} that point to the matched |
| 250 | * activities. |
| 251 | */ |
| 252 | private List<ResolveInfo> queryIntentActivities(Intent queryIntent) { |
| 253 | return mContext.getPackageManager().queryIntentActivities(queryIntent, |
| 254 | PackageManager.GET_META_DATA); |
| 255 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | /** |
| 258 | * Inflates a preference hierarchy from the preference hierarchies of |
| 259 | * {@link Activity Activities} that match the given {@link Intent}. An |
| 260 | * {@link Activity} defines its preference hierarchy with meta-data using |
| 261 | * the {@link #METADATA_KEY_PREFERENCES} key. |
| 262 | * <p> |
| 263 | * If a preference hierarchy is given, the new preference hierarchies will |
| 264 | * be merged in. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 265 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | * @param queryIntent The intent to match activities. |
| 267 | * @param rootPreferences Optional existing hierarchy to merge the new |
| 268 | * hierarchies into. |
| 269 | * @return The root hierarchy (if one was not provided, the new hierarchy's |
| 270 | * root). |
| 271 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 272 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) { |
| 274 | final List<ResolveInfo> activities = queryIntentActivities(queryIntent); |
| 275 | final HashSet<String> inflatedRes = new HashSet<String>(); |
| 276 | |
| 277 | for (int i = activities.size() - 1; i >= 0; i--) { |
| 278 | final ActivityInfo activityInfo = activities.get(i).activityInfo; |
| 279 | final Bundle metaData = activityInfo.metaData; |
| 280 | |
| 281 | if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | // Need to concat the package with res ID since the same res ID |
| 286 | // can be re-used across contexts |
| 287 | final String uniqueResId = activityInfo.packageName + ":" |
| 288 | + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES); |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | if (!inflatedRes.contains(uniqueResId)) { |
| 291 | inflatedRes.add(uniqueResId); |
| 292 | |
| 293 | final Context context; |
| 294 | try { |
| 295 | context = mContext.createPackageContext(activityInfo.packageName, 0); |
| 296 | } catch (NameNotFoundException e) { |
| 297 | Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": " |
Filip Pavlis | 0955793 | 2017-03-03 16:54:22 +0000 | [diff] [blame] | 298 | + Log.getStackTraceString(e)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | continue; |
| 300 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | final PreferenceInflater inflater = new PreferenceInflater(context, this); |
| 303 | final XmlResourceParser parser = activityInfo.loadXmlMetaData(context |
| 304 | .getPackageManager(), METADATA_KEY_PREFERENCES); |
| 305 | rootPreferences = (PreferenceScreen) inflater |
| 306 | .inflate(parser, rootPreferences, true); |
| 307 | parser.close(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | rootPreferences.onAttachedToHierarchy(this); |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | return rootPreferences; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Inflates a preference hierarchy from XML. If a preference hierarchy is |
| 318 | * given, the new preference hierarchies will be merged in. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 319 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | * @param context The context of the resource. |
| 321 | * @param resId The resource ID of the XML to inflate. |
| 322 | * @param rootPreferences Optional existing hierarchy to merge the new |
| 323 | * hierarchies into. |
| 324 | * @return The root hierarchy (if one was not provided, the new hierarchy's |
| 325 | * root). |
Owen Lin | f9b702e | 2009-08-27 18:30:05 +0800 | [diff] [blame] | 326 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 328 | @UnsupportedAppUsage |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 329 | public PreferenceScreen inflateFromResource(Context context, @XmlRes int resId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | PreferenceScreen rootPreferences) { |
| 331 | // Block commits |
| 332 | setNoCommit(true); |
| 333 | |
| 334 | final PreferenceInflater inflater = new PreferenceInflater(context, this); |
| 335 | rootPreferences = (PreferenceScreen) inflater.inflate(resId, rootPreferences, true); |
| 336 | rootPreferences.onAttachedToHierarchy(this); |
| 337 | |
| 338 | // Unblock commits |
| 339 | setNoCommit(false); |
| 340 | |
| 341 | return rootPreferences; |
| 342 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | public PreferenceScreen createPreferenceScreen(Context context) { |
| 345 | final PreferenceScreen preferenceScreen = new PreferenceScreen(context, null); |
| 346 | preferenceScreen.onAttachedToHierarchy(this); |
| 347 | return preferenceScreen; |
| 348 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | /** |
| 351 | * Called by a preference to get a unique ID in its hierarchy. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 352 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | * @return A unique ID. |
| 354 | */ |
| 355 | long getNextId() { |
| 356 | synchronized (this) { |
| 357 | return mNextId++; |
| 358 | } |
| 359 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | /** |
| 362 | * Returns the current name of the SharedPreferences file that preferences managed by |
| 363 | * this will use. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 364 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | * @return The name that can be passed to {@link Context#getSharedPreferences(String, int)}. |
| 366 | * @see Context#getSharedPreferences(String, int) |
| 367 | */ |
| 368 | public String getSharedPreferencesName() { |
| 369 | return mSharedPreferencesName; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Sets the name of the SharedPreferences file that preferences managed by this |
| 374 | * will use. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 375 | * |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 376 | * <p>If custom {@link PreferenceDataStore} is set, this won't override its usage. |
| 377 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | * @param sharedPreferencesName The name of the SharedPreferences file. |
| 379 | * @see Context#getSharedPreferences(String, int) |
Filip Pavlis | fd59645 | 2017-03-01 18:50:00 +0000 | [diff] [blame] | 380 | * @see #setPreferenceDataStore(PreferenceDataStore) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | */ |
| 382 | public void setSharedPreferencesName(String sharedPreferencesName) { |
| 383 | mSharedPreferencesName = sharedPreferencesName; |
| 384 | mSharedPreferences = null; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Returns the current mode of the SharedPreferences file that preferences managed by |
| 389 | * this will use. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 390 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | * @return The mode that can be passed to {@link Context#getSharedPreferences(String, int)}. |
| 392 | * @see Context#getSharedPreferences(String, int) |
| 393 | */ |
| 394 | public int getSharedPreferencesMode() { |
| 395 | return mSharedPreferencesMode; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Sets the mode of the SharedPreferences file that preferences managed by this |
| 400 | * will use. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 401 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | * @param sharedPreferencesMode The mode of the SharedPreferences file. |
| 403 | * @see Context#getSharedPreferences(String, int) |
| 404 | */ |
| 405 | public void setSharedPreferencesMode(int sharedPreferencesMode) { |
| 406 | mSharedPreferencesMode = sharedPreferencesMode; |
| 407 | mSharedPreferences = null; |
| 408 | } |
| 409 | |
| 410 | /** |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 411 | * Sets the storage location used internally by this class to be the default |
| 412 | * provided by the hosting {@link Context}. |
| 413 | */ |
| 414 | public void setStorageDefault() { |
| 415 | mStorage = STORAGE_DEFAULT; |
| 416 | mSharedPreferences = null; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Explicitly set the storage location used internally by this class to be |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 421 | * device-protected storage. |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 422 | * <p> |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 423 | * On devices with direct boot, data stored in this location is encrypted |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 424 | * with a key tied to the physical device, and it can be accessed |
| 425 | * immediately after the device has booted successfully, both |
| 426 | * <em>before and after</em> the user has authenticated with their |
| 427 | * credentials (such as a lock pattern or PIN). |
| 428 | * <p> |
| 429 | * Because device-protected data is available without user authentication, |
| 430 | * you should carefully limit the data you store using this Context. For |
| 431 | * example, storing sensitive authentication tokens or passwords in the |
| 432 | * device-protected area is strongly discouraged. |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 433 | * |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 434 | * @see Context#createDeviceProtectedStorageContext() |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 435 | */ |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 436 | public void setStorageDeviceProtected() { |
| 437 | mStorage = STORAGE_DEVICE_PROTECTED; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 438 | mSharedPreferences = null; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Explicitly set the storage location used internally by this class to be |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 443 | * credential-protected storage. This is the default storage area for apps |
| 444 | * unless {@code forceDeviceProtectedStorage} was requested. |
| 445 | * <p> |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 446 | * On devices with direct boot, data stored in this location is encrypted |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 447 | * with a key tied to user credentials, which can be accessed |
| 448 | * <em>only after</em> the user has entered their credentials (such as a |
| 449 | * lock pattern or PIN). |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 450 | * |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 451 | * @see Context#createCredentialProtectedStorageContext() |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 452 | * @hide |
| 453 | */ |
| 454 | @SystemApi |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 455 | public void setStorageCredentialProtected() { |
| 456 | mStorage = STORAGE_CREDENTIAL_PROTECTED; |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 457 | mSharedPreferences = null; |
| 458 | } |
| 459 | |
| 460 | /** |
Jeff Sharkey | c9a40cd8 | 2016-03-27 12:10:38 -0600 | [diff] [blame] | 461 | * Indicates if the storage location used internally by this class is the |
| 462 | * default provided by the hosting {@link Context}. |
| 463 | * |
| 464 | * @see #setStorageDefault() |
| 465 | * @see #setStorageDeviceProtected() |
| 466 | */ |
| 467 | public boolean isStorageDefault() { |
| 468 | return mStorage == STORAGE_DEFAULT; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Indicates if the storage location used internally by this class is backed |
| 473 | * by device-protected storage. |
| 474 | * |
| 475 | * @see #setStorageDefault() |
| 476 | * @see #setStorageDeviceProtected() |
| 477 | */ |
| 478 | public boolean isStorageDeviceProtected() { |
| 479 | return mStorage == STORAGE_DEVICE_PROTECTED; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Indicates if the storage location used internally by this class is backed |
| 484 | * by credential-protected storage. |
| 485 | * |
| 486 | * @see #setStorageDefault() |
| 487 | * @see #setStorageDeviceProtected() |
| 488 | * @hide |
| 489 | */ |
| 490 | @SystemApi |
| 491 | public boolean isStorageCredentialProtected() { |
| 492 | return mStorage == STORAGE_CREDENTIAL_PROTECTED; |
| 493 | } |
| 494 | |
| 495 | /** |
Filip Pavlis | 0955793 | 2017-03-03 16:54:22 +0000 | [diff] [blame] | 496 | * Gets a {@link SharedPreferences} instance that preferences managed by this will use. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 497 | * |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 498 | * @return a {@link SharedPreferences} instance pointing to the file that contains the values of |
| 499 | * preferences that are managed by this PreferenceManager. If a |
| 500 | * {@link PreferenceDataStore} has been set, this method returns {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | */ |
| 502 | public SharedPreferences getSharedPreferences() { |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 503 | if (mPreferenceDataStore != null) { |
| 504 | return null; |
| 505 | } |
| 506 | |
| 507 | if (mSharedPreferences == null) { |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 508 | final Context storageContext; |
| 509 | switch (mStorage) { |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 510 | case STORAGE_DEVICE_PROTECTED: |
| 511 | storageContext = mContext.createDeviceProtectedStorageContext(); |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 512 | break; |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 513 | case STORAGE_CREDENTIAL_PROTECTED: |
| 514 | storageContext = mContext.createCredentialProtectedStorageContext(); |
Jeff Sharkey | fd37abe | 2016-01-28 11:53:48 -0700 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | storageContext = mContext; |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | mSharedPreferences = storageContext.getSharedPreferences(mSharedPreferencesName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | mSharedPreferencesMode); |
| 523 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | return mSharedPreferences; |
| 526 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | /** |
Filip Pavlis | 0955793 | 2017-03-03 16:54:22 +0000 | [diff] [blame] | 529 | * Gets a {@link SharedPreferences} instance that points to the default file that is used by |
| 530 | * the preference framework in the given context. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 531 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | * @param context The context of the preferences whose values are wanted. |
Filip Pavlis | 0955793 | 2017-03-03 16:54:22 +0000 | [diff] [blame] | 533 | * @return A {@link SharedPreferences} instance that can be used to retrieve and listen |
| 534 | * to values of the preferences. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | */ |
| 536 | public static SharedPreferences getDefaultSharedPreferences(Context context) { |
| 537 | return context.getSharedPreferences(getDefaultSharedPreferencesName(context), |
| 538 | getDefaultSharedPreferencesMode()); |
| 539 | } |
| 540 | |
Jeff Sharkey | 6a6cdaf | 2015-12-07 19:25:19 -0700 | [diff] [blame] | 541 | /** |
| 542 | * Returns the name used for storing default shared preferences. |
| 543 | * |
| 544 | * @see #getDefaultSharedPreferences(Context) |
| 545 | * @see Context#getSharedPreferencesPath(String) |
| 546 | */ |
| 547 | public static String getDefaultSharedPreferencesName(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | return context.getPackageName() + "_preferences"; |
| 549 | } |
| 550 | |
| 551 | private static int getDefaultSharedPreferencesMode() { |
| 552 | return Context.MODE_PRIVATE; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Returns the root of the preference hierarchy managed by this class. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 557 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | * @return The {@link PreferenceScreen} object that is at the root of the hierarchy. |
| 559 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 560 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 561 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | PreferenceScreen getPreferenceScreen() { |
| 563 | return mPreferenceScreen; |
| 564 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 565 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | /** |
| 567 | * Sets the root of the preference hierarchy. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 568 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 570 | * @return Whether the {@link PreferenceScreen} given is different than the previous. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 572 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | boolean setPreferences(PreferenceScreen preferenceScreen) { |
| 574 | if (preferenceScreen != mPreferenceScreen) { |
| 575 | mPreferenceScreen = preferenceScreen; |
| 576 | return true; |
| 577 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | return false; |
| 580 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 581 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | /** |
| 583 | * Finds a {@link Preference} based on its key. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 584 | * |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 585 | * @param key the key of the preference to retrieve |
| 586 | * @return the {@link Preference} with the key, or {@code null} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | * @see PreferenceGroup#findPreference(CharSequence) |
| 588 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 589 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | public Preference findPreference(CharSequence key) { |
| 591 | if (mPreferenceScreen == null) { |
| 592 | return null; |
| 593 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | return mPreferenceScreen.findPreference(key); |
| 596 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | /** |
Scott Main | bbb3f41 | 2012-03-09 19:10:40 -0800 | [diff] [blame] | 599 | * Sets the default values from an XML preference file by reading the values defined |
| 600 | * by each {@link Preference} item's {@code android:defaultValue} attribute. This should |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | * be called by the application's main activity. |
| 602 | * <p> |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 603 | * |
Scott Main | bbb3f41 | 2012-03-09 19:10:40 -0800 | [diff] [blame] | 604 | * @param context The context of the shared preferences. |
| 605 | * @param resId The resource ID of the preference XML file. |
| 606 | * @param readAgain Whether to re-read the default values. |
| 607 | * If false, this method sets the default values only if this |
| 608 | * method has never been called in the past (or if the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | * {@link #KEY_HAS_SET_DEFAULT_VALUES} in the default value shared |
| 610 | * preferences file is false). To attempt to set the default values again |
| 611 | * bypassing this check, set {@code readAgain} to true. |
Scott Main | bbb3f41 | 2012-03-09 19:10:40 -0800 | [diff] [blame] | 612 | * <p class="note"> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | * Note: this will NOT reset preferences back to their default |
| 614 | * values. For that functionality, use |
| 615 | * {@link PreferenceManager#getDefaultSharedPreferences(Context)} |
| 616 | * and clear it followed by a call to this method with this |
| 617 | * parameter set to true. |
| 618 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 619 | public static void setDefaultValues(Context context, @XmlRes int resId, boolean readAgain) { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | // Use the default shared preferences name and mode |
| 622 | setDefaultValues(context, getDefaultSharedPreferencesName(context), |
| 623 | getDefaultSharedPreferencesMode(), resId, readAgain); |
| 624 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | /** |
| 627 | * Similar to {@link #setDefaultValues(Context, int, boolean)} but allows |
| 628 | * the client to provide the filename and mode of the shared preferences |
| 629 | * file. |
Scott Main | bbb3f41 | 2012-03-09 19:10:40 -0800 | [diff] [blame] | 630 | * |
| 631 | * @param context The context of the shared preferences. |
| 632 | * @param sharedPreferencesName A custom name for the shared preferences file. |
| 633 | * @param sharedPreferencesMode The file creation mode for the shared preferences file, such |
| 634 | * as {@link android.content.Context#MODE_PRIVATE} or {@link |
| 635 | * android.content.Context#MODE_PRIVATE} |
| 636 | * @param resId The resource ID of the preference XML file. |
| 637 | * @param readAgain Whether to re-read the default values. |
| 638 | * If false, this method will set the default values only if this |
| 639 | * method has never been called in the past (or if the |
| 640 | * {@link #KEY_HAS_SET_DEFAULT_VALUES} in the default value shared |
| 641 | * preferences file is false). To attempt to set the default values again |
| 642 | * bypassing this check, set {@code readAgain} to true. |
| 643 | * <p class="note"> |
| 644 | * Note: this will NOT reset preferences back to their default |
| 645 | * values. For that functionality, use |
| 646 | * {@link PreferenceManager#getDefaultSharedPreferences(Context)} |
| 647 | * and clear it followed by a call to this method with this |
| 648 | * parameter set to true. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 649 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | * @see #setDefaultValues(Context, int, boolean) |
| 651 | * @see #setSharedPreferencesName(String) |
| 652 | * @see #setSharedPreferencesMode(int) |
| 653 | */ |
| 654 | public static void setDefaultValues(Context context, String sharedPreferencesName, |
| 655 | int sharedPreferencesMode, int resId, boolean readAgain) { |
| 656 | final SharedPreferences defaultValueSp = context.getSharedPreferences( |
| 657 | KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE); |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 658 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | if (readAgain || !defaultValueSp.getBoolean(KEY_HAS_SET_DEFAULT_VALUES, false)) { |
| 660 | final PreferenceManager pm = new PreferenceManager(context); |
| 661 | pm.setSharedPreferencesName(sharedPreferencesName); |
| 662 | pm.setSharedPreferencesMode(sharedPreferencesMode); |
| 663 | pm.inflateFromResource(context, resId, null); |
| 664 | |
Brad Fitzpatrick | dd644c1 | 2010-10-10 10:58:47 -0700 | [diff] [blame] | 665 | SharedPreferences.Editor editor = |
| 666 | defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, true); |
| 667 | try { |
| 668 | editor.apply(); |
| 669 | } catch (AbstractMethodError unused) { |
| 670 | // The app injected its own pre-Gingerbread |
| 671 | // SharedPreferences.Editor implementation without |
| 672 | // an apply method. |
| 673 | editor.commit(); |
| 674 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | } |
| 676 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | /** |
| 679 | * Returns an editor to use when modifying the shared preferences. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 680 | * |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 681 | * <p>Do NOT commit unless {@link #shouldCommit()} returns true. |
| 682 | * |
| 683 | * @return an editor to use to write to shared preferences. If a {@link PreferenceDataStore} |
| 684 | * has been set, this method returns {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | * @see #shouldCommit() |
| 686 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 687 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | SharedPreferences.Editor getEditor() { |
Filip Pavlis | 0955793 | 2017-03-03 16:54:22 +0000 | [diff] [blame] | 689 | if (mPreferenceDataStore != null) { |
| 690 | return null; |
| 691 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | if (mNoCommit) { |
| 694 | if (mEditor == null) { |
| 695 | mEditor = getSharedPreferences().edit(); |
| 696 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | return mEditor; |
| 699 | } else { |
| 700 | return getSharedPreferences().edit(); |
| 701 | } |
| 702 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | /** |
| 705 | * Whether it is the client's responsibility to commit on the |
| 706 | * {@link #getEditor()}. This will return false in cases where the writes |
| 707 | * should be batched, for example when inflating preferences from XML. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 708 | * |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 709 | * <p>If preferences are using {@link PreferenceDataStore} this value is irrelevant. |
| 710 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | * @return Whether the client should commit. |
| 712 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 713 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | boolean shouldCommit() { |
| 715 | return !mNoCommit; |
| 716 | } |
Brad Fitzpatrick | dd644c1 | 2010-10-10 10:58:47 -0700 | [diff] [blame] | 717 | |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 718 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | private void setNoCommit(boolean noCommit) { |
| 720 | if (!noCommit && mEditor != null) { |
Brad Fitzpatrick | dd644c1 | 2010-10-10 10:58:47 -0700 | [diff] [blame] | 721 | try { |
| 722 | mEditor.apply(); |
| 723 | } catch (AbstractMethodError unused) { |
| 724 | // The app injected its own pre-Gingerbread |
| 725 | // SharedPreferences.Editor implementation without |
| 726 | // an apply method. |
| 727 | mEditor.commit(); |
| 728 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | mNoCommit = noCommit; |
| 731 | } |
Brad Fitzpatrick | dd644c1 | 2010-10-10 10:58:47 -0700 | [diff] [blame] | 732 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | /** |
| 734 | * Returns the activity that shows the preferences. This is useful for doing |
| 735 | * managed queries, but in most cases the use of {@link #getContext()} is |
| 736 | * preferred. |
Filip Pavlis | ee3bc34 | 2017-03-13 16:58:24 +0000 | [diff] [blame] | 737 | * |
| 738 | * <p>This will return {@code null} if this class was instantiated with a Context |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | * instead of Activity. For example, when setting the default values. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 740 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | * @return The activity that shows the preferences. |
| 742 | * @see #mContext |
| 743 | */ |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 744 | @Nullable |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 745 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | Activity getActivity() { |
| 747 | return mActivity; |
| 748 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | /** |
| 751 | * Returns the context. This is preferred over {@link #getActivity()} when |
| 752 | * possible. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 753 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | * @return The context. |
| 755 | */ |
| 756 | Context getContext() { |
| 757 | return mContext; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Registers a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 762 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | * @see OnActivityResultListener |
| 764 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 765 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | void registerOnActivityResultListener(OnActivityResultListener listener) { |
| 767 | synchronized (this) { |
| 768 | if (mActivityResultListeners == null) { |
| 769 | mActivityResultListeners = new ArrayList<OnActivityResultListener>(); |
| 770 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | if (!mActivityResultListeners.contains(listener)) { |
| 773 | mActivityResultListeners.add(listener); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Unregisters a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 780 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | * @see OnActivityResultListener |
| 782 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 783 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | void unregisterOnActivityResultListener(OnActivityResultListener listener) { |
| 785 | synchronized (this) { |
| 786 | if (mActivityResultListeners != null) { |
| 787 | mActivityResultListeners.remove(listener); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Called by the {@link PreferenceManager} to dispatch a subactivity result. |
| 794 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 795 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | void dispatchActivityResult(int requestCode, int resultCode, Intent data) { |
| 797 | List<OnActivityResultListener> list; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | synchronized (this) { |
| 800 | if (mActivityResultListeners == null) return; |
| 801 | list = new ArrayList<OnActivityResultListener>(mActivityResultListeners); |
| 802 | } |
| 803 | |
| 804 | final int N = list.size(); |
| 805 | for (int i = 0; i < N; i++) { |
| 806 | if (list.get(i).onActivityResult(requestCode, resultCode, data)) { |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Registers a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 814 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | * @see OnActivityStopListener |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 816 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 818 | @UnsupportedAppUsage |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 819 | public void registerOnActivityStopListener(OnActivityStopListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | synchronized (this) { |
| 821 | if (mActivityStopListeners == null) { |
| 822 | mActivityStopListeners = new ArrayList<OnActivityStopListener>(); |
| 823 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | if (!mActivityStopListeners.contains(listener)) { |
| 826 | mActivityStopListeners.add(listener); |
| 827 | } |
| 828 | } |
| 829 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | /** |
| 832 | * Unregisters a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 833 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | * @see OnActivityStopListener |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 835 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 837 | @UnsupportedAppUsage |
John Spurlock | 74a2e06 | 2014-05-16 21:03:29 -0400 | [diff] [blame] | 838 | public void unregisterOnActivityStopListener(OnActivityStopListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | synchronized (this) { |
| 840 | if (mActivityStopListeners != null) { |
| 841 | mActivityStopListeners.remove(listener); |
| 842 | } |
| 843 | } |
| 844 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 845 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | /** |
| 847 | * Called by the {@link PreferenceManager} to dispatch the activity stop |
| 848 | * event. |
| 849 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 850 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | void dispatchActivityStop() { |
| 852 | List<OnActivityStopListener> list; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | synchronized (this) { |
| 855 | if (mActivityStopListeners == null) return; |
| 856 | list = new ArrayList<OnActivityStopListener>(mActivityStopListeners); |
| 857 | } |
| 858 | |
| 859 | final int N = list.size(); |
| 860 | for (int i = 0; i < N; i++) { |
| 861 | list.get(i).onActivityStop(); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Registers a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 867 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | * @see OnActivityDestroyListener |
| 869 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 870 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | void registerOnActivityDestroyListener(OnActivityDestroyListener listener) { |
| 872 | synchronized (this) { |
| 873 | if (mActivityDestroyListeners == null) { |
| 874 | mActivityDestroyListeners = new ArrayList<OnActivityDestroyListener>(); |
| 875 | } |
| 876 | |
| 877 | if (!mActivityDestroyListeners.contains(listener)) { |
| 878 | mActivityDestroyListeners.add(listener); |
| 879 | } |
| 880 | } |
| 881 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | /** |
| 884 | * Unregisters a listener. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 885 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | * @see OnActivityDestroyListener |
| 887 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 888 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | void unregisterOnActivityDestroyListener(OnActivityDestroyListener listener) { |
| 890 | synchronized (this) { |
| 891 | if (mActivityDestroyListeners != null) { |
| 892 | mActivityDestroyListeners.remove(listener); |
| 893 | } |
| 894 | } |
| 895 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | /** |
| 898 | * Called by the {@link PreferenceManager} to dispatch the activity destroy |
| 899 | * event. |
| 900 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 901 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | void dispatchActivityDestroy() { |
| 903 | List<OnActivityDestroyListener> list = null; |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | synchronized (this) { |
| 906 | if (mActivityDestroyListeners != null) { |
| 907 | list = new ArrayList<OnActivityDestroyListener>(mActivityDestroyListeners); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | if (list != null) { |
| 912 | final int N = list.size(); |
| 913 | for (int i = 0; i < N; i++) { |
| 914 | list.get(i).onActivityDestroy(); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | // Dismiss any PreferenceScreens still showing |
| 919 | dismissAllScreens(); |
| 920 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | /** |
| 923 | * Returns a request code that is unique for the activity. Each subsequent |
| 924 | * call to this method should return another unique request code. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 925 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | * @return A unique request code that will never be used by anyone other |
| 927 | * than the caller of this method. |
| 928 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame^] | 929 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | int getNextRequestCode() { |
| 931 | synchronized (this) { |
| 932 | return mNextRequestCode++; |
| 933 | } |
| 934 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | void addPreferencesScreen(DialogInterface screen) { |
| 937 | synchronized (this) { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | if (mPreferencesScreens == null) { |
| 940 | mPreferencesScreens = new ArrayList<DialogInterface>(); |
| 941 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 942 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | mPreferencesScreens.add(screen); |
| 944 | } |
| 945 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 946 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | void removePreferencesScreen(DialogInterface screen) { |
| 948 | synchronized (this) { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 949 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | if (mPreferencesScreens == null) { |
| 951 | return; |
| 952 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | mPreferencesScreens.remove(screen); |
| 955 | } |
| 956 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 957 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | /** |
| 959 | * Called by {@link PreferenceActivity} to dispatch the new Intent event. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 960 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | * @param intent The new Intent. |
| 962 | */ |
| 963 | void dispatchNewIntent(Intent intent) { |
| 964 | dismissAllScreens(); |
| 965 | } |
| 966 | |
| 967 | private void dismissAllScreens() { |
| 968 | // Remove any of the previously shown preferences screens |
| 969 | ArrayList<DialogInterface> screensToDismiss; |
| 970 | |
| 971 | synchronized (this) { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | if (mPreferencesScreens == null) { |
| 974 | return; |
| 975 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 976 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | screensToDismiss = new ArrayList<DialogInterface>(mPreferencesScreens); |
| 978 | mPreferencesScreens.clear(); |
| 979 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 980 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | for (int i = screensToDismiss.size() - 1; i >= 0; i--) { |
| 982 | screensToDismiss.get(i).dismiss(); |
| 983 | } |
| 984 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 985 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | /** |
| 987 | * Sets the callback to be invoked when a {@link Preference} in the |
| 988 | * hierarchy rooted at this {@link PreferenceManager} is clicked. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 989 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | * @param listener The callback to be invoked. |
| 991 | */ |
| 992 | void setOnPreferenceTreeClickListener(OnPreferenceTreeClickListener listener) { |
| 993 | mOnPreferenceTreeClickListener = listener; |
| 994 | } |
| 995 | |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 996 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | OnPreferenceTreeClickListener getOnPreferenceTreeClickListener() { |
| 998 | return mOnPreferenceTreeClickListener; |
| 999 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1000 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | /** |
| 1002 | * Interface definition for a callback to be invoked when a |
| 1003 | * {@link Preference} in the hierarchy rooted at this {@link PreferenceScreen} is |
| 1004 | * clicked. |
Fabrice Di Meglio | ad2fcfe | 2014-01-20 11:10:52 -0800 | [diff] [blame] | 1005 | * |
| 1006 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | */ |
Fabrice Di Meglio | ad2fcfe | 2014-01-20 11:10:52 -0800 | [diff] [blame] | 1008 | public interface OnPreferenceTreeClickListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | /** |
| 1010 | * Called when a preference in the tree rooted at this |
| 1011 | * {@link PreferenceScreen} has been clicked. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1012 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | * @param preferenceScreen The {@link PreferenceScreen} that the |
| 1014 | * preference is located in. |
| 1015 | * @param preference The preference that was clicked. |
| 1016 | * @return Whether the click was handled. |
| 1017 | */ |
| 1018 | boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference); |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * Interface definition for a class that will be called when the container's activity |
| 1023 | * receives an activity result. |
| 1024 | */ |
| 1025 | public interface OnActivityResultListener { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | /** |
| 1028 | * See Activity's onActivityResult. |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1029 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | * @return Whether the request code was handled (in which case |
| 1031 | * subsequent listeners will not be called. |
| 1032 | */ |
| 1033 | boolean onActivityResult(int requestCode, int resultCode, Intent data); |
| 1034 | } |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1035 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | /** |
| 1037 | * Interface definition for a class that will be called when the container's activity |
| 1038 | * is stopped. |
| 1039 | */ |
| 1040 | public interface OnActivityStopListener { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | /** |
| 1043 | * See Activity's onStop. |
| 1044 | */ |
| 1045 | void onActivityStop(); |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Interface definition for a class that will be called when the container's activity |
| 1050 | * is destroyed. |
| 1051 | */ |
| 1052 | public interface OnActivityDestroyListener { |
Filip Pavlis | 0b0c6cb | 2016-11-16 15:58:28 +0000 | [diff] [blame] | 1053 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | /** |
| 1055 | * See Activity's onDestroy. |
| 1056 | */ |
| 1057 | void onActivityDestroy(); |
| 1058 | } |
| 1059 | |
| 1060 | } |