blob: e0e7095bc89f3a3b6d6ea36a9eaa669a8e5a3884 [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;
Pankaj Garg75273bb2015-08-20 11:43:49 -070022import android.net.Uri;
Ben Murdoch7ed9bd72010-12-13 14:30:33 +000023import android.os.Bundle;
Enrico Rosd6efa972014-12-02 19:49:59 -080024import android.preference.PreferenceActivity;
Pankaj Garg75273bb2015-08-20 11:43:49 -070025import android.text.TextUtils;
Pankaj Garg59608b72014-12-15 11:22:23 -080026import android.view.MenuItem;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010027
Pankaj Garg18902562014-12-05 16:18:51 -080028import com.android.browser.preferences.AboutPreferencesFragment;
Pankaj Gargb3b57462014-11-18 17:28:30 -080029import com.android.browser.preferences.GeneralPreferencesFragment;
John Reck7b182d72011-07-08 10:30:53 -070030
Pankaj Garg18902562014-12-05 16:18:51 -080031import java.lang.reflect.Constructor;
32import java.lang.reflect.InvocationTargetException;
Pankaj Garg75273bb2015-08-20 11:43:49 -070033import java.util.ArrayList;
Pankaj Garg18902562014-12-05 16:18:51 -080034
Pankaj Gargb3b57462014-11-18 17:28:30 -080035public class BrowserPreferencesPage extends Activity {
Pankaj Garg75273bb2015-08-20 11:43:49 -070036 public static String sResultExtra;
37 private static ArrayList<String> sUpdatedUrls =
38 new ArrayList<String>(); //List of URLS for whom settings were updated
The Android Open Source Project0c908882009-03-03 19:32:16 -080039
Enrico Rosd6efa972014-12-02 19:49:59 -080040 public static void startPreferencesForResult(Activity callerActivity, String url, int requestCode) {
41 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
42 intent.putExtra(GeneralPreferencesFragment.EXTRA_CURRENT_PAGE, url);
43 callerActivity.startActivityForResult(intent, requestCode);
44 }
45
46 public static void startPreferenceFragmentForResult(Activity callerActivity, String fragmentName, int requestCode) {
47 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
48 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
49 callerActivity.startActivityForResult(intent, requestCode);
50 }
51
Pankaj Garg18902562014-12-05 16:18:51 -080052 public static void startPreferenceFragmentExtraForResult(Activity callerActivity,
53 String fragmentName,
54 Bundle bundle,
55 int requestCode) {
56 final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
57 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
58 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, bundle);
59 callerActivity.startActivityForResult(intent, requestCode);
60 }
61
Nicolas Roard78a98e42009-05-11 13:34:17 +010062
Ben Murdoch7ed9bd72010-12-13 14:30:33 +000063 @Override
64 public void onCreate(Bundle icicle) {
65 super.onCreate(icicle);
66
Pankaj Gargda9f5772015-02-05 14:20:12 -080067 if (icicle != null) {
68 return;
69 }
70
Pankaj Garg75273bb2015-08-20 11:43:49 -070071 sResultExtra = "";
72 sUpdatedUrls.clear();
Enrico Rosd6efa972014-12-02 19:49:59 -080073 Intent intent = getIntent();
74 if (intent != null) {
75 String action = intent.getAction();
76 // check if this page was invoked by 'App Data Usage' on the global data monitor
77 if ("android.intent.action.MANAGE_NETWORK_USAGE".equals(action)) {
78 // TODO: switch to the Network fragment here?
79 }
Pankaj Garg18902562014-12-05 16:18:51 -080080
81 Bundle extras = intent.getExtras();
Sagar Dhawan80017132015-09-16 14:38:33 -070082 if (extras == null){
83 getFragmentManager().beginTransaction().replace(android.R.id.content,
84 new GeneralPreferencesFragment()).commit();
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -080085 return;
Sagar Dhawan80017132015-09-16 14:38:33 -070086 }
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -080087
Pankaj Garg18902562014-12-05 16:18:51 -080088 String fragment = (String) extras.getCharSequence(PreferenceActivity.EXTRA_SHOW_FRAGMENT);
89 if (fragment != null) {
90 try {
91 Class<?> cls = Class.forName(fragment);
92 Constructor<?> ctor = cls.getConstructor();
93 Object obj = ctor.newInstance();
94
95 if (obj instanceof Fragment) {
96 Fragment frag = (Fragment) obj;
97
98 Bundle bundle = extras.getBundle(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
99 if (bundle != null) {
100 frag.setArguments(bundle);
101 }
102
103 getFragmentManager().beginTransaction().replace(
104 android.R.id.content,
105 (Fragment) obj).commit();
106 }
107 } catch (ClassNotFoundException e) {
108 } catch (NoSuchMethodException e) {
109 } catch (InvocationTargetException e) {
110 } catch (InstantiationException e) {
111 } catch (IllegalAccessException e) {
112 }
113 return;
114 }
Enrico Rosd6efa972014-12-02 19:49:59 -0800115 }
116
Pankaj Gargb3b57462014-11-18 17:28:30 -0800117 getFragmentManager().beginTransaction().replace(android.R.id.content,
118 new GeneralPreferencesFragment()).commit();
Ben Murdoch7ed9bd72010-12-13 14:30:33 +0000119 }
Pankaj Garg59608b72014-12-15 11:22:23 -0800120
121 @Override
122 public boolean onOptionsItemSelected(MenuItem item) {
123 switch (item.getItemId()) {
124 case android.R.id.home:
125 if (getFragmentManager().getBackStackEntryCount() > 0) {
126 getFragmentManager().popBackStack();
127 } else {
128 finish();
129 }
130 return true;
131 }
132 return super.onOptionsItemSelected(item);
133 }
Pankaj Garg75273bb2015-08-20 11:43:49 -0700134
135 @Override
136 public void finish() {
137 if (!TextUtils.isEmpty(sResultExtra)) {
138 Intent intent = this.getIntent();
139 intent.putExtra(Intent.EXTRA_TEXT, sResultExtra);
140 intent.putStringArrayListExtra(Controller.EXTRA_UPDATED_URLS, sUpdatedUrls);
141 this.setResult(RESULT_OK, intent);
142 }
143 super.finish();
144 }
145
146 public static void onUrlNeedsReload(String url) {
147 String host = (Uri.parse(url)).getHost();
148 if (!sUpdatedUrls.contains(host)) {
149 sUpdatedUrls.add(host);
150 }
151 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152}