blob: 41ff87f0c2845e72e2778a27af810a6dcae41b44 [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
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -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;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070024import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.content.pm.PackageManager;
26import android.content.res.Resources;
27import android.os.Bundle;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070028import android.preference.PreferenceActivity;
Amith Yamasanid7993472010-08-18 13:59:28 -070029import android.preference.PreferenceFragment;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070030import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070031import android.util.Log;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070032import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070035
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070036/**
37 * Letting the class, assumed to be Fragment, create a Dialog on it. Should be useful
38 * you want to utilize some capability in {@link SettingsPreferenceFragment} but don't want
39 * the class inherit the class itself (See {@link ProxySelector} for example).
40 */
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070041interface DialogCreatable {
42 public Dialog onCreateDialog(int dialogId);
43}
44
Amith Yamasanid7993472010-08-18 13:59:28 -070045/**
46 * Base class for Settings fragments, with some helper functions and dialog management.
47 */
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070048public class SettingsPreferenceFragment extends PreferenceFragment
49 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070050
51 private static final String TAG = "SettingsPreferenceFragment";
52
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070053 // Originally from PreferenceActivity.
54 private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
55 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
56 private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
57 private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
58
Amith Yamasanid7993472010-08-18 13:59:28 -070059 private SettingsDialogFragment mDialogFragment;
60
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070061 private int mResultCode = Activity.RESULT_CANCELED;
62 private Intent mResultData;
Amith Yamasanid7993472010-08-18 13:59:28 -070063
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070064 private Button mNextButton;
65
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070066 @Override
67 public void onResume() {
68 super.onResume();
69
70 final Fragment f = getTargetFragment();
71 final int requestCode = getTargetRequestCode();
72
73 // TargetFragment becomes invalid when this object is resumed. Notify it to
74 // FragmentManager. Without this code, FragmentManager wrongly take the TargetFragment
75 // as live, and throws IllegalStateException.
76 setTargetFragment(null, -1);
77
78 if (f != null && (f instanceof SettingsPreferenceFragment)) {
79 final SettingsPreferenceFragment spf = (SettingsPreferenceFragment)f;
80 final int resultCode = spf.getResultCode();
81 final Intent resultData = spf.getResultData();
82 onActivityResult(requestCode, resultCode, resultData);
83 }
84 }
85
Amith Yamasanid7993472010-08-18 13:59:28 -070086 @Override
87 public void onActivityCreated(Bundle savedInstanceState) {
88 super.onActivityCreated(savedInstanceState);
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070089 setupButtonBar();
Amith Yamasanid7993472010-08-18 13:59:28 -070090 }
91
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070092 public final void setResult(int resultCode) {
93 mResultCode = resultCode;
94 mResultData = null;
95 }
96
97 public final void setResult(int resultCode, Intent data) {
98 mResultCode = resultCode;
99 mResultData = data;
100 }
101
102 public final int getResultCode() {
103 return mResultCode;
104 }
105
106 public final Intent getResultData() {
107 return mResultData;
108 }
109
110 /*
111 * The name is intentionally made different from Activity#finish(), so that
112 * users won't misunderstand its meaning.
113 */
114 public final void finishFragment() {
115 getActivity().onBackPressed();
116 }
117
Amith Yamasanid7993472010-08-18 13:59:28 -0700118 // Some helpers for functions used by the settings fragments when they were activities
119
120 /**
121 * Returns the ContentResolver from the owning Activity.
122 */
123 protected ContentResolver getContentResolver() {
124 return getActivity().getContentResolver();
125 }
126
127 /**
128 * Returns the specified system service from the owning Activity.
129 */
130 protected Object getSystemService(final String name) {
131 return getActivity().getSystemService(name);
132 }
133
134 /**
Amith Yamasanid7993472010-08-18 13:59:28 -0700135 * Returns the PackageManager from the owning Activity.
136 */
137 protected PackageManager getPackageManager() {
138 return getActivity().getPackageManager();
139 }
140
141 // Dialog management
142
143 protected void showDialog(int dialogId) {
144 if (mDialogFragment != null) {
145 Log.e(TAG, "Old dialog fragment not null!");
146 }
147 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700148 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700149 }
150
151 public Dialog onCreateDialog(int dialogId) {
152 return null;
153 }
154
155 protected void removeDialog(int dialogId) {
156 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId
157 && mDialogFragment.isVisible()) {
158 mDialogFragment.dismiss();
159 }
160 mDialogFragment = null;
161 }
162
163 static class SettingsDialogFragment extends DialogFragment {
164 private int mDialogId;
165
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700166 private DialogCreatable mFragment;
Amith Yamasanid7993472010-08-18 13:59:28 -0700167
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700168 SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700169 mDialogId = dialogId;
170 mFragment = fragment;
171 }
172
173 @Override
174 public Dialog onCreateDialog(Bundle savedInstanceState) {
175 return mFragment.onCreateDialog(mDialogId);
176 }
177
178 public int getDialogId() {
179 return mDialogId;
180 }
181 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700182
183 protected boolean hasNextButton() {
184 return mNextButton != null;
185 }
186
187 protected Button getNextButton() {
188 return mNextButton;
189 }
190
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700191 public void finish() {
192 getActivity().onBackPressed();
193 }
194
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700195 public boolean startFragment(
196 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700197 if (getActivity() instanceof PreferenceActivity) {
198 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
199 Fragment f = Fragment.instantiate(getActivity(), fragmentClass, extras);
200 caller.setTargetFragment(f, requestCode);
201 preferenceActivity.switchToHeader(fragmentClass, extras);
202 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700203 } else {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700204 Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
205 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
206 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700207 return false;
208 }
209 }
210
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700211 /**
212 * Sets up Button Bar possibly required in the Fragment. Probably available only in
213 * phones.
214 *
215 * Previously {@link PreferenceActivity} had the capability as hidden functionality.
216 */
217 private void setupButtonBar() {
218 // Originally from PreferenceActivity, which has had button bar inside its layout.
219 final Activity activity = getActivity();
220 final Intent intent = activity.getIntent();
221 final View buttonBar = activity.findViewById(com.android.internal.R.id.button_bar);
222 if (!intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false) || buttonBar == null) {
223 return;
224 }
225
226 buttonBar.setVisibility(View.VISIBLE);
227 View tmpView = activity.findViewById(com.android.internal.R.id.back_button);
228 if (tmpView != null) {
229 // TODO: Assume this is pressed only in single pane, finishing current Activity.
230 try {
231 final Button backButton = (Button)tmpView;
232 backButton.setOnClickListener(new OnClickListener() {
233 public void onClick(View v) {
234 activity.setResult(Activity.RESULT_CANCELED);
235 activity.finish();
236 }
237 });
238 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
239 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
240 if (TextUtils.isEmpty(buttonText)) {
241 backButton.setVisibility(View.GONE);
242 }
243 else {
244 backButton.setText(buttonText);
245 }
246 }
247 } catch (ClassCastException e) {
248 Log.w(TAG, "The view originally for back_button is used not as Button. " +
249 "Ignored.");
250 }
251 }
252
253 tmpView = activity.findViewById(com.android.internal.R.id.skip_button);
254 if (tmpView != null) {
255 try {
256 final Button skipButton = (Button)tmpView;
257 skipButton.setOnClickListener(new OnClickListener() {
258 public void onClick(View v) {
259 activity.setResult(Activity.RESULT_OK);
260 activity.finish();
261 }
262 });
263 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
264 skipButton.setVisibility(View.VISIBLE);
265 }
266 } catch (ClassCastException e) {
267 Log.w(TAG, "The view originally for skip_button is used not as Button. " +
268 "Ignored.");
269 }
270 }
271
272 tmpView = activity.findViewById(com.android.internal.R.id.next_button);
273 if (tmpView != null) {
274 try {
275 mNextButton = (Button)tmpView;
276 mNextButton.setOnClickListener(new OnClickListener() {
277 public void onClick(View v) {
278 activity.setResult(Activity.RESULT_OK);
279 activity.finish();
280 }
281 });
282 // set our various button parameters
283 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
284 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
285 if (TextUtils.isEmpty(buttonText)) {
286 mNextButton.setVisibility(View.GONE);
287 }
288 else {
289 mNextButton.setText(buttonText);
290 }
291 }
292 } catch (ClassCastException e) {
293 Log.w(TAG, "The view originally for next_button is used not as Button. " +
294 "Ignored.");
295 mNextButton = null;
296 }
297 }
298 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700299}