The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 18 | |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 20 | import android.app.Fragment; |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 22 | import android.net.Uri; |
Ben Murdoch | 7ed9bd7 | 2010-12-13 14:30:33 +0000 | [diff] [blame] | 23 | import android.os.Bundle; |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 24 | import android.preference.PreferenceActivity; |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Sagar Dhawan | 35db4fa | 2015-10-15 20:00:21 -0700 | [diff] [blame^] | 26 | import android.util.Log; |
Pankaj Garg | 59608b7 | 2014-12-15 11:22:23 -0800 | [diff] [blame] | 27 | import android.view.MenuItem; |
Bjorn Bringert | d69f51d | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 28 | |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 29 | import com.android.browser.preferences.AboutPreferencesFragment; |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 30 | import com.android.browser.preferences.GeneralPreferencesFragment; |
John Reck | 7b182d7 | 2011-07-08 10:30:53 -0700 | [diff] [blame] | 31 | |
Sagar Dhawan | 35db4fa | 2015-10-15 20:00:21 -0700 | [diff] [blame^] | 32 | |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 33 | import java.lang.reflect.Constructor; |
| 34 | import java.lang.reflect.InvocationTargetException; |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 36 | |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 37 | public class BrowserPreferencesPage extends Activity { |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 38 | public static String sResultExtra; |
Sagar Dhawan | 35db4fa | 2015-10-15 20:00:21 -0700 | [diff] [blame^] | 39 | public static String LOGTAG = "BrowserPreferencesPage"; |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 40 | private static ArrayList<String> sUpdatedUrls = |
| 41 | new ArrayList<String>(); //List of URLS for whom settings were updated |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 42 | |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 43 | public static void startPreferencesForResult(Activity callerActivity, String url, int requestCode) { |
| 44 | final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class); |
| 45 | intent.putExtra(GeneralPreferencesFragment.EXTRA_CURRENT_PAGE, url); |
| 46 | callerActivity.startActivityForResult(intent, requestCode); |
| 47 | } |
| 48 | |
| 49 | public static void startPreferenceFragmentForResult(Activity callerActivity, String fragmentName, int requestCode) { |
| 50 | final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class); |
| 51 | intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName); |
| 52 | callerActivity.startActivityForResult(intent, requestCode); |
| 53 | } |
| 54 | |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 55 | public static void startPreferenceFragmentExtraForResult(Activity callerActivity, |
| 56 | String fragmentName, |
| 57 | Bundle bundle, |
| 58 | int requestCode) { |
| 59 | final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class); |
| 60 | intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName); |
| 61 | intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, bundle); |
| 62 | callerActivity.startActivityForResult(intent, requestCode); |
| 63 | } |
| 64 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 65 | |
Ben Murdoch | 7ed9bd7 | 2010-12-13 14:30:33 +0000 | [diff] [blame] | 66 | @Override |
| 67 | public void onCreate(Bundle icicle) { |
| 68 | super.onCreate(icicle); |
Pankaj Garg | da9f577 | 2015-02-05 14:20:12 -0800 | [diff] [blame] | 69 | if (icicle != null) { |
| 70 | return; |
| 71 | } |
Sagar Dhawan | 35db4fa | 2015-10-15 20:00:21 -0700 | [diff] [blame^] | 72 | if (!EngineInitializer.isInitialized()) { |
| 73 | Log.e(LOGTAG, "Engine not Initialized"); |
| 74 | finish(); |
| 75 | return; |
| 76 | } |
Pankaj Garg | da9f577 | 2015-02-05 14:20:12 -0800 | [diff] [blame] | 77 | |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 78 | sResultExtra = ""; |
| 79 | sUpdatedUrls.clear(); |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 80 | Intent intent = getIntent(); |
| 81 | if (intent != null) { |
| 82 | String action = intent.getAction(); |
| 83 | // check if this page was invoked by 'App Data Usage' on the global data monitor |
| 84 | if ("android.intent.action.MANAGE_NETWORK_USAGE".equals(action)) { |
| 85 | // TODO: switch to the Network fragment here? |
| 86 | } |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 87 | |
| 88 | Bundle extras = intent.getExtras(); |
Sagar Dhawan | 8001713 | 2015-09-16 14:38:33 -0700 | [diff] [blame] | 89 | if (extras == null){ |
| 90 | getFragmentManager().beginTransaction().replace(android.R.id.content, |
| 91 | new GeneralPreferencesFragment()).commit(); |
Axesh R. Ajmera | db14e45 | 2015-01-21 17:42:59 -0800 | [diff] [blame] | 92 | return; |
Sagar Dhawan | 8001713 | 2015-09-16 14:38:33 -0700 | [diff] [blame] | 93 | } |
Axesh R. Ajmera | db14e45 | 2015-01-21 17:42:59 -0800 | [diff] [blame] | 94 | |
Pankaj Garg | 1890256 | 2014-12-05 16:18:51 -0800 | [diff] [blame] | 95 | String fragment = (String) extras.getCharSequence(PreferenceActivity.EXTRA_SHOW_FRAGMENT); |
| 96 | if (fragment != null) { |
| 97 | try { |
| 98 | Class<?> cls = Class.forName(fragment); |
| 99 | Constructor<?> ctor = cls.getConstructor(); |
| 100 | Object obj = ctor.newInstance(); |
| 101 | |
| 102 | if (obj instanceof Fragment) { |
| 103 | Fragment frag = (Fragment) obj; |
| 104 | |
| 105 | Bundle bundle = extras.getBundle(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS); |
| 106 | if (bundle != null) { |
| 107 | frag.setArguments(bundle); |
| 108 | } |
| 109 | |
| 110 | getFragmentManager().beginTransaction().replace( |
| 111 | android.R.id.content, |
| 112 | (Fragment) obj).commit(); |
| 113 | } |
| 114 | } catch (ClassNotFoundException e) { |
| 115 | } catch (NoSuchMethodException e) { |
| 116 | } catch (InvocationTargetException e) { |
| 117 | } catch (InstantiationException e) { |
| 118 | } catch (IllegalAccessException e) { |
| 119 | } |
| 120 | return; |
| 121 | } |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Pankaj Garg | b3b5746 | 2014-11-18 17:28:30 -0800 | [diff] [blame] | 124 | getFragmentManager().beginTransaction().replace(android.R.id.content, |
| 125 | new GeneralPreferencesFragment()).commit(); |
Ben Murdoch | 7ed9bd7 | 2010-12-13 14:30:33 +0000 | [diff] [blame] | 126 | } |
Pankaj Garg | 59608b7 | 2014-12-15 11:22:23 -0800 | [diff] [blame] | 127 | |
| 128 | @Override |
| 129 | public boolean onOptionsItemSelected(MenuItem item) { |
| 130 | switch (item.getItemId()) { |
| 131 | case android.R.id.home: |
| 132 | if (getFragmentManager().getBackStackEntryCount() > 0) { |
| 133 | getFragmentManager().popBackStack(); |
| 134 | } else { |
| 135 | finish(); |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | return super.onOptionsItemSelected(item); |
| 140 | } |
Pankaj Garg | 75273bb | 2015-08-20 11:43:49 -0700 | [diff] [blame] | 141 | |
| 142 | @Override |
| 143 | public void finish() { |
| 144 | if (!TextUtils.isEmpty(sResultExtra)) { |
| 145 | Intent intent = this.getIntent(); |
| 146 | intent.putExtra(Intent.EXTRA_TEXT, sResultExtra); |
| 147 | intent.putStringArrayListExtra(Controller.EXTRA_UPDATED_URLS, sUpdatedUrls); |
| 148 | this.setResult(RESULT_OK, intent); |
| 149 | } |
| 150 | super.finish(); |
| 151 | } |
| 152 | |
| 153 | public static void onUrlNeedsReload(String url) { |
| 154 | String host = (Uri.parse(url)).getHost(); |
| 155 | if (!sUpdatedUrls.contains(host)) { |
| 156 | sUpdatedUrls.add(host); |
| 157 | } |
| 158 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 159 | } |