OSDN Git Service

47e31eeb6b9f587ec1a3a3137cce08b00a69b4f9
[android-x86/packages-apps-Settings.git] / src / com / android / settings / SettingsPreferenceFragment.java
1 /*
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
17 package com.android.settings;
18
19 import android.app.Activity;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.Fragment;
23 import android.content.ContentResolver;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.os.Bundle;
27 import android.preference.PreferenceActivity;
28 import android.preference.PreferenceFragment;
29 import android.text.TextUtils;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.Button;
34
35 /**
36  * Base class for Settings fragments, with some helper functions and dialog management.
37  */
38 public class SettingsPreferenceFragment extends PreferenceFragment
39         implements DialogCreatable {
40
41     private static final String TAG = "SettingsPreferenceFragment";
42
43     private SettingsDialogFragment mDialogFragment;
44
45     @Override
46     public void onActivityCreated(Bundle savedInstanceState) {
47         super.onActivityCreated(savedInstanceState);
48     }
49
50     /*
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
58     // 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     /**
75      * 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);
88         mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
89     }
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
103     public static class SettingsDialogFragment extends DialogFragment {
104         private static final String KEY_DIALOG_ID = "key_dialog_id";
105         private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id";
106
107         private int mDialogId;
108
109         private Fragment mParentFragment;
110
111         public SettingsDialogFragment() {
112             /* do nothing */
113         }
114
115         public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
116             mDialogId = dialogId;
117             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             }
148         }
149
150         @Override
151         public Dialog onCreateDialog(Bundle savedInstanceState) {
152             return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId);
153         }
154
155         public int getDialogId() {
156             return mDialogId;
157         }
158     }
159
160     protected boolean hasNextButton() {
161         return ((ButtonBarHandler)getActivity()).hasNextButton();
162     }
163
164     protected Button getNextButton() {
165         return ((ButtonBarHandler)getActivity()).getNextButton();
166     }
167
168     public void finish() {
169         getActivity().onBackPressed();
170     }
171
172     public boolean startFragment(
173             Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
174         if (getActivity() instanceof PreferenceActivity) {
175             PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
176             preferenceActivity.startPreferencePanel(fragmentClass, extras, 0, null, caller,
177                     requestCode);
178             return true;
179         } else {
180             Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
181                     + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
182                     + ")");
183             return false;
184         }
185     }
186
187 }