OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.snackbar; | |
6 | |
7 import android.content.Context; | |
8 import android.content.Intent; | |
9 | |
10 import org.chromium.base.CommandLine; | |
11 import org.chromium.chrome.R; | |
12 import org.chromium.chrome.browser.preferences.PreferencesLauncher; | |
13 import org.chromium.chrome.browser.preferences.datareduction.DataReductionPrefer ences; | |
14 import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoU tils; | |
15 import org.chromium.chrome.browser.preferences.datareduction.DataReductionProxyU ma; | |
16 import org.chromium.components.variations.VariationsAssociatedData; | |
17 | |
18 /** | |
19 * The controller for the Data Reduction Proxy promo that lets users of the prox y know when Chrome | |
20 * has saved a given amount of data. | |
21 */ | |
22 public class DataReductionPromoSnackbarController implements SnackbarManager.Sna ckbarController { | |
23 /** | |
24 * A semi-colon delimited list of data saving values in MB that the promo sh ould be shown | |
25 * for. | |
26 */ | |
27 public static final String PROMO_PARAM_NAME = "snackbar_promo_data_savings_i n_megabytes"; | |
28 | |
29 public static final String FROM_PROMO = "FromPromo"; | |
30 public static final String PROMO_FIELD_TRIAL_NAME = "DataCompressionProxyPro moVisibility"; | |
31 private static final String CLEAR_DATA_REDUCITON_PROXY_DATA_SAVINGS_SWITCH = | |
tbansal1
2016/10/25 23:15:13
"REDUCITON"
typo
megjablon
2016/10/25 23:49:57
Done.
| |
32 "clear-data-reduction-proxy-data-savings"; | |
33 private static final long BYTES_PER_MEGABYTE = 1024 * 1024; | |
34 private static final long BYTES_PER_GIGABYTE = 1024 * 1024 * 1024; | |
35 | |
36 private final SnackbarManager mSnackbarManager; | |
37 private final Context mContext; | |
38 private final int[] mPromoDataSavingsMB; | |
39 | |
40 /** | |
41 * Creates an instance of a {@link DataReductionPromoSnackbarController}. | |
42 * | |
43 * @param context The {@link Context} in which snackbar is shown. | |
44 * @param snackbarManager The manager that helps to show the snackbar. | |
45 */ | |
46 public DataReductionPromoSnackbarController(Context context, SnackbarManager snackbarManager) { | |
47 mSnackbarManager = snackbarManager; | |
48 mContext = context; | |
49 | |
50 String variationParamValue = VariationsAssociatedData | |
51 .getVariationParamValue(PROMO_FIELD_TRIAL_NAME, PROMO_PARAM_NAME ); | |
52 | |
53 if (variationParamValue.isEmpty()) { | |
54 mPromoDataSavingsMB = new int[0]; | |
55 } else { | |
56 variationParamValue = variationParamValue.replace(" ", ""); | |
tbansal1
2016/10/25 23:15:13
I wish most of this code was in C++ (except may be
megjablon
2016/10/25 23:49:57
I think right now it makes the most sense to have
tbansal1
2016/10/26 00:09:30
Acknowledged.
| |
57 String[] promoDataSavingStrings = variationParamValue.split(";"); | |
58 mPromoDataSavingsMB = new int[promoDataSavingStrings.length]; | |
59 | |
60 for (int i = 0; i < promoDataSavingStrings.length; i++) { | |
61 try { | |
62 mPromoDataSavingsMB[i] = Integer.parseInt(promoDataSavingStr ings[i]); | |
63 } catch (NumberFormatException e) { | |
64 mPromoDataSavingsMB[i] = -1; | |
65 } | |
66 } | |
67 } | |
68 | |
69 if (CommandLine.getInstance().hasSwitch(CLEAR_DATA_REDUCITON_PROXY_DATA_ SAVINGS_SWITCH)) { | |
70 DataReductionPromoUtils.saveSnackbarPromoDisplayed(0); | |
71 } | |
72 } | |
73 | |
74 /** | |
75 * Shows the Data Reduction Proxy promo snackbar if the current data savings are over | |
76 * specific thresholds set by finch and the snackbar has not been shown for that | |
77 * | |
78 * @param dataSavingInBytes The amount of data the Data Reduction Proxy has saved in bytes. | |
79 */ | |
80 public void maybeShowDataReductionPromoSnackbar(long dataSavingInBytes) { | |
81 // Prevents users who upgrade and have already saved mPromoDataSavingsIn MB from seeing the | |
82 // promo. | |
83 if (!DataReductionPromoUtils.hasSnackbarPromoBeenInitWithStartingContent Length()) { | |
tbansal1
2016/10/25 23:15:13
IIUC, here we should be comparing the saved bytes
megjablon
2016/10/25 23:49:57
That is done below. Initializing the pref here pre
| |
84 DataReductionPromoUtils.saveSnackbarPromoInitWithStartingContentLeng th( | |
85 dataSavingInBytes); | |
86 return; | |
87 } | |
88 | |
89 for (int promoDataSavingMB : mPromoDataSavingsMB) { | |
90 long promoDataSavingBytes = promoDataSavingMB * BYTES_PER_MEGABYTE; | |
91 if (promoDataSavingMB > 0 && dataSavingInBytes >= promoDataSavingByt es | |
92 && DataReductionPromoUtils | |
93 .getDisplayedSnackbarPromoContentLength() < promoDat aSavingBytes) { | |
94 mSnackbarManager.showSnackbar(Snackbar | |
95 .make(getStringForBytes(promoDataSavingBytes), | |
96 this, | |
97 Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DATA_RE DUCTION_PROMO) | |
98 .setAction( | |
99 mContext.getString(R.string.data_reduction_promo _snackbar_button), | |
100 null)); | |
101 DataReductionProxyUma.dataReductionProxySnackbarPromo(promoDataS avingMB); | |
102 DataReductionPromoUtils.saveSnackbarPromoDisplayed(dataSavingInB ytes); | |
103 break; | |
104 } | |
105 } | |
106 } | |
107 | |
108 private String getStringForBytes(long bytes) { | |
109 int resourceId; | |
110 int bytesInCorrectUnits; | |
111 | |
112 if (bytes < BYTES_PER_GIGABYTE) { | |
113 resourceId = R.string.data_reduction_promo_snackbar_text_mb; | |
114 bytesInCorrectUnits = (int) (bytes / BYTES_PER_MEGABYTE); | |
115 } else { | |
116 resourceId = R.string.data_reduction_promo_snackbar_text_gb; | |
117 bytesInCorrectUnits = (int) (bytes / BYTES_PER_GIGABYTE); | |
118 } | |
119 | |
120 return mContext.getResources().getString(resourceId, bytesInCorrectUnits ); | |
121 } | |
122 | |
123 @Override | |
124 public void onAction(Object actionData) { | |
125 assert mContext != null; | |
126 Intent intent = PreferencesLauncher.createIntentForSettingsPage( | |
127 mContext, DataReductionPreferences.class.getName()); | |
128 intent.putExtra(FROM_PROMO, true); | |
129 mContext.startActivity(intent); | |
130 } | |
131 | |
132 @Override | |
133 public void onDismissNoAction(Object actionData) { | |
134 DataReductionProxyUma.dataReductionProxyUIAction( | |
135 DataReductionProxyUma.ACTION_SNACKBAR_DISMISSED); | |
136 } | |
137 } | |
OLD | NEW |