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.List;
29
30public class BrowserPreferencesPage extends PreferenceActivity {
31
32    public static final String CURRENT_PAGE = "currentPage";
33    private List<Header> mHeaders;
34
35    @Override
36    public void onCreate(Bundle icicle) {
37        super.onCreate(icicle);
38
39        ActionBar actionBar = getActionBar();
40        if (actionBar != null) {
41            actionBar.setDisplayOptions(
42                    ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
43        }
44    }
45
46    /**
47     * Populate the activity with the top-level headers.
48     */
49    @Override
50    public void onBuildHeaders(List<Header> target) {
51        loadHeadersFromResource(R.xml.preference_headers, target);
52
53        if (BrowserSettings.getInstance().isDebugEnabled()) {
54            Header debug = new Header();
55            debug.title = getText(R.string.pref_development_title);
56            debug.fragment = DebugPreferencesFragment.class.getName();
57            target.add(debug);
58        }
59        mHeaders = target;
60    }
61
62    @Override
63    public Header onGetInitialHeader() {
64        String action = getIntent().getAction();
65        if (Intent.ACTION_MANAGE_NETWORK_USAGE.equals(action)) {
66            String fragName = BandwidthPreferencesFragment.class.getName();
67            for (Header h : mHeaders) {
68                if (fragName.equals(h.fragment)) {
69                    return h;
70                }
71            }
72        }
73        return super.onGetInitialHeader();
74    }
75
76    @Override
77    public boolean onOptionsItemSelected(MenuItem item) {
78        switch (item.getItemId()) {
79            case android.R.id.home:
80                if (getFragmentManager().getBackStackEntryCount() > 0) {
81                    getFragmentManager().popBackStack();
82                } else {
83                    finish();
84                }
85                return true;
86        }
87
88        return false;
89    }
90
91    @Override
92    public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
93            int titleRes, int shortTitleRes) {
94        Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
95                titleRes, shortTitleRes);
96        String url = getIntent().getStringExtra(CURRENT_PAGE);
97        intent.putExtra(CURRENT_PAGE, url);
98        return intent;
99    }
100
101}
102