blob: bddfb2b7bb086d852b24068b3ca91b5943a2691d [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
35/**
36 * Base class for Settings fragments, with some helper functions and dialog management.
37 */
38public class SettingsPreferenceFragment extends PreferenceFragment {
39
40 private static final String TAG = "SettingsPreferenceFragment";
41
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070042 // 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 Yamasanid7993472010-08-18 13:59:28 -070048 private SettingsDialogFragment mDialogFragment;
49
50 private OnStateListener mOnStateListener;
51
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070052 private Button mNextButton;
53
Amith Yamasanib61cf512010-09-12 08:17:50 -070054 private boolean mReportedCreation;
55
Amith Yamasanid7993472010-08-18 13:59:28 -070056 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 Yamasanib61cf512010-09-12 08:17:50 -070070 if (mOnStateListener != null && !mReportedCreation) {
Amith Yamasanid7993472010-08-18 13:59:28 -070071 mOnStateListener.onCreated(this);
Amith Yamasanib61cf512010-09-12 08:17:50 -070072 // So that we don't report it on the way back to this fragment
73 mReportedCreation = true;
Amith Yamasanid7993472010-08-18 13:59:28 -070074 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -070075
76 setupButtonBar();
Amith Yamasanid7993472010-08-18 13:59:28 -070077 }
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 Miyakawa9c8bde52010-08-25 11:58:37 -0700158
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 Yamasanid7993472010-08-18 13:59:28 -0700255}