blob: 49d72b48be6bcdd9c277799c52d058a97d190ad8 [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 Monk39b46742015-09-10 15:52:51 -040028import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050029import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.support.v7.preference.PreferenceGroupAdapter;
31import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050032import android.support.v7.preference.PreferenceViewHolder;
33import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040034import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070035import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070036import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070037import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070038import android.view.Menu;
39import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070040import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070041import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070042import android.widget.Button;
Jason Monk39b46742015-09-10 15:52:51 -040043import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040044import com.android.settings.widget.FloatingActionButton;
45
Jason Monk39b46742015-09-10 15:52:51 -040046import java.util.UUID;
47
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070048/**
Amith Yamasanid7993472010-08-18 13:59:28 -070049 * Base class for Settings fragments, with some helper functions and dialog management.
50 */
Chris Wren8a963ba2015-03-20 10:29:14 -040051public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
52 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070053
Anna Galusza0285c802016-01-29 17:32:19 -080054 /**
55 * The Help Uri Resource key. This can be passed as an extra argument when creating the
56 * Fragment.
57 **/
58 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
59
Jason Monk65bb0972015-12-17 10:39:44 -050060 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070061
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070062 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070063
64 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070065
Amith Yamasanid7993472010-08-18 13:59:28 -070066 private SettingsDialogFragment mDialogFragment;
67
Jason Monk23acc2b2015-04-14 15:06:39 -040068 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070069
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000070 private static final int ORDER_FIRST = -1;
71 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
72
Amith Yamasani350938e2013-04-09 10:22:47 -070073 // Cache the content resolver for async callbacks
74 private ContentResolver mContentResolver;
75
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070076 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070077 private boolean mPreferenceHighlighted = false;
78
Jason Monk39b46742015-09-10 15:52:51 -040079 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070080 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040081 private RecyclerView.AdapterDataObserver mDataSetObserver =
82 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070083 @Override
84 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040085 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070086 }
87 };
88
Fabrice Di Meglio86159282014-07-21 16:02:27 -070089 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040090 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090091 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070092
Jason Monk39b46742015-09-10 15:52:51 -040093 private LayoutPreference mHeader;
94
95 private LayoutPreference mFooter;
96 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050097 private LinearLayoutManager mLayoutManager;
98 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040099
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700100 @Override
101 public void onCreate(Bundle icicle) {
102 super.onCreate(icicle);
103
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700104 if (icicle != null) {
105 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
106 }
107
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700108 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800109 Bundle arguments = getArguments();
110 int helpResource;
111 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
112 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
113 } else {
114 helpResource = getHelpResource();
115 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700116 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400117 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700118 }
119 }
120
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700121 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700122 public View onCreateView(LayoutInflater inflater, ViewGroup container,
123 Bundle savedInstanceState) {
124 final View root = super.onCreateView(inflater, container, savedInstanceState);
125 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400126 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900127 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700128 return root;
129 }
130
Jason Monk39b46742015-09-10 15:52:51 -0400131 @Override
132 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
133 }
134
John Spurlockb8e02b82015-04-15 21:15:55 -0400135 public FloatingActionButton getFloatingActionButton() {
136 return mFloatingActionButton;
137 }
138
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900139 public ViewGroup getButtonBar() {
140 return mButtonBar;
141 }
142
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700143 public View setPinnedHeaderView(int layoutResId) {
144 final LayoutInflater inflater = getActivity().getLayoutInflater();
145 final View pinnedHeader =
146 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
147 setPinnedHeaderView(pinnedHeader);
148 return pinnedHeader;
149 }
150
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700151 public void setPinnedHeaderView(View pinnedHeader) {
152 mPinnedHeaderFrameLayout.addView(pinnedHeader);
153 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
154 }
155
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700156 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700157 public void onSaveInstanceState(Bundle outState) {
158 super.onSaveInstanceState(outState);
159
160 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
161 }
162
163 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700164 public void onActivityCreated(Bundle savedInstanceState) {
165 super.onActivityCreated(savedInstanceState);
Anna Galusza384fa2e2016-02-03 15:51:12 -0800166 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700167 setHasOptionsMenu(true);
168 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700169 }
170
171 @Override
172 public void onResume() {
173 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700174
175 final Bundle args = getArguments();
176 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700177 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
178 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700179 }
180 }
181
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700182 @Override
183 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700184 registerObserverIfNeeded();
185 }
186
187 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700188 protected void onUnbindPreferences() {
189 unregisterObserverIfNeeded();
190 }
191
Jason Monkb5aa73f2015-03-31 12:59:33 -0400192 public void showLoadingWhenEmpty() {
193 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400194 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400195 }
196
Jason Monkb37e2882016-01-11 14:27:20 -0500197 public void setLoading(boolean loading, boolean animate) {
198 View loading_container = getView().findViewById(R.id.loading_container);
199 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
200 }
201
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700202 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700203 if (!mIsDataSetObserverRegistered) {
204 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400205 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700206 }
Jason Monk39b46742015-09-10 15:52:51 -0400207 mCurrentRootAdapter = getListView().getAdapter();
208 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700209 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500210 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700211 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700212 }
213
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700214 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700215 if (mIsDataSetObserverRegistered) {
216 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400217 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700218 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700219 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700220 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700221 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700222 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700223
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700224 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700225 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700227 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700228 }
229
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000230 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400231 highlightPreferenceIfNeeded();
232 updateEmptyView();
233 }
234
Jason Monk39b46742015-09-10 15:52:51 -0400235 public LayoutPreference getHeaderView() {
236 return mHeader;
237 }
238
239 public LayoutPreference getFooterView() {
240 return mFooter;
241 }
242
243 protected void setHeaderView(int resource) {
244 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000245 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400246 if (getPreferenceScreen() != null) {
247 getPreferenceScreen().addPreference(mHeader);
248 }
249 }
250
251 protected void setFooterView(int resource) {
252 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
253 }
254
255 protected void setFooterView(View v) {
256 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
257 }
258
259 private void setFooterView(LayoutPreference footer) {
260 if (getPreferenceScreen() != null && mFooter != null) {
261 getPreferenceScreen().removePreference(mFooter);
262 }
263 if (footer != null) {
264 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000265 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400266 if (getPreferenceScreen() != null) {
267 getPreferenceScreen().addPreference(mFooter);
268 }
269 } else {
270 mFooter = null;
271 }
272 }
273
274 @Override
275 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
276 super.setPreferenceScreen(preferenceScreen);
277 if (preferenceScreen != null) {
278 if (mHeader != null) {
279 preferenceScreen.addPreference(mHeader);
280 }
281 if (mFooter != null) {
282 preferenceScreen.addPreference(mFooter);
283 }
284 }
285 }
286
287 private void updateEmptyView() {
288 if (mEmptyView == null) return;
289 if (getPreferenceScreen() != null) {
290 boolean show = (getPreferenceScreen().getPreferenceCount()
291 - (mHeader != null ? 1 : 0)
292 - (mFooter != null ? 1 : 0)) <= 0;
293 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
294 } else {
295 mEmptyView.setVisibility(View.VISIBLE);
296 }
297 }
298
299 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000300 if (mEmptyView != null) {
301 mEmptyView.setVisibility(View.GONE);
302 }
Jason Monk39b46742015-09-10 15:52:51 -0400303 mEmptyView = v;
304 updateEmptyView();
305 }
306
307 public View getEmptyView() {
308 return mEmptyView;
309 }
310
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700311 /**
312 * Return a valid ListView position or -1 if none is found
313 */
314 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400315 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700316 return -1;
317 }
318
Jason Monk39b46742015-09-10 15:52:51 -0400319 RecyclerView listView = getListView();
320 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700321
322 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400323 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700324 }
325
326 return -1;
327 }
328
Jason Monk65bb0972015-12-17 10:39:44 -0500329 @Override
330 public RecyclerView.LayoutManager onCreateLayoutManager() {
331 mLayoutManager = new LinearLayoutManager(getContext());
332 return mLayoutManager;
333 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700334
Jason Monk65bb0972015-12-17 10:39:44 -0500335 @Override
336 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
337 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
338 return mAdapter;
339 }
340
341 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 final int position = canUseListViewForHighLighting(key);
343 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700344 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500345 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700346
Jason Monk65bb0972015-12-17 10:39:44 -0500347 getView().postDelayed(new Runnable() {
348 @Override
349 public void run() {
350 mAdapter.highlight(position);
351 }
352 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 }
354 }
355
Jason Monk39b46742015-09-10 15:52:51 -0400356 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
357 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700358 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400359 final Preference preference = adapter.getItem(n);
360 final String preferenceKey = preference.getKey();
361 if (preferenceKey != null && preferenceKey.equals(key)) {
362 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700363 }
364 }
365 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700366 }
367
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700368 protected void removePreference(String key) {
369 Preference pref = findPreference(key);
370 if (pref != null) {
371 getPreferenceScreen().removePreference(pref);
372 }
373 }
374
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700375 /**
376 * Override this if you want to show a help item in the menu, by returning the resource id.
377 * @return the resource id for the help url
378 */
379 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400380 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700381 }
382
383 @Override
384 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400385 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400386 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700387 }
388 }
389
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700390 /*
391 * The name is intentionally made different from Activity#finish(), so that
392 * users won't misunderstand its meaning.
393 */
394 public final void finishFragment() {
395 getActivity().onBackPressed();
396 }
397
Amith Yamasanid7993472010-08-18 13:59:28 -0700398 // Some helpers for functions used by the settings fragments when they were activities
399
400 /**
401 * Returns the ContentResolver from the owning Activity.
402 */
403 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700404 Context context = getActivity();
405 if (context != null) {
406 mContentResolver = context.getContentResolver();
407 }
408 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700409 }
410
411 /**
412 * Returns the specified system service from the owning Activity.
413 */
414 protected Object getSystemService(final String name) {
415 return getActivity().getSystemService(name);
416 }
417
418 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700419 * Returns the PackageManager from the owning Activity.
420 */
421 protected PackageManager getPackageManager() {
422 return getActivity().getPackageManager();
423 }
424
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800425 @Override
426 public void onDetach() {
427 if (isRemoving()) {
428 if (mDialogFragment != null) {
429 mDialogFragment.dismiss();
430 mDialogFragment = null;
431 }
432 }
433 super.onDetach();
434 }
435
Amith Yamasanid7993472010-08-18 13:59:28 -0700436 // Dialog management
437
438 protected void showDialog(int dialogId) {
439 if (mDialogFragment != null) {
440 Log.e(TAG, "Old dialog fragment not null!");
441 }
442 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800443 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700444 }
445
446 public Dialog onCreateDialog(int dialogId) {
447 return null;
448 }
449
450 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800451 // mDialogFragment may not be visible yet in parent fragment's onResume().
452 // To be able to dismiss dialog at that time, don't check
453 // mDialogFragment.isVisible().
454 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700455 mDialogFragment.dismiss();
456 }
457 mDialogFragment = null;
458 }
459
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800460 /**
461 * Sets the OnCancelListener of the dialog shown. This method can only be
462 * called after showDialog(int) and before removeDialog(int). The method
463 * does nothing otherwise.
464 */
465 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
466 if (mDialogFragment != null) {
467 mDialogFragment.mOnCancelListener = listener;
468 }
469 }
470
471 /**
472 * Sets the OnDismissListener of the dialog shown. This method can only be
473 * called after showDialog(int) and before removeDialog(int). The method
474 * does nothing otherwise.
475 */
476 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
477 if (mDialogFragment != null) {
478 mDialogFragment.mOnDismissListener = listener;
479 }
480 }
481
Amith Yamasanic861cf82012-10-02 14:51:46 -0700482 public void onDialogShowing() {
483 // override in subclass to attach a dismiss listener, for instance
484 }
485
Jason Monk39b46742015-09-10 15:52:51 -0400486 @Override
487 public void onDisplayPreferenceDialog(Preference preference) {
488 if (preference.getKey() == null) {
489 // Auto-key preferences that don't have a key, so the dialog can find them.
490 preference.setKey(UUID.randomUUID().toString());
491 }
492 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000493 if (preference instanceof RestrictedListPreference) {
494 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
495 .newInstance(preference.getKey());
496 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400497 f = CustomListPreference.CustomListPreferenceDialogFragment
498 .newInstance(preference.getKey());
499 } else if (preference instanceof CustomDialogPreference) {
500 f = CustomDialogPreference.CustomPreferenceDialogFragment
501 .newInstance(preference.getKey());
502 } else if (preference instanceof CustomEditTextPreference) {
503 f = CustomEditTextPreference.CustomPreferenceDialogFragment
504 .newInstance(preference.getKey());
505 } else {
506 super.onDisplayPreferenceDialog(preference);
507 return;
508 }
509 f.setTargetFragment(this, 0);
510 f.show(getFragmentManager(), "dialog_preference");
511 onDialogShowing();
512 }
513
Amith Yamasani43c69782010-12-01 09:04:36 -0800514 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800515 private static final String KEY_DIALOG_ID = "key_dialog_id";
516 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
517
Amith Yamasanid7993472010-08-18 13:59:28 -0700518 private int mDialogId;
519
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800520 private Fragment mParentFragment;
521
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800522 private DialogInterface.OnCancelListener mOnCancelListener;
523 private DialogInterface.OnDismissListener mOnDismissListener;
524
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800525 public SettingsDialogFragment() {
526 /* do nothing */
527 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700528
Amith Yamasani43c69782010-12-01 09:04:36 -0800529 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700530 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800531 if (!(fragment instanceof Fragment)) {
532 throw new IllegalArgumentException("fragment argument must be an instance of "
533 + Fragment.class.getName());
534 }
535 mParentFragment = (Fragment) fragment;
536 }
537
538 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800539 public void onSaveInstanceState(Bundle outState) {
540 super.onSaveInstanceState(outState);
541 if (mParentFragment != null) {
542 outState.putInt(KEY_DIALOG_ID, mDialogId);
543 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
544 }
545 }
546
547 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700548 public void onStart() {
549 super.onStart();
550
551 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
552 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
553 }
554 }
555
556 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800557 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800558 if (savedInstanceState != null) {
559 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800560 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800561 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700562 if (mParentFragment == null) {
563 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
564 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800565 if (!(mParentFragment instanceof DialogCreatable)) {
566 throw new IllegalArgumentException(
567 (mParentFragment != null
568 ? mParentFragment.getClass().getName()
569 : mParentFragmentId)
570 + " must implement "
571 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800572 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800573 // This dialog fragment could be created from non-SettingsPreferenceFragment
574 if (mParentFragment instanceof SettingsPreferenceFragment) {
575 // restore mDialogFragment in mParentFragment
576 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
577 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800578 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800579 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700580 }
581
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800582 @Override
583 public void onCancel(DialogInterface dialog) {
584 super.onCancel(dialog);
585 if (mOnCancelListener != null) {
586 mOnCancelListener.onCancel(dialog);
587 }
588 }
589
590 @Override
591 public void onDismiss(DialogInterface dialog) {
592 super.onDismiss(dialog);
593 if (mOnDismissListener != null) {
594 mOnDismissListener.onDismiss(dialog);
595 }
596 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800597
Amith Yamasanid7993472010-08-18 13:59:28 -0700598 public int getDialogId() {
599 return mDialogId;
600 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800601
602 @Override
603 public void onDetach() {
604 super.onDetach();
605
Amith Yamasani8875ede2011-01-31 12:46:57 -0800606 // This dialog fragment could be created from non-SettingsPreferenceFragment
607 if (mParentFragment instanceof SettingsPreferenceFragment) {
608 // in case the dialog is not explicitly removed by removeDialog()
609 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
610 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
611 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800612 }
613 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700614 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700615
616 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800617 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700618 }
619
620 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800621 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700622 }
623
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700624 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700625 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500626 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800627 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700628 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700629 }
630
Jason Monk39b46742015-09-10 15:52:51 -0400631 protected final Context getPrefContext() {
632 return getPreferenceManager().getContext();
633 }
634
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700635 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
636 int requestCode, Bundle extras) {
637 final Activity activity = getActivity();
638 if (activity instanceof SettingsActivity) {
639 SettingsActivity sa = (SettingsActivity) activity;
640 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
641 return true;
642 } else if (activity instanceof PreferenceActivity) {
643 PreferenceActivity sa = (PreferenceActivity) activity;
644 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700645 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700646 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700647 Log.w(TAG,
648 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
649 + "launch the given Fragment (name: " + fragmentClass
650 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700651 return false;
652 }
653 }
Jason Monk65bb0972015-12-17 10:39:44 -0500654
655 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
656
657 private int mHighlightPosition = -1;
658
659 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
660 super(preferenceGroup);
661 }
662
663 public void highlight(int position) {
664 mHighlightPosition = position;
665 notifyDataSetChanged();
666 }
667
668 @Override
669 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
670 super.onBindViewHolder(holder, position);
671 if (position == mHighlightPosition) {
672 View v = holder.itemView;
673 if (v.getBackground() != null) {
674 final int centerX = v.getWidth() / 2;
675 final int centerY = v.getHeight() / 2;
676 v.getBackground().setHotspot(centerX, centerY);
677 }
678 v.setPressed(true);
679 v.setPressed(false);
680 mHighlightPosition = -1;
681 }
682 }
683 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700684}