blob: d8b9b9195ed9c2e773202b3ad7bb18d6a4ca7e75 [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
Amith Yamasani350938e2013-04-09 10:22:47 -070065 // Cache the content resolver for async callbacks
66 private ContentResolver mContentResolver;
67
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070068 private String mPreferenceKey;
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070069 private boolean mPreferenceHighlighted = false;
70
Jason Monk39b46742015-09-10 15:52:51 -040071 private RecyclerView.Adapter mCurrentRootAdapter;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -070072 private boolean mIsDataSetObserverRegistered = false;
Jason Monk39b46742015-09-10 15:52:51 -040073 private RecyclerView.AdapterDataObserver mDataSetObserver =
74 new RecyclerView.AdapterDataObserver() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070075 @Override
76 public void onChanged() {
Jason Monk39b46742015-09-10 15:52:51 -040077 onDataSetChanged();
Fabrice Di Meglioc853a422014-04-18 19:40:40 -070078 }
79 };
80
Fabrice Di Meglio86159282014-07-21 16:02:27 -070081 private ViewGroup mPinnedHeaderFrameLayout;
John Spurlockb8e02b82015-04-15 21:15:55 -040082 private FloatingActionButton mFloatingActionButton;
Daichi Hirono5e76cdc2015-07-08 11:38:55 +090083 private ViewGroup mButtonBar;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070084
Jason Monk39b46742015-09-10 15:52:51 -040085 private LayoutPreference mHeader;
86
87 private LayoutPreference mFooter;
88 private View mEmptyView;
Jason Monk65bb0972015-12-17 10:39:44 -050089 private LinearLayoutManager mLayoutManager;
90 private HighlightablePreferenceGroupAdapter mAdapter;
Jason Monk39b46742015-09-10 15:52:51 -040091
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070092 @Override
93 public void onCreate(Bundle icicle) {
94 super.onCreate(icicle);
95
Fabrice Di Meglio6602d022014-04-15 16:45:20 -070096 if (icicle != null) {
97 mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
98 }
99
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700100 // Prepare help url and enable menu if necessary
101 int helpResource = getHelpResource();
102 if (helpResource != 0) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400103 mHelpUri = getResources().getString(helpResource);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700104 }
105 }
106
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700107 @Override
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700108 public View onCreateView(LayoutInflater inflater, ViewGroup container,
109 Bundle savedInstanceState) {
110 final View root = super.onCreateView(inflater, container, savedInstanceState);
111 mPinnedHeaderFrameLayout = (ViewGroup) root.findViewById(R.id.pinned_header);
John Spurlockb8e02b82015-04-15 21:15:55 -0400112 mFloatingActionButton = (FloatingActionButton) root.findViewById(R.id.fab);
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900113 mButtonBar = (ViewGroup) root.findViewById(R.id.button_bar);
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700114 return root;
115 }
116
Jason Monk39b46742015-09-10 15:52:51 -0400117 @Override
118 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
119 }
120
John Spurlockb8e02b82015-04-15 21:15:55 -0400121 public FloatingActionButton getFloatingActionButton() {
122 return mFloatingActionButton;
123 }
124
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900125 public ViewGroup getButtonBar() {
126 return mButtonBar;
127 }
128
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700129 public View setPinnedHeaderView(int layoutResId) {
130 final LayoutInflater inflater = getActivity().getLayoutInflater();
131 final View pinnedHeader =
132 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
133 setPinnedHeaderView(pinnedHeader);
134 return pinnedHeader;
135 }
136
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700137 public void setPinnedHeaderView(View pinnedHeader) {
138 mPinnedHeaderFrameLayout.addView(pinnedHeader);
139 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
140 }
141
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700142 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700143 public void onSaveInstanceState(Bundle outState) {
144 super.onSaveInstanceState(outState);
145
146 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
147 }
148
149 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700150 public void onActivityCreated(Bundle savedInstanceState) {
151 super.onActivityCreated(savedInstanceState);
Anna Galuszab1795f52016-01-08 14:37:16 -0800152 if (!TextUtils.isEmpty(mHelpUri)
153 && !(getActivity() instanceof AccessibilitySettingsForSetupWizardActivity)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700154 setHasOptionsMenu(true);
155 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700156 }
157
158 @Override
159 public void onResume() {
160 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161
162 final Bundle args = getArguments();
163 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700164 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
165 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700166 }
167 }
168
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700169 @Override
170 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700171 registerObserverIfNeeded();
172 }
173
174 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700175 protected void onUnbindPreferences() {
176 unregisterObserverIfNeeded();
177 }
178
Jason Monkb5aa73f2015-03-31 12:59:33 -0400179 public void showLoadingWhenEmpty() {
180 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400181 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400182 }
183
Jason Monkb37e2882016-01-11 14:27:20 -0500184 public void setLoading(boolean loading, boolean animate) {
185 View loading_container = getView().findViewById(R.id.loading_container);
186 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
187 }
188
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700189 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700190 if (!mIsDataSetObserverRegistered) {
191 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400192 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700193 }
Jason Monk39b46742015-09-10 15:52:51 -0400194 mCurrentRootAdapter = getListView().getAdapter();
195 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700196 mIsDataSetObserverRegistered = true;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700197 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700198 }
199
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700200 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700201 if (mIsDataSetObserverRegistered) {
202 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400203 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700204 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700205 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700206 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700207 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700208 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700209
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700210 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700211 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700212 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700213 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700214 }
215
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000216 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400217 highlightPreferenceIfNeeded();
218 updateEmptyView();
219 }
220
Jason Monk39b46742015-09-10 15:52:51 -0400221 public LayoutPreference getHeaderView() {
222 return mHeader;
223 }
224
225 public LayoutPreference getFooterView() {
226 return mFooter;
227 }
228
229 protected void setHeaderView(int resource) {
230 mHeader = new LayoutPreference(getPrefContext(), resource);
231 mHeader.setOrder(-1);
232 if (getPreferenceScreen() != null) {
233 getPreferenceScreen().addPreference(mHeader);
234 }
235 }
236
237 protected void setFooterView(int resource) {
238 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
239 }
240
241 protected void setFooterView(View v) {
242 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
243 }
244
245 private void setFooterView(LayoutPreference footer) {
246 if (getPreferenceScreen() != null && mFooter != null) {
247 getPreferenceScreen().removePreference(mFooter);
248 }
249 if (footer != null) {
250 mFooter = footer;
251 mFooter.setOrder(Integer.MAX_VALUE);
252 if (getPreferenceScreen() != null) {
253 getPreferenceScreen().addPreference(mFooter);
254 }
255 } else {
256 mFooter = null;
257 }
258 }
259
260 @Override
261 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
262 super.setPreferenceScreen(preferenceScreen);
263 if (preferenceScreen != null) {
264 if (mHeader != null) {
265 preferenceScreen.addPreference(mHeader);
266 }
267 if (mFooter != null) {
268 preferenceScreen.addPreference(mFooter);
269 }
270 }
271 }
272
273 private void updateEmptyView() {
274 if (mEmptyView == null) return;
275 if (getPreferenceScreen() != null) {
276 boolean show = (getPreferenceScreen().getPreferenceCount()
277 - (mHeader != null ? 1 : 0)
278 - (mFooter != null ? 1 : 0)) <= 0;
279 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
280 } else {
281 mEmptyView.setVisibility(View.VISIBLE);
282 }
283 }
284
285 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000286 if (mEmptyView != null) {
287 mEmptyView.setVisibility(View.GONE);
288 }
Jason Monk39b46742015-09-10 15:52:51 -0400289 mEmptyView = v;
290 updateEmptyView();
291 }
292
293 public View getEmptyView() {
294 return mEmptyView;
295 }
296
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700297 /**
298 * Return a valid ListView position or -1 if none is found
299 */
300 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400301 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700302 return -1;
303 }
304
Jason Monk39b46742015-09-10 15:52:51 -0400305 RecyclerView listView = getListView();
306 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700307
308 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400309 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700310 }
311
312 return -1;
313 }
314
Jason Monk65bb0972015-12-17 10:39:44 -0500315 @Override
316 public RecyclerView.LayoutManager onCreateLayoutManager() {
317 mLayoutManager = new LinearLayoutManager(getContext());
318 return mLayoutManager;
319 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700320
Jason Monk65bb0972015-12-17 10:39:44 -0500321 @Override
322 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
323 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
324 return mAdapter;
325 }
326
327 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700328 final int position = canUseListViewForHighLighting(key);
329 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700330 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500331 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700332
Jason Monk65bb0972015-12-17 10:39:44 -0500333 getView().postDelayed(new Runnable() {
334 @Override
335 public void run() {
336 mAdapter.highlight(position);
337 }
338 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700339 }
340 }
341
Jason Monk39b46742015-09-10 15:52:51 -0400342 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
343 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700344 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400345 final Preference preference = adapter.getItem(n);
346 final String preferenceKey = preference.getKey();
347 if (preferenceKey != null && preferenceKey.equals(key)) {
348 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700349 }
350 }
351 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700352 }
353
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700354 protected void removePreference(String key) {
355 Preference pref = findPreference(key);
356 if (pref != null) {
357 getPreferenceScreen().removePreference(pref);
358 }
359 }
360
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700361 /**
362 * Override this if you want to show a help item in the menu, by returning the resource id.
363 * @return the resource id for the help url
364 */
365 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400366 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700367 }
368
369 @Override
370 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400371 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400372 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700373 }
374 }
375
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700376 /*
377 * The name is intentionally made different from Activity#finish(), so that
378 * users won't misunderstand its meaning.
379 */
380 public final void finishFragment() {
381 getActivity().onBackPressed();
382 }
383
Amith Yamasanid7993472010-08-18 13:59:28 -0700384 // Some helpers for functions used by the settings fragments when they were activities
385
386 /**
387 * Returns the ContentResolver from the owning Activity.
388 */
389 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700390 Context context = getActivity();
391 if (context != null) {
392 mContentResolver = context.getContentResolver();
393 }
394 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700395 }
396
397 /**
398 * Returns the specified system service from the owning Activity.
399 */
400 protected Object getSystemService(final String name) {
401 return getActivity().getSystemService(name);
402 }
403
404 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700405 * Returns the PackageManager from the owning Activity.
406 */
407 protected PackageManager getPackageManager() {
408 return getActivity().getPackageManager();
409 }
410
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800411 @Override
412 public void onDetach() {
413 if (isRemoving()) {
414 if (mDialogFragment != null) {
415 mDialogFragment.dismiss();
416 mDialogFragment = null;
417 }
418 }
419 super.onDetach();
420 }
421
Amith Yamasanid7993472010-08-18 13:59:28 -0700422 // Dialog management
423
424 protected void showDialog(int dialogId) {
425 if (mDialogFragment != null) {
426 Log.e(TAG, "Old dialog fragment not null!");
427 }
428 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800429 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700430 }
431
432 public Dialog onCreateDialog(int dialogId) {
433 return null;
434 }
435
436 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800437 // mDialogFragment may not be visible yet in parent fragment's onResume().
438 // To be able to dismiss dialog at that time, don't check
439 // mDialogFragment.isVisible().
440 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700441 mDialogFragment.dismiss();
442 }
443 mDialogFragment = null;
444 }
445
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800446 /**
447 * Sets the OnCancelListener of the dialog shown. This method can only be
448 * called after showDialog(int) and before removeDialog(int). The method
449 * does nothing otherwise.
450 */
451 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
452 if (mDialogFragment != null) {
453 mDialogFragment.mOnCancelListener = listener;
454 }
455 }
456
457 /**
458 * Sets the OnDismissListener of the dialog shown. This method can only be
459 * called after showDialog(int) and before removeDialog(int). The method
460 * does nothing otherwise.
461 */
462 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
463 if (mDialogFragment != null) {
464 mDialogFragment.mOnDismissListener = listener;
465 }
466 }
467
Amith Yamasanic861cf82012-10-02 14:51:46 -0700468 public void onDialogShowing() {
469 // override in subclass to attach a dismiss listener, for instance
470 }
471
Jason Monk39b46742015-09-10 15:52:51 -0400472 @Override
473 public void onDisplayPreferenceDialog(Preference preference) {
474 if (preference.getKey() == null) {
475 // Auto-key preferences that don't have a key, so the dialog can find them.
476 preference.setKey(UUID.randomUUID().toString());
477 }
478 DialogFragment f = null;
479 if (preference instanceof CustomListPreference) {
480 f = CustomListPreference.CustomListPreferenceDialogFragment
481 .newInstance(preference.getKey());
482 } else if (preference instanceof CustomDialogPreference) {
483 f = CustomDialogPreference.CustomPreferenceDialogFragment
484 .newInstance(preference.getKey());
485 } else if (preference instanceof CustomEditTextPreference) {
486 f = CustomEditTextPreference.CustomPreferenceDialogFragment
487 .newInstance(preference.getKey());
488 } else {
489 super.onDisplayPreferenceDialog(preference);
490 return;
491 }
492 f.setTargetFragment(this, 0);
493 f.show(getFragmentManager(), "dialog_preference");
494 onDialogShowing();
495 }
496
Amith Yamasani43c69782010-12-01 09:04:36 -0800497 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800498 private static final String KEY_DIALOG_ID = "key_dialog_id";
499 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
500
Amith Yamasanid7993472010-08-18 13:59:28 -0700501 private int mDialogId;
502
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800503 private Fragment mParentFragment;
504
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800505 private DialogInterface.OnCancelListener mOnCancelListener;
506 private DialogInterface.OnDismissListener mOnDismissListener;
507
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800508 public SettingsDialogFragment() {
509 /* do nothing */
510 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700511
Amith Yamasani43c69782010-12-01 09:04:36 -0800512 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700513 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800514 if (!(fragment instanceof Fragment)) {
515 throw new IllegalArgumentException("fragment argument must be an instance of "
516 + Fragment.class.getName());
517 }
518 mParentFragment = (Fragment) fragment;
519 }
520
521 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800522 public void onSaveInstanceState(Bundle outState) {
523 super.onSaveInstanceState(outState);
524 if (mParentFragment != null) {
525 outState.putInt(KEY_DIALOG_ID, mDialogId);
526 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
527 }
528 }
529
530 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700531 public void onStart() {
532 super.onStart();
533
534 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
535 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
536 }
537 }
538
539 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800540 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800541 if (savedInstanceState != null) {
542 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800543 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800544 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700545 if (mParentFragment == null) {
546 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
547 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800548 if (!(mParentFragment instanceof DialogCreatable)) {
549 throw new IllegalArgumentException(
550 (mParentFragment != null
551 ? mParentFragment.getClass().getName()
552 : mParentFragmentId)
553 + " must implement "
554 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800555 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800556 // This dialog fragment could be created from non-SettingsPreferenceFragment
557 if (mParentFragment instanceof SettingsPreferenceFragment) {
558 // restore mDialogFragment in mParentFragment
559 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
560 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800561 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800562 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700563 }
564
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800565 @Override
566 public void onCancel(DialogInterface dialog) {
567 super.onCancel(dialog);
568 if (mOnCancelListener != null) {
569 mOnCancelListener.onCancel(dialog);
570 }
571 }
572
573 @Override
574 public void onDismiss(DialogInterface dialog) {
575 super.onDismiss(dialog);
576 if (mOnDismissListener != null) {
577 mOnDismissListener.onDismiss(dialog);
578 }
579 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800580
Amith Yamasanid7993472010-08-18 13:59:28 -0700581 public int getDialogId() {
582 return mDialogId;
583 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800584
585 @Override
586 public void onDetach() {
587 super.onDetach();
588
Amith Yamasani8875ede2011-01-31 12:46:57 -0800589 // This dialog fragment could be created from non-SettingsPreferenceFragment
590 if (mParentFragment instanceof SettingsPreferenceFragment) {
591 // in case the dialog is not explicitly removed by removeDialog()
592 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
593 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
594 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800595 }
596 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700597 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700598
599 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800600 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700601 }
602
603 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800604 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700605 }
606
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700607 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700608 Activity activity = getActivity();
609 if (activity != null) {
610 activity.onBackPressed();
611 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700612 }
613
Jason Monk39b46742015-09-10 15:52:51 -0400614 protected final Context getPrefContext() {
615 return getPreferenceManager().getContext();
616 }
617
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700618 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
619 int requestCode, Bundle extras) {
620 final Activity activity = getActivity();
621 if (activity instanceof SettingsActivity) {
622 SettingsActivity sa = (SettingsActivity) activity;
623 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
624 return true;
625 } else if (activity instanceof PreferenceActivity) {
626 PreferenceActivity sa = (PreferenceActivity) activity;
627 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700628 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700629 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700630 Log.w(TAG,
631 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
632 + "launch the given Fragment (name: " + fragmentClass
633 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700634 return false;
635 }
636 }
Jason Monk65bb0972015-12-17 10:39:44 -0500637
638 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
639
640 private int mHighlightPosition = -1;
641
642 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
643 super(preferenceGroup);
644 }
645
646 public void highlight(int position) {
647 mHighlightPosition = position;
648 notifyDataSetChanged();
649 }
650
651 @Override
652 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
653 super.onBindViewHolder(holder, position);
654 if (position == mHighlightPosition) {
655 View v = holder.itemView;
656 if (v.getBackground() != null) {
657 final int centerX = v.getWidth() / 2;
658 final int centerY = v.getHeight() / 2;
659 v.getBackground().setHotspot(centerX, centerY);
660 }
661 v.setPressed(true);
662 v.setPressed(false);
663 mHighlightPosition = -1;
664 }
665 }
666 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700667}