blob: d2ce2851180251b6f7ff3c0462e3fec09720e32b [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
Jason Monk65bb0972015-12-17 10:39:44 -050055 private static final String TAG = "SettingsPreference";
Amith Yamasanid7993472010-08-18 13:59:28 -070056
Fabrice Di Meglioeced7802014-09-04 13:01:55 -070057 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070058
59 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070060
Amith Yamasanid7993472010-08-18 13:59:28 -070061 private SettingsDialogFragment mDialogFragment;
62
Jason Monk23acc2b2015-04-14 15:06:39 -040063 private String mHelpUri;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070064
Sudheer Shanka5590e2e2016-01-22 20:40:56 +000065 private static final int ORDER_FIRST = -1;
66 private static final int ORDER_LAST = Integer.MAX_VALUE -1;
67
Amith Yamasani350938e2013-04-09 10:22:47 -070068 // Cache the content resolver for async callbacks
69 private ContentResolver mContentResolver;
70
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070071 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070072 private boolean mPreferenceHighlighted = false;
73
Jason Monk39b46742015-09-10 15:52:51 -040074 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070075 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040076 private RecyclerView.AdapterDataObserver mDataSetObserver =
77 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070078 @Override
79 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040080 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070081 }
82 };
83
Fabrice Di Meglio86159282014-07-21 16:02:27 -070084 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040085 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090086 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070087
Jason Monk39b46742015-09-10 15:52:51 -040088 private LayoutPreference mHeader;
89
90 private LayoutPreference mFooter;
91 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050092 private LinearLayoutManager mLayoutManager;
93 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040094
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070095 @Override
96 public void onCreate(Bundle icicle) {
97 super.onCreate(icicle);
98
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070099 if (icicle != null) {
100 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
101 }
102
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700103 // Prepare help url and enable menu if necessary
104 int helpResource = getHelpResource();
105 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400106 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700107 }
108 }
109
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700110 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700111 public View onCreateView(LayoutInflater inflater, ViewGroup container,
112 Bundle savedInstanceState) {
113 final View root = super.onCreateView(inflater, container, savedInstanceState);
114 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400115 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900116 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700117 return root;
118 }
119
Jason Monk39b46742015-09-10 15:52:51 -0400120 @Override
121 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
122 }
123
John Spurlockb8e02b82015-04-15 21:15:55 -0400124 public FloatingActionButton getFloatingActionButton() {
125 return mFloatingActionButton;
126 }
127
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900128 public ViewGroup getButtonBar() {
129 return mButtonBar;
130 }
131
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700132 public View setPinnedHeaderView(int layoutResId) {
133 final LayoutInflater inflater = getActivity().getLayoutInflater();
134 final View pinnedHeader =
135 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
136 setPinnedHeaderView(pinnedHeader);
137 return pinnedHeader;
138 }
139
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700140 public void setPinnedHeaderView(View pinnedHeader) {
141 mPinnedHeaderFrameLayout.addView(pinnedHeader);
142 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
143 }
144
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700145 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700146 public void onSaveInstanceState(Bundle outState) {
147 super.onSaveInstanceState(outState);
148
149 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
150 }
151
152 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700153 public void onActivityCreated(Bundle savedInstanceState) {
154 super.onActivityCreated(savedInstanceState);
Anna Galuszab1795f52016-01-08 14:37:16 -0800155 if (!TextUtils.isEmpty(mHelpUri)
156 && !(getActivity() instanceof AccessibilitySettingsForSetupWizardActivity)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700157 setHasOptionsMenu(true);
158 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700159 }
160
161 @Override
162 public void onResume() {
163 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700164
165 final Bundle args = getArguments();
166 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700167 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
168 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700169 }
170 }
171
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700172 @Override
173 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700174 registerObserverIfNeeded();
175 }
176
177 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700178 protected void onUnbindPreferences() {
179 unregisterObserverIfNeeded();
180 }
181
Jason Monkb5aa73f2015-03-31 12:59:33 -0400182 public void showLoadingWhenEmpty() {
183 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400184 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400185 }
186
Jason Monkb37e2882016-01-11 14:27:20 -0500187 public void setLoading(boolean loading, boolean animate) {
188 View loading_container = getView().findViewById(R.id.loading_container);
189 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
190 }
191
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700192 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700193 if (!mIsDataSetObserverRegistered) {
194 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400195 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700196 }
Jason Monk39b46742015-09-10 15:52:51 -0400197 mCurrentRootAdapter = getListView().getAdapter();
198 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700199 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700200 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700201 }
202
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700203 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700204 if (mIsDataSetObserverRegistered) {
205 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400206 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700207 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700208 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700209 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700210 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700211 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700212
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700213 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700214 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700215 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700216 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700217 }
218
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000219 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400220 highlightPreferenceIfNeeded();
221 updateEmptyView();
222 }
223
Jason Monk39b46742015-09-10 15:52:51 -0400224 public LayoutPreference getHeaderView() {
225 return mHeader;
226 }
227
228 public LayoutPreference getFooterView() {
229 return mFooter;
230 }
231
232 protected void setHeaderView(int resource) {
233 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000234 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400235 if (getPreferenceScreen() != null) {
236 getPreferenceScreen().addPreference(mHeader);
237 }
238 }
239
240 protected void setFooterView(int resource) {
241 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
242 }
243
244 protected void setFooterView(View v) {
245 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
246 }
247
248 private void setFooterView(LayoutPreference footer) {
249 if (getPreferenceScreen() != null && mFooter != null) {
250 getPreferenceScreen().removePreference(mFooter);
251 }
252 if (footer != null) {
253 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000254 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400255 if (getPreferenceScreen() != null) {
256 getPreferenceScreen().addPreference(mFooter);
257 }
258 } else {
259 mFooter = null;
260 }
261 }
262
263 @Override
264 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
265 super.setPreferenceScreen(preferenceScreen);
266 if (preferenceScreen != null) {
267 if (mHeader != null) {
268 preferenceScreen.addPreference(mHeader);
269 }
270 if (mFooter != null) {
271 preferenceScreen.addPreference(mFooter);
272 }
273 }
274 }
275
276 private void updateEmptyView() {
277 if (mEmptyView == null) return;
278 if (getPreferenceScreen() != null) {
279 boolean show = (getPreferenceScreen().getPreferenceCount()
280 - (mHeader != null ? 1 : 0)
281 - (mFooter != null ? 1 : 0)) <= 0;
282 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
283 } else {
284 mEmptyView.setVisibility(View.VISIBLE);
285 }
286 }
287
288 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000289 if (mEmptyView != null) {
290 mEmptyView.setVisibility(View.GONE);
291 }
Jason Monk39b46742015-09-10 15:52:51 -0400292 mEmptyView = v;
293 updateEmptyView();
294 }
295
296 public View getEmptyView() {
297 return mEmptyView;
298 }
299
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700300 /**
301 * Return a valid ListView position or -1 if none is found
302 */
303 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400304 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700305 return -1;
306 }
307
Jason Monk39b46742015-09-10 15:52:51 -0400308 RecyclerView listView = getListView();
309 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700310
311 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400312 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700313 }
314
315 return -1;
316 }
317
Jason Monk65bb0972015-12-17 10:39:44 -0500318 @Override
319 public RecyclerView.LayoutManager onCreateLayoutManager() {
320 mLayoutManager = new LinearLayoutManager(getContext());
321 return mLayoutManager;
322 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700323
Jason Monk65bb0972015-12-17 10:39:44 -0500324 @Override
325 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
326 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
327 return mAdapter;
328 }
329
330 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700331 final int position = canUseListViewForHighLighting(key);
332 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700333 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500334 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700335
Jason Monk65bb0972015-12-17 10:39:44 -0500336 getView().postDelayed(new Runnable() {
337 @Override
338 public void run() {
339 mAdapter.highlight(position);
340 }
341 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700342 }
343 }
344
Jason Monk39b46742015-09-10 15:52:51 -0400345 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
346 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700347 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400348 final Preference preference = adapter.getItem(n);
349 final String preferenceKey = preference.getKey();
350 if (preferenceKey != null && preferenceKey.equals(key)) {
351 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700352 }
353 }
354 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700355 }
356
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700357 protected void removePreference(String key) {
358 Preference pref = findPreference(key);
359 if (pref != null) {
360 getPreferenceScreen().removePreference(pref);
361 }
362 }
363
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700364 /**
365 * Override this if you want to show a help item in the menu, by returning the resource id.
366 * @return the resource id for the help url
367 */
368 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400369 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700370 }
371
372 @Override
373 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400374 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400375 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700376 }
377 }
378
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700379 /*
380 * The name is intentionally made different from Activity#finish(), so that
381 * users won't misunderstand its meaning.
382 */
383 public final void finishFragment() {
384 getActivity().onBackPressed();
385 }
386
Amith Yamasanid7993472010-08-18 13:59:28 -0700387 // Some helpers for functions used by the settings fragments when they were activities
388
389 /**
390 * Returns the ContentResolver from the owning Activity.
391 */
392 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700393 Context context = getActivity();
394 if (context != null) {
395 mContentResolver = context.getContentResolver();
396 }
397 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700398 }
399
400 /**
401 * Returns the specified system service from the owning Activity.
402 */
403 protected Object getSystemService(final String name) {
404 return getActivity().getSystemService(name);
405 }
406
407 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700408 * Returns the PackageManager from the owning Activity.
409 */
410 protected PackageManager getPackageManager() {
411 return getActivity().getPackageManager();
412 }
413
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800414 @Override
415 public void onDetach() {
416 if (isRemoving()) {
417 if (mDialogFragment != null) {
418 mDialogFragment.dismiss();
419 mDialogFragment = null;
420 }
421 }
422 super.onDetach();
423 }
424
Amith Yamasanid7993472010-08-18 13:59:28 -0700425 // Dialog management
426
427 protected void showDialog(int dialogId) {
428 if (mDialogFragment != null) {
429 Log.e(TAG, "Old dialog fragment not null!");
430 }
431 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800432 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700433 }
434
435 public Dialog onCreateDialog(int dialogId) {
436 return null;
437 }
438
439 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800440 // mDialogFragment may not be visible yet in parent fragment's onResume().
441 // To be able to dismiss dialog at that time, don't check
442 // mDialogFragment.isVisible().
443 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700444 mDialogFragment.dismiss();
445 }
446 mDialogFragment = null;
447 }
448
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800449 /**
450 * Sets the OnCancelListener of the dialog shown. This method can only be
451 * called after showDialog(int) and before removeDialog(int). The method
452 * does nothing otherwise.
453 */
454 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
455 if (mDialogFragment != null) {
456 mDialogFragment.mOnCancelListener = listener;
457 }
458 }
459
460 /**
461 * Sets the OnDismissListener of the dialog shown. This method can only be
462 * called after showDialog(int) and before removeDialog(int). The method
463 * does nothing otherwise.
464 */
465 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
466 if (mDialogFragment != null) {
467 mDialogFragment.mOnDismissListener = listener;
468 }
469 }
470
Amith Yamasanic861cf82012-10-02 14:51:46 -0700471 public void onDialogShowing() {
472 // override in subclass to attach a dismiss listener, for instance
473 }
474
Jason Monk39b46742015-09-10 15:52:51 -0400475 @Override
476 public void onDisplayPreferenceDialog(Preference preference) {
477 if (preference.getKey() == null) {
478 // Auto-key preferences that don't have a key, so the dialog can find them.
479 preference.setKey(UUID.randomUUID().toString());
480 }
481 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000482 if (preference instanceof RestrictedListPreference) {
483 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
484 .newInstance(preference.getKey());
485 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400486 f = CustomListPreference.CustomListPreferenceDialogFragment
487 .newInstance(preference.getKey());
488 } else if (preference instanceof CustomDialogPreference) {
489 f = CustomDialogPreference.CustomPreferenceDialogFragment
490 .newInstance(preference.getKey());
491 } else if (preference instanceof CustomEditTextPreference) {
492 f = CustomEditTextPreference.CustomPreferenceDialogFragment
493 .newInstance(preference.getKey());
494 } else {
495 super.onDisplayPreferenceDialog(preference);
496 return;
497 }
498 f.setTargetFragment(this, 0);
499 f.show(getFragmentManager(), "dialog_preference");
500 onDialogShowing();
501 }
502
Amith Yamasani43c69782010-12-01 09:04:36 -0800503 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800504 private static final String KEY_DIALOG_ID = "key_dialog_id";
505 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
506
Amith Yamasanid7993472010-08-18 13:59:28 -0700507 private int mDialogId;
508
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800509 private Fragment mParentFragment;
510
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800511 private DialogInterface.OnCancelListener mOnCancelListener;
512 private DialogInterface.OnDismissListener mOnDismissListener;
513
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800514 public SettingsDialogFragment() {
515 /* do nothing */
516 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700517
Amith Yamasani43c69782010-12-01 09:04:36 -0800518 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700519 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800520 if (!(fragment instanceof Fragment)) {
521 throw new IllegalArgumentException("fragment argument must be an instance of "
522 + Fragment.class.getName());
523 }
524 mParentFragment = (Fragment) fragment;
525 }
526
527 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800528 public void onSaveInstanceState(Bundle outState) {
529 super.onSaveInstanceState(outState);
530 if (mParentFragment != null) {
531 outState.putInt(KEY_DIALOG_ID, mDialogId);
532 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
533 }
534 }
535
536 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700537 public void onStart() {
538 super.onStart();
539
540 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
541 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
542 }
543 }
544
545 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800546 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800547 if (savedInstanceState != null) {
548 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800549 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800550 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700551 if (mParentFragment == null) {
552 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
553 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800554 if (!(mParentFragment instanceof DialogCreatable)) {
555 throw new IllegalArgumentException(
556 (mParentFragment != null
557 ? mParentFragment.getClass().getName()
558 : mParentFragmentId)
559 + " must implement "
560 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800561 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800562 // This dialog fragment could be created from non-SettingsPreferenceFragment
563 if (mParentFragment instanceof SettingsPreferenceFragment) {
564 // restore mDialogFragment in mParentFragment
565 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
566 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800567 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700569 }
570
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800571 @Override
572 public void onCancel(DialogInterface dialog) {
573 super.onCancel(dialog);
574 if (mOnCancelListener != null) {
575 mOnCancelListener.onCancel(dialog);
576 }
577 }
578
579 @Override
580 public void onDismiss(DialogInterface dialog) {
581 super.onDismiss(dialog);
582 if (mOnDismissListener != null) {
583 mOnDismissListener.onDismiss(dialog);
584 }
585 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800586
Amith Yamasanid7993472010-08-18 13:59:28 -0700587 public int getDialogId() {
588 return mDialogId;
589 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800590
591 @Override
592 public void onDetach() {
593 super.onDetach();
594
Amith Yamasani8875ede2011-01-31 12:46:57 -0800595 // This dialog fragment could be created from non-SettingsPreferenceFragment
596 if (mParentFragment instanceof SettingsPreferenceFragment) {
597 // in case the dialog is not explicitly removed by removeDialog()
598 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
599 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
600 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800601 }
602 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700603 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700604
605 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800606 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700607 }
608
609 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800610 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700611 }
612
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700613 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700614 Activity activity = getActivity();
615 if (activity != null) {
616 activity.onBackPressed();
617 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700618 }
619
Jason Monk39b46742015-09-10 15:52:51 -0400620 protected final Context getPrefContext() {
621 return getPreferenceManager().getContext();
622 }
623
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700624 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
625 int requestCode, Bundle extras) {
626 final Activity activity = getActivity();
627 if (activity instanceof SettingsActivity) {
628 SettingsActivity sa = (SettingsActivity) activity;
629 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
630 return true;
631 } else if (activity instanceof PreferenceActivity) {
632 PreferenceActivity sa = (PreferenceActivity) activity;
633 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700634 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700635 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700636 Log.w(TAG,
637 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
638 + "launch the given Fragment (name: " + fragmentClass
639 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700640 return false;
641 }
642 }
Jason Monk65bb0972015-12-17 10:39:44 -0500643
644 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
645
646 private int mHighlightPosition = -1;
647
648 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
649 super(preferenceGroup);
650 }
651
652 public void highlight(int position) {
653 mHighlightPosition = position;
654 notifyDataSetChanged();
655 }
656
657 @Override
658 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
659 super.onBindViewHolder(holder, position);
660 if (position == mHighlightPosition) {
661 View v = holder.itemView;
662 if (v.getBackground() != null) {
663 final int centerX = v.getWidth() / 2;
664 final int centerY = v.getHeight() / 2;
665 v.getBackground().setHotspot(centerX, centerY);
666 }
667 v.setPressed(true);
668 v.setPressed(false);
669 mHighlightPosition = -1;
670 }
671 }
672 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700673}