OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.datausage; | 5 package org.chromium.chrome.browser.datausage; |
6 | 6 |
| 7 import android.app.Activity; |
| 8 import android.content.Context; |
| 9 import android.content.DialogInterface; |
| 10 import android.content.DialogInterface.OnClickListener; |
| 11 import android.preference.PreferenceManager; |
| 12 import android.support.v7.app.AlertDialog; |
| 13 import android.text.TextUtils; |
| 14 import android.view.View; |
| 15 import android.widget.CheckBox; |
| 16 |
| 17 import org.chromium.chrome.R; |
7 import org.chromium.chrome.browser.profiles.Profile; | 18 import org.chromium.chrome.browser.profiles.Profile; |
8 import org.chromium.chrome.browser.sessions.SessionTabHelper; | 19 import org.chromium.chrome.browser.sessions.SessionTabHelper; |
9 import org.chromium.chrome.browser.tab.Tab; | 20 import org.chromium.chrome.browser.tab.Tab; |
| 21 import org.chromium.content_public.browser.LoadUrlParams; |
| 22 import org.chromium.content_public.common.Referrer; |
10 | 23 |
11 /** | 24 /** |
12 * Entry point to manage all UI details for measuring data use within a Tab. | 25 * Entry point to manage all UI details for measuring data use within a Tab. |
13 */ | 26 */ |
14 public class DataUseTabUIManager { | 27 public class DataUseTabUIManager { |
15 | 28 |
| 29 private static final String SHARED_PREF_DATA_USE_DIALOG_OPT_OUT = "data_use_
dialog_opt_out"; |
| 30 |
16 /** | 31 /** |
17 * Returns true if data use tracking has started within a Tab. | 32 * Returns true if data use tracking has started within a Tab. When data use
tracking has |
| 33 * started, returns true only once to signify the started event. |
18 * | 34 * |
19 * @param tab The tab to see if tracking has started in. | 35 * @param tab The tab that may have started tracking data use. |
20 * @return If data use tracking has started. | 36 * @return true If data use tracking has indeed started. |
21 */ | 37 */ |
22 public static boolean hasDataUseTrackingStarted(Tab tab) { | 38 public static boolean checkDataUseTrackingStarted(Tab tab) { |
23 return nativeHasDataUseTrackingStarted( | 39 return nativeCheckDataUseTrackingStarted( |
24 SessionTabHelper.sessionIdForTab(tab.getWebContents()), tab.getP
rofile()); | 40 SessionTabHelper.sessionIdForTab(tab.getWebContents()), tab.getP
rofile()); |
25 } | 41 } |
26 | 42 |
27 /** | 43 /** |
28 * Returns true if data use tracking has ended within a Tab. | 44 * Returns true if data use tracking has ended within a Tab. When data use t
racking has |
| 45 * ended, returns true only once to signify the ended event. |
29 * | 46 * |
30 * @param tab The tab to see if tracking has ended in. | 47 * @param tab The tab that may have ended tracking data use. |
31 * @return If data use tracking has ended. | 48 * @return true If data use tracking has indeed ended. |
32 */ | 49 */ |
33 public static boolean hasDataUseTrackingEnded(Tab tab) { | 50 public static boolean checkDataUseTrackingEnded(Tab tab) { |
34 return nativeHasDataUseTrackingEnded( | 51 return nativeCheckDataUseTrackingEnded( |
35 SessionTabHelper.sessionIdForTab(tab.getWebContents()), tab.getP
rofile()); | 52 SessionTabHelper.sessionIdForTab(tab.getWebContents()), tab.getP
rofile()); |
36 } | 53 } |
37 | 54 |
38 private static native boolean nativeHasDataUseTrackingStarted(int tabId, Pro
file profile); | 55 /** |
39 private static native boolean nativeHasDataUseTrackingEnded(int tabId, Profi
le profile); | 56 * Returns whether a navigation should be paused to show a dialog telling th
e user that data use |
| 57 * tracking has ended within a Tab. If the navigation should be paused, show
s a dialog with the |
| 58 * option to cancel the navigation or continue. |
| 59 * |
| 60 * @param activity Current activity. |
| 61 * @param tab The tab to see if tracking has ended in. |
| 62 * @param url URL that is pending. |
| 63 * @param pageTransitionType The type of transition. see |
| 64 * {@link org.chromium.content.browser.PageTransition} for valid
values. |
| 65 * @param referrerUrl URL for the referrer. |
| 66 * @return true If the URL loading should be overriden. |
| 67 */ |
| 68 public static boolean shouldOverrideUrlLoading(Activity activity, |
| 69 final Tab tab, final String url, final int pageTransitionType, |
| 70 final String referrerUrl) { |
| 71 if (!getOptedOutOfDataUseDialog(activity) && checkDataUseTrackingEnded(t
ab)) { |
| 72 startDataUseDialog(activity, tab, url, pageTransitionType, referrerU
rl); |
| 73 return true; |
| 74 } |
| 75 return false; |
| 76 } |
| 77 |
| 78 /** |
| 79 * Shows a dialog with the option to cancel the navigation or continue. Also
allows the user to |
| 80 * opt out of seeing this dialog again. |
| 81 * |
| 82 * @param activity Current activity. |
| 83 * @param tab The tab loading the url. |
| 84 * @param url URL that is pending. |
| 85 * @param pageTransitionType The type of transition. see |
| 86 * {@link org.chromium.content.browser.PageTransition} for valid
values. |
| 87 * @param referrerUrl URL for the referrer. |
| 88 */ |
| 89 private static void startDataUseDialog(final Activity activity, final Tab ta
b, |
| 90 final String url, final int pageTransitionType, final String referre
rUrl) { |
| 91 View checkBoxView = View.inflate(activity, R.layout.data_use_dialog, nul
l); |
| 92 final CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.data
_use_checkbox); |
| 93 new AlertDialog.Builder(activity, R.style.AlertDialogTheme) |
| 94 .setTitle(R.string.data_use_tracking_ended_title) |
| 95 .setMessage(R.string.data_use_tracking_ended_message) |
| 96 .setView(checkBoxView) |
| 97 .setPositiveButton(R.string.data_use_tracking_ended_continue, |
| 98 new OnClickListener() { |
| 99 @Override |
| 100 public void onClick(DialogInterface dialog, int whic
h) { |
| 101 setOptedOutOfDataUseDialog(activity, checkBox.is
Checked()); |
| 102 LoadUrlParams loadUrlParams = new LoadUrlParams(
url, |
| 103 pageTransitionType); |
| 104 if (!TextUtils.isEmpty(referrerUrl)) { |
| 105 Referrer referrer = new Referrer(referrerUrl
, |
| 106 Referrer.REFERRER_POLICY_ALWAYS); |
| 107 loadUrlParams.setReferrer(referrer); |
| 108 } |
| 109 tab.loadUrl(loadUrlParams); |
| 110 } |
| 111 }) |
| 112 .setNegativeButton(R.string.cancel, new OnClickListener() { |
| 113 @Override |
| 114 public void onClick(DialogInterface dialog, int which) { |
| 115 setOptedOutOfDataUseDialog(activity, checkBox.isChecked(
)); |
| 116 } |
| 117 }) |
| 118 .show(); |
| 119 } |
| 120 |
| 121 /** |
| 122 * Returns true if the user has opted out of seeing the data use dialog. |
| 123 * |
| 124 * @param context An Android context. |
| 125 * @return true If the user has opted out of seeing the data use dialog. |
| 126 */ |
| 127 public static boolean getOptedOutOfDataUseDialog(Context context) { |
| 128 return PreferenceManager.getDefaultSharedPreferences(context).getBoolean
( |
| 129 SHARED_PREF_DATA_USE_DIALOG_OPT_OUT, false); |
| 130 } |
| 131 |
| 132 /** |
| 133 * Sets whether the user has opted out of seeing the data use dialog. |
| 134 * |
| 135 * @param context An Android context. |
| 136 * @param optedOut Whether the user has opted out of seeing the data use dia
log. |
| 137 */ |
| 138 private static void setOptedOutOfDataUseDialog(Context context, boolean opte
dOut) { |
| 139 PreferenceManager.getDefaultSharedPreferences(context).edit() |
| 140 .putBoolean(SHARED_PREF_DATA_USE_DIALOG_OPT_OUT, optedOut) |
| 141 .apply(); |
| 142 } |
| 143 |
| 144 private static native boolean nativeCheckDataUseTrackingStarted(int tabId, P
rofile profile); |
| 145 private static native boolean nativeCheckDataUseTrackingEnded(int tabId, Pro
file profile); |
40 } | 146 } |
OLD | NEW |