blob: 3c1ba9d02d9395c2ca6588db1779f3bb6e81fd2b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.preference;
18
Tor Norbyec615c6f2015-03-02 10:11:44 -080019import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
21import android.annotation.LayoutRes;
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +000022import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.StringRes;
Mathew Inwoodf2217132018-08-17 13:41:55 +010024import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
27import android.content.SharedPreferences;
28import android.content.res.TypedArray;
Amith Yamasanib65897b2010-11-17 13:49:27 -080029import android.graphics.drawable.Drawable;
Mathew Inwood8c854f82018-09-14 12:35:36 +010030import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Bundle;
32import android.os.Parcel;
33import android.os.Parcelable;
34import android.text.TextUtils;
35import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.view.AbsSavedState;
John Reck014fea22011-06-15 16:46:36 -070037import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.view.LayoutInflater;
39import android.view.View;
40import android.view.ViewGroup;
Amith Yamasanib65897b2010-11-17 13:49:27 -080041import android.widget.ImageView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.widget.ListView;
43import android.widget.TextView;
44
Fan Zhangc1caca12017-04-24 10:05:12 -070045import com.android.internal.util.CharSequences;
46
Adam Powell212db7d2010-04-08 16:24:46 -070047import java.util.ArrayList;
48import java.util.List;
49import java.util.Set;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * Represents the basic Preference UI building
53 * block displayed by a {@link PreferenceActivity} in the form of a
54 * {@link ListView}. This class provides the {@link View} to be displayed in
55 * the activity and associates with a {@link SharedPreferences} to
56 * store/retrieve the preference data.
57 * <p>
58 * When specifying a preference hierarchy in XML, each element can point to a
59 * subclass of {@link Preference}, similar to the view hierarchy and layouts.
60 * <p>
61 * This class contains a {@code key} that will be used as the key into the
62 * {@link SharedPreferences}. It is up to the subclass to decide how to store
63 * the value.
Tony Mantler9a6b11b2016-09-13 15:53:35 -070064 *
Scott Maincdd0c592012-07-26 17:03:51 -070065 * <div class="special reference">
66 * <h3>Developer Guides</h3>
67 * <p>For information about building a settings UI with Preferences,
68 * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
69 * guide.</p>
70 * </div>
Tony Mantler9a6b11b2016-09-13 15:53:35 -070071 *
Amith Yamasanib65897b2010-11-17 13:49:27 -080072 * @attr ref android.R.styleable#Preference_icon
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * @attr ref android.R.styleable#Preference_key
74 * @attr ref android.R.styleable#Preference_title
75 * @attr ref android.R.styleable#Preference_summary
76 * @attr ref android.R.styleable#Preference_order
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -070077 * @attr ref android.R.styleable#Preference_fragment
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * @attr ref android.R.styleable#Preference_layout
79 * @attr ref android.R.styleable#Preference_widgetLayout
80 * @attr ref android.R.styleable#Preference_enabled
81 * @attr ref android.R.styleable#Preference_selectable
82 * @attr ref android.R.styleable#Preference_dependency
83 * @attr ref android.R.styleable#Preference_persistent
84 * @attr ref android.R.styleable#Preference_defaultValue
85 * @attr ref android.R.styleable#Preference_shouldDisableView
Filip Pavlis4186b342017-03-07 14:58:41 +000086 * @attr ref android.R.styleable#Preference_recycleEnabled
Doris Ling2cb4bab2017-03-20 13:33:45 -070087 * @attr ref android.R.styleable#Preference_singleLineTitle
Doris Ling9ed53e32017-03-27 13:32:04 -070088 * @attr ref android.R.styleable#Preference_iconSpaceReserved
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000089 *
90 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
91 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
92 * Preference Library</a> for consistent behavior across all devices. For more information on
93 * using the AndroidX Preference Library see
94 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000096@Deprecated
Alan Viverette02f56802013-08-19 16:32:56 -070097public class Preference implements Comparable<Preference> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
99 * Specify for {@link #setOrder(int)} if a specific order is not required.
100 */
101 public static final int DEFAULT_ORDER = Integer.MAX_VALUE;
102
103 private Context mContext;
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000104
105 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private PreferenceManager mPreferenceManager;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000109 * The data store that should be used by this Preference to store / retrieve data. If null then
110 * {@link PreferenceManager#getPreferenceDataStore()} needs to be checked. If that one is null
111 * too it means that we are using {@link android.content.SharedPreferences} to store the data.
112 */
113 @Nullable
114 private PreferenceDataStore mPreferenceDataStore;
115
116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * Set when added to hierarchy since we need a unique ID within that
118 * hierarchy.
119 */
120 private long mId;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private OnPreferenceChangeListener mOnChangeListener;
123 private OnPreferenceClickListener mOnClickListener;
124
125 private int mOrder = DEFAULT_ORDER;
126 private CharSequence mTitle;
Dianne Hackborne72f2372011-03-16 10:43:18 -0700127 private int mTitleRes;
Mathew Inwoodf2217132018-08-17 13:41:55 +0100128 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 private CharSequence mSummary;
Amith Yamasanib65897b2010-11-17 13:49:27 -0800130 /**
131 * mIconResId is overridden by mIcon, if mIcon is specified.
132 */
133 private int mIconResId;
134 private Drawable mIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private String mKey;
136 private Intent mIntent;
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700137 private String mFragment;
Dianne Hackborndef15372010-08-15 12:43:52 -0700138 private Bundle mExtras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 private boolean mEnabled = true;
140 private boolean mSelectable = true;
141 private boolean mRequiresKey;
142 private boolean mPersistent = true;
143 private String mDependencyKey;
144 private Object mDefaultValue;
Michael Chanda53eca2009-03-27 17:46:36 -0700145 private boolean mDependencyMet = true;
Alan Viverette02f56802013-08-19 16:32:56 -0700146 private boolean mParentDependencyMet = true;
Filip Pavlis4186b342017-03-07 14:58:41 +0000147 private boolean mRecycleEnabled = true;
Doris Lingce580462017-04-14 14:04:32 -0700148 private boolean mHasSingleLineTitleAttr;
Doris Ling2cb4bab2017-03-20 13:33:45 -0700149 private boolean mSingleLineTitle = true;
Doris Ling9ed53e32017-03-27 13:32:04 -0700150 private boolean mIconSpaceReserved;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
153 * @see #setShouldDisableView(boolean)
154 */
155 private boolean mShouldDisableView = true;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700156
Mathew Inwoodf2217132018-08-17 13:41:55 +0100157 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 private int mLayoutResId = com.android.internal.R.layout.preference;
Mathew Inwoodf2217132018-08-17 13:41:55 +0100159 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private int mWidgetLayoutResId;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private OnPreferenceChangeInternalListener mListener;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private List<Preference> mDependents;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700165
Filip Pavliscba64582017-01-10 19:17:09 +0000166 private PreferenceGroup mParentGroup;
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private boolean mBaseMethodCalled;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
171 * Interface definition for a callback to be invoked when the value of this
172 * {@link Preference} has been changed by the user and is
173 * about to be set and/or persisted. This gives the client a chance
174 * to prevent setting and/or persisting the value.
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +0000175 *
176 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
177 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
178 * Preference Library</a> for consistent behavior across all devices.
179 * For more information on using the AndroidX Preference Library see
180 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +0000182 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public interface OnPreferenceChangeListener {
184 /**
185 * Called when a Preference has been changed by the user. This is
186 * called before the state of the Preference is about to be updated and
187 * before the state is persisted.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * @param preference The changed Preference.
190 * @param newValue The new value of the Preference.
191 * @return True to update the state of the Preference with the new value.
192 */
193 boolean onPreferenceChange(Preference preference, Object newValue);
194 }
195
196 /**
197 * Interface definition for a callback to be invoked when a {@link Preference} is
198 * clicked.
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +0000199 *
200 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
201 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
202 * Preference Library</a> for consistent behavior across all devices.
203 * For more information on using the AndroidX Preference Library see
204 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +0000206 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 public interface OnPreferenceClickListener {
208 /**
209 * Called when a Preference has been clicked.
210 *
211 * @param preference The Preference that was clicked.
212 * @return True if the click was handled.
213 */
214 boolean onPreferenceClick(Preference preference);
215 }
216
217 /**
218 * Interface definition for a callback to be invoked when this
219 * {@link Preference} is changed or, if this is a group, there is an
220 * addition/removal of {@link Preference}(s). This is used internally.
221 */
222 interface OnPreferenceChangeInternalListener {
223 /**
224 * Called when this Preference has changed.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * @param preference This preference.
227 */
228 void onPreferenceChange(Preference preference);
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * Called when this group has added/removed {@link Preference}(s).
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700232 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 * @param preference This Preference.
234 */
235 void onPreferenceHierarchyChange(Preference preference);
236 }
237
238 /**
239 * Perform inflation from XML and apply a class-specific base style. This
Alan Viverette617feb92013-09-09 18:09:13 -0700240 * constructor of Preference allows subclasses to use their own base style
241 * when they are inflating. For example, a {@link CheckBoxPreference}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * constructor calls this version of the super class constructor and
Alan Viverette617feb92013-09-09 18:09:13 -0700243 * supplies {@code android.R.attr.checkBoxPreferenceStyle} for
244 * <var>defStyleAttr</var>. This allows the theme's checkbox preference
245 * style to modify all of the base preference attributes as well as the
246 * {@link CheckBoxPreference} class's attributes.
247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * @param context The Context this is associated with, through which it can
Alan Viverette617feb92013-09-09 18:09:13 -0700249 * access the current theme, resources,
250 * {@link SharedPreferences}, etc.
251 * @param attrs The attributes of the XML tag that is inflating the
252 * preference.
253 * @param defStyleAttr An attribute in the current theme that contains a
254 * reference to a style resource that supplies default values for
255 * the view. Can be 0 to not look for defaults.
256 * @param defStyleRes A resource identifier of a style resource that
257 * supplies default values for the view, used only if
258 * defStyleAttr is 0 or can not be found in the theme. Can be 0
259 * to not look for defaults.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * @see #Preference(Context, AttributeSet)
261 */
Alan Viverette617feb92013-09-09 18:09:13 -0700262 public Preference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 mContext = context;
264
Alan Viverette617feb92013-09-09 18:09:13 -0700265 final TypedArray a = context.obtainStyledAttributes(
266 attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
Deepanshu Gupta10bb1372014-10-05 14:41:17 -0700267 for (int i = a.getIndexCount() - 1; i >= 0; i--) {
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700268 int attr = a.getIndex(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 switch (attr) {
Amith Yamasanib65897b2010-11-17 13:49:27 -0800270 case com.android.internal.R.styleable.Preference_icon:
271 mIconResId = a.getResourceId(attr, 0);
272 break;
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 case com.android.internal.R.styleable.Preference_key:
275 mKey = a.getString(attr);
276 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 case com.android.internal.R.styleable.Preference_title:
Dianne Hackborne72f2372011-03-16 10:43:18 -0700279 mTitleRes = a.getResourceId(attr, 0);
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700280 mTitle = a.getText(attr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 case com.android.internal.R.styleable.Preference_summary:
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700284 mSummary = a.getText(attr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 case com.android.internal.R.styleable.Preference_order:
288 mOrder = a.getInt(attr, mOrder);
289 break;
290
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700291 case com.android.internal.R.styleable.Preference_fragment:
292 mFragment = a.getString(attr);
293 break;
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 case com.android.internal.R.styleable.Preference_layout:
296 mLayoutResId = a.getResourceId(attr, mLayoutResId);
297 break;
298
299 case com.android.internal.R.styleable.Preference_widgetLayout:
300 mWidgetLayoutResId = a.getResourceId(attr, mWidgetLayoutResId);
301 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 case com.android.internal.R.styleable.Preference_enabled:
304 mEnabled = a.getBoolean(attr, true);
305 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 case com.android.internal.R.styleable.Preference_selectable:
308 mSelectable = a.getBoolean(attr, true);
309 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 case com.android.internal.R.styleable.Preference_persistent:
312 mPersistent = a.getBoolean(attr, mPersistent);
313 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 case com.android.internal.R.styleable.Preference_dependency:
316 mDependencyKey = a.getString(attr);
317 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 case com.android.internal.R.styleable.Preference_defaultValue:
320 mDefaultValue = onGetDefaultValue(a, attr);
321 break;
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 case com.android.internal.R.styleable.Preference_shouldDisableView:
324 mShouldDisableView = a.getBoolean(attr, mShouldDisableView);
325 break;
Filip Pavlis4186b342017-03-07 14:58:41 +0000326
327 case com.android.internal.R.styleable.Preference_recycleEnabled:
328 mRecycleEnabled = a.getBoolean(attr, mRecycleEnabled);
329 break;
Doris Ling2cb4bab2017-03-20 13:33:45 -0700330
331 case com.android.internal.R.styleable.Preference_singleLineTitle:
332 mSingleLineTitle = a.getBoolean(attr, mSingleLineTitle);
Doris Lingce580462017-04-14 14:04:32 -0700333 mHasSingleLineTitleAttr = true;
Doris Ling2cb4bab2017-03-20 13:33:45 -0700334 break;
Doris Ling9ed53e32017-03-27 13:32:04 -0700335
336 case com.android.internal.R.styleable.Preference_iconSpaceReserved:
337 mIconSpaceReserved = a.getBoolean(attr, mIconSpaceReserved);
338 break;
339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341 a.recycle();
342 }
Alan Viverette617feb92013-09-09 18:09:13 -0700343
344 /**
345 * Perform inflation from XML and apply a class-specific base style. This
346 * constructor of Preference allows subclasses to use their own base style
347 * when they are inflating. For example, a {@link CheckBoxPreference}
348 * constructor calls this version of the super class constructor and
349 * supplies {@code android.R.attr.checkBoxPreferenceStyle} for
350 * <var>defStyleAttr</var>. This allows the theme's checkbox preference
351 * style to modify all of the base preference attributes as well as the
352 * {@link CheckBoxPreference} class's attributes.
353 *
354 * @param context The Context this is associated with, through which it can
355 * access the current theme, resources,
356 * {@link SharedPreferences}, etc.
357 * @param attrs The attributes of the XML tag that is inflating the
358 * preference.
359 * @param defStyleAttr An attribute in the current theme that contains a
360 * reference to a style resource that supplies default values for
361 * the view. Can be 0 to not look for defaults.
362 * @see #Preference(Context, AttributeSet)
363 */
364 public Preference(Context context, AttributeSet attrs, int defStyleAttr) {
365 this(context, attrs, defStyleAttr, 0);
366 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Constructor that is called when inflating a Preference from XML. This is
370 * called when a Preference is being constructed from an XML file, supplying
371 * attributes that were specified in the XML file. This version uses a
372 * default style of 0, so the only attribute values applied are those in the
373 * Context's Theme and the given AttributeSet.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 * @param context The Context this is associated with, through which it can
376 * access the current theme, resources, {@link SharedPreferences},
377 * etc.
378 * @param attrs The attributes of the XML tag that is inflating the
379 * preference.
380 * @see #Preference(Context, AttributeSet, int)
381 */
382 public Preference(Context context, AttributeSet attrs) {
Amith Yamasanie4946622011-01-16 16:12:51 -0800383 this(context, attrs, com.android.internal.R.attr.preferenceStyle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 }
385
386 /**
387 * Constructor to create a Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700388 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * @param context The Context in which to store Preference values.
390 */
391 public Preference(Context context) {
392 this(context, null);
393 }
394
395 /**
396 * Called when a Preference is being inflated and the default value
397 * attribute needs to be read. Since different Preference types have
398 * different value types, the subclass should get and return the default
399 * value which will be its value type.
400 * <p>
401 * For example, if the value type is String, the body of the method would
402 * proxy to {@link TypedArray#getString(int)}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700403 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * @param a The set of attributes.
405 * @param index The index of the default value attribute.
406 * @return The default value of this preference type.
407 */
408 protected Object onGetDefaultValue(TypedArray a, int index) {
409 return null;
410 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * Sets an {@link Intent} to be used for
414 * {@link Context#startActivity(Intent)} when this Preference is clicked.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 * @param intent The intent associated with this Preference.
417 */
418 public void setIntent(Intent intent) {
419 mIntent = intent;
420 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 /**
423 * Return the {@link Intent} associated with this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * @return The {@link Intent} last set via {@link #setIntent(Intent)} or XML.
426 */
427 public Intent getIntent() {
428 return mIntent;
429 }
430
431 /**
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700432 * Sets the class name of a fragment to be shown when this Preference is clicked.
433 *
434 * @param fragment The class name of the fragment associated with this Preference.
435 */
436 public void setFragment(String fragment) {
437 mFragment = fragment;
438 }
439
440 /**
441 * Return the fragment class name associated with this Preference.
442 *
443 * @return The fragment class name last set via {@link #setFragment} or XML.
444 */
445 public String getFragment() {
446 return mFragment;
447 }
448
449 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000450 * Sets a {@link PreferenceDataStore} to be used by this Preference instead of using
451 * {@link android.content.SharedPreferences}.
Filip Pavlisfd596452017-03-01 18:50:00 +0000452 *
453 * <p>The data store will remain assigned even if the Preference is moved around the preference
454 * hierarchy. It will also override a data store propagated from the {@link PreferenceManager}
455 * that owns this Preference.
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000456 *
457 * @param dataStore The {@link PreferenceDataStore} to be used by this Preference.
Filip Pavlisfd596452017-03-01 18:50:00 +0000458 * @see PreferenceManager#setPreferenceDataStore(PreferenceDataStore)
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000459 */
460 public void setPreferenceDataStore(PreferenceDataStore dataStore) {
461 mPreferenceDataStore = dataStore;
462 }
463
464 /**
465 * Returns {@link PreferenceDataStore} used by this Preference. Returns {@code null} if
466 * {@link android.content.SharedPreferences} is used instead.
467 *
Filip Pavlisfd596452017-03-01 18:50:00 +0000468 * <p>By default preferences always use {@link android.content.SharedPreferences}. To make this
469 * preference to use the {@link PreferenceDataStore} you need to assign your implementation
470 * to the Preference itself via {@link #setPreferenceDataStore(PreferenceDataStore)} or to its
471 * {@link PreferenceManager} via
472 * {@link PreferenceManager#setPreferenceDataStore(PreferenceDataStore)}.
473 *
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +0000474 * @return The {@link PreferenceDataStore} used by this Preference or {@code null} if none.
475 */
476 @Nullable
477 public PreferenceDataStore getPreferenceDataStore() {
478 if (mPreferenceDataStore != null) {
479 return mPreferenceDataStore;
480 } else if (mPreferenceManager != null) {
481 return mPreferenceManager.getPreferenceDataStore();
482 }
483
484 return null;
485 }
486
487 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700488 * Return the extras Bundle object associated with this preference, creating
489 * a new Bundle if there currently isn't one. You can use this to get and
490 * set individual extra key/value pairs.
491 */
492 public Bundle getExtras() {
493 if (mExtras == null) {
494 mExtras = new Bundle();
495 }
496 return mExtras;
497 }
498
499 /**
Filip Pavlisee3bc342017-03-13 16:58:24 +0000500 * Return the extras Bundle object associated with this preference, returning {@code null} if
501 * there is not currently one.
Dianne Hackborndef15372010-08-15 12:43:52 -0700502 */
503 public Bundle peekExtras() {
504 return mExtras;
505 }
506
507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 * Sets the layout resource that is inflated as the {@link View} to be shown
509 * for this Preference. In most cases, the default layout is sufficient for
510 * custom Preference objects and only the widget layout needs to be changed.
511 * <p>
512 * This layout should contain a {@link ViewGroup} with ID
513 * {@link android.R.id#widget_frame} to be the parent of the specific widget
514 * for this Preference. It should similarly contain
515 * {@link android.R.id#title} and {@link android.R.id#summary}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 * @param layoutResId The layout resource ID to be inflated and returned as
518 * a {@link View}.
519 * @see #setWidgetLayoutResource(int)
520 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700521 public void setLayoutResource(@LayoutRes int layoutResId) {
Amith Yamasania98129b2009-10-05 15:31:47 -0700522 if (layoutResId != mLayoutResId) {
523 // Layout changed
Filip Pavlis4186b342017-03-07 14:58:41 +0000524 mRecycleEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
Amith Yamasania98129b2009-10-05 15:31:47 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mLayoutResId = layoutResId;
528 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 /**
531 * Gets the layout resource that will be shown as the {@link View} for this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * @return The layout resource ID.
534 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700535 @LayoutRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public int getLayoutResource() {
537 return mLayoutResId;
538 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 /**
Alan Viverette791b37e2013-08-20 17:34:19 -0700541 * Sets the layout for the controllable widget portion of this Preference. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 * is inflated into the main layout. For example, a {@link CheckBoxPreference}
543 * would specify a custom layout (consisting of just the CheckBox) here,
544 * instead of creating its own main layout.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * @param widgetLayoutResId The layout resource ID to be inflated into the
547 * main layout.
548 * @see #setLayoutResource(int)
549 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700550 public void setWidgetLayoutResource(@LayoutRes int widgetLayoutResId) {
Amith Yamasania98129b2009-10-05 15:31:47 -0700551 if (widgetLayoutResId != mWidgetLayoutResId) {
552 // Layout changed
Filip Pavlis4186b342017-03-07 14:58:41 +0000553 mRecycleEnabled = false;
Amith Yamasania98129b2009-10-05 15:31:47 -0700554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 mWidgetLayoutResId = widgetLayoutResId;
556 }
557
558 /**
559 * Gets the layout resource for the controllable widget portion of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * @return The layout resource ID.
562 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700563 @LayoutRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 public int getWidgetLayoutResource() {
565 return mWidgetLayoutResId;
566 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 /**
569 * Gets the View that will be shown in the {@link PreferenceActivity}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700570 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 * @param convertView The old View to reuse, if possible. Note: You should
572 * check that this View is non-null and of an appropriate type
573 * before using. If it is not possible to convert this View to
574 * display the correct data, this method can create a new View.
575 * @param parent The parent that this View will eventually be attached to.
576 * @return Returns the same Preference object, for chaining multiple calls
577 * into a single statement.
578 * @see #onCreateView(ViewGroup)
579 * @see #onBindView(View)
580 */
581 public View getView(View convertView, ViewGroup parent) {
582 if (convertView == null) {
Amith Yamasanibae6fc22009-09-30 14:08:07 -0700583 convertView = onCreateView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 onBindView(convertView);
586 return convertView;
587 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 /**
590 * Creates the View to be shown for this Preference in the
591 * {@link PreferenceActivity}. The default behavior is to inflate the main
592 * layout of this Preference (see {@link #setLayoutResource(int)}. If
593 * changing this behavior, please specify a {@link ViewGroup} with ID
594 * {@link android.R.id#widget_frame}.
595 * <p>
596 * Make sure to call through to the superclass's implementation.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700597 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 * @param parent The parent that this View will eventually be attached to.
599 * @return The View that displays this Preference.
600 * @see #onBindView(View)
601 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800602 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 protected View onCreateView(ViewGroup parent) {
604 final LayoutInflater layoutInflater =
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700605 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
606
607 final View layout = layoutInflater.inflate(mLayoutResId, parent, false);
608
Amith Yamasani405c1af2011-05-26 13:08:25 -0700609 final ViewGroup widgetFrame = (ViewGroup) layout
610 .findViewById(com.android.internal.R.id.widget_frame);
611 if (widgetFrame != null) {
612 if (mWidgetLayoutResId != 0) {
613 layoutInflater.inflate(mWidgetLayoutResId, widgetFrame);
614 } else {
615 widgetFrame.setVisibility(View.GONE);
616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 return layout;
619 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 /**
622 * Binds the created View to the data for this Preference.
623 * <p>
624 * This is a good place to grab references to custom Views in the layout and
625 * set properties on them.
626 * <p>
627 * Make sure to call through to the superclass's implementation.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700628 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * @param view The View that shows this Preference.
630 * @see #onCreateView(ViewGroup)
631 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800632 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 protected void onBindView(View view) {
Alan Viverette55565ec2014-04-09 16:52:54 -0700634 final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);
Jeff Sharkey0f1c3af2012-04-03 20:46:42 -0700635 if (titleView != null) {
636 final CharSequence title = getTitle();
637 if (!TextUtils.isEmpty(title)) {
638 titleView.setText(title);
639 titleView.setVisibility(View.VISIBLE);
Doris Lingce580462017-04-14 14:04:32 -0700640 if (mHasSingleLineTitleAttr) {
641 titleView.setSingleLine(mSingleLineTitle);
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 } else {
Jeff Sharkey0f1c3af2012-04-03 20:46:42 -0700644 titleView.setVisibility(View.GONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 }
646 }
Jeff Sharkey0f1c3af2012-04-03 20:46:42 -0700647
648 final TextView summaryView = (TextView) view.findViewById(
649 com.android.internal.R.id.summary);
650 if (summaryView != null) {
651 final CharSequence summary = getSummary();
652 if (!TextUtils.isEmpty(summary)) {
653 summaryView.setText(summary);
654 summaryView.setVisibility(View.VISIBLE);
655 } else {
656 summaryView.setVisibility(View.GONE);
657 }
658 }
659
Alan Viverette55565ec2014-04-09 16:52:54 -0700660 final ImageView imageView = (ImageView) view.findViewById(com.android.internal.R.id.icon);
Amith Yamasani405c1af2011-05-26 13:08:25 -0700661 if (imageView != null) {
662 if (mIconResId != 0 || mIcon != null) {
663 if (mIcon == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800664 mIcon = getContext().getDrawable(mIconResId);
Amith Yamasani405c1af2011-05-26 13:08:25 -0700665 }
666 if (mIcon != null) {
667 imageView.setImageDrawable(mIcon);
668 }
Amith Yamasanib65897b2010-11-17 13:49:27 -0800669 }
Doris Ling9ed53e32017-03-27 13:32:04 -0700670 if (mIcon != null) {
671 imageView.setVisibility(View.VISIBLE);
672 } else {
673 imageView.setVisibility(mIconSpaceReserved ? View.INVISIBLE : View.GONE);
674 }
Amith Yamasanib65897b2010-11-17 13:49:27 -0800675 }
Amith Yamasani405c1af2011-05-26 13:08:25 -0700676
Alan Viverette55565ec2014-04-09 16:52:54 -0700677 final View imageFrame = view.findViewById(com.android.internal.R.id.icon_frame);
678 if (imageFrame != null) {
Fan Zhangc1caca12017-04-24 10:05:12 -0700679 if (mIcon != null) {
680 imageFrame.setVisibility(View.VISIBLE);
681 } else {
682 imageFrame.setVisibility(mIconSpaceReserved ? View.INVISIBLE : View.GONE);
683 }
Alan Viverette55565ec2014-04-09 16:52:54 -0700684 }
685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (mShouldDisableView) {
687 setEnabledStateOnViews(view, isEnabled());
688 }
689 }
Amith Yamasanib65897b2010-11-17 13:49:27 -0800690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 /**
692 * Makes sure the view (and any children) get the enabled state changed.
693 */
694 private void setEnabledStateOnViews(View v, boolean enabled) {
695 v.setEnabled(enabled);
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 if (v instanceof ViewGroup) {
698 final ViewGroup vg = (ViewGroup) v;
699 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
700 setEnabledStateOnViews(vg.getChildAt(i), enabled);
701 }
702 }
703 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000706 * Sets the order of this Preference with respect to other Preference objects on the same level.
707 * If this is not specified, the default behavior is to sort alphabetically. The
708 * {@link PreferenceGroup#setOrderingAsAdded(boolean)} can be used to order Preference objects
709 * based on the order they appear in the XML.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700710 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000711 * @param order the order for this Preference. A lower value will be shown first. Use
712 * {@link #DEFAULT_ORDER} to sort alphabetically or allow ordering from XML
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * @see PreferenceGroup#setOrderingAsAdded(boolean)
714 * @see #DEFAULT_ORDER
715 */
716 public void setOrder(int order) {
717 if (order != mOrder) {
718 mOrder = order;
719
Filip Pavlis4186b342017-03-07 14:58:41 +0000720 // Reorder the list
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 notifyHierarchyChanged();
722 }
723 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000726 * Gets the order of this Preference with respect to other Preference objects on the same level.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700727 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000728 * @return the order of this Preference
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 * @see #setOrder(int)
730 */
731 public int getOrder() {
732 return mOrder;
733 }
734
735 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000736 * Sets the title for this Preference with a CharSequence. This title will be placed into the ID
737 * {@link android.R.id#title} within the View created by {@link #onCreateView(ViewGroup)}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700738 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000739 * @param title the title for this Preference
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 */
741 public void setTitle(CharSequence title) {
742 if (title == null && mTitle != null || title != null && !title.equals(mTitle)) {
Dianne Hackborne72f2372011-03-16 10:43:18 -0700743 mTitleRes = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 mTitle = title;
745 notifyChanged();
746 }
747 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000750 * Sets the title for this Preference with a resource ID.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700751 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 * @see #setTitle(CharSequence)
Filip Pavlis4186b342017-03-07 14:58:41 +0000753 * @param titleResId the title as a resource ID
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700755 public void setTitle(@StringRes int titleResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 setTitle(mContext.getString(titleResId));
Dianne Hackborne72f2372011-03-16 10:43:18 -0700757 mTitleRes = titleResId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000761 * Returns the title resource ID of this Preference. If the title did not come from a resource,
762 * {@code 0} is returned.
Dianne Hackborne72f2372011-03-16 10:43:18 -0700763 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000764 * @return the title resource
Dianne Hackborne72f2372011-03-16 10:43:18 -0700765 * @see #setTitle(int)
766 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700767 @StringRes
Dianne Hackborne72f2372011-03-16 10:43:18 -0700768 public int getTitleRes() {
769 return mTitleRes;
770 }
771
772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 * Returns the title of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700774 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000775 * @return the title
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * @see #setTitle(CharSequence)
777 */
778 public CharSequence getTitle() {
779 return mTitle;
780 }
781
782 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000783 * Sets the icon for this Preference with a Drawable. This icon will be placed into the ID
784 * {@link android.R.id#icon} within the View created by {@link #onCreateView(ViewGroup)}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700785 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000786 * @param icon the optional icon for this Preference
Amith Yamasanib65897b2010-11-17 13:49:27 -0800787 */
788 public void setIcon(Drawable icon) {
789 if ((icon == null && mIcon != null) || (icon != null && mIcon != icon)) {
790 mIcon = icon;
Amith Yamasani405c1af2011-05-26 13:08:25 -0700791
Amith Yamasanib65897b2010-11-17 13:49:27 -0800792 notifyChanged();
793 }
794 }
795
796 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000797 * Sets the icon for this Preference with a resource ID.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700798 *
Amith Yamasanib65897b2010-11-17 13:49:27 -0800799 * @see #setIcon(Drawable)
Filip Pavlis4186b342017-03-07 14:58:41 +0000800 * @param iconResId the icon as a resource ID
Amith Yamasanib65897b2010-11-17 13:49:27 -0800801 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700802 public void setIcon(@DrawableRes int iconResId) {
Svet Ganov3695b8a2015-03-24 16:30:25 -0700803 if (mIconResId != iconResId) {
804 mIconResId = iconResId;
805 setIcon(mContext.getDrawable(iconResId));
806 }
Amith Yamasanib65897b2010-11-17 13:49:27 -0800807 }
808
809 /**
810 * Returns the icon of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700811 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000812 * @return the icon
Amith Yamasanib65897b2010-11-17 13:49:27 -0800813 * @see #setIcon(Drawable)
814 */
815 public Drawable getIcon() {
Michael Kwanf049e242016-09-07 13:15:55 -0700816 if (mIcon == null && mIconResId != 0) {
817 mIcon = getContext().getDrawable(mIconResId);
818 }
Amith Yamasanib65897b2010-11-17 13:49:27 -0800819 return mIcon;
820 }
821
822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 * Returns the summary of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700824 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000825 * @return the summary
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 * @see #setSummary(CharSequence)
827 */
828 public CharSequence getSummary() {
829 return mSummary;
830 }
831
832 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000833 * Sets the summary for this Preference with a CharSequence.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700834 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000835 * @param summary the summary for the preference
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 */
837 public void setSummary(CharSequence summary) {
838 if (summary == null && mSummary != null || summary != null && !summary.equals(mSummary)) {
839 mSummary = summary;
840 notifyChanged();
841 }
842 }
843
844 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000845 * Sets the summary for this Preference with a resource ID.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700846 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 * @see #setSummary(CharSequence)
Filip Pavlis4186b342017-03-07 14:58:41 +0000848 * @param summaryResId the summary as a resource
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700850 public void setSummary(@StringRes int summaryResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 setSummary(mContext.getString(summaryResId));
852 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /**
855 * Sets whether this Preference is enabled. If disabled, it will
856 * not handle clicks.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700857 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000858 * @param enabled set {@code true} to enable it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 */
860 public void setEnabled(boolean enabled) {
861 if (mEnabled != enabled) {
862 mEnabled = enabled;
863
864 // Enabled state can change dependent preferences' states, so notify
865 notifyDependencyChange(shouldDisableDependents());
866
867 notifyChanged();
868 }
869 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 /**
872 * Checks whether this Preference should be enabled in the list.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700873 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000874 * @return {@code true} if this Preference is enabled, false otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public boolean isEnabled() {
Alan Viverette02f56802013-08-19 16:32:56 -0700877 return mEnabled && mDependencyMet && mParentDependencyMet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
880 /**
881 * Sets whether this Preference is selectable.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700882 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000883 * @param selectable set {@code true} to make it selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 */
885 public void setSelectable(boolean selectable) {
886 if (mSelectable != selectable) {
887 mSelectable = selectable;
888 notifyChanged();
889 }
890 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 /**
893 * Checks whether this Preference should be selectable in the list.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700894 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000895 * @return {@code true} if it is selectable, {@code false} otherwise
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 */
897 public boolean isSelectable() {
898 return mSelectable;
899 }
900
901 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000902 * Sets whether this Preference should disable its view when it gets disabled.
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700903 *
Filip Pavlis4186b342017-03-07 14:58:41 +0000904 * <p>For example, set this and {@link #setEnabled(boolean)} to false for preferences that are
905 * only displaying information and 1) should not be clickable 2) should not have the view set to
906 * the disabled state.
907 *
908 * @param shouldDisableView set {@code true} if this preference should disable its view when
909 * the preference is disabled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 */
911 public void setShouldDisableView(boolean shouldDisableView) {
912 mShouldDisableView = shouldDisableView;
913 notifyChanged();
914 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 /**
917 * Checks whether this Preference should disable its view when it's action is disabled.
Filip Pavlis4186b342017-03-07 14:58:41 +0000918 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 * @see #setShouldDisableView(boolean)
Filip Pavlis4186b342017-03-07 14:58:41 +0000920 * @return {@code true} if it should disable the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 */
922 public boolean getShouldDisableView() {
923 return mShouldDisableView;
924 }
925
926 /**
Filip Pavlis4186b342017-03-07 14:58:41 +0000927 * Sets whether this Preference has enabled to have its view recycled when used in the list
928 * view. By default the recycling is enabled.
929 *
930 * <p>The value can be changed only before this preference is added to the preference hierarchy.
931 *
932 * <p>If view recycling is not allowed then each time the list view populates this preference
933 * the {@link #getView(View, ViewGroup)} method receives a {@code null} convert view and needs
934 * to recreate the view. Otherwise view gets recycled and only {@link #onBindView(View)} gets
935 * called.
936 *
937 * @param enabled set {@code true} if this preference view should be recycled
938 */
939 @CallSuper
940 public void setRecycleEnabled(boolean enabled) {
941 mRecycleEnabled = enabled;
942 notifyChanged();
943 }
944
945 /**
946 * Checks whether this Preference has enabled to have its view recycled when used in the list
947 * view.
948 *
949 * @see #setRecycleEnabled(boolean)
950 * @return {@code true} if this preference view should be recycled
951 */
952 public boolean isRecycleEnabled() {
953 return mRecycleEnabled;
954 }
955
956 /**
Doris Ling2cb4bab2017-03-20 13:33:45 -0700957 * Sets whether to constrain the title of this Preference to a single line instead of
958 * letting it wrap onto multiple lines.
959 *
960 * @param singleLineTitle set {@code true} if the title should be constrained to one line
961 */
962 public void setSingleLineTitle(boolean singleLineTitle) {
Doris Ling846234f2017-06-23 13:41:22 -0700963 mHasSingleLineTitleAttr = true;
Doris Ling2cb4bab2017-03-20 13:33:45 -0700964 mSingleLineTitle = singleLineTitle;
965 notifyChanged();
966 }
967
968 /**
969 * Gets whether the title of this preference is constrained to a single line.
970 *
971 * @see #setSingleLineTitle(boolean)
972 * @return {@code true} if the title of this preference is constrained to a single line
973 */
974 public boolean isSingleLineTitle() {
975 return mSingleLineTitle;
976 }
977
978 /**
Doris Ling9ed53e32017-03-27 13:32:04 -0700979 * Sets whether to reserve the space of this Preference icon view when no icon is provided.
980 *
981 * @param iconSpaceReserved set {@code true} if the space for the icon view should be reserved
982 */
983 public void setIconSpaceReserved(boolean iconSpaceReserved) {
984 mIconSpaceReserved = iconSpaceReserved;
985 notifyChanged();
986 }
987
988 /**
989 * Gets whether the space this preference icon view is reserved.
990 *
991 * @see #setIconSpaceReserved(boolean)
992 * @return {@code true} if the space of this preference icon view is reserved
993 */
994 public boolean isIconSpaceReserved() {
995 return mIconSpaceReserved;
996 }
997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 * Returns a unique ID for this Preference. This ID should be unique across all
999 * Preference objects in a hierarchy.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001000 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 * @return A unique ID for this Preference.
1002 */
Mathew Inwoodf2217132018-08-17 13:41:55 +01001003 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 long getId() {
1005 return mId;
1006 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 /**
1009 * Processes a click on the preference. This includes saving the value to
1010 * the {@link SharedPreferences}. However, the overridden method should
1011 * call {@link #callChangeListener(Object)} to make sure the client wants to
1012 * update the preference's state with the new value.
1013 */
1014 protected void onClick() {
1015 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001018 * Sets the key for this Preference, which is used as a key to the {@link SharedPreferences} or
1019 * {@link PreferenceDataStore}. This should be unique for the package.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001020 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * @param key The key for the preference.
1022 */
1023 public void setKey(String key) {
1024 mKey = key;
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (mRequiresKey && !hasKey()) {
1027 requireKey();
1028 }
1029 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001032 * Gets the key for this Preference, which is also the key used for storing values into
1033 * {@link SharedPreferences} or {@link PreferenceDataStore}.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * @return The key.
1036 */
1037 public String getKey() {
1038 return mKey;
1039 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 /**
1042 * Checks whether the key is present, and if it isn't throws an
Filip Pavlis09557932017-03-03 16:54:22 +00001043 * exception. This should be called by subclasses that persist their preferences.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001044 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 * @throws IllegalStateException If there is no key assigned.
1046 */
1047 void requireKey() {
1048 if (mKey == null) {
1049 throw new IllegalStateException("Preference does not have a key assigned.");
1050 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 mRequiresKey = true;
1053 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Checks whether this Preference has a valid key.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001057 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 * @return True if the key exists and is not a blank string, false otherwise.
1059 */
1060 public boolean hasKey() {
1061 return !TextUtils.isEmpty(mKey);
1062 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Checks whether this Preference is persistent. If it is, it stores its value(s) into
Filip Pavlis09557932017-03-03 16:54:22 +00001066 * the persistent {@link SharedPreferences} storage by default or into
1067 * {@link PreferenceDataStore} if assigned.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001068 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * @return True if it is persistent.
1070 */
1071 public boolean isPersistent() {
1072 return mPersistent;
1073 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001076 * Checks whether, at the given time this method is called, this Preference should store/restore
1077 * its value(s) into the {@link SharedPreferences} or into {@link PreferenceDataStore} if
1078 * assigned. This, at minimum, checks whether this Preference is persistent and it currently has
1079 * a key. Before you save/restore from the storage, check this first.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * @return True if it should persist the value.
1082 */
1083 protected boolean shouldPersist() {
1084 return mPreferenceManager != null && isPersistent() && hasKey();
1085 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001088 * Sets whether this Preference is persistent. When persistent, it stores its value(s) into
1089 * the persistent {@link SharedPreferences} storage by default or into
1090 * {@link PreferenceDataStore} if assigned.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001091 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001092 * @param persistent set {@code true} if it should store its value(s) into the storage.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 */
1094 public void setPersistent(boolean persistent) {
1095 mPersistent = persistent;
1096 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Call this method after the user changes the preference, but before the
1100 * internal state is set. This allows the client to ignore the user value.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 * @param newValue The new value of this Preference.
1103 * @return True if the user value should be set as the preference
1104 * value (and persisted).
1105 */
1106 protected boolean callChangeListener(Object newValue) {
Filip Pavlis09557932017-03-03 16:54:22 +00001107 return mOnChangeListener == null || mOnChangeListener.onPreferenceChange(this, newValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Sets the callback to be invoked when this Preference is changed by the
1112 * user (but before the internal state has been updated).
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001113 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * @param onPreferenceChangeListener The callback to be invoked.
1115 */
1116 public void setOnPreferenceChangeListener(OnPreferenceChangeListener onPreferenceChangeListener) {
1117 mOnChangeListener = onPreferenceChangeListener;
1118 }
1119
1120 /**
1121 * Returns the callback to be invoked when this Preference is changed by the
1122 * user (but before the internal state has been updated).
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 * @return The callback to be invoked.
1125 */
1126 public OnPreferenceChangeListener getOnPreferenceChangeListener() {
1127 return mOnChangeListener;
1128 }
1129
1130 /**
1131 * Sets the callback to be invoked when this Preference is clicked.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 * @param onPreferenceClickListener The callback to be invoked.
1134 */
1135 public void setOnPreferenceClickListener(OnPreferenceClickListener onPreferenceClickListener) {
1136 mOnClickListener = onPreferenceClickListener;
1137 }
1138
1139 /**
1140 * Returns the callback to be invoked when this Preference is clicked.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 * @return The callback to be invoked.
1143 */
1144 public OnPreferenceClickListener getOnPreferenceClickListener() {
1145 return mOnClickListener;
1146 }
1147
1148 /**
1149 * Called when a click should be performed.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 * @param preferenceScreen A {@link PreferenceScreen} whose hierarchy click
1152 * listener should be called in the proper order (between other
Filip Pavlisee3bc342017-03-13 16:58:24 +00001153 * processing). May be {@code null}.
Justin Koh37ae5582012-10-25 15:26:36 -07001154 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 */
Mathew Inwoodf2217132018-08-17 13:41:55 +01001156 @UnsupportedAppUsage
Justin Koh37ae5582012-10-25 15:26:36 -07001157 public void performClick(PreferenceScreen preferenceScreen) {
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (!isEnabled()) {
1160 return;
1161 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 onClick();
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (mOnClickListener != null && mOnClickListener.onPreferenceClick(this)) {
1166 return;
1167 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 PreferenceManager preferenceManager = getPreferenceManager();
1170 if (preferenceManager != null) {
1171 PreferenceManager.OnPreferenceTreeClickListener listener = preferenceManager
1172 .getOnPreferenceTreeClickListener();
1173 if (preferenceScreen != null && listener != null
1174 && listener.onPreferenceTreeClick(preferenceScreen, this)) {
1175 return;
1176 }
1177 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 if (mIntent != null) {
1180 Context context = getContext();
1181 context.startActivity(mIntent);
1182 }
1183 }
John Reck014fea22011-06-15 16:46:36 -07001184
1185 /**
1186 * Allows a Preference to intercept key events without having focus.
1187 * For example, SeekBarPreference uses this to intercept +/- to adjust
1188 * the progress.
1189 * @return True if the Preference handled the key. Returns false by default.
1190 * @hide
1191 */
Mathew Inwood8c854f82018-09-14 12:35:36 +01001192 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
John Reck014fea22011-06-15 16:46:36 -07001193 public boolean onKey(View v, int keyCode, KeyEvent event) {
1194 return false;
1195 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001198 * Returns the {@link android.content.Context} of this Preference.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 * Each Preference in a Preference hierarchy can be
1200 * from different Context (for example, if multiple activities provide preferences into a single
1201 * {@link PreferenceActivity}). This Context will be used to save the Preference values.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001202 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 * @return The Context of this Preference.
1204 */
1205 public Context getContext() {
1206 return mContext;
1207 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Returns the {@link SharedPreferences} where this Preference can read its
1211 * value(s). Usually, it's easier to use one of the helper read methods:
1212 * {@link #getPersistedBoolean(boolean)}, {@link #getPersistedFloat(float)},
1213 * {@link #getPersistedInt(int)}, {@link #getPersistedLong(long)},
1214 * {@link #getPersistedString(String)}. To save values, see
1215 * {@link #getEditor()}.
1216 * <p>
1217 * In some cases, writes to the {@link #getEditor()} will not be committed
1218 * right away and hence not show up in the returned
1219 * {@link SharedPreferences}, this is intended behavior to improve
1220 * performance.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001221 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001222 * @return the {@link SharedPreferences} where this Preference reads its value(s). If
1223 * this preference isn't attached to a Preference hierarchy or if
1224 * a {@link PreferenceDataStore} has been set, this method returns {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 * @see #getEditor()
Filip Pavlis09557932017-03-03 16:54:22 +00001226 * @see #setPreferenceDataStore(PreferenceDataStore)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 */
1228 public SharedPreferences getSharedPreferences() {
Filip Pavlis09557932017-03-03 16:54:22 +00001229 if (mPreferenceManager == null || getPreferenceDataStore() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 return null;
1231 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 return mPreferenceManager.getSharedPreferences();
1234 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Returns an {@link SharedPreferences.Editor} where this Preference can
1238 * save its value(s). Usually it's easier to use one of the helper save
1239 * methods: {@link #persistBoolean(boolean)}, {@link #persistFloat(float)},
1240 * {@link #persistInt(int)}, {@link #persistLong(long)},
1241 * {@link #persistString(String)}. To read values, see
1242 * {@link #getSharedPreferences()}. If {@link #shouldCommit()} returns
1243 * true, it is this Preference's responsibility to commit.
1244 * <p>
1245 * In some cases, writes to this will not be committed right away and hence
1246 * not show up in the SharedPreferences, this is intended behavior to
1247 * improve performance.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001248 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001249 * @return a {@link SharedPreferences.Editor} where this preference saves its value(s). If
1250 * this preference isn't attached to a Preference hierarchy or if
1251 * a {@link PreferenceDataStore} has been set, this method returns {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 * @see #shouldCommit()
1253 * @see #getSharedPreferences()
Filip Pavlis09557932017-03-03 16:54:22 +00001254 * @see #setPreferenceDataStore(PreferenceDataStore)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 */
1256 public SharedPreferences.Editor getEditor() {
Filip Pavlis09557932017-03-03 16:54:22 +00001257 if (mPreferenceManager == null || getPreferenceDataStore() != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 return null;
1259 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 return mPreferenceManager.getEditor();
1262 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Returns whether the {@link Preference} should commit its saved value(s) in
1266 * {@link #getEditor()}. This may return false in situations where batch
1267 * committing is being done (by the manager) to improve performance.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001268 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001269 * <p>If this preference is using {@link PreferenceDataStore} this value is irrelevant.
Filip Pavlis09557932017-03-03 16:54:22 +00001270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 * @return Whether the Preference should commit its saved value(s).
1272 * @see #getEditor()
1273 */
1274 public boolean shouldCommit() {
1275 if (mPreferenceManager == null) {
1276 return false;
1277 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 return mPreferenceManager.shouldCommit();
1280 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Compares Preference objects based on order (if set), otherwise alphabetically on the titles.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 * @param another The Preference to compare to this one.
1286 * @return 0 if the same; less than 0 if this Preference sorts ahead of <var>another</var>;
1287 * greater than 0 if this Preference sorts after <var>another</var>.
1288 */
Jeff Brown75af1712013-11-10 18:54:03 -08001289 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 public int compareTo(Preference another) {
Jeff Brown75af1712013-11-10 18:54:03 -08001291 if (mOrder != another.mOrder) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 // Do order comparison
Jeff Brown75af1712013-11-10 18:54:03 -08001293 return mOrder - another.mOrder;
Carsten Haugeb2e61e42013-02-04 14:45:20 +01001294 } else if (mTitle == another.mTitle) {
1295 // If titles are null or share same object comparison
1296 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 } else if (mTitle == null) {
1298 return 1;
1299 } else if (another.mTitle == null) {
1300 return -1;
1301 } else {
1302 // Do name comparison
1303 return CharSequences.compareToIgnoreCase(mTitle, another.mTitle);
1304 }
1305 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 /**
1308 * Sets the internal change listener.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 * @param listener The listener.
1311 * @see #notifyChanged()
1312 */
Mathew Inwoodf2217132018-08-17 13:41:55 +01001313 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 final void setOnPreferenceChangeInternalListener(OnPreferenceChangeInternalListener listener) {
1315 mListener = listener;
1316 }
1317
1318 /**
1319 * Should be called when the data of this {@link Preference} has changed.
1320 */
1321 protected void notifyChanged() {
1322 if (mListener != null) {
1323 mListener.onPreferenceChange(this);
1324 }
1325 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Should be called when a Preference has been
1329 * added/removed from this group, or the ordering should be
1330 * re-evaluated.
1331 */
1332 protected void notifyHierarchyChanged() {
1333 if (mListener != null) {
1334 mListener.onPreferenceHierarchyChange(this);
1335 }
1336 }
1337
1338 /**
1339 * Gets the {@link PreferenceManager} that manages this Preference object's tree.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001340 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 * @return The {@link PreferenceManager}.
1342 */
1343 public PreferenceManager getPreferenceManager() {
1344 return mPreferenceManager;
1345 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 /**
1348 * Called when this Preference has been attached to a Preference hierarchy.
1349 * Make sure to call the super implementation.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001350 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 * @param preferenceManager The PreferenceManager of the hierarchy.
1352 */
1353 protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
1354 mPreferenceManager = preferenceManager;
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 mId = preferenceManager.getNextId();
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 dispatchSetInitialValue();
1359 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 /**
1362 * Called when the Preference hierarchy has been attached to the
Jason Monkb65b7102016-04-04 13:19:31 +00001363 * {@link PreferenceActivity}. This can also be called when this
1364 * Preference has been attached to a group that was already attached
1365 * to the {@link PreferenceActivity}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 */
1367 protected void onAttachedToActivity() {
1368 // At this point, the hierarchy that this preference is in is connected
1369 // with all other preferences.
1370 registerDependency();
1371 }
1372
Filip Pavliscba64582017-01-10 19:17:09 +00001373 /**
Filip Pavlisee3bc342017-03-13 16:58:24 +00001374 * Assigns a {@link PreferenceGroup} as the parent of this Preference. Set {@code null} to
1375 * remove the current parent.
Filip Pavliscba64582017-01-10 19:17:09 +00001376 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001377 * @param parentGroup Parent preference group of this Preference or {@code null} if none.
Filip Pavliscba64582017-01-10 19:17:09 +00001378 */
1379 void assignParent(@Nullable PreferenceGroup parentGroup) {
1380 mParentGroup = parentGroup;
1381 }
1382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 private void registerDependency() {
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 if (TextUtils.isEmpty(mDependencyKey)) return;
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 Preference preference = findPreferenceInHierarchy(mDependencyKey);
1388 if (preference != null) {
1389 preference.registerDependent(this);
1390 } else {
1391 throw new IllegalStateException("Dependency \"" + mDependencyKey
1392 + "\" not found for preference \"" + mKey + "\" (title: \"" + mTitle + "\"");
1393 }
1394 }
1395
1396 private void unregisterDependency() {
1397 if (mDependencyKey != null) {
1398 final Preference oldDependency = findPreferenceInHierarchy(mDependencyKey);
1399 if (oldDependency != null) {
1400 oldDependency.unregisterDependent(this);
1401 }
1402 }
1403 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 /**
1406 * Finds a Preference in this hierarchy (the whole thing,
1407 * even above/below your {@link PreferenceScreen} screen break) with the given
1408 * key.
1409 * <p>
1410 * This only functions after we have been attached to a hierarchy.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001411 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 * @param key The key of the Preference to find.
1413 * @return The Preference that uses the given key.
1414 */
1415 protected Preference findPreferenceInHierarchy(String key) {
1416 if (TextUtils.isEmpty(key) || mPreferenceManager == null) {
1417 return null;
1418 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 return mPreferenceManager.findPreference(key);
1421 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 /**
1424 * Adds a dependent Preference on this Preference so we can notify it.
1425 * Usually, the dependent Preference registers itself (it's good for it to
1426 * know it depends on something), so please use
1427 * {@link Preference#setDependency(String)} on the dependent Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 * @param dependent The dependent Preference that will be enabled/disabled
1430 * according to the state of this Preference.
1431 */
Mathew Inwoodf2217132018-08-17 13:41:55 +01001432 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 private void registerDependent(Preference dependent) {
1434 if (mDependents == null) {
1435 mDependents = new ArrayList<Preference>();
1436 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 mDependents.add(dependent);
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 dependent.onDependencyChanged(this, shouldDisableDependents());
1441 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 /**
1444 * Removes a dependent Preference on this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 * @param dependent The dependent Preference that will be enabled/disabled
1447 * according to the state of this Preference.
1448 * @return Returns the same Preference object, for chaining multiple calls
1449 * into a single statement.
1450 */
1451 private void unregisterDependent(Preference dependent) {
1452 if (mDependents != null) {
1453 mDependents.remove(dependent);
1454 }
1455 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 /**
1458 * Notifies any listening dependents of a change that affects the
1459 * dependency.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 * @param disableDependents Whether this Preference should disable
1462 * its dependents.
1463 */
1464 public void notifyDependencyChange(boolean disableDependents) {
1465 final List<Preference> dependents = mDependents;
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 if (dependents == null) {
1468 return;
1469 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 final int dependentsCount = dependents.size();
1472 for (int i = 0; i < dependentsCount; i++) {
1473 dependents.get(i).onDependencyChanged(this, disableDependents);
1474 }
1475 }
1476
1477 /**
1478 * Called when the dependency changes.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001479 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * @param dependency The Preference that this Preference depends on.
1481 * @param disableDependent Set true to disable this Preference.
1482 */
1483 public void onDependencyChanged(Preference dependency, boolean disableDependent) {
Michael Chanda53eca2009-03-27 17:46:36 -07001484 if (mDependencyMet == disableDependent) {
1485 mDependencyMet = !disableDependent;
1486
1487 // Enabled state can change dependent preferences' states, so notify
1488 notifyDependencyChange(shouldDisableDependents());
1489
1490 notifyChanged();
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
Alan Viverette02f56802013-08-19 16:32:56 -07001493
1494 /**
1495 * Called when the implicit parent dependency changes.
1496 *
1497 * @param parent The Preference that this Preference depends on.
1498 * @param disableChild Set true to disable this Preference.
1499 */
1500 public void onParentChanged(Preference parent, boolean disableChild) {
1501 if (mParentDependencyMet == disableChild) {
1502 mParentDependencyMet = !disableChild;
1503
1504 // Enabled state can change dependent preferences' states, so notify
1505 notifyDependencyChange(shouldDisableDependents());
1506
1507 notifyChanged();
1508 }
1509 }
1510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 /**
1512 * Checks whether this preference's dependents should currently be
1513 * disabled.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001514 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * @return True if the dependents should be disabled, otherwise false.
1516 */
1517 public boolean shouldDisableDependents() {
1518 return !isEnabled();
1519 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 /**
1522 * Sets the key of a Preference that this Preference will depend on. If that
1523 * Preference is not set or is off, this Preference will be disabled.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001524 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 * @param dependencyKey The key of the Preference that this depends on.
1526 */
1527 public void setDependency(String dependencyKey) {
1528 // Unregister the old dependency, if we had one
1529 unregisterDependency();
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 // Register the new
1532 mDependencyKey = dependencyKey;
1533 registerDependency();
1534 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 /**
1537 * Returns the key of the dependency on this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001538 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 * @return The key of the dependency.
1540 * @see #setDependency(String)
1541 */
1542 public String getDependency() {
1543 return mDependencyKey;
1544 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 /**
Filip Pavlisee3bc342017-03-13 16:58:24 +00001547 * Returns the {@link PreferenceGroup} which is this Preference assigned to or {@code null} if
1548 * this preference is not assigned to any group or is a root Preference.
Filip Pavliscba64582017-01-10 19:17:09 +00001549 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00001550 * @return the parent PreferenceGroup or {@code null} if not attached to any
Filip Pavliscba64582017-01-10 19:17:09 +00001551 */
1552 @Nullable
1553 public PreferenceGroup getParent() {
1554 return mParentGroup;
1555 }
1556
1557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * Called when this Preference is being removed from the hierarchy. You
1559 * should remove any references to this Preference that you know about. Make
1560 * sure to call through to the superclass implementation.
1561 */
Tor Norbyec615c6f2015-03-02 10:11:44 -08001562 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 protected void onPrepareForRemoval() {
1564 unregisterDependency();
1565 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 /**
1568 * Sets the default value for this Preference, which will be set either if
1569 * persistence is off or persistence is on and the preference is not found
1570 * in the persistent storage.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001571 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 * @param defaultValue The default value.
1573 */
1574 public void setDefaultValue(Object defaultValue) {
1575 mDefaultValue = defaultValue;
1576 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 private void dispatchSetInitialValue() {
Filip Pavlis09557932017-03-03 16:54:22 +00001579 if (getPreferenceDataStore() != null) {
1580 onSetInitialValue(true, mDefaultValue);
1581 return;
1582 }
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 // By now, we know if we are persistent.
1585 final boolean shouldPersist = shouldPersist();
1586 if (!shouldPersist || !getSharedPreferences().contains(mKey)) {
1587 if (mDefaultValue != null) {
1588 onSetInitialValue(false, mDefaultValue);
1589 }
1590 } else {
1591 onSetInitialValue(true, null);
1592 }
1593 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 /**
Filip Pavlisfd596452017-03-01 18:50:00 +00001596 * Implement this to set the initial value of the Preference.
Filip Pavlis09557932017-03-03 16:54:22 +00001597 *
1598 * <p>If <var>restorePersistedValue</var> is true, you should restore the
Filip Pavlisfd596452017-03-01 18:50:00 +00001599 * Preference value from the {@link android.content.SharedPreferences}. If
1600 * <var>restorePersistedValue</var> is false, you should set the Preference
1601 * value to defaultValue that is given (and possibly store to SharedPreferences
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * if {@link #shouldPersist()} is true).
Filip Pavlis09557932017-03-03 16:54:22 +00001603 *
1604 * <p>In case of using {@link PreferenceDataStore}, the <var>restorePersistedValue</var> is
Filip Pavlisee3bc342017-03-13 16:58:24 +00001605 * always {@code true}. But the default value (if provided) is set.
Filip Pavlis09557932017-03-03 16:54:22 +00001606 *
1607 * <p>This may not always be called. One example is if it should not persist
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 * but there is no default value given.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001609 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 * @param restorePersistedValue True to restore the persisted value;
1611 * false to use the given <var>defaultValue</var>.
1612 * @param defaultValue The default value for this Preference. Only use this
1613 * if <var>restorePersistedValue</var> is false.
1614 */
1615 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
1616 }
1617
1618 private void tryCommit(SharedPreferences.Editor editor) {
1619 if (mPreferenceManager.shouldCommit()) {
Brad Fitzpatrickdd644c12010-10-10 10:58:47 -07001620 try {
1621 editor.apply();
1622 } catch (AbstractMethodError unused) {
1623 // The app injected its own pre-Gingerbread
1624 // SharedPreferences.Editor implementation without
1625 // an apply method.
1626 editor.commit();
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
1629 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001632 * Attempts to persist a String if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 * @param value The value to persist.
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001635 * @return True if this Preference is persistent. (This is not whether the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 * value was persisted, since we may not necessarily commit if there
1637 * will be a batch commit later.)
1638 * @see #getPersistedString(String)
1639 */
1640 protected boolean persistString(String value) {
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001641 if (!shouldPersist()) {
1642 return false;
1643 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001644
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001645 // Shouldn't store null
1646 if (TextUtils.equals(value, getPersistedString(null))) {
1647 // It's already there, so the same as persisting
1648 return true;
1649 }
1650
1651 PreferenceDataStore dataStore = getPreferenceDataStore();
1652 if (dataStore != null) {
1653 dataStore.putString(mKey, value);
1654 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1656 editor.putString(mKey, value);
1657 tryCommit(editor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001659 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001663 * Attempts to get a persisted String if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001664 *
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001665 * @param defaultReturnValue The default value to return if either this
1666 * Preference is not persistent or this Preference is not present.
1667 * @return The value from the data store or the default return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 * value.
1669 * @see #persistString(String)
1670 */
1671 protected String getPersistedString(String defaultReturnValue) {
1672 if (!shouldPersist()) {
1673 return defaultReturnValue;
1674 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001675
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001676 PreferenceDataStore dataStore = getPreferenceDataStore();
1677 if (dataStore != null) {
1678 return dataStore.getString(mKey, defaultReturnValue);
1679 }
1680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return mPreferenceManager.getSharedPreferences().getString(mKey, defaultReturnValue);
1682 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001685 * Attempts to persist a set of Strings if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001686 *
Adam Powell212db7d2010-04-08 16:24:46 -07001687 * @param values The values to persist.
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001688 * @return True if this Preference is persistent. (This is not whether the
Adam Powell212db7d2010-04-08 16:24:46 -07001689 * value was persisted, since we may not necessarily commit if there
1690 * will be a batch commit later.)
Adam Powell5c2f8392016-02-23 16:06:42 -08001691 * @see #getPersistedStringSet(Set)
Adam Powell212db7d2010-04-08 16:24:46 -07001692 */
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001693 public boolean persistStringSet(Set<String> values) {
1694 if (!shouldPersist()) {
1695 return false;
1696 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001697
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001698 // Shouldn't store null
1699 if (values.equals(getPersistedStringSet(null))) {
1700 // It's already there, so the same as persisting
1701 return true;
1702 }
1703
1704 PreferenceDataStore dataStore = getPreferenceDataStore();
1705 if (dataStore != null) {
1706 dataStore.putStringSet(mKey, values);
1707 } else {
Adam Powell212db7d2010-04-08 16:24:46 -07001708 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1709 editor.putStringSet(mKey, values);
1710 tryCommit(editor);
Adam Powell212db7d2010-04-08 16:24:46 -07001711 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001712 return true;
Adam Powell212db7d2010-04-08 16:24:46 -07001713 }
1714
1715 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001716 * Attempts to get a persisted set of Strings if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001717 *
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001718 * @param defaultReturnValue The default value to return if either this
1719 * Preference is not persistent or this Preference is not present.
1720 * @return The value from the data store or the default return
Adam Powell212db7d2010-04-08 16:24:46 -07001721 * value.
1722 * @see #persistStringSet(Set)
Adam Powell212db7d2010-04-08 16:24:46 -07001723 */
Adam Powell5c2f8392016-02-23 16:06:42 -08001724 public Set<String> getPersistedStringSet(Set<String> defaultReturnValue) {
Adam Powell212db7d2010-04-08 16:24:46 -07001725 if (!shouldPersist()) {
1726 return defaultReturnValue;
1727 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001728
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001729 PreferenceDataStore dataStore = getPreferenceDataStore();
1730 if (dataStore != null) {
1731 return dataStore.getStringSet(mKey, defaultReturnValue);
1732 }
1733
Adam Powell212db7d2010-04-08 16:24:46 -07001734 return mPreferenceManager.getSharedPreferences().getStringSet(mKey, defaultReturnValue);
1735 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001736
Adam Powell212db7d2010-04-08 16:24:46 -07001737 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001738 * Attempts to persist an int if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 * @param value The value to persist.
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001741 * @return True if this Preference is persistent. (This is not whether the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * value was persisted, since we may not necessarily commit if there
1743 * will be a batch commit later.)
1744 * @see #persistString(String)
1745 * @see #getPersistedInt(int)
1746 */
1747 protected boolean persistInt(int value) {
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001748 if (!shouldPersist()) {
1749 return false;
1750 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001751
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001752 if (value == getPersistedInt(~value)) {
1753 // It's already there, so the same as persisting
1754 return true;
1755 }
1756
1757 PreferenceDataStore dataStore = getPreferenceDataStore();
1758 if (dataStore != null) {
1759 dataStore.putInt(mKey, value);
1760 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1762 editor.putInt(mKey, value);
1763 tryCommit(editor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001765 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001769 * Attempts to get a persisted int if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001770 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 * @param defaultReturnValue The default value to return if either this
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001772 * Preference is not persistent or this Preference is not present.
1773 * @return The value from the data store or the default return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 * value.
1775 * @see #getPersistedString(String)
1776 * @see #persistInt(int)
1777 */
1778 protected int getPersistedInt(int defaultReturnValue) {
1779 if (!shouldPersist()) {
1780 return defaultReturnValue;
1781 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001782
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001783 PreferenceDataStore dataStore = getPreferenceDataStore();
1784 if (dataStore != null) {
1785 return dataStore.getInt(mKey, defaultReturnValue);
1786 }
1787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 return mPreferenceManager.getSharedPreferences().getInt(mKey, defaultReturnValue);
1789 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001792 * Attempts to persist a long if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001793 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 * @param value The value to persist.
1795 * @return True if this Preference is persistent. (This is not whether the
1796 * value was persisted, since we may not necessarily commit if there
1797 * will be a batch commit later.)
1798 * @see #persistString(String)
1799 * @see #getPersistedFloat(float)
1800 */
1801 protected boolean persistFloat(float value) {
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001802 if (!shouldPersist()) {
1803 return false;
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001806 if (value == getPersistedFloat(Float.NaN)) {
1807 // It's already there, so the same as persisting
1808 return true;
1809 }
1810
1811 PreferenceDataStore dataStore = getPreferenceDataStore();
1812 if (dataStore != null) {
1813 dataStore.putFloat(mKey, value);
1814 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1816 editor.putFloat(mKey, value);
1817 tryCommit(editor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001819 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001823 * Attempts to get a persisted float if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001824 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 * @param defaultReturnValue The default value to return if either this
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001826 * Preference is not persistent or this Preference is not present.
1827 * @return The value from the data store or the default return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 * value.
1829 * @see #getPersistedString(String)
1830 * @see #persistFloat(float)
1831 */
1832 protected float getPersistedFloat(float defaultReturnValue) {
1833 if (!shouldPersist()) {
1834 return defaultReturnValue;
1835 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001836
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001837 PreferenceDataStore dataStore = getPreferenceDataStore();
1838 if (dataStore != null) {
1839 return dataStore.getFloat(mKey, defaultReturnValue);
1840 }
1841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 return mPreferenceManager.getSharedPreferences().getFloat(mKey, defaultReturnValue);
1843 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001846 * Attempts to persist a long if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 * @param value The value to persist.
1849 * @return True if this Preference is persistent. (This is not whether the
1850 * value was persisted, since we may not necessarily commit if there
1851 * will be a batch commit later.)
1852 * @see #persistString(String)
1853 * @see #getPersistedLong(long)
1854 */
1855 protected boolean persistLong(long value) {
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001856 if (!shouldPersist()) {
1857 return false;
1858 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001859
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001860 if (value == getPersistedLong(~value)) {
1861 // It's already there, so the same as persisting
1862 return true;
1863 }
1864
1865 PreferenceDataStore dataStore = getPreferenceDataStore();
1866 if (dataStore != null) {
1867 dataStore.putLong(mKey, value);
1868 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1870 editor.putLong(mKey, value);
1871 tryCommit(editor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001873 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001877 * Attempts to get a persisted long if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001878 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 * @param defaultReturnValue The default value to return if either this
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001880 * Preference is not persistent or this Preference is not present.
1881 * @return The value from the data store or the default return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 * value.
1883 * @see #getPersistedString(String)
1884 * @see #persistLong(long)
1885 */
1886 protected long getPersistedLong(long defaultReturnValue) {
1887 if (!shouldPersist()) {
1888 return defaultReturnValue;
1889 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001890
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001891 PreferenceDataStore dataStore = getPreferenceDataStore();
1892 if (dataStore != null) {
1893 return dataStore.getLong(mKey, defaultReturnValue);
1894 }
1895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 return mPreferenceManager.getSharedPreferences().getLong(mKey, defaultReturnValue);
1897 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001900 * Attempts to persist a boolean if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001901 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 * @param value The value to persist.
1903 * @return True if this Preference is persistent. (This is not whether the
1904 * value was persisted, since we may not necessarily commit if there
1905 * will be a batch commit later.)
1906 * @see #persistString(String)
1907 * @see #getPersistedBoolean(boolean)
1908 */
1909 protected boolean persistBoolean(boolean value) {
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001910 if (!shouldPersist()) {
1911 return false;
1912 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001913
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001914 if (value == getPersistedBoolean(!value)) {
1915 // It's already there, so the same as persisting
1916 return true;
1917 }
1918
1919 PreferenceDataStore dataStore = getPreferenceDataStore();
1920 if (dataStore != null) {
1921 dataStore.putBoolean(mKey, value);
1922 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 SharedPreferences.Editor editor = mPreferenceManager.getEditor();
1924 editor.putBoolean(mKey, value);
1925 tryCommit(editor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001927 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 /**
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001931 * Attempts to get a persisted boolean if this Preference is persistent.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001932 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 * @param defaultReturnValue The default value to return if either this
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001934 * Preference is not persistent or this Preference is not present.
1935 * @return The value from the data store or the default return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 * value.
1937 * @see #getPersistedString(String)
1938 * @see #persistBoolean(boolean)
1939 */
1940 protected boolean getPersistedBoolean(boolean defaultReturnValue) {
1941 if (!shouldPersist()) {
1942 return defaultReturnValue;
1943 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001944
Filip Pavlis0b0c6cb2016-11-16 15:58:28 +00001945 PreferenceDataStore dataStore = getPreferenceDataStore();
1946 if (dataStore != null) {
1947 return dataStore.getBoolean(mKey, defaultReturnValue);
1948 }
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 return mPreferenceManager.getSharedPreferences().getBoolean(mKey, defaultReturnValue);
1951 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 @Override
1954 public String toString() {
1955 return getFilterableStringBuilder().toString();
1956 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 /**
1959 * Returns the text that will be used to filter this Preference depending on
1960 * user input.
1961 * <p>
1962 * If overridding and calling through to the superclass, make sure to prepend
1963 * your additions with a space.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001964 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 * @return Text as a {@link StringBuilder} that will be used to filter this
1966 * preference. By default, this is the title and summary
1967 * (concatenated with a space).
1968 */
1969 StringBuilder getFilterableStringBuilder() {
1970 StringBuilder sb = new StringBuilder();
1971 CharSequence title = getTitle();
1972 if (!TextUtils.isEmpty(title)) {
1973 sb.append(title).append(' ');
1974 }
1975 CharSequence summary = getSummary();
1976 if (!TextUtils.isEmpty(summary)) {
1977 sb.append(summary).append(' ');
1978 }
Tammo Spalink0bb99602009-09-08 18:30:33 +08001979 if (sb.length() > 0) {
1980 // Drop the last space
1981 sb.setLength(sb.length() - 1);
1982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 return sb;
1984 }
1985
1986 /**
1987 * Store this Preference hierarchy's frozen state into the given container.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 * @param container The Bundle in which to save the instance of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07001990 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 * @see #restoreHierarchyState
1992 * @see #onSaveInstanceState
1993 */
1994 public void saveHierarchyState(Bundle container) {
1995 dispatchSaveInstanceState(container);
1996 }
1997
1998 /**
Filip Pavlis09557932017-03-03 16:54:22 +00001999 * Called by {@link #saveHierarchyState} to store the instance for this Preference and its
2000 * children. May be overridden to modify how the save happens for children. For example, some
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 * Preference objects may want to not store an instance for their children.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 * @param container The Bundle in which to save the instance of this Preference.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 * @see #saveHierarchyState
2006 * @see #onSaveInstanceState
2007 */
2008 void dispatchSaveInstanceState(Bundle container) {
2009 if (hasKey()) {
2010 mBaseMethodCalled = false;
2011 Parcelable state = onSaveInstanceState();
2012 if (!mBaseMethodCalled) {
2013 throw new IllegalStateException(
2014 "Derived class did not call super.onSaveInstanceState()");
2015 }
2016 if (state != null) {
2017 container.putParcelable(mKey, state);
2018 }
2019 }
2020 }
2021
2022 /**
2023 * Hook allowing a Preference to generate a representation of its internal
2024 * state that can later be used to create a new instance with that same
2025 * state. This state should only contain information that is not persistent
2026 * or can be reconstructed later.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002027 *
Filip Pavlisee3bc342017-03-13 16:58:24 +00002028 * @return A Parcelable object containing the current dynamic state of this Preference, or
2029 * {@code null} if there is nothing interesting to save. The default implementation
2030 * returns {@code null}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 * @see #onRestoreInstanceState
2032 * @see #saveHierarchyState
2033 */
2034 protected Parcelable onSaveInstanceState() {
2035 mBaseMethodCalled = true;
2036 return BaseSavedState.EMPTY_STATE;
2037 }
2038
2039 /**
2040 * Restore this Preference hierarchy's previously saved state from the given container.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 * @param container The Bundle that holds the previously saved state.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002043 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 * @see #saveHierarchyState
2045 * @see #onRestoreInstanceState
2046 */
2047 public void restoreHierarchyState(Bundle container) {
2048 dispatchRestoreInstanceState(container);
2049 }
2050
2051 /**
2052 * Called by {@link #restoreHierarchyState} to retrieve the saved state for this
2053 * Preference and its children. May be overridden to modify how restoring
2054 * happens to the children of a Preference. For example, some Preference objects may
2055 * not want to save state for their children.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 * @param container The Bundle that holds the previously saved state.
2058 * @see #restoreHierarchyState
2059 * @see #onRestoreInstanceState
2060 */
2061 void dispatchRestoreInstanceState(Bundle container) {
2062 if (hasKey()) {
2063 Parcelable state = container.getParcelable(mKey);
2064 if (state != null) {
2065 mBaseMethodCalled = false;
2066 onRestoreInstanceState(state);
2067 if (!mBaseMethodCalled) {
2068 throw new IllegalStateException(
2069 "Derived class did not call super.onRestoreInstanceState()");
2070 }
2071 }
2072 }
2073 }
2074
2075 /**
Filip Pavlisee3bc342017-03-13 16:58:24 +00002076 * Hook allowing a Preference to re-apply a representation of its internal state that had
2077 * previously been generated by {@link #onSaveInstanceState}. This function will never be called
2078 * with a {@code null} state.
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002079 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 * @param state The saved state that had previously been returned by
2081 * {@link #onSaveInstanceState}.
2082 * @see #onSaveInstanceState
2083 * @see #restoreHierarchyState
2084 */
2085 protected void onRestoreInstanceState(Parcelable state) {
2086 mBaseMethodCalled = true;
2087 if (state != BaseSavedState.EMPTY_STATE && state != null) {
2088 throw new IllegalArgumentException("Wrong state class -- expecting Preference State");
2089 }
2090 }
2091
2092 /**
2093 * A base class for managing the instance state of a {@link Preference}.
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +00002094 *
2095 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
2096 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
2097 * Preference Library</a> for consistent behavior across all devices.
2098 * For more information on using the AndroidX Preference Library see
2099 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +00002101 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 public static class BaseSavedState extends AbsSavedState {
2103 public BaseSavedState(Parcel source) {
2104 super(source);
2105 }
2106
2107 public BaseSavedState(Parcelable superState) {
2108 super(superState);
2109 }
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 public static final Parcelable.Creator<BaseSavedState> CREATOR =
2112 new Parcelable.Creator<BaseSavedState>() {
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002113 public BaseSavedState createFromParcel(Parcel in) {
2114 return new BaseSavedState(in);
2115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116
Tony Mantler9a6b11b2016-09-13 15:53:35 -07002117 public BaseSavedState[] newArray(int size) {
2118 return new BaseSavedState[size];
2119 }
2120 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
2122
2123}