blob: 46fde6a02602f6a2f7307e2b8c85ddfd5d93142e [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();
Axesh R. Ajmeradb14e452015-01-21 17:42:59 -080082 if (extras == null)
83 return;
84
Pankaj Garg18902562014-12-05 16:18:51 -080085 String fragment = (String) extras.getCharSequence(PreferenceActivity.EXTRA_SHOW_FRAGMENT);
86 if (fragment != null) {
87 try {
88 Class<?> cls = Class.forName(fragment);
89 Constructor<?> ctor = cls.getConstructor();
90 Object obj = ctor.newInstance();
91
92 if (obj instanceof Fragment) {
93 Fragment frag = (Fragment) obj;
94
95 Bundle bundle = extras.getBundle(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
96 if (bundle != null) {
97 frag.setArguments(bundle);
98 }
99
100 getFragmentManager().beginTransaction().replace(
101 android.R.id.content,
102 (Fragment) obj).commit();
103 }
104 } catch (ClassNotFoundException e) {
105 } catch (NoSuchMethodException e) {
106 } catch (InvocationTargetException e) {
107 } catch (InstantiationException e) {
108 } catch (IllegalAccessException e) {
109 }
110 return;
111 }
Enrico Rosd6efa972014-12-02 19:49:59 -0800112 }
113
Pankaj Gargb3b57462014-11-18 17:28:30 -0800114 getFragmentManager().beginTransaction().replace(android.R.id.content,
115 new GeneralPreferencesFragment()).commit();
Ben Murdoch7ed9bd72010-12-13 14:30:33 +0000116 }
Pankaj Garg59608b72014-12-15 11:22:23 -0800117
118 @Override
119 public boolean onOptionsItemSelected(MenuItem item) {
120 switch (item.getItemId()) {
121 case android.R.id.home:
122 if (getFragmentManager().getBackStackEntryCount() > 0) {
123 getFragmentManager().popBackStack();
124 } else {
125 finish();
126 }
127 return true;
128 }
129 return super.onOptionsItemSelected(item);
130 }
Pankaj Garg75273bb2015-08-20 11:43:49 -0700131
132 @Override
133 public void finish() {
134 if (!TextUtils.isEmpty(sResultExtra)) {
135 Intent intent = this.getIntent();
136 intent.putExtra(Intent.EXTRA_TEXT, sResultExtra);
137 intent.putStringArrayListExtra(Controller.EXTRA_UPDATED_URLS, sUpdatedUrls);
138 this.setResult(RESULT_OK, intent);
139 }
140 super.finish();
141 }
142
143 public static void onUrlNeedsReload(String url) {
144 String host = (Uri.parse(url)).getHost();
145 if (!sUpdatedUrls.contains(host)) {
146 sUpdatedUrls.add(host);
147 }
148 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800149}