blob: 47e31eeb6b9f587ec1a3a3137cce08b00a69b4f9 [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;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import 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/**
Amith Yamasanid7993472010-08-18 13:59:28 -070036 * Base class for Settings fragments, with some helper functions and dialog management.
37 */
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070038public class SettingsPreferenceFragment extends PreferenceFragment
39 implements DialogCreatable {
Amith Yamasanid7993472010-08-18 13:59:28 -070040
41 private static final String TAG = "SettingsPreferenceFragment";
42
43 private SettingsDialogFragment mDialogFragment;
44
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070045 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070046 public void onActivityCreated(Bundle savedInstanceState) {
47 super.onActivityCreated(savedInstanceState);
Amith Yamasanid7993472010-08-18 13:59:28 -070048 }
49
Daisuke Miyakawab5647c52010-09-10 18:04:02 -070050 /*
51 * The name is intentionally made different from Activity#finish(), so that
52 * users won't misunderstand its meaning.
53 */
54 public final void finishFragment() {
55 getActivity().onBackPressed();
56 }
57
Amith Yamasanid7993472010-08-18 13:59:28 -070058 // Some helpers for functions used by the settings fragments when they were activities
59
60 /**
61 * Returns the ContentResolver from the owning Activity.
62 */
63 protected ContentResolver getContentResolver() {
64 return getActivity().getContentResolver();
65 }
66
67 /**
68 * Returns the specified system service from the owning Activity.
69 */
70 protected Object getSystemService(final String name) {
71 return getActivity().getSystemService(name);
72 }
73
74 /**
Amith Yamasanid7993472010-08-18 13:59:28 -070075 * Returns the PackageManager from the owning Activity.
76 */
77 protected PackageManager getPackageManager() {
78 return getActivity().getPackageManager();
79 }
80
81 // Dialog management
82
83 protected void showDialog(int dialogId) {
84 if (mDialogFragment != null) {
85 Log.e(TAG, "Old dialog fragment not null!");
86 }
87 mDialogFragment = new SettingsDialogFragment(this, dialogId);
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070088 mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
Amith Yamasanid7993472010-08-18 13:59:28 -070089 }
90
91 public Dialog onCreateDialog(int dialogId) {
92 return null;
93 }
94
95 protected void removeDialog(int dialogId) {
96 if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId
97 && mDialogFragment.isVisible()) {
98 mDialogFragment.dismiss();
99 }
100 mDialogFragment = null;
101 }
102
Amith Yamasani43c69782010-12-01 09:04:36 -0800103 public static class SettingsDialogFragment extends DialogFragment {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800104 private static final String KEY_DIALOG_ID = "key_dialog_id";
105 private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
106
Amith Yamasanid7993472010-08-18 13:59:28 -0700107 private int mDialogId;
108
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800109 private Fragment mParentFragment;
110
111 public SettingsDialogFragment() {
112 /* do nothing */
113 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700114
Amith Yamasani43c69782010-12-01 09:04:36 -0800115 public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
Amith Yamasanid7993472010-08-18 13:59:28 -0700116 mDialogId = dialogId;
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800117 if (!(fragment instanceof Fragment)) {
118 throw new IllegalArgumentException("fragment argument must be an instance of "
119 + Fragment.class.getName());
120 }
121 mParentFragment = (Fragment) fragment;
122 }
123
124 @Override
125 public void onActivityCreated(Bundle savedInstanceState) {
126 if (savedInstanceState != null) {
127 mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0);
128 int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1);
129 if (mParentFragmentId > -1) {
130 mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId);
131 if (!(mParentFragment instanceof DialogCreatable)) {
132 throw new IllegalArgumentException(
133 KEY_PARENT_FRAGMENT_ID + " must implement "
134 + DialogCreatable.class.getName());
135 }
136 }
137 }
138 super.onActivityCreated(savedInstanceState);
139 }
140
141 @Override
142 public void onSaveInstanceState(Bundle outState) {
143 super.onSaveInstanceState(outState);
144 if (mParentFragment != null) {
145 outState.putInt(KEY_DIALOG_ID, mDialogId);
146 outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId());
147 }
Amith Yamasanid7993472010-08-18 13:59:28 -0700148 }
149
150 @Override
151 public Dialog onCreateDialog(Bundle savedInstanceState) {
Svetoslav Ganov749ba652010-12-09 14:53:02 -0800152 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
Amith Yamasanid7993472010-08-18 13:59:28 -0700153 }
154
155 public int getDialogId() {
156 return mDialogId;
157 }
158 }
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700159
160 protected boolean hasNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800161 return ((ButtonBarHandler)getActivity()).hasNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700162 }
163
164 protected Button getNextButton() {
Daisuke Miyakawa79c5fd92011-01-15 14:58:00 -0800165 return ((ButtonBarHandler)getActivity()).getNextButton();
Daisuke Miyakawa9c8bde52010-08-25 11:58:37 -0700166 }
167
Daisuke Miyakawa6ebf8612010-09-10 09:48:51 -0700168 public void finish() {
169 getActivity().onBackPressed();
170 }
171
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700172 public boolean startFragment(
173 Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700174 if (getActivity() instanceof PreferenceActivity) {
175 PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
Amith Yamasani928e78a2010-11-12 08:51:01 -0800176 preferenceActivity.startPreferencePanel(fragmentClass, extras, 0, null, caller,
177 requestCode);
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700178 return true;
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700179 } else {
Daisuke Miyakawa25af1502010-09-24 11:29:31 -0700180 Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
181 + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
182 + ")");
Daisuke Miyakawab5647c52010-09-10 18:04:02 -0700183 return false;
184 }
185 }
186
Amith Yamasanid7993472010-08-18 13:59:28 -0700187}