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