blob: 41fd79549cf3b681c176dd2f3bd731a33b6ea73a [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;
Jason Monk77467e02016-01-30 12:15:11 -0500200 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700201 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700202 }
203
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700204 public void unregisterObserverIfNeeded() {
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 Megliod83b3c22014-08-13 10:45:19 -0700208 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700209 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700210 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700211 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700212 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700213
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700214 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700215 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700216 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700217 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700218 }
219
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000220 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400221 highlightPreferenceIfNeeded();
222 updateEmptyView();
223 }
224
Jason Monk39b46742015-09-10 15:52:51 -0400225 public LayoutPreference getHeaderView() {
226 return mHeader;
227 }
228
229 public LayoutPreference getFooterView() {
230 return mFooter;
231 }
232
233 protected void setHeaderView(int resource) {
234 mHeader = new LayoutPreference(getPrefContext(), resource);
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000235 mHeader.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400236 if (getPreferenceScreen() != null) {
237 getPreferenceScreen().addPreference(mHeader);
238 }
239 }
240
241 protected void setFooterView(int resource) {
242 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
243 }
244
245 protected void setFooterView(View v) {
246 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
247 }
248
249 private void setFooterView(LayoutPreference footer) {
250 if (getPreferenceScreen() != null && mFooter != null) {
251 getPreferenceScreen().removePreference(mFooter);
252 }
253 if (footer != null) {
254 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000255 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400256 if (getPreferenceScreen() != null) {
257 getPreferenceScreen().addPreference(mFooter);
258 }
259 } else {
260 mFooter = null;
261 }
262 }
263
264 @Override
265 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
266 super.setPreferenceScreen(preferenceScreen);
267 if (preferenceScreen != null) {
268 if (mHeader != null) {
269 preferenceScreen.addPreference(mHeader);
270 }
271 if (mFooter != null) {
272 preferenceScreen.addPreference(mFooter);
273 }
274 }
275 }
276
277 private void updateEmptyView() {
278 if (mEmptyView == null) return;
279 if (getPreferenceScreen() != null) {
280 boolean show = (getPreferenceScreen().getPreferenceCount()
281 - (mHeader != null ? 1 : 0)
282 - (mFooter != null ? 1 : 0)) <= 0;
283 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
284 } else {
285 mEmptyView.setVisibility(View.VISIBLE);
286 }
287 }
288
289 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000290 if (mEmptyView != null) {
291 mEmptyView.setVisibility(View.GONE);
292 }
Jason Monk39b46742015-09-10 15:52:51 -0400293 mEmptyView = v;
294 updateEmptyView();
295 }
296
297 public View getEmptyView() {
298 return mEmptyView;
299 }
300
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700301 /**
302 * Return a valid ListView position or -1 if none is found
303 */
304 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400305 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700306 return -1;
307 }
308
Jason Monk39b46742015-09-10 15:52:51 -0400309 RecyclerView listView = getListView();
310 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700311
312 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400313 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700314 }
315
316 return -1;
317 }
318
Jason Monk65bb0972015-12-17 10:39:44 -0500319 @Override
320 public RecyclerView.LayoutManager onCreateLayoutManager() {
321 mLayoutManager = new LinearLayoutManager(getContext());
322 return mLayoutManager;
323 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700324
Jason Monk65bb0972015-12-17 10:39:44 -0500325 @Override
326 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
327 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
328 return mAdapter;
329 }
330
331 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700332 final int position = canUseListViewForHighLighting(key);
333 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700334 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500335 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700336
Jason Monk65bb0972015-12-17 10:39:44 -0500337 getView().postDelayed(new Runnable() {
338 @Override
339 public void run() {
340 mAdapter.highlight(position);
341 }
342 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700343 }
344 }
345
Jason Monk39b46742015-09-10 15:52:51 -0400346 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
347 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700348 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400349 final Preference preference = adapter.getItem(n);
350 final String preferenceKey = preference.getKey();
351 if (preferenceKey != null && preferenceKey.equals(key)) {
352 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700353 }
354 }
355 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700356 }
357
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700358 protected void removePreference(String key) {
359 Preference pref = findPreference(key);
360 if (pref != null) {
361 getPreferenceScreen().removePreference(pref);
362 }
363 }
364
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700365 /**
366 * Override this if you want to show a help item in the menu, by returning the resource id.
367 * @return the resource id for the help url
368 */
369 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400370 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700371 }
372
373 @Override
374 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400375 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400376 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700377 }
378 }
379
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700380 /*
381 * The name is intentionally made different from Activity#finish(), so that
382 * users won't misunderstand its meaning.
383 */
384 public final void finishFragment() {
385 getActivity().onBackPressed();
386 }
387
Amith Yamasanid7993472010-08-18 13:59:28 -0700388 // Some helpers for functions used by the settings fragments when they were activities
389
390 /**
391 * Returns the ContentResolver from the owning Activity.
392 */
393 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700394 Context context = getActivity();
395 if (context != null) {
396 mContentResolver = context.getContentResolver();
397 }
398 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700399 }
400
401 /**
402 * Returns the specified system service from the owning Activity.
403 */
404 protected Object getSystemService(final String name) {
405 return getActivity().getSystemService(name);
406 }
407
408 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700409 * Returns the PackageManager from the owning Activity.
410 */
411 protected PackageManager getPackageManager() {
412 return getActivity().getPackageManager();
413 }
414
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800415 @Override
416 public void onDetach() {
417 if (isRemoving()) {
418 if (mDialogFragment != null) {
419 mDialogFragment.dismiss();
420 mDialogFragment = null;
421 }
422 }
423 super.onDetach();
424 }
425
Amith Yamasanid7993472010-08-18 13:59:28 -0700426 // Dialog management
427
428 protected void showDialog(int dialogId) {
429 if (mDialogFragment != null) {
430 Log.e(TAG, "Old dialog fragment not null!");
431 }
432 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800433 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700434 }
435
436 public Dialog onCreateDialog(int dialogId) {
437 return null;
438 }
439
440 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800441 // mDialogFragment may not be visible yet in parent fragment's onResume().
442 // To be able to dismiss dialog at that time, don't check
443 // mDialogFragment.isVisible().
444 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700445 mDialogFragment.dismiss();
446 }
447 mDialogFragment = null;
448 }
449
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800450 /**
451 * Sets the OnCancelListener of the dialog shown. This method can only be
452 * called after showDialog(int) and before removeDialog(int). The method
453 * does nothing otherwise.
454 */
455 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
456 if (mDialogFragment != null) {
457 mDialogFragment.mOnCancelListener = listener;
458 }
459 }
460
461 /**
462 * Sets the OnDismissListener 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 setOnDismissListener(DialogInterface.OnDismissListener listener) {
467 if (mDialogFragment != null) {
468 mDialogFragment.mOnDismissListener = listener;
469 }
470 }
471
Amith Yamasanic861cf82012-10-02 14:51:46 -0700472 public void onDialogShowing() {
473 // override in subclass to attach a dismiss listener, for instance
474 }
475
Jason Monk39b46742015-09-10 15:52:51 -0400476 @Override
477 public void onDisplayPreferenceDialog(Preference preference) {
478 if (preference.getKey() == null) {
479 // Auto-key preferences that don't have a key, so the dialog can find them.
480 preference.setKey(UUID.randomUUID().toString());
481 }
482 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000483 if (preference instanceof RestrictedListPreference) {
484 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
485 .newInstance(preference.getKey());
486 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400487 f = CustomListPreference.CustomListPreferenceDialogFragment
488 .newInstance(preference.getKey());
489 } else if (preference instanceof CustomDialogPreference) {
490 f = CustomDialogPreference.CustomPreferenceDialogFragment
491 .newInstance(preference.getKey());
492 } else if (preference instanceof CustomEditTextPreference) {
493 f = CustomEditTextPreference.CustomPreferenceDialogFragment
494 .newInstance(preference.getKey());
495 } else {
496 super.onDisplayPreferenceDialog(preference);
497 return;
498 }
499 f.setTargetFragment(this, 0);
500 f.show(getFragmentManager(), "dialog_preference");
501 onDialogShowing();
502 }
503
Amith Yamasani43c69782010-12-01 09:04:36 -0800504 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800505 private static final String KEY_DIALOG_ID = "key_dialog_id";
506 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
507
Amith Yamasanid7993472010-08-18 13:59:28 -0700508 private int mDialogId;
509
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800510 private Fragment mParentFragment;
511
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800512 private DialogInterface.OnCancelListener mOnCancelListener;
513 private DialogInterface.OnDismissListener mOnDismissListener;
514
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800515 public SettingsDialogFragment() {
516 /* do nothing */
517 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700518
Amith Yamasani43c69782010-12-01 09:04:36 -0800519 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700520 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800521 if (!(fragment instanceof Fragment)) {
522 throw new IllegalArgumentException("fragment argument must be an instance of "
523 + Fragment.class.getName());
524 }
525 mParentFragment = (Fragment) fragment;
526 }
527
528 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800529 public void onSaveInstanceState(Bundle outState) {
530 super.onSaveInstanceState(outState);
531 if (mParentFragment != null) {
532 outState.putInt(KEY_DIALOG_ID, mDialogId);
533 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
534 }
535 }
536
537 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700538 public void onStart() {
539 super.onStart();
540
541 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
542 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
543 }
544 }
545
546 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800547 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800548 if (savedInstanceState != null) {
549 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800550 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800551 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700552 if (mParentFragment == null) {
553 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
554 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800555 if (!(mParentFragment instanceof DialogCreatable)) {
556 throw new IllegalArgumentException(
557 (mParentFragment != null
558 ? mParentFragment.getClass().getName()
559 : mParentFragmentId)
560 + " must implement "
561 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800562 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800563 // This dialog fragment could be created from non-SettingsPreferenceFragment
564 if (mParentFragment instanceof SettingsPreferenceFragment) {
565 // restore mDialogFragment in mParentFragment
566 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
567 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800568 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800569 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700570 }
571
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800572 @Override
573 public void onCancel(DialogInterface dialog) {
574 super.onCancel(dialog);
575 if (mOnCancelListener != null) {
576 mOnCancelListener.onCancel(dialog);
577 }
578 }
579
580 @Override
581 public void onDismiss(DialogInterface dialog) {
582 super.onDismiss(dialog);
583 if (mOnDismissListener != null) {
584 mOnDismissListener.onDismiss(dialog);
585 }
586 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800587
Amith Yamasanid7993472010-08-18 13:59:28 -0700588 public int getDialogId() {
589 return mDialogId;
590 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800591
592 @Override
593 public void onDetach() {
594 super.onDetach();
595
Amith Yamasani8875ede2011-01-31 12:46:57 -0800596 // This dialog fragment could be created from non-SettingsPreferenceFragment
597 if (mParentFragment instanceof SettingsPreferenceFragment) {
598 // in case the dialog is not explicitly removed by removeDialog()
599 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
600 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
601 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800602 }
603 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700604 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700605
606 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800607 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700608 }
609
610 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800611 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700612 }
613
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700614 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700615 Activity activity = getActivity();
616 if (activity != null) {
617 activity.onBackPressed();
618 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700619 }
620
Jason Monk39b46742015-09-10 15:52:51 -0400621 protected final Context getPrefContext() {
622 return getPreferenceManager().getContext();
623 }
624
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700625 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
626 int requestCode, Bundle extras) {
627 final Activity activity = getActivity();
628 if (activity instanceof SettingsActivity) {
629 SettingsActivity sa = (SettingsActivity) activity;
630 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
631 return true;
632 } else if (activity instanceof PreferenceActivity) {
633 PreferenceActivity sa = (PreferenceActivity) activity;
634 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700635 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700636 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700637 Log.w(TAG,
638 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
639 + "launch the given Fragment (name: " + fragmentClass
640 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700641 return false;
642 }
643 }
Jason Monk65bb0972015-12-17 10:39:44 -0500644
645 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
646
647 private int mHighlightPosition = -1;
648
649 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
650 super(preferenceGroup);
651 }
652
653 public void highlight(int position) {
654 mHighlightPosition = position;
655 notifyDataSetChanged();
656 }
657
658 @Override
659 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
660 super.onBindViewHolder(holder, position);
661 if (position == mHighlightPosition) {
662 View v = holder.itemView;
663 if (v.getBackground() != null) {
664 final int centerX = v.getWidth() / 2;
665 final int centerY = v.getHeight() / 2;
666 v.getBackground().setHotspot(centerX, centerY);
667 }
668 v.setPressed(true);
669 v.setPressed(false);
670 mHighlightPosition = -1;
671 }
672 }
673 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700674}