blob: 6144f72d7e7553d64f0896ca1f82bb4c275327c0 [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 Monk91e2f892016-02-23 15:31:09 -050028import android.support.annotation.XmlRes;
Jason Monk39b46742015-09-10 15:52:51 -040029import android.support.v7.preference.Preference;
Jason Monk65bb0972015-12-17 10:39:44 -050030import android.support.v7.preference.PreferenceGroup;
Jason Monk39b46742015-09-10 15:52:51 -040031import android.support.v7.preference.PreferenceGroupAdapter;
32import android.support.v7.preference.PreferenceScreen;
Jason Monk65bb0972015-12-17 10:39:44 -050033import android.support.v7.preference.PreferenceViewHolder;
34import android.support.v7.widget.LinearLayoutManager;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v7.widget.RecyclerView;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070036import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070037import android.util.Log;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070038import android.view.LayoutInflater;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070039import android.view.Menu;
40import android.view.MenuInflater;
Fabrice Di Megliof2a52262014-04-17 17:20:27 -070041import android.view.View;
Fabrice Di Meglio86159282014-07-21 16:02:27 -070042import android.view.ViewGroup;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070043import android.widget.Button;
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
Jason Monk91e2f892016-02-23 15:31:09 -0500136 @Override
137 public void addPreferencesFromResource(@XmlRes int preferencesResId) {
138 super.addPreferencesFromResource(preferencesResId);
139 checkAvailablePrefs(getPreferenceScreen());
140 }
141
142 private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
143 if (preferenceGroup == null) return;
144 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
145 Preference pref = preferenceGroup.getPreference(i);
146 if (pref instanceof SelfAvailablePreference
147 && !((SelfAvailablePreference) pref).isAvailable(getContext())) {
148 preferenceGroup.removePreference(pref);
149 } else if (pref instanceof PreferenceGroup) {
150 checkAvailablePrefs((PreferenceGroup) pref);
151 }
152 }
153 }
154
John Spurlockb8e02b82015-04-15 21:15:55 -0400155 public FloatingActionButton getFloatingActionButton() {
156 return mFloatingActionButton;
157 }
158
Daichi Hirono5e76cdc2015-07-08 11:38:55 +0900159 public ViewGroup getButtonBar() {
160 return mButtonBar;
161 }
162
Maurice Lam28c3f6b2015-04-21 23:01:11 -0700163 public View setPinnedHeaderView(int layoutResId) {
164 final LayoutInflater inflater = getActivity().getLayoutInflater();
165 final View pinnedHeader =
166 inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false);
167 setPinnedHeaderView(pinnedHeader);
168 return pinnedHeader;
169 }
170
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700171 public void setPinnedHeaderView(View pinnedHeader) {
172 mPinnedHeaderFrameLayout.addView(pinnedHeader);
173 mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE);
174 }
175
Fabrice Di Meglio86159282014-07-21 16:02:27 -0700176 @Override
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700177 public void onSaveInstanceState(Bundle outState) {
178 super.onSaveInstanceState(outState);
179
180 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
181 }
182
183 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700184 public void onActivityCreated(Bundle savedInstanceState) {
185 super.onActivityCreated(savedInstanceState);
Anna Galusza384fa2e2016-02-03 15:51:12 -0800186 if (!TextUtils.isEmpty(mHelpUri)) {
Amith Yamasanib3a593e2012-04-23 18:03:52 -0700187 setHasOptionsMenu(true);
188 }
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700189 }
190
191 @Override
192 public void onResume() {
193 super.onResume();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700194
195 final Bundle args = getArguments();
196 if (args != null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700197 mPreferenceKey = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
198 highlightPreferenceIfNeeded();
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700199 }
200 }
201
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700202 @Override
203 protected void onBindPreferences() {
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700204 registerObserverIfNeeded();
205 }
206
207 @Override
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700208 protected void onUnbindPreferences() {
209 unregisterObserverIfNeeded();
210 }
211
Jason Monkb5aa73f2015-03-31 12:59:33 -0400212 public void showLoadingWhenEmpty() {
213 View loading = getView().findViewById(R.id.loading_container);
Jason Monk39b46742015-09-10 15:52:51 -0400214 setEmptyView(loading);
Jason Monkb5aa73f2015-03-31 12:59:33 -0400215 }
216
Jason Monkb37e2882016-01-11 14:27:20 -0500217 public void setLoading(boolean loading, boolean animate) {
218 View loading_container = getView().findViewById(R.id.loading_container);
219 Utils.handleLoadingContainer(loading_container, getListView(), !loading, animate);
220 }
221
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700222 public void registerObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700223 if (!mIsDataSetObserverRegistered) {
224 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400225 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700226 }
Jason Monk39b46742015-09-10 15:52:51 -0400227 mCurrentRootAdapter = getListView().getAdapter();
228 mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700229 mIsDataSetObserverRegistered = true;
Jason Monk77467e02016-01-30 12:15:11 -0500230 onDataSetChanged();
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700231 }
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700232 }
233
Fabrice Di Meglio405febf2014-04-24 10:13:59 -0700234 public void unregisterObserverIfNeeded() {
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700235 if (mIsDataSetObserverRegistered) {
236 if (mCurrentRootAdapter != null) {
Jason Monk39b46742015-09-10 15:52:51 -0400237 mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver);
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700238 mCurrentRootAdapter = null;
Fabrice Di Meglio7c435f62014-07-29 16:02:22 -0700239 }
Fabrice Di Megliod83b3c22014-08-13 10:45:19 -0700240 mIsDataSetObserverRegistered = false;
Fabrice Di Meglio829c8fb2014-04-21 11:40:21 -0700241 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700242 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700243
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700244 public void highlightPreferenceIfNeeded() {
Fabrice Di Meglioc853a422014-04-18 19:40:40 -0700245 if (isAdded() && !mPreferenceHighlighted &&!TextUtils.isEmpty(mPreferenceKey)) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700246 highlightPreference(mPreferenceKey);
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700247 }
Fabrice Di Meglio6602d022014-04-15 16:45:20 -0700248 }
249
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000250 protected void onDataSetChanged() {
Jason Monk39b46742015-09-10 15:52:51 -0400251 highlightPreferenceIfNeeded();
252 updateEmptyView();
253 }
254
Jason Monk39b46742015-09-10 15:52:51 -0400255 public LayoutPreference getHeaderView() {
256 return mHeader;
257 }
258
259 public LayoutPreference getFooterView() {
260 return mFooter;
261 }
262
263 protected void setHeaderView(int resource) {
264 mHeader = new LayoutPreference(getPrefContext(), resource);
Udam Sainid553abc2016-02-16 17:54:13 -0800265 addPreferenceToTop(mHeader);
266 }
267
268 protected void setHeaderView(View view) {
269 mHeader = new LayoutPreference(getPrefContext(), view);
270 addPreferenceToTop(mHeader);
271 }
272
273 private void addPreferenceToTop(LayoutPreference preference) {
274 preference.setOrder(ORDER_FIRST);
Jason Monk39b46742015-09-10 15:52:51 -0400275 if (getPreferenceScreen() != null) {
Udam Sainid553abc2016-02-16 17:54:13 -0800276 getPreferenceScreen().addPreference(preference);
Jason Monk39b46742015-09-10 15:52:51 -0400277 }
278 }
279
280 protected void setFooterView(int resource) {
281 setFooterView(resource != 0 ? new LayoutPreference(getPrefContext(), resource) : null);
282 }
283
284 protected void setFooterView(View v) {
285 setFooterView(v != null ? new LayoutPreference(getPrefContext(), v) : null);
286 }
287
288 private void setFooterView(LayoutPreference footer) {
289 if (getPreferenceScreen() != null && mFooter != null) {
290 getPreferenceScreen().removePreference(mFooter);
291 }
292 if (footer != null) {
293 mFooter = footer;
Sudheer Shanka5590e2e2016-01-22 20:40:56 +0000294 mFooter.setOrder(ORDER_LAST);
Jason Monk39b46742015-09-10 15:52:51 -0400295 if (getPreferenceScreen() != null) {
296 getPreferenceScreen().addPreference(mFooter);
297 }
298 } else {
299 mFooter = null;
300 }
301 }
302
303 @Override
304 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
305 super.setPreferenceScreen(preferenceScreen);
306 if (preferenceScreen != null) {
307 if (mHeader != null) {
308 preferenceScreen.addPreference(mHeader);
309 }
310 if (mFooter != null) {
311 preferenceScreen.addPreference(mFooter);
312 }
313 }
314 }
315
316 private void updateEmptyView() {
317 if (mEmptyView == null) return;
318 if (getPreferenceScreen() != null) {
319 boolean show = (getPreferenceScreen().getPreferenceCount()
320 - (mHeader != null ? 1 : 0)
321 - (mFooter != null ? 1 : 0)) <= 0;
322 mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE);
323 } else {
324 mEmptyView.setVisibility(View.VISIBLE);
325 }
326 }
327
328 public void setEmptyView(View v) {
Sudheer Shanka95a71e02016-01-12 10:36:18 +0000329 if (mEmptyView != null) {
330 mEmptyView.setVisibility(View.GONE);
331 }
Jason Monk39b46742015-09-10 15:52:51 -0400332 mEmptyView = v;
333 updateEmptyView();
334 }
335
336 public View getEmptyView() {
337 return mEmptyView;
338 }
339
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700340 /**
341 * Return a valid ListView position or -1 if none is found
342 */
343 private int canUseListViewForHighLighting(String key) {
Jason Monk39b46742015-09-10 15:52:51 -0400344 if (getListView() == null) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700345 return -1;
346 }
347
Jason Monk39b46742015-09-10 15:52:51 -0400348 RecyclerView listView = getListView();
349 RecyclerView.Adapter adapter = listView.getAdapter();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700350
351 if (adapter != null && adapter instanceof PreferenceGroupAdapter) {
Jason Monk39b46742015-09-10 15:52:51 -0400352 return findListPositionFromKey((PreferenceGroupAdapter) adapter, key);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700353 }
354
355 return -1;
356 }
357
Jason Monk65bb0972015-12-17 10:39:44 -0500358 @Override
359 public RecyclerView.LayoutManager onCreateLayoutManager() {
360 mLayoutManager = new LinearLayoutManager(getContext());
361 return mLayoutManager;
362 }
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700363
Jason Monk65bb0972015-12-17 10:39:44 -0500364 @Override
365 protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
366 mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen);
367 return mAdapter;
368 }
369
370 private void highlightPreference(String key) {
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700371 final int position = canUseListViewForHighLighting(key);
372 if (position >= 0) {
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700373 mPreferenceHighlighted = true;
Jason Monk65bb0972015-12-17 10:39:44 -0500374 mLayoutManager.scrollToPosition(position);
Fabrice Di Meglio4a2ee7e2014-05-21 16:19:41 -0700375
Jason Monk65bb0972015-12-17 10:39:44 -0500376 getView().postDelayed(new Runnable() {
377 @Override
378 public void run() {
379 mAdapter.highlight(position);
380 }
381 }, DELAY_HIGHLIGHT_DURATION_MILLIS);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700382 }
383 }
384
Jason Monk39b46742015-09-10 15:52:51 -0400385 private int findListPositionFromKey(PreferenceGroupAdapter adapter, String key) {
386 final int count = adapter.getItemCount();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700387 for (int n = 0; n < count; n++) {
Jason Monk39b46742015-09-10 15:52:51 -0400388 final Preference preference = adapter.getItem(n);
389 final String preferenceKey = preference.getKey();
390 if (preferenceKey != null && preferenceKey.equals(key)) {
391 return n;
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700392 }
393 }
394 return -1;
Amith Yamasanid7993472010-08-18 13:59:28 -0700395 }
396
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700397 protected void removePreference(String key) {
398 Preference pref = findPreference(key);
399 if (pref != null) {
400 getPreferenceScreen().removePreference(pref);
401 }
402 }
403
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700404 /**
405 * Override this if you want to show a help item in the menu, by returning the resource id.
406 * @return the resource id for the help url
407 */
408 protected int getHelpResource() {
Jason Monk23acc2b2015-04-14 15:06:39 -0400409 return R.string.help_uri_default;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700410 }
411
412 @Override
413 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Jason Monk23acc2b2015-04-14 15:06:39 -0400414 if (mHelpUri != null && getActivity() != null) {
Jason Monk15dcebe2015-05-27 16:02:08 -0400415 HelpUtils.prepareHelpMenuItem(getActivity(), menu, mHelpUri, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700416 }
417 }
418
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700419 /*
420 * The name is intentionally made different from Activity#finish(), so that
421 * users won't misunderstand its meaning.
422 */
423 public final void finishFragment() {
424 getActivity().onBackPressed();
425 }
426
Amith Yamasanid7993472010-08-18 13:59:28 -0700427 // Some helpers for functions used by the settings fragments when they were activities
428
429 /**
430 * Returns the ContentResolver from the owning Activity.
431 */
432 protected ContentResolver getContentResolver() {
Amith Yamasani350938e2013-04-09 10:22:47 -0700433 Context context = getActivity();
434 if (context != null) {
435 mContentResolver = context.getContentResolver();
436 }
437 return mContentResolver;
Amith Yamasanid7993472010-08-18 13:59:28 -0700438 }
439
440 /**
441 * Returns the specified system service from the owning Activity.
442 */
443 protected Object getSystemService(final String name) {
444 return getActivity().getSystemService(name);
445 }
446
447 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700448 * Returns the PackageManager from the owning Activity.
449 */
450 protected PackageManager getPackageManager() {
451 return getActivity().getPackageManager();
452 }
453
Dianne Hackborn0385cf12011-01-24 16:22:13 -0800454 @Override
455 public void onDetach() {
456 if (isRemoving()) {
457 if (mDialogFragment != null) {
458 mDialogFragment.dismiss();
459 mDialogFragment = null;
460 }
461 }
462 super.onDetach();
463 }
464
Amith Yamasanid7993472010-08-18 13:59:28 -0700465 // Dialog management
466
467 protected void showDialog(int dialogId) {
468 if (mDialogFragment != null) {
469 Log.e(TAG, "Old dialog fragment not null!");
470 }
471 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800472 mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700473 }
474
475 public Dialog onCreateDialog(int dialogId) {
476 return null;
477 }
478
479 protected void removeDialog(int dialogId) {
Hung-ying Tyanadc83d82011-01-24 15:05:27 +0800480 // mDialogFragment may not be visible yet in parent fragment's onResume().
481 // To be able to dismiss dialog at that time, don't check
482 // mDialogFragment.isVisible().
483 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700484 mDialogFragment.dismiss();
485 }
486 mDialogFragment = null;
487 }
488
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800489 /**
490 * Sets the OnCancelListener of the dialog shown. This method can only be
491 * called after showDialog(int) and before removeDialog(int). The method
492 * does nothing otherwise.
493 */
494 protected void setOnCancelListener(DialogInterface.OnCancelListener listener) {
495 if (mDialogFragment != null) {
496 mDialogFragment.mOnCancelListener = listener;
497 }
498 }
499
500 /**
501 * Sets the OnDismissListener of the dialog shown. This method can only be
502 * called after showDialog(int) and before removeDialog(int). The method
503 * does nothing otherwise.
504 */
505 protected void setOnDismissListener(DialogInterface.OnDismissListener listener) {
506 if (mDialogFragment != null) {
507 mDialogFragment.mOnDismissListener = listener;
508 }
509 }
510
Amith Yamasanic861cf82012-10-02 14:51:46 -0700511 public void onDialogShowing() {
512 // override in subclass to attach a dismiss listener, for instance
513 }
514
Jason Monk39b46742015-09-10 15:52:51 -0400515 @Override
516 public void onDisplayPreferenceDialog(Preference preference) {
517 if (preference.getKey() == null) {
518 // Auto-key preferences that don't have a key, so the dialog can find them.
519 preference.setKey(UUID.randomUUID().toString());
520 }
521 DialogFragment f = null;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000522 if (preference instanceof RestrictedListPreference) {
523 f = RestrictedListPreference.RestrictedListPreferenceDialogFragment
524 .newInstance(preference.getKey());
525 } else if (preference instanceof CustomListPreference) {
Jason Monk39b46742015-09-10 15:52:51 -0400526 f = CustomListPreference.CustomListPreferenceDialogFragment
527 .newInstance(preference.getKey());
528 } else if (preference instanceof CustomDialogPreference) {
529 f = CustomDialogPreference.CustomPreferenceDialogFragment
530 .newInstance(preference.getKey());
531 } else if (preference instanceof CustomEditTextPreference) {
532 f = CustomEditTextPreference.CustomPreferenceDialogFragment
533 .newInstance(preference.getKey());
534 } else {
535 super.onDisplayPreferenceDialog(preference);
536 return;
537 }
538 f.setTargetFragment(this, 0);
539 f.show(getFragmentManager(), "dialog_preference");
540 onDialogShowing();
541 }
542
Amith Yamasani43c69782010-12-01 09:04:36 -0800543 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800544 private static final String KEY_DIALOG_ID = "key_dialog_id";
545 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
546
Amith Yamasanid7993472010-08-18 13:59:28 -0700547 private int mDialogId;
548
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800549 private Fragment mParentFragment;
550
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800551 private DialogInterface.OnCancelListener mOnCancelListener;
552 private DialogInterface.OnDismissListener mOnDismissListener;
553
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800554 public SettingsDialogFragment() {
555 /* do nothing */
556 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700557
Amith Yamasani43c69782010-12-01 09:04:36 -0800558 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700559 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800560 if (!(fragment instanceof Fragment)) {
561 throw new IllegalArgumentException("fragment argument must be an instance of "
562 + Fragment.class.getName());
563 }
564 mParentFragment = (Fragment) fragment;
565 }
566
567 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800568 public void onSaveInstanceState(Bundle outState) {
569 super.onSaveInstanceState(outState);
570 if (mParentFragment != null) {
571 outState.putInt(KEY_DIALOG_ID, mDialogId);
572 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
573 }
574 }
575
576 @Override
Amith Yamasanic861cf82012-10-02 14:51:46 -0700577 public void onStart() {
578 super.onStart();
579
580 if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) {
581 ((SettingsPreferenceFragment) mParentFragment).onDialogShowing();
582 }
583 }
584
585 @Override
Dianne Hackborn300768f2011-01-27 20:39:21 -0800586 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800587 if (savedInstanceState != null) {
588 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800589 mParentFragment = getParentFragment();
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800590 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
Fabrice Di Megliob7bd72f2014-07-25 13:03:09 -0700591 if (mParentFragment == null) {
592 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
593 }
Fabrice Di Meglio377dd622014-02-12 20:05:57 -0800594 if (!(mParentFragment instanceof DialogCreatable)) {
595 throw new IllegalArgumentException(
596 (mParentFragment != null
597 ? mParentFragment.getClass().getName()
598 : mParentFragmentId)
599 + " must implement "
600 + DialogCreatable.class.getName());
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800601 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800602 // This dialog fragment could be created from non-SettingsPreferenceFragment
603 if (mParentFragment instanceof SettingsPreferenceFragment) {
604 // restore mDialogFragment in mParentFragment
605 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this;
606 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800607 }
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800608 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700609 }
610
Hung-ying Tyan0ee51e02011-01-25 16:42:14 +0800611 @Override
612 public void onCancel(DialogInterface dialog) {
613 super.onCancel(dialog);
614 if (mOnCancelListener != null) {
615 mOnCancelListener.onCancel(dialog);
616 }
617 }
618
619 @Override
620 public void onDismiss(DialogInterface dialog) {
621 super.onDismiss(dialog);
622 if (mOnDismissListener != null) {
623 mOnDismissListener.onDismiss(dialog);
624 }
625 }
Amith Yamasani8875ede2011-01-31 12:46:57 -0800626
Amith Yamasanid7993472010-08-18 13:59:28 -0700627 public int getDialogId() {
628 return mDialogId;
629 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800630
631 @Override
632 public void onDetach() {
633 super.onDetach();
634
Amith Yamasani8875ede2011-01-31 12:46:57 -0800635 // This dialog fragment could be created from non-SettingsPreferenceFragment
636 if (mParentFragment instanceof SettingsPreferenceFragment) {
637 // in case the dialog is not explicitly removed by removeDialog()
638 if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) {
639 ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null;
640 }
Hung-ying Tyan18eb39d2011-01-28 16:17:27 +0800641 }
642 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700643 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700644
645 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800646 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700647 }
648
649 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800650 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700651 }
652
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700653 public void finish() {
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700654 Activity activity = getActivity();
Jason Monk30e9fc82016-02-10 12:55:37 -0500655 if (activity != null && !activity.getFragmentManager().popBackStackImmediate()) {
Udam Saini6a8b99d2016-02-10 16:07:41 -0800656 activity.finish();
Jorim Jaggif92fbc12015-08-10 18:11:07 -0700657 }
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700658 }
659
Jason Monk39b46742015-09-10 15:52:51 -0400660 protected final Context getPrefContext() {
661 return getPreferenceManager().getContext();
662 }
663
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700664 public boolean startFragment(Fragment caller, String fragmentClass, int titleRes,
665 int requestCode, Bundle extras) {
666 final Activity activity = getActivity();
667 if (activity instanceof SettingsActivity) {
668 SettingsActivity sa = (SettingsActivity) activity;
669 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
670 return true;
671 } else if (activity instanceof PreferenceActivity) {
672 PreferenceActivity sa = (PreferenceActivity) activity;
673 sa.startPreferencePanel(fragmentClass, extras, titleRes, null, caller, requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700674 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700675 } else {
Fabrice Di Meglio5bdf0422014-07-01 15:15:18 -0700676 Log.w(TAG,
677 "Parent isn't SettingsActivity nor PreferenceActivity, thus there's no way to "
678 + "launch the given Fragment (name: " + fragmentClass
679 + ", requestCode: " + requestCode + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700680 return false;
681 }
682 }
Jason Monk65bb0972015-12-17 10:39:44 -0500683
684 public static class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
685
686 private int mHighlightPosition = -1;
687
688 public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
689 super(preferenceGroup);
690 }
691
692 public void highlight(int position) {
693 mHighlightPosition = position;
694 notifyDataSetChanged();
695 }
696
697 @Override
698 public void onBindViewHolder(PreferenceViewHolder holder, int position) {
699 super.onBindViewHolder(holder, position);
700 if (position == mHighlightPosition) {
701 View v = holder.itemView;
702 if (v.getBackground() != null) {
703 final int centerX = v.getWidth() / 2;
704 final int centerY = v.getHeight() / 2;
705 v.getBackground().setHotspot(centerX, centerY);
706 }
707 v.setPressed(true);
708 v.setPressed(false);
709 mHighlightPosition = -1;
710 }
711 }
712 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700713}