blob: daa2f1b7c11379333e4cb6b964865cd4a787b4b7 [file] [log] [blame]
Amith Yamasanid7993472010-08-18 13:59:28 -07001/*
2 * Copyright (C) 2010 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 com.android.settings;
18
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -070019import android.app.Activity;
Amith Yamasanid7993472010-08-18 13:59:28 -070020import android.app.Dialog;
21import android.app.DialogFragment;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070022import android.app.Fragment;
Amith Yamasanid7993472010-08-18 13:59:28 -070023import android.content.ContentResolver;
Amith Yamasani350938e2013-04-09 10:22:47 -070024import android.content.Context;
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +080025import android.content.DialogInterface;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Jason Monk91e2f892016-02-23 15:31:09 -050028import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040029import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050030import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.PreferenceGroupAdapter;
32import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050033import android.support.v7.preference.PreferenceViewHolder;
34import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.text.TextUtils;
Jason Monk2071eda2016-02-25 13:55:48 -050037import android.util.ArrayMap;
Amith Yamasanid7993472010-08-18 13:59:28 -070038import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070039import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070040import android.view.Menu;
41import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070042import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070043import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070044import android.widget.Button;
Jason Monk39b46742015-09-10 15:52:51 -040045import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040046import com.android.settings.widget.FloatingActionButton;
47
Jason Monk39b46742015-09-10 15:52:51 -040048import java.util.UUID;
49
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070050/**
Amith Yamasanid7993472010-08-18 13:59:28 -070051 * Base class for Settings fragments, with some helper functions and dialog management.
52 */
Chris Wren8a963ba2015-03-20 10:29:14 -040053public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
54 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070055
Anna Galusza0285c802016-01-29 17:32:19 -080056 /**
57 * The Help Uri Resource key. This can be passed as an extra argument when creating the
58 * Fragment.
59 **/
60 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
61
Jason Monk65bb0972015-12-17 10:39:44 -050062 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070063
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070064 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070065
66 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070067
Amith Yamasanid7993472010-08-18 13:59:28 -070068 private SettingsDialogFragment mDialogFragment;
69
Jason Monk23acc2b2015-04-14 15:06:39 -040070 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070071
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000072 private static final int ORDER_FIRST = -1;
73 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
74
Amith Yamasani350938e2013-04-09 10:22:47 -070075 // Cache the content resolver for async callbacks
76 private ContentResolver mContentResolver;
77
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070078 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070079 private boolean mPreferenceHighlighted = false;
80
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070082 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040083 private RecyclerView.AdapterDataObserver mDataSetObserver =
84 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070085 @Override
86 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040087 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070088 }
89 };
90
Fabrice Di Meglio86159282014-07-21 16:02:27 -070091 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040092 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090093 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070094
Jason Monk39b46742015-09-10 15:52:51 -040095 private LayoutPreference mHeader;
96
97 private LayoutPreference mFooter;
98 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050099 private LinearLayoutManager mLayoutManager;
100 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk2071eda2016-02-25 13:55:48 -0500101 private ArrayMap<String, Preference> mPreferenceCache;
Jason Monk39b46742015-09-10 15:52:51 -0400102
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700103 @Override
104 public void onCreate(Bundle icicle) {
105 super.onCreate(icicle);
106
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700107 if (icicle != null) {
108 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
109 }
110
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700111 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800112 Bundle arguments = getArguments();
113 int helpResource;
114 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
115 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
116 } else {
117 helpResource = getHelpResource();
118 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700119 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400120 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700121 }
122 }
123
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700124 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700125 public View onCreateView(LayoutInflater inflater, ViewGroup container,
126 Bundle savedInstanceState) {
127 final View root = super.onCreateView(inflater, container, savedInstanceState);
128 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400129 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900130 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700131 return root;
132 }
133
Jason Monk39b46742015-09-10 15:52:51 -0400134 @Override
135 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
136 }
137
Jason Monk91e2f892016-02-23 15:31:09 -0500138 @Override
139 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
140 super.addPreferencesFromResource(preferencesResId);
141 checkAvailablePrefs(getPreferenceScreen());
142 }
143
144 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
145 if (preferenceGroup == null) return;
146 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
147 Preference pref = preferenceGroup.getPreference(i);
148 if (pref instanceof SelfAvailablePreference
149 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
150 preferenceGroup.removePreference(pref);
151 } else if (pref instanceof PreferenceGroup) {
152 checkAvailablePrefs((PreferenceGroup) pref);
153 }
154 }
155 }
156
John Spurlockb8e02b82015-04-15 21:15:55 -0400157 public FloatingActionButton getFloatingActionButton() {
158 return mFloatingActionButton;
159 }
160
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900161 public ViewGroup getButtonBar() {
162 return mButtonBar;
163 }
164
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700165 public View setPinnedHeaderView(int layoutResId) {
166 final LayoutInflater inflater = getActivity().getLayoutInflater();
167 final View pinnedHeader =
168 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
169 setPinnedHeaderView(pinnedHeader);
170 return pinnedHeader;
171 }
172
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700173 public void setPinnedHeaderView(View pinnedHeader) {
174 mPinnedHeaderFrameLayout.addView(pinnedHeader);
175 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
176 }
177
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700178 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700179 public void onSaveInstanceState(Bundle outState) {
180 super.onSaveInstanceState(outState);
181
182 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
183 }
184
185 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700186 public void onActivityCreated(Bundle savedInstanceState) {
187 super.onActivityCreated(savedInstanceState);
Anna Galusza384fa2e2016-02-03 15:51:12 -0800188 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700189 setHasOptionsMenu(true);
190 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700191 }
192
193 @Override
194 public void onResume() {
195 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700196
197 final Bundle args = getArguments();
198 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700199 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
200 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700201 }
202 }
203
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700204 @Override
205 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700206 registerObserverIfNeeded();
207 }
208
209 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700210 protected void onUnbindPreferences() {
211 unregisterObserverIfNeeded();
212 }
213
Jason Monkb5aa73f2015-03-31 12:59:33 -0400214 public void showLoadingWhenEmpty() {
215 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400216 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400217 }
218
Jason Monkb37e2882016-01-11 14:27:20 -0500219 public void setLoading(boolean loading, boolean animate) {
220 View loading_container = getView().findViewById(R.id.loading_container);
221 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
222 }
223
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700224 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700225 if (!mIsDataSetObserverRegistered) {
226 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400227 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700228 }
Jason Monk39b46742015-09-10 15:52:51 -0400229 mCurrentRootAdapter = getListView().getAdapter();
230 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700231 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500232 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700233 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700234 }
235
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700236 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700237 if (mIsDataSetObserverRegistered) {
238 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400239 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700241 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700242 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700243 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700244 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700245
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700247 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700248 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700249 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700250 }
251
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000252 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400253 highlightPreferenceIfNeeded();
254 updateEmptyView();
255 }
256
Jason Monk39b46742015-09-10 15:52:51 -0400257 public LayoutPreference getHeaderView() {
258 return mHeader;
259 }
260
261 public LayoutPreference getFooterView() {
262 return mFooter;
263 }
264
265 protected void setHeaderView(int resource) {
266 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800267 addPreferenceToTop(mHeader);
268 }
269
270 protected void setHeaderView(View view) {
271 mHeader = new LayoutPreference(getPrefContext(), view);
272 addPreferenceToTop(mHeader);
273 }
274
275 private void addPreferenceToTop(LayoutPreference preference) {
276 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400277 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800278 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400279 }
280 }
281
282 protected void setFooterView(int resource) {
283 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
284 }
285
286 protected void setFooterView(View v) {
287 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
288 }
289
290 private void setFooterView(LayoutPreference footer) {
291 if (getPreferenceScreen() != null && mFooter != null) {
292 getPreferenceScreen().removePreference(mFooter);
293 }
294 if (footer != null) {
295 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000296 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400297 if (getPreferenceScreen() != null) {
298 getPreferenceScreen().addPreference(mFooter);
299 }
300 } else {
301 mFooter = null;
302 }
303 }
304
305 @Override
306 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
307 super.setPreferenceScreen(preferenceScreen);
308 if (preferenceScreen != null) {
309 if (mHeader != null) {
310 preferenceScreen.addPreference(mHeader);
311 }
312 if (mFooter != null) {
313 preferenceScreen.addPreference(mFooter);
314 }
315 }
316 }
317
318 private void updateEmptyView() {
319 if (mEmptyView == null) return;
320 if (getPreferenceScreen() != null) {
321 boolean show = (getPreferenceScreen().getPreferenceCount()
322 - (mHeader != null ? 1 : 0)
323 - (mFooter != null ? 1 : 0)) <= 0;
324 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
325 } else {
326 mEmptyView.setVisibility(View.VISIBLE);
327 }
328 }
329
330 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000331 if (mEmptyView != null) {
332 mEmptyView.setVisibility(View.GONE);
333 }
Jason Monk39b46742015-09-10 15:52:51 -0400334 mEmptyView = v;
335 updateEmptyView();
336 }
337
338 public View getEmptyView() {
339 return mEmptyView;
340 }
341
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 /**
343 * Return a valid ListView position or -1 if none is found
344 */
345 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400346 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700347 return -1;
348 }
349
Jason Monk39b46742015-09-10 15:52:51 -0400350 RecyclerView listView = getListView();
351 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700352
353 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400354 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700355 }
356
357 return -1;
358 }
359
Jason Monk65bb0972015-12-17 10:39:44 -0500360 @Override
361 public RecyclerView.LayoutManager onCreateLayoutManager() {
362 mLayoutManager = new LinearLayoutManager(getContext());
363 return mLayoutManager;
364 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700365
Jason Monk65bb0972015-12-17 10:39:44 -0500366 @Override
367 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
368 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
369 return mAdapter;
370 }
371
Jason Monk2071eda2016-02-25 13:55:48 -0500372 protected void cacheRemoveAllPrefs(PreferenceGroup group) {
373 mPreferenceCache = new ArrayMap<String, Preference>();
374 final int N = group.getPreferenceCount();
375 for (int i = 0; i < N; i++) {
376 Preference p = group.getPreference(i);
377 if (TextUtils.isEmpty(p.getKey())) {
378 continue;
379 }
380 mPreferenceCache.put(p.getKey(), p);
381 }
382 }
383
384 protected Preference getCachedPreference(String key) {
385 return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
386 }
387
388 protected void removeCachedPrefs(PreferenceGroup group) {
389 for (Preference p : mPreferenceCache.values()) {
390 group.removePreference(p);
391 }
392 }
393
Jason Monk65bb0972015-12-17 10:39:44 -0500394 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700395 final int position = canUseListViewForHighLighting(key);
396 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700397 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500398 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700399
Jason Monk65bb0972015-12-17 10:39:44 -0500400 getView().postDelayed(new Runnable() {
401 @Override
402 public void run() {
403 mAdapter.highlight(position);
404 }
405 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700406 }
407 }
408
Jason Monk39b46742015-09-10 15:52:51 -0400409 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
410 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700411 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400412 final Preference preference = adapter.getItem(n);
413 final String preferenceKey = preference.getKey();
414 if (preferenceKey != null && preferenceKey.equals(key)) {
415 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700416 }
417 }
418 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700419 }
420
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700421 protected void removePreference(String key) {
422 Preference pref = findPreference(key);
423 if (pref != null) {
424 getPreferenceScreen().removePreference(pref);
425 }
426 }
427
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700428 /**
429 * Override this if you want to show a help item in the menu, by returning the resource id.
430 * @return the resource id for the help url
431 */
432 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400433 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700434 }
435
436 @Override
437 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400438 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400439 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700440 }
441 }
442
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700443 /*
444 * The name is intentionally made different from Activity#finish(), so that
445 * users won't misunderstand its meaning.
446 */
447 public final void finishFragment() {
448 getActivity().onBackPressed();
449 }
450
Amith Yamasanid7993472010-08-18 13:59:28 -0700451 // Some helpers for functions used by the settings fragments when they were activities
452
453 /**
454 * Returns the ContentResolver from the owning Activity.
455 */
456 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700457 Context context = getActivity();
458 if (context != null) {
459 mContentResolver = context.getContentResolver();
460 }
461 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700462 }
463
464 /**
465 * Returns the specified system service from the owning Activity.
466 */
467 protected Object getSystemService(final String name) {
468 return getActivity().getSystemService(name);
469 }
470
471 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700472 * Returns the PackageManager from the owning Activity.
473 */
474 protected PackageManager getPackageManager() {
475 return getActivity().getPackageManager();
476 }
477
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800478 @Override
479 public void onDetach() {
480 if (isRemoving()) {
481 if (mDialogFragment != null) {
482 mDialogFragment.dismiss();
483 mDialogFragment = null;
484 }
485 }
486 super.onDetach();
487 }
488
Amith Yamasanid7993472010-08-18 13:59:28 -0700489 // Dialog management
490
491 protected void showDialog(int dialogId) {
492 if (mDialogFragment != null) {
493 Log.e(TAG, "Old dialog fragment not null!");
494 }
495 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800496 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700497 }
498
499 public Dialog onCreateDialog(int dialogId) {
500 return null;
501 }
502
503 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800504 // mDialogFragment may not be visible yet in parent fragment's onResume().
505 // To be able to dismiss dialog at that time, don't check
506 // mDialogFragment.isVisible().
507 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700508 mDialogFragment.dismiss();
509 }
510 mDialogFragment = null;
511 }
512
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800513 /**
514 * Sets the OnCancelListener of the dialog shown. This method can only be
515 * called after showDialog(int) and before removeDialog(int). The method
516 * does nothing otherwise.
517 */
518 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
519 if (mDialogFragment != null) {
520 mDialogFragment.mOnCancelListener = listener;
521 }
522 }
523
524 /**
525 * Sets the OnDismissListener of the dialog shown. This method can only be
526 * called after showDialog(int) and before removeDialog(int). The method
527 * does nothing otherwise.
528 */
529 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
530 if (mDialogFragment != null) {
531 mDialogFragment.mOnDismissListener = listener;
532 }
533 }
534
Amith Yamasanic861cf82012-10-02 14:51:46 -0700535 public void onDialogShowing() {
536 // override in subclass to attach a dismiss listener, for instance
537 }
538
Jason Monk39b46742015-09-10 15:52:51 -0400539 @Override
540 public void onDisplayPreferenceDialog(Preference preference) {
541 if (preference.getKey() == null) {
542 // Auto-key preferences that don't have a key, so the dialog can find them.
543 preference.setKey(UUID.randomUUID().toString());
544 }
545 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000546 if (preference instanceof RestrictedListPreference) {
547 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
548 .newInstance(preference.getKey());
549 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400550 f = CustomListPreference.CustomListPreferenceDialogFragment
551 .newInstance(preference.getKey());
552 } else if (preference instanceof CustomDialogPreference) {
553 f = CustomDialogPreference.CustomPreferenceDialogFragment
554 .newInstance(preference.getKey());
555 } else if (preference instanceof CustomEditTextPreference) {
556 f = CustomEditTextPreference.CustomPreferenceDialogFragment
557 .newInstance(preference.getKey());
558 } else {
559 super.onDisplayPreferenceDialog(preference);
560 return;
561 }
562 f.setTargetFragment(this, 0);
563 f.show(getFragmentManager(), "dialog_preference");
564 onDialogShowing();
565 }
566
Amith Yamasani43c69782010-12-01 09:04:36 -0800567 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 private static final String KEY_DIALOG_ID = "key_dialog_id";
569 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
570
Amith Yamasanid7993472010-08-18 13:59:28 -0700571 private int mDialogId;
572
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800573 private Fragment mParentFragment;
574
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800575 private DialogInterface.OnCancelListener mOnCancelListener;
576 private DialogInterface.OnDismissListener mOnDismissListener;
577
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800578 public SettingsDialogFragment() {
579 /* do nothing */
580 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700581
Amith Yamasani43c69782010-12-01 09:04:36 -0800582 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700583 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800584 if (!(fragment instanceof Fragment)) {
585 throw new IllegalArgumentException("fragment argument must be an instance of "
586 + Fragment.class.getName());
587 }
588 mParentFragment = (Fragment) fragment;
589 }
590
591 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800592 public void onSaveInstanceState(Bundle outState) {
593 super.onSaveInstanceState(outState);
594 if (mParentFragment != null) {
595 outState.putInt(KEY_DIALOG_ID, mDialogId);
596 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
597 }
598 }
599
600 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700601 public void onStart() {
602 super.onStart();
603
604 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
605 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
606 }
607 }
608
609 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800610 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800611 if (savedInstanceState != null) {
612 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800613 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800614 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700615 if (mParentFragment == null) {
616 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
617 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800618 if (!(mParentFragment instanceof DialogCreatable)) {
619 throw new IllegalArgumentException(
620 (mParentFragment != null
621 ? mParentFragment.getClass().getName()
622 : mParentFragmentId)
623 + " must implement "
624 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800625 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800626 // This dialog fragment could be created from non-SettingsPreferenceFragment
627 if (mParentFragment instanceof SettingsPreferenceFragment) {
628 // restore mDialogFragment in mParentFragment
629 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
630 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800631 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800632 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700633 }
634
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800635 @Override
636 public void onCancel(DialogInterface dialog) {
637 super.onCancel(dialog);
638 if (mOnCancelListener != null) {
639 mOnCancelListener.onCancel(dialog);
640 }
641 }
642
643 @Override
644 public void onDismiss(DialogInterface dialog) {
645 super.onDismiss(dialog);
646 if (mOnDismissListener != null) {
647 mOnDismissListener.onDismiss(dialog);
648 }
649 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800650
Amith Yamasanid7993472010-08-18 13:59:28 -0700651 public int getDialogId() {
652 return mDialogId;
653 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800654
655 @Override
656 public void onDetach() {
657 super.onDetach();
658
Amith Yamasani8875ede2011-01-31 12:46:57 -0800659 // This dialog fragment could be created from non-SettingsPreferenceFragment
660 if (mParentFragment instanceof SettingsPreferenceFragment) {
661 // in case the dialog is not explicitly removed by removeDialog()
662 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
663 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
664 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800665 }
666 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700667 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700668
669 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800670 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700671 }
672
673 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800674 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700675 }
676
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700677 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700678 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500679 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800680 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700681 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700682 }
683
Jason Monk39b46742015-09-10 15:52:51 -0400684 protected final Context getPrefContext() {
685 return getPreferenceManager().getContext();
686 }
687
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700688 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
689 int requestCode, Bundle extras) {
690 final Activity activity = getActivity();
691 if (activity instanceof SettingsActivity) {
692 SettingsActivity sa = (SettingsActivity) activity;
693 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
694 return true;
695 } else if (activity instanceof PreferenceActivity) {
696 PreferenceActivity sa = (PreferenceActivity) activity;
697 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700698 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700699 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700700 Log.w(TAG,
701 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
702 + "launch the given Fragment (name: " + fragmentClass
703 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700704 return false;
705 }
706 }
Jason Monk65bb0972015-12-17 10:39:44 -0500707
708 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
709
710 private int mHighlightPosition = -1;
711
712 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
713 super(preferenceGroup);
714 }
715
716 public void highlight(int position) {
717 mHighlightPosition = position;
718 notifyDataSetChanged();
719 }
720
721 @Override
722 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
723 super.onBindViewHolder(holder, position);
724 if (position == mHighlightPosition) {
725 View v = holder.itemView;
726 if (v.getBackground() != null) {
727 final int centerX = v.getWidth() / 2;
728 final int centerY = v.getHeight() / 2;
729 v.getBackground().setHotspot(centerX, centerY);
730 }
731 v.setPressed(true);
732 v.setPressed(false);
733 mHighlightPosition = -1;
734 }
735 }
736 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700737}