summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/SettingsPreferenceFragment.java
blob: 41ff87f0c2845e72e2778a27af810a6dcae41b44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Letting the class, assumed to be Fragment, create a Dialog on it. Should be useful
 * you want to utilize some capability in {@link SettingsPreferenceFragment} but don't want
 * the class inherit the class itself (See {@link ProxySelector} for example).
 */
interface DialogCreatable {
    public Dialog onCreateDialog(int dialogId);
}

/**
 * Base class for Settings fragments, with some helper functions and dialog management.
 */
public class SettingsPreferenceFragment extends PreferenceFragment
        implements DialogCreatable {

    private static final String TAG = "SettingsPreferenceFragment";

    // Originally from PreferenceActivity.
    private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
    private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
    private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";

    private SettingsDialogFragment mDialogFragment;

    private int mResultCode = Activity.RESULT_CANCELED;
    private Intent mResultData;

    private Button mNextButton;

    @Override
    public void onResume() {
        super.onResume();

        final Fragment f = getTargetFragment();
        final int requestCode = getTargetRequestCode();

        // TargetFragment becomes invalid when this object is resumed. Notify it to
        // FragmentManager. Without this code, FragmentManager wrongly take the TargetFragment
        // as live, and throws IllegalStateException.
        setTargetFragment(null, -1);

        if (f != null && (f instanceof SettingsPreferenceFragment)) {
            final SettingsPreferenceFragment spf = (SettingsPreferenceFragment)f;
            final int resultCode = spf.getResultCode();
            final Intent resultData = spf.getResultData();
            onActivityResult(requestCode, resultCode, resultData);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setupButtonBar();
    }

    public final void setResult(int resultCode) {
        mResultCode = resultCode;
        mResultData = null;
    }

    public final void setResult(int resultCode, Intent data) {
        mResultCode = resultCode;
        mResultData = data;
    }

    public final int getResultCode() {
        return mResultCode;
    }

    public final Intent getResultData() {
        return mResultData;
    }

    /*
     * The name is intentionally made different from Activity#finish(), so that
     * users won't misunderstand its meaning.
     */
    public final void finishFragment() {
        getActivity().onBackPressed();
    }

    // Some helpers for functions used by the settings fragments when they were activities

    /**
     * Returns the ContentResolver from the owning Activity.
     */
    protected ContentResolver getContentResolver() {
        return getActivity().getContentResolver();
    }

    /**
     * Returns the specified system service from the owning Activity.
     */
    protected Object getSystemService(final String name) {
        return getActivity().getSystemService(name);
    }

    /**
     * Returns the PackageManager from the owning Activity.
     */
    protected PackageManager getPackageManager() {
        return getActivity().getPackageManager();
    }

    // Dialog management

    protected void showDialog(int dialogId) {
        if (mDialogFragment != null) {
            Log.e(TAG, "Old dialog fragment not null!");
        }
        mDialogFragment = new SettingsDialogFragment(this, dialogId);
        mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
    }

    public Dialog onCreateDialog(int dialogId) {
        return null;
    }

    protected void removeDialog(int dialogId) {
        if (mDialogFragment != null && mDialogFragment.getDialogId() == dialogId
                && mDialogFragment.isVisible()) {
            mDialogFragment.dismiss();
        }
        mDialogFragment = null;
    }

    static class SettingsDialogFragment extends DialogFragment {
        private int mDialogId;

        private DialogCreatable mFragment;

        SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
            mDialogId = dialogId;
            mFragment = fragment;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mFragment.onCreateDialog(mDialogId);
        }

        public int getDialogId() {
            return mDialogId;
        }
    }

    protected boolean hasNextButton() {
        return mNextButton != null;
    }

    protected Button getNextButton() {
        return mNextButton;
    }

    public void finish() {
        getActivity().onBackPressed();
    }

    public boolean startFragment(
            Fragment caller, String fragmentClass, int requestCode, Bundle extras) {
        if (getActivity() instanceof PreferenceActivity) {
            PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity();
            Fragment f = Fragment.instantiate(getActivity(), fragmentClass, extras);
            caller.setTargetFragment(f, requestCode);
            preferenceActivity.switchToHeader(fragmentClass, extras);
            return true;
        } else {
            Log.w(TAG, "Parent isn't PreferenceActivity, thus there's no way to launch the "
                    + "given Fragment (name: " + fragmentClass + ", requestCode: " + requestCode
                    + ")");
            return false;
        }
    }

    /**
     * Sets up Button Bar possibly required in the Fragment. Probably available only in
     * phones.
     *
     * Previously {@link PreferenceActivity} had the capability as hidden functionality.
     */
    private void setupButtonBar() {
        // Originally from PreferenceActivity, which has had button bar inside its layout.
        final Activity activity = getActivity();
        final Intent intent = activity.getIntent();
        final View buttonBar = activity.findViewById(com.android.internal.R.id.button_bar);
        if (!intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false) || buttonBar == null) {
            return;
        }

        buttonBar.setVisibility(View.VISIBLE);
        View tmpView = activity.findViewById(com.android.internal.R.id.back_button);
        if (tmpView != null) {
            // TODO: Assume this is pressed only in single pane, finishing current Activity.
            try {
                final Button backButton = (Button)tmpView;
                backButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        activity.setResult(Activity.RESULT_CANCELED);
                        activity.finish();
                    }
                });
                if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
                    if (TextUtils.isEmpty(buttonText)) {
                        backButton.setVisibility(View.GONE);
                    }
                    else {
                        backButton.setText(buttonText);
                    }
                }
            } catch (ClassCastException e) {
                Log.w(TAG, "The view originally for back_button is used not as Button. " +
                        "Ignored.");
            }
        }

        tmpView = activity.findViewById(com.android.internal.R.id.skip_button);
        if (tmpView != null) {
            try {
                final Button skipButton = (Button)tmpView;
                skipButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        activity.setResult(Activity.RESULT_OK);
                        activity.finish();
                    }
                });
                if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
                    skipButton.setVisibility(View.VISIBLE);
                }
            } catch (ClassCastException e) {
                Log.w(TAG, "The view originally for skip_button is used not as Button. " +
                        "Ignored.");
            }
        }

        tmpView = activity.findViewById(com.android.internal.R.id.next_button);
        if (tmpView != null) {
            try {
                mNextButton = (Button)tmpView;
                mNextButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        activity.setResult(Activity.RESULT_OK);
                        activity.finish();
                    }
                });
                // set our various button parameters
                if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
                    String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
                    if (TextUtils.isEmpty(buttonText)) {
                        mNextButton.setVisibility(View.GONE);
                    }
                    else {
                        mNextButton.setText(buttonText);
                    }
                }
            } catch (ClassCastException e) {
                Log.w(TAG, "The view originally for next_button is used not as Button. " +
                        "Ignored.");
                mNextButton = null;
            }
        }
    }
}