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 import android.text.format.Formatter; | |
10 | |
11 import org.chromium.base.CommandLine; | |
12 import org.chromium.chrome.R; | |
13 import org.chromium.chrome.browser.preferences.PreferencesLauncher; | |
14 import org.chromium.chrome.browser.preferences.datareduction.DataReductionPrefer ences; | |
15 import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoU tils; | |
16 import org.chromium.chrome.browser.preferences.datareduction.DataReductionProxyU ma; | |
17 import org.chromium.components.variations.VariationsAssociatedData; | |
18 | |
19 /** | |
20 * The controller for the Data Reduction Proxy promo that lets users of the prox y know when Chrome | |
21 * has saved a given amount of data. | |
22 */ | |
23 public class DataReductionPromoSnackbarController implements SnackbarManager.Sna ckbarController { | |
24 public static final String FROM_PROMO = "FromPromo"; | |
25 public static final String PROMO_FIELD_TRIAL_NAME = "DataCompressionProxyPro moVisibility"; | |
26 // A semi-colon delimited list various data saving values in mb that the pro mo should be shown | |
27 // for. | |
gone
2016/10/13 01:03:11
Sentence needs to be reworked, too.
megjablon
2016/10/24 23:59:00
Done.
| |
28 public static final String PROMO_PARAM_NAME = "snackbar_promo_data_savings_i n_megabytes"; | |
29 | |
30 private static final String CLEAR_DATA_REDUCITON_PROXY_DATA_SAVINGS_SWITCH = | |
31 "clear-data-reduction-proxy-data-savings"; | |
32 private static final int BYTES_IN_MB = 1024 * 1024; | |
33 | |
34 private final SnackbarManager mSnackbarManager; | |
35 private final Context mContext; | |
36 private final int[] mPromoDataSavingsMB; | |
gone
2016/10/13 01:03:11
nit: newline here
megjablon
2016/10/24 23:59:00
Done.
| |
37 /** | |
38 * Creates an instance of a {@link DataReductionPromoSnackbarController}. | |
39 * | |
40 * @param context The {@link Context} in which snackbar is shown. | |
41 * @param snackbarManager The manager that helps to show the snackbar. | |
42 */ | |
43 public DataReductionPromoSnackbarController(Context context, SnackbarManager snackbarManager) { | |
44 mSnackbarManager = snackbarManager; | |
45 mContext = context; | |
46 | |
47 String variationParamValue = VariationsAssociatedData | |
48 .getVariationParamValue(PROMO_FIELD_TRIAL_NAME, PROMO_PARAM_NAME ); | |
49 | |
50 if (variationParamValue.isEmpty()) { | |
51 mPromoDataSavingsMB = new int[0]; | |
52 } else { | |
53 variationParamValue = variationParamValue.replace(" ", ""); | |
54 String[] promoDataSavingStrings = variationParamValue.split(";"); | |
55 mPromoDataSavingsMB = new int[promoDataSavingStrings.length]; | |
56 | |
57 for (int i = 0; i < promoDataSavingStrings.length; i++) { | |
58 try { | |
59 mPromoDataSavingsMB[i] = Integer.parseInt(promoDataSavingStr ings[i]); | |
60 } catch (NumberFormatException e) { | |
61 mPromoDataSavingsMB[i] = -1; | |
62 } | |
63 } | |
64 } | |
65 | |
66 if (CommandLine.getInstance().hasSwitch(CLEAR_DATA_REDUCITON_PROXY_DATA_ SAVINGS_SWITCH)) { | |
67 DataReductionPromoUtils.saveSnackbarPromoDisplayed(0); | |
68 } | |
69 } | |
70 | |
71 /** | |
72 * Shows the Data Reduction Proxy promo snackbar if the current data savings are over | |
73 * specific thresholds set by finch and the snackbar has not been shown for that | |
74 * | |
75 * @param dataSavingInBytes The amount of data the Data Reduction Proxy has saved in bytes. | |
76 */ | |
77 public void maybeShowDataReductionPromoSnackbar(long dataSavingInBytes) { | |
78 // Prevents users who upgrade and have already saved mPromoDataSavingsIn MB from seeing the | |
79 // promo. | |
80 if (!DataReductionPromoUtils.hasSnackbarPromoBeenInitWithStartingContent Length()) { | |
81 DataReductionPromoUtils.saveSnackbarPromoInitWithStartingContentLeng th( | |
82 dataSavingInBytes); | |
83 return; | |
84 } | |
85 | |
86 for (int promoDataSavingMB : mPromoDataSavingsMB) { | |
87 if (dataSavingInBytes > 0 && dataSavingInBytes >= promoDataSavingMB * BYTES_IN_MB | |
88 && DataReductionPromoUtils | |
89 .getDisplayedSnackbarPromoContentLength() < promoDat aSavingMB | |
90 * BYTES_IN_MB) { | |
91 String dataSavedString = Formatter.formatShortFileSize(mContext, | |
92 promoDataSavingMB * BYTES_IN_MB).replace(".0", "\u00A0") ; | |
gone
2016/10/13 01:03:11
What's this unicode character for?
megjablon
2016/10/24 23:59:00
Removed. Fixed using the templated strings.
| |
93 mSnackbarManager.showSnackbar(Snackbar | |
94 .make(mContext.getString(R.string.data_reduction_promo_s nackbar_text) | |
95 .concat(" ").concat(dataSavedString), | |
gone
2016/10/13 01:03:11
You can't just concatenate these two strings becau
megjablon
2016/10/24 23:59:00
Ah thank you so much for this. Fixed.
| |
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 @Override | |
109 public void onAction(Object actionData) { | |
110 assert mContext != null; | |
111 Intent intent = PreferencesLauncher.createIntentForSettingsPage( | |
112 mContext, DataReductionPreferences.class.getName()); | |
113 intent.putExtra(FROM_PROMO, true); | |
114 mContext.startActivity(intent); | |
115 } | |
116 | |
117 @Override | |
118 public void onDismissNoAction(Object actionData) { | |
119 DataReductionProxyUma.dataReductionProxyUIAction( | |
120 DataReductionProxyUma.ACTION_SNACKBAR_DISMISSED); | |
121 } | |
122 } | |
OLD | NEW |