Fields Summary |
---|
private static final String | TAG |
public static final String | METADATA_KEY_PREFERENCESThe Activity meta-data key for its XML preference hierarchy. |
public static final String | KEY_HAS_SET_DEFAULT_VALUES |
private android.app.Activity | mActivity |
private android.content.Context | mContextThe context to use. This should always be set. |
private long | mNextIdThe counter for unique IDs. |
private int | mNextRequestCodeThe counter for unique request codes. |
private android.content.SharedPreferences | mSharedPreferencesCached shared preferences. |
private SharedPreferences.Editor | mEditorIf in no-commit mode, the shared editor to give out (which will be
committed when exiting no-commit mode). |
private boolean | mNoCommitBlocks commits from happening on the shared editor. This is used when
inflating the hierarchy. Do not set this directly, use {@link #setNoCommit(boolean)} |
private String | mSharedPreferencesNameThe SharedPreferences name that will be used for all {@link Preference}s
managed by this instance. |
private int | mSharedPreferencesModeThe SharedPreferences mode that will be used for all {@link Preference}s
managed by this instance. |
private PreferenceScreen | mPreferenceScreenThe {@link PreferenceScreen} at the root of the preference hierarchy. |
private List | mActivityResultListenersList of activity result listeners. |
private List | mActivityStopListenersList of activity stop listeners. |
private List | mActivityDestroyListenersList of activity destroy listeners. |
private List | mPreferencesScreensList of dialogs that should be dismissed when we receive onNewIntent in
our PreferenceActivity. |
private OnPreferenceTreeClickListener | mOnPreferenceTreeClickListener |
Methods Summary |
---|
void | addPreferencesScreen(android.content.DialogInterface screen)
synchronized (this) {
if (mPreferencesScreens == null) {
mPreferencesScreens = new ArrayList<DialogInterface>();
}
mPreferencesScreens.add(screen);
}
|
public PreferenceScreen | createPreferenceScreen(android.content.Context context)
final PreferenceScreen preferenceScreen = new PreferenceScreen(context, null);
preferenceScreen.onAttachedToHierarchy(this);
return preferenceScreen;
|
private void | dismissAllScreens()
// Remove any of the previously shown preferences screens
ArrayList<DialogInterface> screensToDismiss;
synchronized (this) {
if (mPreferencesScreens == null) {
return;
}
screensToDismiss = new ArrayList<DialogInterface>(mPreferencesScreens);
mPreferencesScreens.clear();
}
for (int i = screensToDismiss.size() - 1; i >= 0; i--) {
screensToDismiss.get(i).dismiss();
}
|
void | dispatchActivityDestroy()Called by the {@link PreferenceManager} to dispatch the activity destroy
event.
List<OnActivityDestroyListener> list = null;
synchronized (this) {
if (mActivityDestroyListeners != null) {
list = new ArrayList<OnActivityDestroyListener>(mActivityDestroyListeners);
}
}
if (list != null) {
final int N = list.size();
for (int i = 0; i < N; i++) {
list.get(i).onActivityDestroy();
}
}
// Dismiss any PreferenceScreens still showing
dismissAllScreens();
|
void | dispatchActivityResult(int requestCode, int resultCode, android.content.Intent data)Called by the {@link PreferenceManager} to dispatch a subactivity result.
List<OnActivityResultListener> list;
synchronized (this) {
if (mActivityResultListeners == null) return;
list = new ArrayList<OnActivityResultListener>(mActivityResultListeners);
}
final int N = list.size();
for (int i = 0; i < N; i++) {
if (list.get(i).onActivityResult(requestCode, resultCode, data)) {
break;
}
}
|
void | dispatchActivityStop()Called by the {@link PreferenceManager} to dispatch the activity stop
event.
List<OnActivityStopListener> list;
synchronized (this) {
if (mActivityStopListeners == null) return;
list = new ArrayList<OnActivityStopListener>(mActivityStopListeners);
}
final int N = list.size();
for (int i = 0; i < N; i++) {
list.get(i).onActivityStop();
}
|
void | dispatchNewIntent(android.content.Intent intent)Called by {@link PreferenceActivity} to dispatch the new Intent event.
dismissAllScreens();
|
public Preference | findPreference(java.lang.CharSequence key)Finds a {@link Preference} based on its key.
if (mPreferenceScreen == null) {
return null;
}
return mPreferenceScreen.findPreference(key);
|
android.app.Activity | getActivity()Returns the activity that shows the preferences. This is useful for doing
managed queries, but in most cases the use of {@link #getContext()} is
preferred.
This will return null if this class was instantiated with a Context
instead of Activity. For example, when setting the default values.
return mActivity;
|
android.content.Context | getContext()Returns the context. This is preferred over {@link #getActivity()} when
possible.
return mContext;
|
public static android.content.SharedPreferences | getDefaultSharedPreferences(android.content.Context context)Gets a SharedPreferences instance that points to the default file that is
used by the preference framework in the given context.
return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
getDefaultSharedPreferencesMode());
|
private static int | getDefaultSharedPreferencesMode()
return Context.MODE_PRIVATE;
|
private static java.lang.String | getDefaultSharedPreferencesName(android.content.Context context)
return context.getPackageName() + "_preferences";
|
SharedPreferences.Editor | getEditor()Returns an editor to use when modifying the shared preferences.
Do NOT commit unless {@link #shouldCommit()} returns true.
if (mNoCommit) {
if (mEditor == null) {
mEditor = getSharedPreferences().edit();
}
return mEditor;
} else {
return getSharedPreferences().edit();
}
|
long | getNextId()Called by a preference to get a unique ID in its hierarchy.
synchronized (this) {
return mNextId++;
}
|
int | getNextRequestCode()Returns a request code that is unique for the activity. Each subsequent
call to this method should return another unique request code.
synchronized (this) {
return mNextRequestCode++;
}
|
android.preference.PreferenceManager$OnPreferenceTreeClickListener | getOnPreferenceTreeClickListener()
return mOnPreferenceTreeClickListener;
|
PreferenceScreen | getPreferenceScreen()Returns the root of the preference hierarchy managed by this class.
return mPreferenceScreen;
|
public android.content.SharedPreferences | getSharedPreferences()Gets a SharedPreferences instance that preferences managed by this will
use.
if (mSharedPreferences == null) {
mSharedPreferences = mContext.getSharedPreferences(mSharedPreferencesName,
mSharedPreferencesMode);
}
return mSharedPreferences;
|
public int | getSharedPreferencesMode()Returns the current mode of the SharedPreferences file that preferences managed by
this will use.
return mSharedPreferencesMode;
|
public java.lang.String | getSharedPreferencesName()Returns the current name of the SharedPreferences file that preferences managed by
this will use.
return mSharedPreferencesName;
|
PreferenceScreen | inflateFromIntent(android.content.Intent queryIntent, PreferenceScreen rootPreferences)Inflates a preference hierarchy from the preference hierarchies of
{@link Activity Activities} that match the given {@link Intent}. An
{@link Activity} defines its preference hierarchy with meta-data using
the {@link #METADATA_KEY_PREFERENCES} key.
If a preference hierarchy is given, the new preference hierarchies will
be merged in.
final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
final HashSet<String> inflatedRes = new HashSet<String>();
for (int i = activities.size() - 1; i >= 0; i--) {
final ActivityInfo activityInfo = activities.get(i).activityInfo;
final Bundle metaData = activityInfo.metaData;
if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
continue;
}
// Need to concat the package with res ID since the same res ID
// can be re-used across contexts
final String uniqueResId = activityInfo.packageName + ":"
+ activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
if (!inflatedRes.contains(uniqueResId)) {
inflatedRes.add(uniqueResId);
final Context context;
try {
context = mContext.createPackageContext(activityInfo.packageName, 0);
} catch (NameNotFoundException e) {
Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": "
+ Log.getStackTraceString(e));
continue;
}
final PreferenceInflater inflater = new PreferenceInflater(context, this);
final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
.getPackageManager(), METADATA_KEY_PREFERENCES);
rootPreferences = (PreferenceScreen) inflater
.inflate(parser, rootPreferences, true);
parser.close();
}
}
rootPreferences.onAttachedToHierarchy(this);
return rootPreferences;
|
PreferenceScreen | inflateFromResource(android.content.Context context, int resId, PreferenceScreen rootPreferences)Inflates a preference hierarchy from XML. If a preference hierarchy is
given, the new preference hierarchies will be merged in.
// Block commits
setNoCommit(true);
final PreferenceInflater inflater = new PreferenceInflater(context, this);
rootPreferences = (PreferenceScreen) inflater.inflate(resId, rootPreferences, true);
rootPreferences.onAttachedToHierarchy(this);
// Unblock commits
setNoCommit(false);
return rootPreferences;
|
private void | init(android.content.Context context)
mContext = context;
setSharedPreferencesName(getDefaultSharedPreferencesName(context));
|
private java.util.List | queryIntentActivities(android.content.Intent queryIntent)Returns a list of {@link Activity} (indirectly) that match a given
{@link Intent}.
return mContext.getPackageManager().queryIntentActivities(queryIntent,
PackageManager.GET_META_DATA);
|
void | registerOnActivityDestroyListener(android.preference.PreferenceManager$OnActivityDestroyListener listener)Registers a listener.
synchronized (this) {
if (mActivityDestroyListeners == null) {
mActivityDestroyListeners = new ArrayList<OnActivityDestroyListener>();
}
if (!mActivityDestroyListeners.contains(listener)) {
mActivityDestroyListeners.add(listener);
}
}
|
void | registerOnActivityResultListener(android.preference.PreferenceManager$OnActivityResultListener listener)Registers a listener.
synchronized (this) {
if (mActivityResultListeners == null) {
mActivityResultListeners = new ArrayList<OnActivityResultListener>();
}
if (!mActivityResultListeners.contains(listener)) {
mActivityResultListeners.add(listener);
}
}
|
void | registerOnActivityStopListener(android.preference.PreferenceManager$OnActivityStopListener listener)Registers a listener.
synchronized (this) {
if (mActivityStopListeners == null) {
mActivityStopListeners = new ArrayList<OnActivityStopListener>();
}
if (!mActivityStopListeners.contains(listener)) {
mActivityStopListeners.add(listener);
}
}
|
void | removePreferencesScreen(android.content.DialogInterface screen)
synchronized (this) {
if (mPreferencesScreens == null) {
return;
}
mPreferencesScreens.remove(screen);
}
|
public static void | setDefaultValues(android.content.Context context, int resId, boolean readAgain)Sets the default values from a preference hierarchy in XML. This should
be called by the application's main activity.
If {@code readAgain} is false, this will only set the default values if this
method has never been called in the past (or the
{@link #KEY_HAS_SET_DEFAULT_VALUES} in the default value shared
preferences file is false). To attempt to set the default values again
bypassing this check, set {@code readAgain} to true.
// Use the default shared preferences name and mode
setDefaultValues(context, getDefaultSharedPreferencesName(context),
getDefaultSharedPreferencesMode(), resId, readAgain);
|
public static void | setDefaultValues(android.content.Context context, java.lang.String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)Similar to {@link #setDefaultValues(Context, int, boolean)} but allows
the client to provide the filename and mode of the shared preferences
file.
final SharedPreferences defaultValueSp = context.getSharedPreferences(
KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE);
if (readAgain || !defaultValueSp.getBoolean(KEY_HAS_SET_DEFAULT_VALUES, false)) {
final PreferenceManager pm = new PreferenceManager(context);
pm.setSharedPreferencesName(sharedPreferencesName);
pm.setSharedPreferencesMode(sharedPreferencesMode);
pm.inflateFromResource(context, resId, null);
defaultValueSp.edit().putBoolean(KEY_HAS_SET_DEFAULT_VALUES, true).commit();
}
|
private void | setNoCommit(boolean noCommit)
if (!noCommit && mEditor != null) {
mEditor.commit();
}
mNoCommit = noCommit;
|
void | setOnPreferenceTreeClickListener(android.preference.PreferenceManager$OnPreferenceTreeClickListener listener)Sets the callback to be invoked when a {@link Preference} in the
hierarchy rooted at this {@link PreferenceManager} is clicked.
mOnPreferenceTreeClickListener = listener;
|
boolean | setPreferences(PreferenceScreen preferenceScreen)Sets the root of the preference hierarchy.
if (preferenceScreen != mPreferenceScreen) {
mPreferenceScreen = preferenceScreen;
return true;
}
return false;
|
public void | setSharedPreferencesMode(int sharedPreferencesMode)Sets the mode of the SharedPreferences file that preferences managed by this
will use.
mSharedPreferencesMode = sharedPreferencesMode;
mSharedPreferences = null;
|
public void | setSharedPreferencesName(java.lang.String sharedPreferencesName)Sets the name of the SharedPreferences file that preferences managed by this
will use.
mSharedPreferencesName = sharedPreferencesName;
mSharedPreferences = null;
|
boolean | shouldCommit()Whether it is the client's responsibility to commit on the
{@link #getEditor()}. This will return false in cases where the writes
should be batched, for example when inflating preferences from XML.
return !mNoCommit;
|
void | unregisterOnActivityDestroyListener(android.preference.PreferenceManager$OnActivityDestroyListener listener)Unregisters a listener.
synchronized (this) {
if (mActivityDestroyListeners != null) {
mActivityDestroyListeners.remove(listener);
}
}
|
void | unregisterOnActivityResultListener(android.preference.PreferenceManager$OnActivityResultListener listener)Unregisters a listener.
synchronized (this) {
if (mActivityResultListeners != null) {
mActivityResultListeners.remove(listener);
}
}
|
void | unregisterOnActivityStopListener(android.preference.PreferenceManager$OnActivityStopListener listener)Unregisters a listener.
synchronized (this) {
if (mActivityStopListeners != null) {
mActivityStopListeners.remove(listener);
}
}
|