blob: 935c72a6c522969fc331fba3759b20b955276f2c [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;
22import android.content.ContentResolver;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070023import android.content.Intent;
Amith Yamasanid7993472010-08-18 13:59:28 -070024import android.content.pm.PackageManager;
25import android.content.res.Resources;
26import android.os.Bundle;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070027import android.preference.PreferenceActivity;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.preference.PreferenceFragment;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070029import android.text.TextUtils;
Amith Yamasanid7993472010-08-18 13:59:28 -070030import android.util.Log;
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070031import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.Button;
Amith Yamasanid7993472010-08-18 13:59:28 -070034
Daisuke Miyakawaf58090d2010-09-12 17:27:33 -070035/**
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 Miyakawa21c1abc2010-09-12 15:42:56 -070040interface DialogCreatable {
41 public Dialog onCreateDialog(int dialogId);
42}
43
Amith Yamasanid7993472010-08-18 13:59:28 -070044/**
45 * Base class for Settings fragments, with some helper functions and dialog management.
46 */
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070047public class SettingsPreferenceFragment extends PreferenceFragment
48 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070049
50 private static final String TAG = "SettingsPreferenceFragment";
51
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070052 // 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 Yamasanid7993472010-08-18 13:59:28 -070058 private SettingsDialogFragment mDialogFragment;
59
60 private OnStateListener mOnStateListener;
61
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070062 private Button mNextButton;
63
Amith Yamasanib61cf512010-09-12 08:17:50 -070064 private boolean mReportedCreation;
65
Amith Yamasanid7993472010-08-18 13:59:28 -070066 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 Yamasanib61cf512010-09-12 08:17:50 -070080 if (mOnStateListener != null && !mReportedCreation) {
Amith Yamasanid7993472010-08-18 13:59:28 -070081 mOnStateListener.onCreated(this);
Amith Yamasanib61cf512010-09-12 08:17:50 -070082 // So that we don't report it on the way back to this fragment
83 mReportedCreation = true;
Amith Yamasanid7993472010-08-18 13:59:28 -070084 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070085
86 setupButtonBar();
Amith Yamasanid7993472010-08-18 13:59:28 -070087 }
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 Miyakawa21c1abc2010-09-12 15:42:56 -0700134 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700135 }
136
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700137 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700138 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 Miyakawa21c1abc2010-09-12 15:42:56 -0700153 private DialogCreatable mFragment;
Amith Yamasanid7993472010-08-18 13:59:28 -0700154
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700155 SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700156 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 Miyakawa9c8bde52010-08-25 11:58:37 -0700169
170 protected boolean hasNextButton() {
171 return mNextButton != null;
172 }
173
174 protected Button getNextButton() {
175 return mNextButton;
176 }
177
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700178 public void finish() {
179 getActivity().onBackPressed();
180 }
181
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700182 /**
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 Yamasanid7993472010-08-18 13:59:28 -0700270}