Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Fabrice Di Meglio | 5bdf042 | 2014-07-01 15:15:18 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 20 | import android.app.Dialog; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 23 | import android.content.DialogInterface; |
Jason Monk | b7e4380 | 2016-06-06 16:01:58 -0400 | [diff] [blame] | 24 | import android.content.Intent; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 28 | import android.util.ArrayMap; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 31 | import android.view.View; |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 33 | import android.widget.Button; |
Jason Monk | b7e4380 | 2016-06-06 16:01:58 -0400 | [diff] [blame] | 34 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 35 | import androidx.annotation.VisibleForTesting; |
| 36 | import androidx.annotation.XmlRes; |
| 37 | import androidx.fragment.app.DialogFragment; |
| 38 | import androidx.fragment.app.Fragment; |
| 39 | import androidx.preference.Preference; |
| 40 | import androidx.preference.PreferenceGroup; |
| 41 | import androidx.preference.PreferenceScreen; |
| 42 | import androidx.recyclerview.widget.LinearLayoutManager; |
| 43 | import androidx.recyclerview.widget.RecyclerView; |
| 44 | |
Fan Zhang | 2d0b344 | 2016-12-05 17:02:33 -0800 | [diff] [blame] | 45 | import com.android.settings.core.InstrumentedPreferenceFragment; |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 46 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
Fan Zhang | 78ea7da | 2018-07-02 13:44:57 -0700 | [diff] [blame] | 47 | import com.android.settings.search.Indexable; |
Fan Zhang | 681a4cd | 2017-11-29 16:57:19 -0800 | [diff] [blame] | 48 | import com.android.settings.search.actionbar.SearchMenuController; |
Fan Zhang | e0b0e9f | 2017-11-29 14:55:59 -0800 | [diff] [blame] | 49 | import com.android.settings.support.actionbar.HelpMenuController; |
| 50 | import com.android.settings.support.actionbar.HelpResourceProvider; |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 51 | import com.android.settings.widget.HighlightablePreferenceGroupAdapter; |
Fan Zhang | 896f1b3 | 2017-06-26 14:22:45 -0700 | [diff] [blame] | 52 | import com.android.settings.widget.LoadingViewController; |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 53 | import com.android.settingslib.CustomDialogPreferenceCompat; |
| 54 | import com.android.settingslib.CustomEditTextPreferenceCompat; |
Leif Hendrik Wilden | 28dee1f | 2018-01-11 10:15:36 -0800 | [diff] [blame] | 55 | import com.android.settingslib.core.instrumentation.Instrumentable; |
tmfang | dce94bb | 2018-11-26 18:41:01 +0800 | [diff] [blame] | 56 | import com.android.settingslib.widget.LayoutPreference; |
John Spurlock | b8e02b8 | 2015-04-15 21:15:55 -0400 | [diff] [blame] | 57 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 58 | import java.util.UUID; |
| 59 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 60 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 61 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 62 | */ |
Fan Zhang | 2d0b344 | 2016-12-05 17:02:33 -0800 | [diff] [blame] | 63 | public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment |
Fan Zhang | 78ea7da | 2018-07-02 13:44:57 -0700 | [diff] [blame] | 64 | implements DialogCreatable, HelpResourceProvider, Indexable { |
Anna Galusza | 0285c80 | 2016-01-29 17:32:19 -0800 | [diff] [blame] | 65 | |
Jason Monk | 65bb097 | 2015-12-17 10:39:44 -0500 | [diff] [blame] | 66 | private static final String TAG = "SettingsPreference"; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 67 | |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 68 | private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted"; |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 69 | |
Sudheer Shanka | 5590e2e | 2016-01-22 20:40:56 +0000 | [diff] [blame] | 70 | private static final int ORDER_FIRST = -1; |
Sudheer Shanka | 5590e2e | 2016-01-22 20:40:56 +0000 | [diff] [blame] | 71 | |
Fan Zhang | e0b0e9f | 2017-11-29 14:55:59 -0800 | [diff] [blame] | 72 | private SettingsDialogFragment mDialogFragment; |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame] | 73 | // Cache the content resolver for async callbacks |
| 74 | private ContentResolver mContentResolver; |
| 75 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 76 | private RecyclerView.Adapter mCurrentRootAdapter; |
Fabrice Di Meglio | 829c8fb | 2014-04-21 11:40:21 -0700 | [diff] [blame] | 77 | private boolean mIsDataSetObserverRegistered = false; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 78 | private RecyclerView.AdapterDataObserver mDataSetObserver = |
| 79 | new RecyclerView.AdapterDataObserver() { |
Tony Mantler | 0b825f5 | 2016-09-27 14:48:16 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public void onChanged() { |
| 82 | onDataSetChanged(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onItemRangeChanged(int positionStart, int itemCount) { |
| 87 | onDataSetChanged(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { |
| 92 | onDataSetChanged(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onItemRangeInserted(int positionStart, int itemCount) { |
| 97 | onDataSetChanged(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onItemRangeRemoved(int positionStart, int itemCount) { |
| 102 | onDataSetChanged(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { |
| 107 | onDataSetChanged(); |
| 108 | } |
| 109 | }; |
Fabrice Di Meglio | c853a42 | 2014-04-18 19:40:40 -0700 | [diff] [blame] | 110 | |
Jason Chiu | cfa36db | 2019-06-05 17:31:31 +0800 | [diff] [blame] | 111 | @VisibleForTesting |
| 112 | ViewGroup mPinnedHeaderFrameLayout; |
Daichi Hirono | 5e76cdc | 2015-07-08 11:38:55 +0900 | [diff] [blame] | 113 | private ViewGroup mButtonBar; |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 114 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 115 | private LayoutPreference mHeader; |
| 116 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 117 | private View mEmptyView; |
Jason Monk | 65bb097 | 2015-12-17 10:39:44 -0500 | [diff] [blame] | 118 | private LinearLayoutManager mLayoutManager; |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 119 | private ArrayMap<String, Preference> mPreferenceCache; |
Jason Monk | f38fb38 | 2016-03-18 14:23:01 -0400 | [diff] [blame] | 120 | private boolean mAnimationAllowed; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 121 | |
Matthew Fritze | 33f3e3f | 2017-06-06 17:14:33 -0700 | [diff] [blame] | 122 | @VisibleForTesting |
| 123 | public HighlightablePreferenceGroupAdapter mAdapter; |
| 124 | @VisibleForTesting |
| 125 | public boolean mPreferenceHighlighted = false; |
| 126 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void onCreate(Bundle icicle) { |
| 129 | super.onCreate(icicle); |
Fan Zhang | 681a4cd | 2017-11-29 16:57:19 -0800 | [diff] [blame] | 130 | SearchMenuController.init(this /* host */); |
Fan Zhang | e0b0e9f | 2017-11-29 14:55:59 -0800 | [diff] [blame] | 131 | HelpMenuController.init(this /* host */); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 132 | |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 133 | if (icicle != null) { |
| 134 | mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY); |
| 135 | } |
Fan Zhang | 72456a9 | 2018-02-20 11:25:56 -0800 | [diff] [blame] | 136 | HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 139 | @Override |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 140 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 141 | Bundle savedInstanceState) { |
| 142 | final View root = super.onCreateView(inflater, container, savedInstanceState); |
Fan Zhang | e0b0e9f | 2017-11-29 14:55:59 -0800 | [diff] [blame] | 143 | mPinnedHeaderFrameLayout = root.findViewById(R.id.pinned_header); |
| 144 | mButtonBar = root.findViewById(R.id.button_bar); |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 145 | return root; |
| 146 | } |
| 147 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 148 | @Override |
Jason Monk | 91e2f89 | 2016-02-23 15:31:09 -0500 | [diff] [blame] | 149 | public void addPreferencesFromResource(@XmlRes int preferencesResId) { |
| 150 | super.addPreferencesFromResource(preferencesResId); |
| 151 | checkAvailablePrefs(getPreferenceScreen()); |
| 152 | } |
| 153 | |
Doris Ling | fe52594 | 2018-11-27 14:58:55 -0800 | [diff] [blame] | 154 | @VisibleForTesting |
| 155 | void checkAvailablePrefs(PreferenceGroup preferenceGroup) { |
Jason Monk | 91e2f89 | 2016-02-23 15:31:09 -0500 | [diff] [blame] | 156 | if (preferenceGroup == null) return; |
| 157 | for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) { |
| 158 | Preference pref = preferenceGroup.getPreference(i); |
| 159 | if (pref instanceof SelfAvailablePreference |
| 160 | && !((SelfAvailablePreference) pref).isAvailable(getContext())) { |
Doris Ling | fe52594 | 2018-11-27 14:58:55 -0800 | [diff] [blame] | 161 | pref.setVisible(false); |
Jason Monk | 91e2f89 | 2016-02-23 15:31:09 -0500 | [diff] [blame] | 162 | } else if (pref instanceof PreferenceGroup) { |
| 163 | checkAvailablePrefs((PreferenceGroup) pref); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Daichi Hirono | 5e76cdc | 2015-07-08 11:38:55 +0900 | [diff] [blame] | 168 | public ViewGroup getButtonBar() { |
| 169 | return mButtonBar; |
| 170 | } |
| 171 | |
Maurice Lam | 28c3f6b | 2015-04-21 23:01:11 -0700 | [diff] [blame] | 172 | public View setPinnedHeaderView(int layoutResId) { |
| 173 | final LayoutInflater inflater = getActivity().getLayoutInflater(); |
| 174 | final View pinnedHeader = |
| 175 | inflater.inflate(layoutResId, mPinnedHeaderFrameLayout, false); |
| 176 | setPinnedHeaderView(pinnedHeader); |
| 177 | return pinnedHeader; |
| 178 | } |
| 179 | |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 180 | public void setPinnedHeaderView(View pinnedHeader) { |
| 181 | mPinnedHeaderFrameLayout.addView(pinnedHeader); |
| 182 | mPinnedHeaderFrameLayout.setVisibility(View.VISIBLE); |
| 183 | } |
| 184 | |
Jason Chiu | cfa36db | 2019-06-05 17:31:31 +0800 | [diff] [blame] | 185 | public void showPinnedHeader(boolean show) { |
| 186 | mPinnedHeaderFrameLayout.setVisibility(show ? View.VISIBLE : View.INVISIBLE); |
| 187 | } |
| 188 | |
Fabrice Di Meglio | 8615928 | 2014-07-21 16:02:27 -0700 | [diff] [blame] | 189 | @Override |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 190 | public void onSaveInstanceState(Bundle outState) { |
| 191 | super.onSaveInstanceState(outState); |
| 192 | |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 193 | if (mAdapter != null) { |
| 194 | outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mAdapter.isHighlightRequested()); |
| 195 | } |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 199 | public void onActivityCreated(Bundle savedInstanceState) { |
| 200 | super.onActivityCreated(savedInstanceState); |
Johan Redestig | 76218e5 | 2016-04-19 08:29:30 +0200 | [diff] [blame] | 201 | setHasOptionsMenu(true); |
Fabrice Di Meglio | 4a2ee7e | 2014-05-21 16:19:41 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void onResume() { |
| 206 | super.onResume(); |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 207 | highlightPreferenceIfNeeded(); |
Fabrice Di Meglio | c145732 | 2014-04-04 19:07:50 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 210 | @Override |
| 211 | protected void onBindPreferences() { |
Fabrice Di Meglio | 405febf | 2014-04-24 10:13:59 -0700 | [diff] [blame] | 212 | registerObserverIfNeeded(); |
| 213 | } |
| 214 | |
| 215 | @Override |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 216 | protected void onUnbindPreferences() { |
| 217 | unregisterObserverIfNeeded(); |
| 218 | } |
| 219 | |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 220 | public void setLoading(boolean loading, boolean animate) { |
Fan Zhang | 896f1b3 | 2017-06-26 14:22:45 -0700 | [diff] [blame] | 221 | View loadingContainer = getView().findViewById(R.id.loading_container); |
| 222 | LoadingViewController.handleLoadingContainer(loadingContainer, getListView(), |
| 223 | !loading /* done */, |
| 224 | animate); |
Jason Monk | b37e288 | 2016-01-11 14:27:20 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Fabrice Di Meglio | 405febf | 2014-04-24 10:13:59 -0700 | [diff] [blame] | 227 | public void registerObserverIfNeeded() { |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 228 | if (!mIsDataSetObserverRegistered) { |
| 229 | if (mCurrentRootAdapter != null) { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 230 | mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver); |
Fabrice Di Meglio | 7c435f6 | 2014-07-29 16:02:22 -0700 | [diff] [blame] | 231 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 232 | mCurrentRootAdapter = getListView().getAdapter(); |
| 233 | mCurrentRootAdapter.registerAdapterDataObserver(mDataSetObserver); |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 234 | mIsDataSetObserverRegistered = true; |
Jason Monk | 77467e0 | 2016-01-30 12:15:11 -0500 | [diff] [blame] | 235 | onDataSetChanged(); |
Fabrice Di Meglio | 829c8fb | 2014-04-21 11:40:21 -0700 | [diff] [blame] | 236 | } |
Fabrice Di Meglio | c853a42 | 2014-04-18 19:40:40 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Fabrice Di Meglio | 405febf | 2014-04-24 10:13:59 -0700 | [diff] [blame] | 239 | public void unregisterObserverIfNeeded() { |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 240 | if (mIsDataSetObserverRegistered) { |
| 241 | if (mCurrentRootAdapter != null) { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 242 | mCurrentRootAdapter.unregisterAdapterDataObserver(mDataSetObserver); |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 243 | mCurrentRootAdapter = null; |
Fabrice Di Meglio | 7c435f6 | 2014-07-29 16:02:22 -0700 | [diff] [blame] | 244 | } |
Fabrice Di Meglio | d83b3c2 | 2014-08-13 10:45:19 -0700 | [diff] [blame] | 245 | mIsDataSetObserverRegistered = false; |
Fabrice Di Meglio | 829c8fb | 2014-04-21 11:40:21 -0700 | [diff] [blame] | 246 | } |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 247 | } |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 248 | |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 249 | public void highlightPreferenceIfNeeded() { |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 250 | if (!isAdded()) { |
| 251 | return; |
| 252 | } |
| 253 | if (mAdapter != null) { |
| 254 | mAdapter.requestHighlight(getView(), getListView()); |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 255 | } |
Fabrice Di Meglio | 6602d02 | 2014-04-15 16:45:20 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Fan Zhang | 72456a9 | 2018-02-20 11:25:56 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Returns initial expanded child count. |
| 260 | * <p/> |
| 261 | * Only override this method if the initial expanded child must be determined at run time. |
| 262 | */ |
| 263 | public int getInitialExpandedChildCount() { |
| 264 | return 0; |
| 265 | } |
| 266 | |
Sudheer Shanka | 95a71e0 | 2016-01-12 10:36:18 +0000 | [diff] [blame] | 267 | protected void onDataSetChanged() { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 268 | highlightPreferenceIfNeeded(); |
| 269 | updateEmptyView(); |
| 270 | } |
| 271 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 272 | public LayoutPreference getHeaderView() { |
| 273 | return mHeader; |
| 274 | } |
| 275 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 276 | protected void setHeaderView(int resource) { |
| 277 | mHeader = new LayoutPreference(getPrefContext(), resource); |
Yanting Yang | 70a8e31 | 2019-05-21 21:05:32 +0800 | [diff] [blame] | 278 | mHeader.setSelectable(false); |
Udam Saini | d553abc | 2016-02-16 17:54:13 -0800 | [diff] [blame] | 279 | addPreferenceToTop(mHeader); |
| 280 | } |
| 281 | |
| 282 | protected void setHeaderView(View view) { |
| 283 | mHeader = new LayoutPreference(getPrefContext(), view); |
Yanting Yang | 70a8e31 | 2019-05-21 21:05:32 +0800 | [diff] [blame] | 284 | mHeader.setSelectable(false); |
Udam Saini | d553abc | 2016-02-16 17:54:13 -0800 | [diff] [blame] | 285 | addPreferenceToTop(mHeader); |
| 286 | } |
| 287 | |
| 288 | private void addPreferenceToTop(LayoutPreference preference) { |
| 289 | preference.setOrder(ORDER_FIRST); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 290 | if (getPreferenceScreen() != null) { |
Udam Saini | d553abc | 2016-02-16 17:54:13 -0800 | [diff] [blame] | 291 | getPreferenceScreen().addPreference(preference); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 295 | @Override |
| 296 | public void setPreferenceScreen(PreferenceScreen preferenceScreen) { |
Jason Monk | 1cb12bb | 2016-03-29 13:21:48 -0400 | [diff] [blame] | 297 | if (preferenceScreen != null && !preferenceScreen.isAttached()) { |
Jason Monk | f38fb38 | 2016-03-18 14:23:01 -0400 | [diff] [blame] | 298 | // Without ids generated, the RecyclerView won't animate changes to the preferences. |
| 299 | preferenceScreen.setShouldUseGeneratedIds(mAnimationAllowed); |
| 300 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 301 | super.setPreferenceScreen(preferenceScreen); |
| 302 | if (preferenceScreen != null) { |
| 303 | if (mHeader != null) { |
| 304 | preferenceScreen.addPreference(mHeader); |
| 305 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
jackqdyulei | 2b2abac | 2017-05-26 10:47:55 -0700 | [diff] [blame] | 309 | @VisibleForTesting |
| 310 | void updateEmptyView() { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 311 | if (mEmptyView == null) return; |
| 312 | if (getPreferenceScreen() != null) { |
jackqdyulei | 2b2abac | 2017-05-26 10:47:55 -0700 | [diff] [blame] | 313 | final View listContainer = getActivity().findViewById(android.R.id.list_container); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 314 | boolean show = (getPreferenceScreen().getPreferenceCount() |
Sunny Shao | bf43497 | 2019-08-29 14:28:13 +0800 | [diff] [blame] | 315 | - (mHeader != null ? 1 : 0)) <= 0 |
jackqdyulei | 2b2abac | 2017-05-26 10:47:55 -0700 | [diff] [blame] | 316 | || (listContainer != null && listContainer.getVisibility() != View.VISIBLE); |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 317 | mEmptyView.setVisibility(show ? View.VISIBLE : View.GONE); |
| 318 | } else { |
| 319 | mEmptyView.setVisibility(View.VISIBLE); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | public void setEmptyView(View v) { |
Sudheer Shanka | 95a71e0 | 2016-01-12 10:36:18 +0000 | [diff] [blame] | 324 | if (mEmptyView != null) { |
| 325 | mEmptyView.setVisibility(View.GONE); |
| 326 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 327 | mEmptyView = v; |
| 328 | updateEmptyView(); |
| 329 | } |
| 330 | |
| 331 | public View getEmptyView() { |
| 332 | return mEmptyView; |
| 333 | } |
| 334 | |
Jason Monk | 65bb097 | 2015-12-17 10:39:44 -0500 | [diff] [blame] | 335 | @Override |
| 336 | public RecyclerView.LayoutManager onCreateLayoutManager() { |
| 337 | mLayoutManager = new LinearLayoutManager(getContext()); |
| 338 | return mLayoutManager; |
| 339 | } |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 340 | |
Jason Monk | 65bb097 | 2015-12-17 10:39:44 -0500 | [diff] [blame] | 341 | @Override |
| 342 | protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) { |
Fan Zhang | a100046 | 2018-02-02 12:15:37 -0800 | [diff] [blame] | 343 | final Bundle arguments = getArguments(); |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 344 | mAdapter = new HighlightablePreferenceGroupAdapter(preferenceScreen, |
Fan Zhang | a100046 | 2018-02-02 12:15:37 -0800 | [diff] [blame] | 345 | arguments == null |
| 346 | ? null : arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY), |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 347 | mPreferenceHighlighted); |
Jason Monk | 65bb097 | 2015-12-17 10:39:44 -0500 | [diff] [blame] | 348 | return mAdapter; |
| 349 | } |
| 350 | |
Jason Monk | f38fb38 | 2016-03-18 14:23:01 -0400 | [diff] [blame] | 351 | protected void setAnimationAllowed(boolean animationAllowed) { |
| 352 | mAnimationAllowed = animationAllowed; |
| 353 | } |
| 354 | |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 355 | protected void cacheRemoveAllPrefs(PreferenceGroup group) { |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 356 | mPreferenceCache = new ArrayMap<>(); |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 357 | final int N = group.getPreferenceCount(); |
| 358 | for (int i = 0; i < N; i++) { |
| 359 | Preference p = group.getPreference(i); |
| 360 | if (TextUtils.isEmpty(p.getKey())) { |
| 361 | continue; |
| 362 | } |
| 363 | mPreferenceCache.put(p.getKey(), p); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | protected Preference getCachedPreference(String key) { |
| 368 | return mPreferenceCache != null ? mPreferenceCache.remove(key) : null; |
| 369 | } |
| 370 | |
| 371 | protected void removeCachedPrefs(PreferenceGroup group) { |
| 372 | for (Preference p : mPreferenceCache.values()) { |
| 373 | group.removePreference(p); |
| 374 | } |
Jason Monk | db7868e | 2016-06-30 15:17:57 -0400 | [diff] [blame] | 375 | mPreferenceCache = null; |
Jason Monk | 2071eda | 2016-02-25 13:55:48 -0500 | [diff] [blame] | 376 | } |
| 377 | |
Jason Monk | a627844 | 2016-04-21 10:12:30 -0400 | [diff] [blame] | 378 | protected int getCachedCount() { |
Jason Monk | db7868e | 2016-06-30 15:17:57 -0400 | [diff] [blame] | 379 | return mPreferenceCache != null ? mPreferenceCache.size() : 0; |
Jason Monk | a627844 | 2016-04-21 10:12:30 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Jan Nordqvist | 9eb43dd | 2018-03-26 15:29:44 -0700 | [diff] [blame] | 382 | @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) |
| 383 | public boolean removePreference(String key) { |
Fan Zhang | e84407f | 2017-05-24 11:19:52 -0700 | [diff] [blame] | 384 | return removePreference(getPreferenceScreen(), key); |
| 385 | } |
| 386 | |
| 387 | @VisibleForTesting |
| 388 | boolean removePreference(PreferenceGroup group, String key) { |
| 389 | final int preferenceCount = group.getPreferenceCount(); |
| 390 | for (int i = 0; i < preferenceCount; i++) { |
| 391 | final Preference preference = group.getPreference(i); |
| 392 | final String curKey = preference.getKey(); |
| 393 | |
| 394 | if (TextUtils.equals(curKey, key)) { |
| 395 | return group.removePreference(preference); |
| 396 | } |
| 397 | |
| 398 | if (preference instanceof PreferenceGroup) { |
| 399 | if (removePreference((PreferenceGroup) preference, key)) { |
| 400 | return true; |
| 401 | } |
| 402 | } |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 403 | } |
Fan Zhang | e84407f | 2017-05-24 11:19:52 -0700 | [diff] [blame] | 404 | return false; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Daisuke Miyakawa | b5647c5 | 2010-09-10 18:04:02 -0700 | [diff] [blame] | 407 | /* |
| 408 | * The name is intentionally made different from Activity#finish(), so that |
| 409 | * users won't misunderstand its meaning. |
| 410 | */ |
| 411 | public final void finishFragment() { |
| 412 | getActivity().onBackPressed(); |
| 413 | } |
| 414 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 415 | // Some helpers for functions used by the settings fragments when they were activities |
| 416 | |
| 417 | /** |
| 418 | * Returns the ContentResolver from the owning Activity. |
| 419 | */ |
| 420 | protected ContentResolver getContentResolver() { |
Amith Yamasani | 350938e | 2013-04-09 10:22:47 -0700 | [diff] [blame] | 421 | Context context = getActivity(); |
| 422 | if (context != null) { |
| 423 | mContentResolver = context.getContentResolver(); |
| 424 | } |
| 425 | return mContentResolver; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Returns the specified system service from the owning Activity. |
| 430 | */ |
| 431 | protected Object getSystemService(final String name) { |
| 432 | return getActivity().getSystemService(name); |
| 433 | } |
| 434 | |
| 435 | /** |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 436 | * Returns the PackageManager from the owning Activity. |
| 437 | */ |
| 438 | protected PackageManager getPackageManager() { |
| 439 | return getActivity().getPackageManager(); |
| 440 | } |
| 441 | |
Dianne Hackborn | 0385cf1 | 2011-01-24 16:22:13 -0800 | [diff] [blame] | 442 | @Override |
| 443 | public void onDetach() { |
| 444 | if (isRemoving()) { |
| 445 | if (mDialogFragment != null) { |
| 446 | mDialogFragment.dismiss(); |
| 447 | mDialogFragment = null; |
| 448 | } |
| 449 | } |
| 450 | super.onDetach(); |
| 451 | } |
| 452 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 453 | // Dialog management |
| 454 | |
| 455 | protected void showDialog(int dialogId) { |
| 456 | if (mDialogFragment != null) { |
| 457 | Log.e(TAG, "Old dialog fragment not null!"); |
| 458 | } |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 459 | mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId); |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 460 | mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 463 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 464 | public Dialog onCreateDialog(int dialogId) { |
| 465 | return null; |
| 466 | } |
| 467 | |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public int getDialogMetricsCategory(int dialogId) { |
| 470 | return 0; |
| 471 | } |
| 472 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 473 | protected void removeDialog(int dialogId) { |
Hung-ying Tyan | adc83d8 | 2011-01-24 15:05:27 +0800 | [diff] [blame] | 474 | // mDialogFragment may not be visible yet in parent fragment's onResume(). |
| 475 | // To be able to dismiss dialog at that time, don't check |
| 476 | // mDialogFragment.isVisible(). |
| 477 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId) { |
Jason Monk | 8a7d074 | 2016-07-15 13:18:48 -0400 | [diff] [blame] | 478 | mDialogFragment.dismissAllowingStateLoss(); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 479 | } |
| 480 | mDialogFragment = null; |
| 481 | } |
| 482 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 483 | /** |
| 484 | * Sets the OnCancelListener of the dialog shown. This method can only be |
| 485 | * called after showDialog(int) and before removeDialog(int). The method |
| 486 | * does nothing otherwise. |
| 487 | */ |
| 488 | protected void setOnCancelListener(DialogInterface.OnCancelListener listener) { |
| 489 | if (mDialogFragment != null) { |
| 490 | mDialogFragment.mOnCancelListener = listener; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Sets the OnDismissListener of the dialog shown. This method can only be |
| 496 | * called after showDialog(int) and before removeDialog(int). The method |
| 497 | * does nothing otherwise. |
| 498 | */ |
| 499 | protected void setOnDismissListener(DialogInterface.OnDismissListener listener) { |
| 500 | if (mDialogFragment != null) { |
| 501 | mDialogFragment.mOnDismissListener = listener; |
| 502 | } |
| 503 | } |
| 504 | |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 505 | public void onDialogShowing() { |
| 506 | // override in subclass to attach a dismiss listener, for instance |
| 507 | } |
| 508 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 509 | @Override |
| 510 | public void onDisplayPreferenceDialog(Preference preference) { |
| 511 | if (preference.getKey() == null) { |
| 512 | // Auto-key preferences that don't have a key, so the dialog can find them. |
| 513 | preference.setKey(UUID.randomUUID().toString()); |
| 514 | } |
| 515 | DialogFragment f = null; |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 516 | if (preference instanceof RestrictedListPreference) { |
| 517 | f = RestrictedListPreference.RestrictedListPreferenceDialogFragment |
| 518 | .newInstance(preference.getKey()); |
| 519 | } else if (preference instanceof CustomListPreference) { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 520 | f = CustomListPreference.CustomListPreferenceDialogFragment |
| 521 | .newInstance(preference.getKey()); |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 522 | } else if (preference instanceof CustomDialogPreferenceCompat) { |
| 523 | f = CustomDialogPreferenceCompat.CustomPreferenceDialogFragment |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 524 | .newInstance(preference.getKey()); |
tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 525 | } else if (preference instanceof CustomEditTextPreferenceCompat) { |
| 526 | f = CustomEditTextPreferenceCompat.CustomPreferenceDialogFragment |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 527 | .newInstance(preference.getKey()); |
| 528 | } else { |
| 529 | super.onDisplayPreferenceDialog(preference); |
| 530 | return; |
| 531 | } |
| 532 | f.setTargetFragment(this, 0); |
| 533 | f.show(getFragmentManager(), "dialog_preference"); |
| 534 | onDialogShowing(); |
| 535 | } |
| 536 | |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 537 | public static class SettingsDialogFragment extends InstrumentedDialogFragment { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 538 | private static final String KEY_DIALOG_ID = "key_dialog_id"; |
| 539 | private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; |
| 540 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 541 | private Fragment mParentFragment; |
| 542 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 543 | private DialogInterface.OnCancelListener mOnCancelListener; |
| 544 | private DialogInterface.OnDismissListener mOnDismissListener; |
| 545 | |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 546 | public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 547 | if (!(fragment instanceof Fragment)) { |
| 548 | throw new IllegalArgumentException("fragment argument must be an instance of " |
| 549 | + Fragment.class.getName()); |
| 550 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 551 | |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 552 | final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment(); |
| 553 | settingsDialogFragment.setParentFragment(fragment); |
| 554 | settingsDialogFragment.setDialogId(dialogId); |
| 555 | |
| 556 | return settingsDialogFragment; |
| 557 | } |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 558 | |
| 559 | @Override |
| 560 | public int getMetricsCategory() { |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 561 | if (mParentFragment == null) { |
Fan Zhang | 4fe7c08 | 2016-10-03 13:48:55 -0700 | [diff] [blame] | 562 | return Instrumentable.METRICS_CATEGORY_UNKNOWN; |
| 563 | } |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 564 | final int metricsCategory = |
| 565 | ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId); |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 566 | if (metricsCategory <= 0) { |
| 567 | throw new IllegalStateException("Dialog must provide a metrics category"); |
| 568 | } |
| 569 | return metricsCategory; |
| 570 | } |
| 571 | |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 572 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 573 | public void onSaveInstanceState(Bundle outState) { |
| 574 | super.onSaveInstanceState(outState); |
| 575 | if (mParentFragment != null) { |
| 576 | outState.putInt(KEY_DIALOG_ID, mDialogId); |
| 577 | outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | @Override |
Amith Yamasani | c861cf8 | 2012-10-02 14:51:46 -0700 | [diff] [blame] | 582 | public void onStart() { |
| 583 | super.onStart(); |
| 584 | |
| 585 | if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { |
| 586 | ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | @Override |
Dianne Hackborn | 300768f | 2011-01-27 20:39:21 -0800 | [diff] [blame] | 591 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 592 | if (savedInstanceState != null) { |
| 593 | mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 594 | mParentFragment = getParentFragment(); |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 595 | int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); |
Fabrice Di Meglio | b7bd72f | 2014-07-25 13:03:09 -0700 | [diff] [blame] | 596 | if (mParentFragment == null) { |
| 597 | mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); |
| 598 | } |
Fabrice Di Meglio | 377dd62 | 2014-02-12 20:05:57 -0800 | [diff] [blame] | 599 | if (!(mParentFragment instanceof DialogCreatable)) { |
| 600 | throw new IllegalArgumentException( |
| 601 | (mParentFragment != null |
| 602 | ? mParentFragment.getClass().getName() |
| 603 | : mParentFragmentId) |
| 604 | + " must implement " |
| 605 | + DialogCreatable.class.getName()); |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 606 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 607 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 608 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 609 | // restore mDialogFragment in mParentFragment |
| 610 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; |
| 611 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 612 | } |
Svetoslav Ganov | 749ba65 | 2010-12-09 14:53:02 -0800 | [diff] [blame] | 613 | return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Hung-ying Tyan | 0ee51e0 | 2011-01-25 16:42:14 +0800 | [diff] [blame] | 616 | @Override |
| 617 | public void onCancel(DialogInterface dialog) { |
| 618 | super.onCancel(dialog); |
| 619 | if (mOnCancelListener != null) { |
| 620 | mOnCancelListener.onCancel(dialog); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void onDismiss(DialogInterface dialog) { |
| 626 | super.onDismiss(dialog); |
| 627 | if (mOnDismissListener != null) { |
| 628 | mOnDismissListener.onDismiss(dialog); |
| 629 | } |
| 630 | } |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 631 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 632 | public int getDialogId() { |
| 633 | return mDialogId; |
| 634 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 635 | |
| 636 | @Override |
| 637 | public void onDetach() { |
| 638 | super.onDetach(); |
| 639 | |
Amith Yamasani | 8875ede | 2011-01-31 12:46:57 -0800 | [diff] [blame] | 640 | // This dialog fragment could be created from non-SettingsPreferenceFragment |
| 641 | if (mParentFragment instanceof SettingsPreferenceFragment) { |
| 642 | // in case the dialog is not explicitly removed by removeDialog() |
| 643 | if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { |
| 644 | ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; |
| 645 | } |
Hung-ying Tyan | 18eb39d | 2011-01-28 16:17:27 +0800 | [diff] [blame] | 646 | } |
| 647 | } |
tmfang | d5405cf | 2018-10-05 18:45:07 +0800 | [diff] [blame] | 648 | |
| 649 | private void setParentFragment(DialogCreatable fragment) { |
| 650 | mParentFragment = (Fragment) fragment; |
| 651 | } |
| 652 | |
| 653 | private void setDialogId(int dialogId) { |
| 654 | mDialogId = dialogId; |
| 655 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 656 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 657 | |
| 658 | protected boolean hasNextButton() { |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 659 | return ((ButtonBarHandler) getActivity()).hasNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | protected Button getNextButton() { |
Fan Zhang | 3d516e7 | 2018-01-31 14:14:41 -0800 | [diff] [blame] | 663 | return ((ButtonBarHandler) getActivity()).getNextButton(); |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 666 | public void finish() { |
Jorim Jaggi | f92fbc1 | 2015-08-10 18:11:07 -0700 | [diff] [blame] | 667 | Activity activity = getActivity(); |
Jason Monk | 656bc60 | 2016-06-10 09:49:12 -0400 | [diff] [blame] | 668 | if (activity == null) return; |
| 669 | if (getFragmentManager().getBackStackEntryCount() > 0) { |
| 670 | getFragmentManager().popBackStack(); |
| 671 | } else { |
Udam Saini | 6a8b99d | 2016-02-10 16:07:41 -0800 | [diff] [blame] | 672 | activity.finish(); |
Jorim Jaggi | f92fbc1 | 2015-08-10 18:11:07 -0700 | [diff] [blame] | 673 | } |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Jason Monk | b7e4380 | 2016-06-06 16:01:58 -0400 | [diff] [blame] | 676 | protected Intent getIntent() { |
| 677 | if (getActivity() == null) { |
| 678 | return null; |
| 679 | } |
| 680 | return getActivity().getIntent(); |
| 681 | } |
| 682 | |
| 683 | protected void setResult(int result, Intent intent) { |
| 684 | if (getActivity() == null) { |
| 685 | return; |
| 686 | } |
| 687 | getActivity().setResult(result, intent); |
| 688 | } |
| 689 | |
| 690 | protected void setResult(int result) { |
| 691 | if (getActivity() == null) { |
| 692 | return; |
| 693 | } |
| 694 | getActivity().setResult(result); |
| 695 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 696 | } |