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 | |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -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; |
| 21 | import android.app.DialogFragment; |
| 22 | import android.content.ContentResolver; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.res.Resources; |
| 26 | import android.os.Bundle; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 27 | import android.preference.PreferenceActivity; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 28 | import android.preference.PreferenceFragment; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.View.OnClickListener; |
| 33 | import android.widget.Button; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 34 | |
Daisuke Miyakawa | f58090d | 2010-09-12 17:27:33 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Letting the class, assumed to be Fragment, create a Dialog on it. Should be useful |
| 37 | * you want to utilize some capability in {@link SettingsPreferenceFragment} but don't want |
| 38 | * the class inherit the class itself (See {@link ProxySelector} for example). |
| 39 | */ |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 40 | interface DialogCreatable { |
| 41 | public Dialog onCreateDialog(int dialogId); |
| 42 | } |
| 43 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 46 | */ |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 47 | public class SettingsPreferenceFragment extends PreferenceFragment |
| 48 | implements DialogCreatable { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 49 | |
| 50 | private static final String TAG = "SettingsPreferenceFragment"; |
| 51 | |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 52 | // Originally from PreferenceActivity. |
| 53 | private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; |
| 54 | private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip"; |
| 55 | private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text"; |
| 56 | private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text"; |
| 57 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 58 | private SettingsDialogFragment mDialogFragment; |
| 59 | |
| 60 | private OnStateListener mOnStateListener; |
| 61 | |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 62 | private Button mNextButton; |
| 63 | |
Amith Yamasani | b61cf51 | 2010-09-12 08:17:50 -0700 | [diff] [blame] | 64 | private boolean mReportedCreation; |
| 65 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 66 | interface OnStateListener { |
| 67 | |
| 68 | void onCreated(SettingsPreferenceFragment fragment); |
| 69 | |
| 70 | void onDestroyed(SettingsPreferenceFragment fragment); |
| 71 | } |
| 72 | |
| 73 | public void setOnStateListener(OnStateListener listener) { |
| 74 | mOnStateListener = listener; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onActivityCreated(Bundle savedInstanceState) { |
| 79 | super.onActivityCreated(savedInstanceState); |
Amith Yamasani | b61cf51 | 2010-09-12 08:17:50 -0700 | [diff] [blame] | 80 | if (mOnStateListener != null && !mReportedCreation) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 81 | mOnStateListener.onCreated(this); |
Amith Yamasani | b61cf51 | 2010-09-12 08:17:50 -0700 | [diff] [blame] | 82 | // So that we don't report it on the way back to this fragment |
| 83 | mReportedCreation = true; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 84 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 85 | |
| 86 | setupButtonBar(); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onDestroy() { |
| 91 | super.onDestroy(); |
| 92 | if (mOnStateListener != null) { |
| 93 | mOnStateListener.onDestroyed(this); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Some helpers for functions used by the settings fragments when they were activities |
| 98 | |
| 99 | /** |
| 100 | * Returns the ContentResolver from the owning Activity. |
| 101 | */ |
| 102 | protected ContentResolver getContentResolver() { |
| 103 | return getActivity().getContentResolver(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the specified system service from the owning Activity. |
| 108 | */ |
| 109 | protected Object getSystemService(final String name) { |
| 110 | return getActivity().getSystemService(name); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the Resources from the owning Activity. |
| 115 | */ |
| 116 | protected Resources getResources() { |
| 117 | return getActivity().getResources(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the PackageManager from the owning Activity. |
| 122 | */ |
| 123 | protected PackageManager getPackageManager() { |
| 124 | return getActivity().getPackageManager(); |
| 125 | } |
| 126 | |
| 127 | // Dialog management |
| 128 | |
| 129 | protected void showDialog(int dialogId) { |
| 130 | if (mDialogFragment != null) { |
| 131 | Log.e(TAG, "Old dialog fragment not null!"); |
| 132 | } |
| 133 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 134 | mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 137 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 138 | public Dialog onCreateDialog(int dialogId) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | protected void removeDialog(int dialogId) { |
| 143 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId |
| 144 | && mDialogFragment.isVisible()) { |
| 145 | mDialogFragment.dismiss(); |
| 146 | } |
| 147 | mDialogFragment = null; |
| 148 | } |
| 149 | |
| 150 | static class SettingsDialogFragment extends DialogFragment { |
| 151 | private int mDialogId; |
| 152 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 153 | private DialogCreatable mFragment; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 154 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 155 | SettingsDialogFragment(DialogCreatable fragment, int dialogId) { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 156 | mDialogId = dialogId; |
| 157 | mFragment = fragment; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 162 | return mFragment.onCreateDialog(mDialogId); |
| 163 | } |
| 164 | |
| 165 | public int getDialogId() { |
| 166 | return mDialogId; |
| 167 | } |
| 168 | } |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 169 | |
| 170 | protected boolean hasNextButton() { |
| 171 | return mNextButton != null; |
| 172 | } |
| 173 | |
| 174 | protected Button getNextButton() { |
| 175 | return mNextButton; |
| 176 | } |
| 177 | |
Daisuke Miyakawa | 6ebf861 | 2010-09-10 09:48:51 -0700 | [diff] [blame^] | 178 | public void finish() { |
| 179 | getActivity().onBackPressed(); |
| 180 | } |
| 181 | |
Daisuke Miyakawa | 9c8bde5 | 2010-08-25 11:58:37 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Sets up Button Bar possibly required in the Fragment. Probably available only in |
| 184 | * phones. |
| 185 | * |
| 186 | * Previously {@link PreferenceActivity} had the capability as hidden functionality. |
| 187 | */ |
| 188 | private void setupButtonBar() { |
| 189 | // Originally from PreferenceActivity, which has had button bar inside its layout. |
| 190 | final Activity activity = getActivity(); |
| 191 | final Intent intent = activity.getIntent(); |
| 192 | final View buttonBar = activity.findViewById(com.android.internal.R.id.button_bar); |
| 193 | if (!intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false) || buttonBar == null) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | buttonBar.setVisibility(View.VISIBLE); |
| 198 | View tmpView = activity.findViewById(com.android.internal.R.id.back_button); |
| 199 | if (tmpView != null) { |
| 200 | // TODO: Assume this is pressed only in single pane, finishing current Activity. |
| 201 | try { |
| 202 | final Button backButton = (Button)tmpView; |
| 203 | backButton.setOnClickListener(new OnClickListener() { |
| 204 | public void onClick(View v) { |
| 205 | activity.setResult(Activity.RESULT_CANCELED); |
| 206 | activity.finish(); |
| 207 | } |
| 208 | }); |
| 209 | if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { |
| 210 | String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); |
| 211 | if (TextUtils.isEmpty(buttonText)) { |
| 212 | backButton.setVisibility(View.GONE); |
| 213 | } |
| 214 | else { |
| 215 | backButton.setText(buttonText); |
| 216 | } |
| 217 | } |
| 218 | } catch (ClassCastException e) { |
| 219 | Log.w(TAG, "The view originally for back_button is used not as Button. " + |
| 220 | "Ignored."); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | tmpView = activity.findViewById(com.android.internal.R.id.skip_button); |
| 225 | if (tmpView != null) { |
| 226 | try { |
| 227 | final Button skipButton = (Button)tmpView; |
| 228 | skipButton.setOnClickListener(new OnClickListener() { |
| 229 | public void onClick(View v) { |
| 230 | activity.setResult(Activity.RESULT_OK); |
| 231 | activity.finish(); |
| 232 | } |
| 233 | }); |
| 234 | if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) { |
| 235 | skipButton.setVisibility(View.VISIBLE); |
| 236 | } |
| 237 | } catch (ClassCastException e) { |
| 238 | Log.w(TAG, "The view originally for skip_button is used not as Button. " + |
| 239 | "Ignored."); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | tmpView = activity.findViewById(com.android.internal.R.id.next_button); |
| 244 | if (tmpView != null) { |
| 245 | try { |
| 246 | mNextButton = (Button)tmpView; |
| 247 | mNextButton.setOnClickListener(new OnClickListener() { |
| 248 | public void onClick(View v) { |
| 249 | activity.setResult(Activity.RESULT_OK); |
| 250 | activity.finish(); |
| 251 | } |
| 252 | }); |
| 253 | // set our various button parameters |
| 254 | if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { |
| 255 | String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); |
| 256 | if (TextUtils.isEmpty(buttonText)) { |
| 257 | mNextButton.setVisibility(View.GONE); |
| 258 | } |
| 259 | else { |
| 260 | mNextButton.setText(buttonText); |
| 261 | } |
| 262 | } |
| 263 | } catch (ClassCastException e) { |
| 264 | Log.w(TAG, "The view originally for next_button is used not as Button. " + |
| 265 | "Ignored."); |
| 266 | mNextButton = null; |
| 267 | } |
| 268 | } |
| 269 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 270 | } |