Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 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.Dialog; |
| 20 | import android.app.DialogFragment; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.res.Resources; |
| 24 | import android.os.Bundle; |
| 25 | import android.preference.PreferenceFragment; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | /** |
| 29 | * Base class for Settings fragments, with some helper functions and dialog management. |
| 30 | */ |
| 31 | public class SettingsPreferenceFragment extends PreferenceFragment { |
| 32 | |
| 33 | private static final String TAG = "SettingsPreferenceFragment"; |
| 34 | |
| 35 | private SettingsDialogFragment mDialogFragment; |
| 36 | |
| 37 | private OnStateListener mOnStateListener; |
| 38 | |
| 39 | interface OnStateListener { |
| 40 | |
| 41 | void onCreated(SettingsPreferenceFragment fragment); |
| 42 | |
| 43 | void onDestroyed(SettingsPreferenceFragment fragment); |
| 44 | } |
| 45 | |
| 46 | public void setOnStateListener(OnStateListener listener) { |
| 47 | mOnStateListener = listener; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void onActivityCreated(Bundle savedInstanceState) { |
| 52 | super.onActivityCreated(savedInstanceState); |
| 53 | if (mOnStateListener != null) { |
| 54 | mOnStateListener.onCreated(this); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onDestroy() { |
| 60 | super.onDestroy(); |
| 61 | if (mOnStateListener != null) { |
| 62 | mOnStateListener.onDestroyed(this); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Some helpers for functions used by the settings fragments when they were activities |
| 67 | |
| 68 | /** |
| 69 | * Returns the ContentResolver from the owning Activity. |
| 70 | */ |
| 71 | protected ContentResolver getContentResolver() { |
| 72 | return getActivity().getContentResolver(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the specified system service from the owning Activity. |
| 77 | */ |
| 78 | protected Object getSystemService(final String name) { |
| 79 | return getActivity().getSystemService(name); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the Resources from the owning Activity. |
| 84 | */ |
| 85 | protected Resources getResources() { |
| 86 | return getActivity().getResources(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the PackageManager from the owning Activity. |
| 91 | */ |
| 92 | protected PackageManager getPackageManager() { |
| 93 | return getActivity().getPackageManager(); |
| 94 | } |
| 95 | |
| 96 | // Dialog management |
| 97 | |
| 98 | protected void showDialog(int dialogId) { |
| 99 | if (mDialogFragment != null) { |
| 100 | Log.e(TAG, "Old dialog fragment not null!"); |
| 101 | } |
| 102 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
| 103 | mDialogFragment.show(getActivity(), Integer.toString(dialogId)); |
| 104 | } |
| 105 | |
| 106 | public Dialog onCreateDialog(int dialogId) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | protected void removeDialog(int dialogId) { |
| 111 | if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId |
| 112 | && mDialogFragment.isVisible()) { |
| 113 | mDialogFragment.dismiss(); |
| 114 | } |
| 115 | mDialogFragment = null; |
| 116 | } |
| 117 | |
| 118 | static class SettingsDialogFragment extends DialogFragment { |
| 119 | private int mDialogId; |
| 120 | |
| 121 | private SettingsPreferenceFragment mFragment; |
| 122 | |
| 123 | SettingsDialogFragment(SettingsPreferenceFragment fragment, int dialogId) { |
| 124 | mDialogId = dialogId; |
| 125 | mFragment = fragment; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 130 | return mFragment.onCreateDialog(mDialogId); |
| 131 | } |
| 132 | |
| 133 | public int getDialogId() { |
| 134 | return mDialogId; |
| 135 | } |
| 136 | } |
| 137 | } |