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