blob: 66d45de58ac00231db7b591538a4d426d4f93755 [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;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700212 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700213 }
214
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700215 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700216 if (mIsDataSetObserverRegistered) {
217 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400218 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700219 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700220 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700221 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700222 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700223 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700224
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700225 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700226 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700227 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700228 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700229 }
230
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000231 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400232 highlightPreferenceIfNeeded();
233 updateEmptyView();
234 }
235
Jason Monk39b46742015-09-10 15:52:51 -0400236 public LayoutPreference getHeaderView() {
237 return mHeader;
238 }
239
240 public LayoutPreference getFooterView() {
241 return mFooter;
242 }
243
244 protected void setHeaderView(int resource) {
245 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000246 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400247 if (getPreferenceScreen() != null) {
248 getPreferenceScreen().addPreference(mHeader);
249 }
250 }
251
252 protected void setFooterView(int resource) {
253 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
254 }
255
256 protected void setFooterView(View v) {
257 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
258 }
259
260 private void setFooterView(LayoutPreference footer) {
261 if (getPreferenceScreen() != null && mFooter != null) {
262 getPreferenceScreen().removePreference(mFooter);
263 }
264 if (footer != null) {
265 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000266 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400267 if (getPreferenceScreen() != null) {
268 getPreferenceScreen().addPreference(mFooter);
269 }
270 } else {
271 mFooter = null;
272 }
273 }
274
275 @Override
276 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
277 super.setPreferenceScreen(preferenceScreen);
278 if (preferenceScreen != null) {
279 if (mHeader != null) {
280 preferenceScreen.addPreference(mHeader);
281 }
282 if (mFooter != null) {
283 preferenceScreen.addPreference(mFooter);
284 }
285 }
286 }
287
288 private void updateEmptyView() {
289 if (mEmptyView == null) return;
290 if (getPreferenceScreen() != null) {
291 boolean show = (getPreferenceScreen().getPreferenceCount()
292 - (mHeader != null ? 1 : 0)
293 - (mFooter != null ? 1 : 0)) <= 0;
294 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
295 } else {
296 mEmptyView.setVisibility(View.VISIBLE);
297 }
298 }
299
300 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000301 if (mEmptyView != null) {
302 mEmptyView.setVisibility(View.GONE);
303 }
Jason Monk39b46742015-09-10 15:52:51 -0400304 mEmptyView = v;
305 updateEmptyView();
306 }
307
308 public View getEmptyView() {
309 return mEmptyView;
310 }
311
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700312 /**
313 * Return a valid ListView position or -1 if none is found
314 */
315 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400316 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700317 return -1;
318 }
319
Jason Monk39b46742015-09-10 15:52:51 -0400320 RecyclerView listView = getListView();
321 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700322
323 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400324 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700325 }
326
327 return -1;
328 }
329
Jason Monk65bb0972015-12-17 10:39:44 -0500330 @Override
331 public RecyclerView.LayoutManager onCreateLayoutManager() {
332 mLayoutManager = new LinearLayoutManager(getContext());
333 return mLayoutManager;
334 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700335
Jason Monk65bb0972015-12-17 10:39:44 -0500336 @Override
337 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
338 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
339 return mAdapter;
340 }
341
342 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 final int position = canUseListViewForHighLighting(key);
344 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700345 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500346 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700347
Jason Monk65bb0972015-12-17 10:39:44 -0500348 getView().postDelayed(new Runnable() {
349 @Override
350 public void run() {
351 mAdapter.highlight(position);
352 }
353 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700354 }
355 }
356
Jason Monk39b46742015-09-10 15:52:51 -0400357 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
358 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700359 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400360 final Preference preference = adapter.getItem(n);
361 final String preferenceKey = preference.getKey();
362 if (preferenceKey != null && preferenceKey.equals(key)) {
363 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700364 }
365 }
366 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700367 }
368
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700369 protected void removePreference(String key) {
370 Preference pref = findPreference(key);
371 if (pref != null) {
372 getPreferenceScreen().removePreference(pref);
373 }
374 }
375
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700376 /**
377 * Override this if you want to show a help item in the menu, by returning the resource id.
378 * @return the resource id for the help url
379 */
380 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400381 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700382 }
383
384 @Override
385 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400386 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400387 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700388 }
389 }
390
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700391 /*
392 * The name is intentionally made different from Activity#finish(), so that
393 * users won't misunderstand its meaning.
394 */
395 public final void finishFragment() {
396 getActivity().onBackPressed();
397 }
398
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 // Some helpers for functions used by the settings fragments when they were activities
400
401 /**
402 * Returns the ContentResolver from the owning Activity.
403 */
404 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700405 Context context = getActivity();
406 if (context != null) {
407 mContentResolver = context.getContentResolver();
408 }
409 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700410 }
411
412 /**
413 * Returns the specified system service from the owning Activity.
414 */
415 protected Object getSystemService(final String name) {
416 return getActivity().getSystemService(name);
417 }
418
419 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700420 * Returns the PackageManager from the owning Activity.
421 */
422 protected PackageManager getPackageManager() {
423 return getActivity().getPackageManager();
424 }
425
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800426 @Override
427 public void onDetach() {
428 if (isRemoving()) {
429 if (mDialogFragment != null) {
430 mDialogFragment.dismiss();
431 mDialogFragment = null;
432 }
433 }
434 super.onDetach();
435 }
436
Amith Yamasanid7993472010-08-18 13:59:28 -0700437 // Dialog management
438
439 protected void showDialog(int dialogId) {
440 if (mDialogFragment != null) {
441 Log.e(TAG, "Old dialog fragment not null!");
442 }
443 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800444 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700445 }
446
447 public Dialog onCreateDialog(int dialogId) {
448 return null;
449 }
450
451 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800452 // mDialogFragment may not be visible yet in parent fragment's onResume().
453 // To be able to dismiss dialog at that time, don't check
454 // mDialogFragment.isVisible().
455 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700456 mDialogFragment.dismiss();
457 }
458 mDialogFragment = null;
459 }
460
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800461 /**
462 * Sets the OnCancelListener of the dialog shown. This method can only be
463 * called after showDialog(int) and before removeDialog(int). The method
464 * does nothing otherwise.
465 */
466 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
467 if (mDialogFragment != null) {
468 mDialogFragment.mOnCancelListener = listener;
469 }
470 }
471
472 /**
473 * Sets the OnDismissListener of the dialog shown. This method can only be
474 * called after showDialog(int) and before removeDialog(int). The method
475 * does nothing otherwise.
476 */
477 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
478 if (mDialogFragment != null) {
479 mDialogFragment.mOnDismissListener = listener;
480 }
481 }
482
Amith Yamasanic861cf82012-10-02 14:51:46 -0700483 public void onDialogShowing() {
484 // override in subclass to attach a dismiss listener, for instance
485 }
486
Jason Monk39b46742015-09-10 15:52:51 -0400487 @Override
488 public void onDisplayPreferenceDialog(Preference preference) {
489 if (preference.getKey() == null) {
490 // Auto-key preferences that don't have a key, so the dialog can find them.
491 preference.setKey(UUID.randomUUID().toString());
492 }
493 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000494 if (preference instanceof RestrictedListPreference) {
495 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
496 .newInstance(preference.getKey());
497 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400498 f = CustomListPreference.CustomListPreferenceDialogFragment
499 .newInstance(preference.getKey());
500 } else if (preference instanceof CustomDialogPreference) {
501 f = CustomDialogPreference.CustomPreferenceDialogFragment
502 .newInstance(preference.getKey());
503 } else if (preference instanceof CustomEditTextPreference) {
504 f = CustomEditTextPreference.CustomPreferenceDialogFragment
505 .newInstance(preference.getKey());
506 } else {
507 super.onDisplayPreferenceDialog(preference);
508 return;
509 }
510 f.setTargetFragment(this, 0);
511 f.show(getFragmentManager(), "dialog_preference");
512 onDialogShowing();
513 }
514
Amith Yamasani43c69782010-12-01 09:04:36 -0800515 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800516 private static final String KEY_DIALOG_ID = "key_dialog_id";
517 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
518
Amith Yamasanid7993472010-08-18 13:59:28 -0700519 private int mDialogId;
520
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800521 private Fragment mParentFragment;
522
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800523 private DialogInterface.OnCancelListener mOnCancelListener;
524 private DialogInterface.OnDismissListener mOnDismissListener;
525
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800526 public SettingsDialogFragment() {
527 /* do nothing */
528 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700529
Amith Yamasani43c69782010-12-01 09:04:36 -0800530 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700531 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800532 if (!(fragment instanceof Fragment)) {
533 throw new IllegalArgumentException("fragment argument must be an instance of "
534 + Fragment.class.getName());
535 }
536 mParentFragment = (Fragment) fragment;
537 }
538
539 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800540 public void onSaveInstanceState(Bundle outState) {
541 super.onSaveInstanceState(outState);
542 if (mParentFragment != null) {
543 outState.putInt(KEY_DIALOG_ID, mDialogId);
544 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
545 }
546 }
547
548 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700549 public void onStart() {
550 super.onStart();
551
552 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
553 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
554 }
555 }
556
557 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800558 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800559 if (savedInstanceState != null) {
560 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800561 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800562 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700563 if (mParentFragment == null) {
564 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
565 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800566 if (!(mParentFragment instanceof DialogCreatable)) {
567 throw new IllegalArgumentException(
568 (mParentFragment != null
569 ? mParentFragment.getClass().getName()
570 : mParentFragmentId)
571 + " must implement "
572 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800573 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800574 // This dialog fragment could be created from non-SettingsPreferenceFragment
575 if (mParentFragment instanceof SettingsPreferenceFragment) {
576 // restore mDialogFragment in mParentFragment
577 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
578 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800579 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800580 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700581 }
582
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800583 @Override
584 public void onCancel(DialogInterface dialog) {
585 super.onCancel(dialog);
586 if (mOnCancelListener != null) {
587 mOnCancelListener.onCancel(dialog);
588 }
589 }
590
591 @Override
592 public void onDismiss(DialogInterface dialog) {
593 super.onDismiss(dialog);
594 if (mOnDismissListener != null) {
595 mOnDismissListener.onDismiss(dialog);
596 }
597 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800598
Amith Yamasanid7993472010-08-18 13:59:28 -0700599 public int getDialogId() {
600 return mDialogId;
601 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800602
603 @Override
604 public void onDetach() {
605 super.onDetach();
606
Amith Yamasani8875ede2011-01-31 12:46:57 -0800607 // This dialog fragment could be created from non-SettingsPreferenceFragment
608 if (mParentFragment instanceof SettingsPreferenceFragment) {
609 // in case the dialog is not explicitly removed by removeDialog()
610 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
611 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
612 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800613 }
614 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700615 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700616
617 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800618 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700619 }
620
621 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800622 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700623 }
624
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700625 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700626 Activity activity = getActivity();
627 if (activity != null) {
628 activity.onBackPressed();
629 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700630 }
631
Jason Monk39b46742015-09-10 15:52:51 -0400632 protected final Context getPrefContext() {
633 return getPreferenceManager().getContext();
634 }
635
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700636 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
637 int requestCode, Bundle extras) {
638 final Activity activity = getActivity();
639 if (activity instanceof SettingsActivity) {
640 SettingsActivity sa = (SettingsActivity) activity;
641 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
642 return true;
643 } else if (activity instanceof PreferenceActivity) {
644 PreferenceActivity sa = (PreferenceActivity) activity;
645 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700646 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700647 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700648 Log.w(TAG,
649 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
650 + "launch the given Fragment (name: " + fragmentClass
651 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700652 return false;
653 }
654 }
Jason Monk65bb0972015-12-17 10:39:44 -0500655
656 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
657
658 private int mHighlightPosition = -1;
659
660 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
661 super(preferenceGroup);
662 }
663
664 public void highlight(int position) {
665 mHighlightPosition = position;
666 notifyDataSetChanged();
667 }
668
669 @Override
670 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
671 super.onBindViewHolder(holder, position);
672 if (position == mHighlightPosition) {
673 View v = holder.itemView;
674 if (v.getBackground() != null) {
675 final int centerX = v.getWidth() / 2;
676 final int centerY = v.getHeight() / 2;
677 v.getBackground().setHotspot(centerX, centerY);
678 }
679 v.setPressed(true);
680 v.setPressed(false);
681 mHighlightPosition = -1;
682 }
683 }
684 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700685}