blob: 2d376417b980e33487069f2d5412c8bc7ae74bd7 [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 Miyakawa21c1abc2010-09-12 15:42:56 -070035interface DialogCreatable {
36 public Dialog onCreateDialog(int dialogId);
37}
38
Amith Yamasanid7993472010-08-18 13:59:28 -070039/**
40 * Base class for Settings fragments, with some helper functions and dialog management.
41 */
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070042public class SettingsPreferenceFragment extends PreferenceFragment
43 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070044
45 private static final String TAG = "SettingsPreferenceFragment";
46
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070047 // 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 Yamasanid7993472010-08-18 13:59:28 -070053 private SettingsDialogFragment mDialogFragment;
54
55 private OnStateListener mOnStateListener;
56
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070057 private Button mNextButton;
58
Amith Yamasanib61cf512010-09-12 08:17:50 -070059 private boolean mReportedCreation;
60
Amith Yamasanid7993472010-08-18 13:59:28 -070061 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 Yamasanib61cf512010-09-12 08:17:50 -070075 if (mOnStateListener != null && !mReportedCreation) {
Amith Yamasanid7993472010-08-18 13:59:28 -070076 mOnStateListener.onCreated(this);
Amith Yamasanib61cf512010-09-12 08:17:50 -070077 // So that we don't report it on the way back to this fragment
78 mReportedCreation = true;
Amith Yamasanid7993472010-08-18 13:59:28 -070079 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070080
81 setupButtonBar();
Amith Yamasanid7993472010-08-18 13:59:28 -070082 }
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 Miyakawa21c1abc2010-09-12 15:42:56 -0700129 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -0700130 }
131
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700132 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700133 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 Miyakawa21c1abc2010-09-12 15:42:56 -0700148 private DialogCreatable mFragment;
Amith Yamasanid7993472010-08-18 13:59:28 -0700149
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700150 SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700151 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 Miyakawa9c8bde52010-08-25 11:58:37 -0700164
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 Yamasanid7993472010-08-18 13:59:28 -0700261}