blob: 214149074526da4940abd3a3f5e9a1395661ebb4 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
Pankaj Gargb3b57462014-11-18 17:28:30 -080019import android.app.Activity;
Pankaj Garg18902562014-12-05 16:18:51 -080020import android.app.Fragment;
Enrico Rosd6efa972014-12-02 19:49:59 -080021import android.content.Intent;
Ben Murdoch7ed9bd72010-12-13 14:30:33 +000022import android.os.Bundle;
Enrico Rosd6efa972014-12-02 19:49:59 -080023import android.preference.PreferenceActivity;
Pankaj Garg59608b72014-12-15 11:22:23 -080024import android.view.MenuItem;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010025
Pankaj Garg18902562014-12-05 16:18:51 -080026import com.android.browser.preferences.AboutPreferencesFragment;
Pankaj Gargb3b57462014-11-18 17:28:30 -080027import com.android.browser.preferences.GeneralPreferencesFragment;
John Reck7b182d72011-07-08 10:30:53 -070028
Pankaj Garg18902562014-12-05 16:18:51 -080029import java.lang.reflect.Constructor;
30import java.lang.reflect.InvocationTargetException;
31
Pankaj Gargb3b57462014-11-18 17:28:30 -080032public class BrowserPreferencesPage extends Activity {
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
Enrico Rosd6efa972014-12-02 19:49:59 -080034 public static void startPreferencesForResult(Activity callerActivity, String url, int requestCode) {
35 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
36 intent.putExtra(GeneralPreferencesFragment.EXTRA_CURRENT_PAGE, url);
37 callerActivity.startActivityForResult(intent, requestCode);
38 }
39
40 public static void startPreferenceFragmentForResult(Activity callerActivity, String fragmentName, int requestCode) {
41 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
42 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
43 callerActivity.startActivityForResult(intent, requestCode);
44 }
45
Pankaj Garg18902562014-12-05 16:18:51 -080046 public static void startPreferenceFragmentExtraForResult(Activity callerActivity,
47 String fragmentName,
48 Bundle bundle,
49 int requestCode) {
50 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
51 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
52 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, bundle);
53 callerActivity.startActivityForResult(intent, requestCode);
54 }
55
Nicolas Roard78a98e42009-05-11 13:34:17 +010056
Ben Murdoch7ed9bd72010-12-13 14:30:33 +000057 @Override
58 public void onCreate(Bundle icicle) {
59 super.onCreate(icicle);
60
Pankaj Gargda9f5772015-02-05 14:20:12 -080061 if (icicle != null) {
62 return;
63 }
64
Enrico Rosd6efa972014-12-02 19:49:59 -080065 Intent intent = getIntent();
66 if (intent != null) {
67 String action = intent.getAction();
68 // check if this page was invoked by 'App Data Usage' on the global data monitor
69 if ("android.intent.action.MANAGE_NETWORK_USAGE".equals(action)) {
70 // TODO: switch to the Network fragment here?
71 }
Pankaj Garg18902562014-12-05 16:18:51 -080072
73 Bundle extras = intent.getExtras();
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -080074 if (extras == null)
75 return;
76
Pankaj Garg18902562014-12-05 16:18:51 -080077 String fragment = (String) extras.getCharSequence(PreferenceActivity.EXTRA_SHOW_FRAGMENT);
78 if (fragment != null) {
79 try {
80 Class<?> cls = Class.forName(fragment);
81 Constructor<?> ctor = cls.getConstructor();
82 Object obj = ctor.newInstance();
83
84 if (obj instanceof Fragment) {
85 Fragment frag = (Fragment) obj;
86
87 Bundle bundle = extras.getBundle(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
88 if (bundle != null) {
89 frag.setArguments(bundle);
90 }
91
92 getFragmentManager().beginTransaction().replace(
93 android.R.id.content,
94 (Fragment) obj).commit();
95 }
96 } catch (ClassNotFoundException e) {
97 } catch (NoSuchMethodException e) {
98 } catch (InvocationTargetException e) {
99 } catch (InstantiationException e) {
100 } catch (IllegalAccessException e) {
101 }
102 return;
103 }
Enrico Rosd6efa972014-12-02 19:49:59 -0800104 }
105
Pankaj Gargb3b57462014-11-18 17:28:30 -0800106 getFragmentManager().beginTransaction().replace(android.R.id.content,
107 new GeneralPreferencesFragment()).commit();
Ben Murdoch7ed9bd72010-12-13 14:30:33 +0000108 }
Pankaj Garg59608b72014-12-15 11:22:23 -0800109
110 @Override
111 public boolean onOptionsItemSelected(MenuItem item) {
112 switch (item.getItemId()) {
113 case android.R.id.home:
114 if (getFragmentManager().getBackStackEntryCount() > 0) {
115 getFragmentManager().popBackStack();
116 } else {
117 finish();
118 }
119 return true;
120 }
121 return super.onOptionsItemSelected(item);
122 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123}