blob: e871c82b0b34d66349701c1b8b4a0df9cd5305a1 [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);
Udam Sainid553abc2016-02-16 17:54:13 -0800245 addPreferenceToTop(mHeader);
246 }
247
248 protected void setHeaderView(View view) {
249 mHeader = new LayoutPreference(getPrefContext(), view);
250 addPreferenceToTop(mHeader);
251 }
252
253 private void addPreferenceToTop(LayoutPreference preference) {
254 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400255 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800256 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400257 }
258 }
259
260 protected void setFooterView(int resource) {
261 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
262 }
263
264 protected void setFooterView(View v) {
265 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
266 }
267
268 private void setFooterView(LayoutPreference footer) {
269 if (getPreferenceScreen() != null && mFooter != null) {
270 getPreferenceScreen().removePreference(mFooter);
271 }
272 if (footer != null) {
273 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000274 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400275 if (getPreferenceScreen() != null) {
276 getPreferenceScreen().addPreference(mFooter);
277 }
278 } else {
279 mFooter = null;
280 }
281 }
282
283 @Override
284 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
285 super.setPreferenceScreen(preferenceScreen);
286 if (preferenceScreen != null) {
287 if (mHeader != null) {
288 preferenceScreen.addPreference(mHeader);
289 }
290 if (mFooter != null) {
291 preferenceScreen.addPreference(mFooter);
292 }
293 }
294 }
295
296 private void updateEmptyView() {
297 if (mEmptyView == null) return;
298 if (getPreferenceScreen() != null) {
299 boolean show = (getPreferenceScreen().getPreferenceCount()
300 - (mHeader != null ? 1 : 0)
301 - (mFooter != null ? 1 : 0)) <= 0;
302 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
303 } else {
304 mEmptyView.setVisibility(View.VISIBLE);
305 }
306 }
307
308 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000309 if (mEmptyView != null) {
310 mEmptyView.setVisibility(View.GONE);
311 }
Jason Monk39b46742015-09-10 15:52:51 -0400312 mEmptyView = v;
313 updateEmptyView();
314 }
315
316 public View getEmptyView() {
317 return mEmptyView;
318 }
319
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700320 /**
321 * Return a valid ListView position or -1 if none is found
322 */
323 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400324 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700325 return -1;
326 }
327
Jason Monk39b46742015-09-10 15:52:51 -0400328 RecyclerView listView = getListView();
329 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700330
331 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400332 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700333 }
334
335 return -1;
336 }
337
Jason Monk65bb0972015-12-17 10:39:44 -0500338 @Override
339 public RecyclerView.LayoutManager onCreateLayoutManager() {
340 mLayoutManager = new LinearLayoutManager(getContext());
341 return mLayoutManager;
342 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343
Jason Monk65bb0972015-12-17 10:39:44 -0500344 @Override
345 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
346 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
347 return mAdapter;
348 }
349
350 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700351 final int position = canUseListViewForHighLighting(key);
352 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700353 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500354 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700355
Jason Monk65bb0972015-12-17 10:39:44 -0500356 getView().postDelayed(new Runnable() {
357 @Override
358 public void run() {
359 mAdapter.highlight(position);
360 }
361 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700362 }
363 }
364
Jason Monk39b46742015-09-10 15:52:51 -0400365 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
366 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700367 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400368 final Preference preference = adapter.getItem(n);
369 final String preferenceKey = preference.getKey();
370 if (preferenceKey != null && preferenceKey.equals(key)) {
371 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700372 }
373 }
374 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700375 }
376
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700377 protected void removePreference(String key) {
378 Preference pref = findPreference(key);
379 if (pref != null) {
380 getPreferenceScreen().removePreference(pref);
381 }
382 }
383
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700384 /**
385 * Override this if you want to show a help item in the menu, by returning the resource id.
386 * @return the resource id for the help url
387 */
388 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400389 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700390 }
391
392 @Override
393 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400394 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400395 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700396 }
397 }
398
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700399 /*
400 * The name is intentionally made different from Activity#finish(), so that
401 * users won't misunderstand its meaning.
402 */
403 public final void finishFragment() {
404 getActivity().onBackPressed();
405 }
406
Amith Yamasanid7993472010-08-18 13:59:28 -0700407 // Some helpers for functions used by the settings fragments when they were activities
408
409 /**
410 * Returns the ContentResolver from the owning Activity.
411 */
412 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700413 Context context = getActivity();
414 if (context != null) {
415 mContentResolver = context.getContentResolver();
416 }
417 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700418 }
419
420 /**
421 * Returns the specified system service from the owning Activity.
422 */
423 protected Object getSystemService(final String name) {
424 return getActivity().getSystemService(name);
425 }
426
427 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700428 * Returns the PackageManager from the owning Activity.
429 */
430 protected PackageManager getPackageManager() {
431 return getActivity().getPackageManager();
432 }
433
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800434 @Override
435 public void onDetach() {
436 if (isRemoving()) {
437 if (mDialogFragment != null) {
438 mDialogFragment.dismiss();
439 mDialogFragment = null;
440 }
441 }
442 super.onDetach();
443 }
444
Amith Yamasanid7993472010-08-18 13:59:28 -0700445 // Dialog management
446
447 protected void showDialog(int dialogId) {
448 if (mDialogFragment != null) {
449 Log.e(TAG, "Old dialog fragment not null!");
450 }
451 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800452 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700453 }
454
455 public Dialog onCreateDialog(int dialogId) {
456 return null;
457 }
458
459 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800460 // mDialogFragment may not be visible yet in parent fragment's onResume().
461 // To be able to dismiss dialog at that time, don't check
462 // mDialogFragment.isVisible().
463 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700464 mDialogFragment.dismiss();
465 }
466 mDialogFragment = null;
467 }
468
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800469 /**
470 * Sets the OnCancelListener of the dialog shown. This method can only be
471 * called after showDialog(int) and before removeDialog(int). The method
472 * does nothing otherwise.
473 */
474 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
475 if (mDialogFragment != null) {
476 mDialogFragment.mOnCancelListener = listener;
477 }
478 }
479
480 /**
481 * Sets the OnDismissListener of the dialog shown. This method can only be
482 * called after showDialog(int) and before removeDialog(int). The method
483 * does nothing otherwise.
484 */
485 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
486 if (mDialogFragment != null) {
487 mDialogFragment.mOnDismissListener = listener;
488 }
489 }
490
Amith Yamasanic861cf82012-10-02 14:51:46 -0700491 public void onDialogShowing() {
492 // override in subclass to attach a dismiss listener, for instance
493 }
494
Jason Monk39b46742015-09-10 15:52:51 -0400495 @Override
496 public void onDisplayPreferenceDialog(Preference preference) {
497 if (preference.getKey() == null) {
498 // Auto-key preferences that don't have a key, so the dialog can find them.
499 preference.setKey(UUID.randomUUID().toString());
500 }
501 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000502 if (preference instanceof RestrictedListPreference) {
503 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
504 .newInstance(preference.getKey());
505 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400506 f = CustomListPreference.CustomListPreferenceDialogFragment
507 .newInstance(preference.getKey());
508 } else if (preference instanceof CustomDialogPreference) {
509 f = CustomDialogPreference.CustomPreferenceDialogFragment
510 .newInstance(preference.getKey());
511 } else if (preference instanceof CustomEditTextPreference) {
512 f = CustomEditTextPreference.CustomPreferenceDialogFragment
513 .newInstance(preference.getKey());
514 } else {
515 super.onDisplayPreferenceDialog(preference);
516 return;
517 }
518 f.setTargetFragment(this, 0);
519 f.show(getFragmentManager(), "dialog_preference");
520 onDialogShowing();
521 }
522
Amith Yamasani43c69782010-12-01 09:04:36 -0800523 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800524 private static final String KEY_DIALOG_ID = "key_dialog_id";
525 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
526
Amith Yamasanid7993472010-08-18 13:59:28 -0700527 private int mDialogId;
528
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800529 private Fragment mParentFragment;
530
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800531 private DialogInterface.OnCancelListener mOnCancelListener;
532 private DialogInterface.OnDismissListener mOnDismissListener;
533
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800534 public SettingsDialogFragment() {
535 /* do nothing */
536 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700537
Amith Yamasani43c69782010-12-01 09:04:36 -0800538 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700539 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800540 if (!(fragment instanceof Fragment)) {
541 throw new IllegalArgumentException("fragment argument must be an instance of "
542 + Fragment.class.getName());
543 }
544 mParentFragment = (Fragment) fragment;
545 }
546
547 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800548 public void onSaveInstanceState(Bundle outState) {
549 super.onSaveInstanceState(outState);
550 if (mParentFragment != null) {
551 outState.putInt(KEY_DIALOG_ID, mDialogId);
552 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
553 }
554 }
555
556 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700557 public void onStart() {
558 super.onStart();
559
560 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
561 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
562 }
563 }
564
565 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800566 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800567 if (savedInstanceState != null) {
568 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800569 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800570 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700571 if (mParentFragment == null) {
572 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
573 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800574 if (!(mParentFragment instanceof DialogCreatable)) {
575 throw new IllegalArgumentException(
576 (mParentFragment != null
577 ? mParentFragment.getClass().getName()
578 : mParentFragmentId)
579 + " must implement "
580 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800581 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800582 // This dialog fragment could be created from non-SettingsPreferenceFragment
583 if (mParentFragment instanceof SettingsPreferenceFragment) {
584 // restore mDialogFragment in mParentFragment
585 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
586 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800588 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700589 }
590
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800591 @Override
592 public void onCancel(DialogInterface dialog) {
593 super.onCancel(dialog);
594 if (mOnCancelListener != null) {
595 mOnCancelListener.onCancel(dialog);
596 }
597 }
598
599 @Override
600 public void onDismiss(DialogInterface dialog) {
601 super.onDismiss(dialog);
602 if (mOnDismissListener != null) {
603 mOnDismissListener.onDismiss(dialog);
604 }
605 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800606
Amith Yamasanid7993472010-08-18 13:59:28 -0700607 public int getDialogId() {
608 return mDialogId;
609 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800610
611 @Override
612 public void onDetach() {
613 super.onDetach();
614
Amith Yamasani8875ede2011-01-31 12:46:57 -0800615 // This dialog fragment could be created from non-SettingsPreferenceFragment
616 if (mParentFragment instanceof SettingsPreferenceFragment) {
617 // in case the dialog is not explicitly removed by removeDialog()
618 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
619 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
620 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800621 }
622 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700623 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700624
625 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800626 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700627 }
628
629 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800630 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700631 }
632
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700633 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700634 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500635 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800636 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700637 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700638 }
639
Jason Monk39b46742015-09-10 15:52:51 -0400640 protected final Context getPrefContext() {
641 return getPreferenceManager().getContext();
642 }
643
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700644 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
645 int requestCode, Bundle extras) {
646 final Activity activity = getActivity();
647 if (activity instanceof SettingsActivity) {
648 SettingsActivity sa = (SettingsActivity) activity;
649 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
650 return true;
651 } else if (activity instanceof PreferenceActivity) {
652 PreferenceActivity sa = (PreferenceActivity) activity;
653 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700654 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700655 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700656 Log.w(TAG,
657 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
658 + "launch the given Fragment (name: " + fragmentClass
659 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700660 return false;
661 }
662 }
Jason Monk65bb0972015-12-17 10:39:44 -0500663
664 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
665
666 private int mHighlightPosition = -1;
667
668 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
669 super(preferenceGroup);
670 }
671
672 public void highlight(int position) {
673 mHighlightPosition = position;
674 notifyDataSetChanged();
675 }
676
677 @Override
678 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
679 super.onBindViewHolder(holder, position);
680 if (position == mHighlightPosition) {
681 View v = holder.itemView;
682 if (v.getBackground() != null) {
683 final int centerX = v.getWidth() / 2;
684 final int centerY = v.getHeight() / 2;
685 v.getBackground().setHotspot(centerX, centerY);
686 }
687 v.setPressed(true);
688 v.setPressed(false);
689 mHighlightPosition = -1;
690 }
691 }
692 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700693}