blob: 9aebdbfb7cf76c0d1fbec4a7cb73f5c0249b2749 [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;
Anna Galuszab1795f52016-01-08 14:37:16 -080043import com.android.settings.accessibility.AccessibilitySettingsForSetupWizardActivity;
Jason Monk39b46742015-09-10 15:52:51 -040044import com.android.settings.applications.LayoutPreference;
John Spurlockb8e02b82015-04-15 21:15:55 -040045import com.android.settings.widget.FloatingActionButton;
46
Jason Monk39b46742015-09-10 15:52:51 -040047import java.util.UUID;
48
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070049/**
Amith Yamasanid7993472010-08-18 13:59:28 -070050 * Base class for Settings fragments, with some helper functions and dialog management.
51 */
Chris Wren8a963ba2015-03-20 10:29:14 -040052public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment
53 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070054
Anna Galusza0285c802016-01-29 17:32:19 -080055 /**
56 * The Help Uri Resource key. This can be passed as an extra argument when creating the
57 * Fragment.
58 **/
59 public static final String HELP_URI_RESOURCE_KEY = "help_uri_resource";
60
Jason Monk65bb0972015-12-17 10:39:44 -050061 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070062
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070063 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070064
65 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070066
Amith Yamasanid7993472010-08-18 13:59:28 -070067 private SettingsDialogFragment mDialogFragment;
68
Jason Monk23acc2b2015-04-14 15:06:39 -040069 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070070
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000071 private static final int ORDER_FIRST = -1;
72 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
73
Amith Yamasani350938e2013-04-09 10:22:47 -070074 // Cache the content resolver for async callbacks
75 private ContentResolver mContentResolver;
76
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070077 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070078 private boolean mPreferenceHighlighted = false;
79
Jason Monk39b46742015-09-10 15:52:51 -040080 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070081 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040082 private RecyclerView.AdapterDataObserver mDataSetObserver =
83 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070084 @Override
85 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040086 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070087 }
88 };
89
Fabrice Di Meglio86159282014-07-21 16:02:27 -070090 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040091 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090092 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070093
Jason Monk39b46742015-09-10 15:52:51 -040094 private LayoutPreference mHeader;
95
96 private LayoutPreference mFooter;
97 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050098 private LinearLayoutManager mLayoutManager;
99 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -0400100
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700101 @Override
102 public void onCreate(Bundle icicle) {
103 super.onCreate(icicle);
104
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700105 if (icicle != null) {
106 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
107 }
108
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700109 // Prepare help url and enable menu if necessary
Anna Galusza0285c802016-01-29 17:32:19 -0800110 Bundle arguments = getArguments();
111 int helpResource;
112 if (arguments != null && arguments.containsKey(HELP_URI_RESOURCE_KEY)) {
113 helpResource = arguments.getInt(HELP_URI_RESOURCE_KEY);
114 } else {
115 helpResource = getHelpResource();
116 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700117 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400118 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700119 }
120 }
121
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700122 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700123 public View onCreateView(LayoutInflater inflater, ViewGroup container,
124 Bundle savedInstanceState) {
125 final View root = super.onCreateView(inflater, container, savedInstanceState);
126 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400127 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900128 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700129 return root;
130 }
131
Jason Monk39b46742015-09-10 15:52:51 -0400132 @Override
133 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
134 }
135
John Spurlockb8e02b82015-04-15 21:15:55 -0400136 public FloatingActionButton getFloatingActionButton() {
137 return mFloatingActionButton;
138 }
139
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900140 public ViewGroup getButtonBar() {
141 return mButtonBar;
142 }
143
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700144 public View setPinnedHeaderView(int layoutResId) {
145 final LayoutInflater inflater = getActivity().getLayoutInflater();
146 final View pinnedHeader =
147 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
148 setPinnedHeaderView(pinnedHeader);
149 return pinnedHeader;
150 }
151
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700152 public void setPinnedHeaderView(View pinnedHeader) {
153 mPinnedHeaderFrameLayout.addView(pinnedHeader);
154 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
155 }
156
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700157 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700158 public void onSaveInstanceState(Bundle outState) {
159 super.onSaveInstanceState(outState);
160
161 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
162 }
163
164 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700165 public void onActivityCreated(Bundle savedInstanceState) {
166 super.onActivityCreated(savedInstanceState);
Anna Galuszab1795f52016-01-08 14:37:16 -0800167 if (!TextUtils.isEmpty(mHelpUri)
168 && !(getActivity() instanceof AccessibilitySettingsForSetupWizardActivity)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700169 setHasOptionsMenu(true);
170 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700171 }
172
173 @Override
174 public void onResume() {
175 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700176
177 final Bundle args = getArguments();
178 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700179 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
180 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700181 }
182 }
183
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700184 @Override
185 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700186 registerObserverIfNeeded();
187 }
188
189 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700190 protected void onUnbindPreferences() {
191 unregisterObserverIfNeeded();
192 }
193
Jason Monkb5aa73f2015-03-31 12:59:33 -0400194 public void showLoadingWhenEmpty() {
195 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400196 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400197 }
198
Jason Monkb37e2882016-01-11 14:27:20 -0500199 public void setLoading(boolean loading, boolean animate) {
200 View loading_container = getView().findViewById(R.id.loading_container);
201 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
202 }
203
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700204 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700205 if (!mIsDataSetObserverRegistered) {
206 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400207 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700208 }
Jason Monk39b46742015-09-10 15:52:51 -0400209 mCurrentRootAdapter = getListView().getAdapter();
210 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700211 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500212 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700213 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700214 }
215
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700216 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700217 if (mIsDataSetObserverRegistered) {
218 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400219 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700220 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700221 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700222 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700223 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700224 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700225
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700226 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700227 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700228 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700229 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700230 }
231
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000232 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400233 highlightPreferenceIfNeeded();
234 updateEmptyView();
235 }
236
Jason Monk39b46742015-09-10 15:52:51 -0400237 public LayoutPreference getHeaderView() {
238 return mHeader;
239 }
240
241 public LayoutPreference getFooterView() {
242 return mFooter;
243 }
244
245 protected void setHeaderView(int resource) {
246 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000247 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400248 if (getPreferenceScreen() != null) {
249 getPreferenceScreen().addPreference(mHeader);
250 }
251 }
252
253 protected void setFooterView(int resource) {
254 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
255 }
256
257 protected void setFooterView(View v) {
258 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
259 }
260
261 private void setFooterView(LayoutPreference footer) {
262 if (getPreferenceScreen() != null && mFooter != null) {
263 getPreferenceScreen().removePreference(mFooter);
264 }
265 if (footer != null) {
266 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000267 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400268 if (getPreferenceScreen() != null) {
269 getPreferenceScreen().addPreference(mFooter);
270 }
271 } else {
272 mFooter = null;
273 }
274 }
275
276 @Override
277 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
278 super.setPreferenceScreen(preferenceScreen);
279 if (preferenceScreen != null) {
280 if (mHeader != null) {
281 preferenceScreen.addPreference(mHeader);
282 }
283 if (mFooter != null) {
284 preferenceScreen.addPreference(mFooter);
285 }
286 }
287 }
288
289 private void updateEmptyView() {
290 if (mEmptyView == null) return;
291 if (getPreferenceScreen() != null) {
292 boolean show = (getPreferenceScreen().getPreferenceCount()
293 - (mHeader != null ? 1 : 0)
294 - (mFooter != null ? 1 : 0)) <= 0;
295 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
296 } else {
297 mEmptyView.setVisibility(View.VISIBLE);
298 }
299 }
300
301 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000302 if (mEmptyView != null) {
303 mEmptyView.setVisibility(View.GONE);
304 }
Jason Monk39b46742015-09-10 15:52:51 -0400305 mEmptyView = v;
306 updateEmptyView();
307 }
308
309 public View getEmptyView() {
310 return mEmptyView;
311 }
312
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700313 /**
314 * Return a valid ListView position or -1 if none is found
315 */
316 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400317 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700318 return -1;
319 }
320
Jason Monk39b46742015-09-10 15:52:51 -0400321 RecyclerView listView = getListView();
322 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700323
324 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400325 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700326 }
327
328 return -1;
329 }
330
Jason Monk65bb0972015-12-17 10:39:44 -0500331 @Override
332 public RecyclerView.LayoutManager onCreateLayoutManager() {
333 mLayoutManager = new LinearLayoutManager(getContext());
334 return mLayoutManager;
335 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700336
Jason Monk65bb0972015-12-17 10:39:44 -0500337 @Override
338 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
339 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
340 return mAdapter;
341 }
342
343 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700344 final int position = canUseListViewForHighLighting(key);
345 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700346 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500347 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700348
Jason Monk65bb0972015-12-17 10:39:44 -0500349 getView().postDelayed(new Runnable() {
350 @Override
351 public void run() {
352 mAdapter.highlight(position);
353 }
354 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700355 }
356 }
357
Jason Monk39b46742015-09-10 15:52:51 -0400358 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
359 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700360 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400361 final Preference preference = adapter.getItem(n);
362 final String preferenceKey = preference.getKey();
363 if (preferenceKey != null && preferenceKey.equals(key)) {
364 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700365 }
366 }
367 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700368 }
369
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700370 protected void removePreference(String key) {
371 Preference pref = findPreference(key);
372 if (pref != null) {
373 getPreferenceScreen().removePreference(pref);
374 }
375 }
376
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700377 /**
378 * Override this if you want to show a help item in the menu, by returning the resource id.
379 * @return the resource id for the help url
380 */
381 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400382 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700383 }
384
385 @Override
386 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400387 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400388 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700389 }
390 }
391
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700392 /*
393 * The name is intentionally made different from Activity#finish(), so that
394 * users won't misunderstand its meaning.
395 */
396 public final void finishFragment() {
397 getActivity().onBackPressed();
398 }
399
Amith Yamasanid7993472010-08-18 13:59:28 -0700400 // Some helpers for functions used by the settings fragments when they were activities
401
402 /**
403 * Returns the ContentResolver from the owning Activity.
404 */
405 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700406 Context context = getActivity();
407 if (context != null) {
408 mContentResolver = context.getContentResolver();
409 }
410 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700411 }
412
413 /**
414 * Returns the specified system service from the owning Activity.
415 */
416 protected Object getSystemService(final String name) {
417 return getActivity().getSystemService(name);
418 }
419
420 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700421 * Returns the PackageManager from the owning Activity.
422 */
423 protected PackageManager getPackageManager() {
424 return getActivity().getPackageManager();
425 }
426
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800427 @Override
428 public void onDetach() {
429 if (isRemoving()) {
430 if (mDialogFragment != null) {
431 mDialogFragment.dismiss();
432 mDialogFragment = null;
433 }
434 }
435 super.onDetach();
436 }
437
Amith Yamasanid7993472010-08-18 13:59:28 -0700438 // Dialog management
439
440 protected void showDialog(int dialogId) {
441 if (mDialogFragment != null) {
442 Log.e(TAG, "Old dialog fragment not null!");
443 }
444 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800445 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700446 }
447
448 public Dialog onCreateDialog(int dialogId) {
449 return null;
450 }
451
452 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800453 // mDialogFragment may not be visible yet in parent fragment's onResume().
454 // To be able to dismiss dialog at that time, don't check
455 // mDialogFragment.isVisible().
456 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700457 mDialogFragment.dismiss();
458 }
459 mDialogFragment = null;
460 }
461
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800462 /**
463 * Sets the OnCancelListener of the dialog shown. This method can only be
464 * called after showDialog(int) and before removeDialog(int). The method
465 * does nothing otherwise.
466 */
467 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
468 if (mDialogFragment != null) {
469 mDialogFragment.mOnCancelListener = listener;
470 }
471 }
472
473 /**
474 * Sets the OnDismissListener of the dialog shown. This method can only be
475 * called after showDialog(int) and before removeDialog(int). The method
476 * does nothing otherwise.
477 */
478 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
479 if (mDialogFragment != null) {
480 mDialogFragment.mOnDismissListener = listener;
481 }
482 }
483
Amith Yamasanic861cf82012-10-02 14:51:46 -0700484 public void onDialogShowing() {
485 // override in subclass to attach a dismiss listener, for instance
486 }
487
Jason Monk39b46742015-09-10 15:52:51 -0400488 @Override
489 public void onDisplayPreferenceDialog(Preference preference) {
490 if (preference.getKey() == null) {
491 // Auto-key preferences that don't have a key, so the dialog can find them.
492 preference.setKey(UUID.randomUUID().toString());
493 }
494 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000495 if (preference instanceof RestrictedListPreference) {
496 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
497 .newInstance(preference.getKey());
498 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400499 f = CustomListPreference.CustomListPreferenceDialogFragment
500 .newInstance(preference.getKey());
501 } else if (preference instanceof CustomDialogPreference) {
502 f = CustomDialogPreference.CustomPreferenceDialogFragment
503 .newInstance(preference.getKey());
504 } else if (preference instanceof CustomEditTextPreference) {
505 f = CustomEditTextPreference.CustomPreferenceDialogFragment
506 .newInstance(preference.getKey());
507 } else {
508 super.onDisplayPreferenceDialog(preference);
509 return;
510 }
511 f.setTargetFragment(this, 0);
512 f.show(getFragmentManager(), "dialog_preference");
513 onDialogShowing();
514 }
515
Amith Yamasani43c69782010-12-01 09:04:36 -0800516 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800517 private static final String KEY_DIALOG_ID = "key_dialog_id";
518 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
519
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 private int mDialogId;
521
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800522 private Fragment mParentFragment;
523
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800524 private DialogInterface.OnCancelListener mOnCancelListener;
525 private DialogInterface.OnDismissListener mOnDismissListener;
526
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800527 public SettingsDialogFragment() {
528 /* do nothing */
529 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700530
Amith Yamasani43c69782010-12-01 09:04:36 -0800531 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700532 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800533 if (!(fragment instanceof Fragment)) {
534 throw new IllegalArgumentException("fragment argument must be an instance of "
535 + Fragment.class.getName());
536 }
537 mParentFragment = (Fragment) fragment;
538 }
539
540 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800541 public void onSaveInstanceState(Bundle outState) {
542 super.onSaveInstanceState(outState);
543 if (mParentFragment != null) {
544 outState.putInt(KEY_DIALOG_ID, mDialogId);
545 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
546 }
547 }
548
549 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700550 public void onStart() {
551 super.onStart();
552
553 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
554 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
555 }
556 }
557
558 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800559 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800560 if (savedInstanceState != null) {
561 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800562 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800563 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700564 if (mParentFragment == null) {
565 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
566 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800567 if (!(mParentFragment instanceof DialogCreatable)) {
568 throw new IllegalArgumentException(
569 (mParentFragment != null
570 ? mParentFragment.getClass().getName()
571 : mParentFragmentId)
572 + " must implement "
573 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800574 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800575 // This dialog fragment could be created from non-SettingsPreferenceFragment
576 if (mParentFragment instanceof SettingsPreferenceFragment) {
577 // restore mDialogFragment in mParentFragment
578 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
579 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800580 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800581 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700582 }
583
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800584 @Override
585 public void onCancel(DialogInterface dialog) {
586 super.onCancel(dialog);
587 if (mOnCancelListener != null) {
588 mOnCancelListener.onCancel(dialog);
589 }
590 }
591
592 @Override
593 public void onDismiss(DialogInterface dialog) {
594 super.onDismiss(dialog);
595 if (mOnDismissListener != null) {
596 mOnDismissListener.onDismiss(dialog);
597 }
598 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800599
Amith Yamasanid7993472010-08-18 13:59:28 -0700600 public int getDialogId() {
601 return mDialogId;
602 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800603
604 @Override
605 public void onDetach() {
606 super.onDetach();
607
Amith Yamasani8875ede2011-01-31 12:46:57 -0800608 // This dialog fragment could be created from non-SettingsPreferenceFragment
609 if (mParentFragment instanceof SettingsPreferenceFragment) {
610 // in case the dialog is not explicitly removed by removeDialog()
611 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
612 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
613 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800614 }
615 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700616 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700617
618 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800619 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700620 }
621
622 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800623 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700624 }
625
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700626 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700627 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500628 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800629 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700630 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700631 }
632
Jason Monk39b46742015-09-10 15:52:51 -0400633 protected final Context getPrefContext() {
634 return getPreferenceManager().getContext();
635 }
636
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700637 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
638 int requestCode, Bundle extras) {
639 final Activity activity = getActivity();
640 if (activity instanceof SettingsActivity) {
641 SettingsActivity sa = (SettingsActivity) activity;
642 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
643 return true;
644 } else if (activity instanceof PreferenceActivity) {
645 PreferenceActivity sa = (PreferenceActivity) activity;
646 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700647 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700648 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700649 Log.w(TAG,
650 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
651 + "launch the given Fragment (name: " + fragmentClass
652 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700653 return false;
654 }
655 }
Jason Monk65bb0972015-12-17 10:39:44 -0500656
657 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
658
659 private int mHighlightPosition = -1;
660
661 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
662 super(preferenceGroup);
663 }
664
665 public void highlight(int position) {
666 mHighlightPosition = position;
667 notifyDataSetChanged();
668 }
669
670 @Override
671 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
672 super.onBindViewHolder(holder, position);
673 if (position == mHighlightPosition) {
674 View v = holder.itemView;
675 if (v.getBackground() != null) {
676 final int centerX = v.getWidth() / 2;
677 final int centerY = v.getHeight() / 2;
678 v.getBackground().setHotspot(centerX, centerY);
679 }
680 v.setPressed(true);
681 v.setPressed(false);
682 mHighlightPosition = -1;
683 }
684 }
685 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700686}