1/*
2 * Copyright (C) 2008 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.browser;
18
19import android.app.ActionBar;
20import android.content.Intent;
21import android.os.Bundle;
22import android.preference.PreferenceActivity;
23import android.view.MenuItem;
24
25import com.android.browser.preferences.BandwidthPreferencesFragment;
26import com.android.browser.preferences.DebugPreferencesFragment;
27
28import java.util.Arrays;
29import java.util.HashSet;
30import java.util.List;
31import java.util.Set;
32
33public class BrowserPreferencesPage extends PreferenceActivity {
34
35    public static final String CURRENT_PAGE = "currentPage";
36    private List<Header> mHeaders;
37
38    @Override
39    public void onCreate(Bundle icicle) {
40        super.onCreate(icicle);
41
42        ActionBar actionBar = getActionBar();
43        if (actionBar != null) {
44            actionBar.setDisplayOptions(
45                    ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
46        }
47    }
48
49    /**
50     * Populate the activity with the top-level headers.
51     */
52    @Override
53    public void onBuildHeaders(List<Header> target) {
54        loadHeadersFromResource(R.xml.preference_headers, target);
55
56        if (BrowserSettings.getInstance().isDebugEnabled()) {
57            Header debug = new Header();
58            debug.title = getText(R.string.pref_development_title);
59            debug.fragment = DebugPreferencesFragment.class.getName();
60            target.add(debug);
61        }
62        mHeaders = target;
63    }
64
65    @Override
66    public Header onGetInitialHeader() {
67        String action = getIntent().getAction();
68        if (Intent.ACTION_MANAGE_NETWORK_USAGE.equals(action)) {
69            String fragName = BandwidthPreferencesFragment.class.getName();
70            for (Header h : mHeaders) {
71                if (fragName.equals(h.fragment)) {
72                    return h;
73                }
74            }
75        }
76        return super.onGetInitialHeader();
77    }
78
79    @Override
80    public boolean onOptionsItemSelected(MenuItem item) {
81        switch (item.getItemId()) {
82            case android.R.id.home:
83                if (getFragmentManager().getBackStackEntryCount() > 0) {
84                    getFragmentManager().popBackStack();
85                } else {
86                    finish();
87                }
88                return true;
89        }
90
91        return false;
92    }
93
94    @Override
95    public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
96            int titleRes, int shortTitleRes) {
97        Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
98                titleRes, shortTitleRes);
99        String url = getIntent().getStringExtra(CURRENT_PAGE);
100        intent.putExtra(CURRENT_PAGE, url);
101        return intent;
102    }
103
104    private static final Set<String> sKnownFragments = new HashSet<String>(Arrays.asList(
105            "com.android.browser.preferences.GeneralPreferencesFragment",
106            "com.android.browser.preferences.PrivacySecurityPreferencesFragment",
107            "com.android.browser.preferences.AccessibilityPreferencesFragment",
108            "com.android.browser.preferences.AdvancedPreferencesFragment",
109            "com.android.browser.preferences.BandwidthPreferencesFragment",
110            "com.android.browser.preferences.LabPreferencesFragment"));
111
112    @Override
113    protected boolean isValidFragment(String fragmentName) {
114        return sKnownFragments.contains(fragmentName);
115    }
116}
117