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 savings values in MB that the promo s
hould 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_REDUCTION_PROXY_DATA_SAVINGS_SWITCH = |
| 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(" ", ""); |
| 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_REDUCTION_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 dataSavingsInBytes The amount of data the Data Reduction Proxy has
saved in bytes. |
| 79 */ |
| 80 public void maybeShowDataReductionPromoSnackbar(long dataSavingsInBytes) { |
| 81 // Prevents users who upgrade and have already saved mPromoDataSavingsIn
MB from seeing the |
| 82 // promo. |
| 83 if (!DataReductionPromoUtils.hasSnackbarPromoBeenInitWithStartingSavedBy
tes()) { |
| 84 DataReductionPromoUtils.saveSnackbarPromoDisplayed(dataSavingsInByte
s); |
| 85 return; |
| 86 } |
| 87 |
| 88 for (int promoDataSavingsMB : mPromoDataSavingsMB) { |
| 89 long promoDataSavingsBytes = promoDataSavingsMB * BYTES_PER_MEGABYTE
; |
| 90 if (promoDataSavingsMB > 0 && dataSavingsInBytes >= promoDataSavings
Bytes |
| 91 && DataReductionPromoUtils |
| 92 .getDisplayedSnackbarPromoSavedBytes() < promoDataSa
vingsBytes) { |
| 93 mSnackbarManager.showSnackbar(Snackbar |
| 94 .make(getStringForBytes(promoDataSavingsBytes), |
| 95 this, |
| 96 Snackbar.TYPE_NOTIFICATION, Snackbar.UMA_DATA_RE
DUCTION_PROMO) |
| 97 .setAction( |
| 98 mContext.getString(R.string.data_reduction_promo
_snackbar_button), |
| 99 null)); |
| 100 DataReductionProxyUma.dataReductionProxySnackbarPromo(promoDataS
avingsMB); |
| 101 DataReductionPromoUtils.saveSnackbarPromoDisplayed(dataSavingsIn
Bytes); |
| 102 break; |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 private String getStringForBytes(long bytes) { |
| 108 int resourceId; |
| 109 int bytesInCorrectUnits; |
| 110 |
| 111 if (bytes < BYTES_PER_GIGABYTE) { |
| 112 resourceId = R.string.data_reduction_promo_snackbar_text_mb; |
| 113 bytesInCorrectUnits = (int) (bytes / BYTES_PER_MEGABYTE); |
| 114 } else { |
| 115 resourceId = R.string.data_reduction_promo_snackbar_text_gb; |
| 116 bytesInCorrectUnits = (int) (bytes / BYTES_PER_GIGABYTE); |
| 117 } |
| 118 |
| 119 return mContext.getResources().getString(resourceId, bytesInCorrectUnits
); |
| 120 } |
| 121 |
| 122 @Override |
| 123 public void onAction(Object actionData) { |
| 124 assert mContext != null; |
| 125 Intent intent = PreferencesLauncher.createIntentForSettingsPage( |
| 126 mContext, DataReductionPreferences.class.getName()); |
| 127 intent.putExtra(FROM_PROMO, true); |
| 128 mContext.startActivity(intent); |
| 129 } |
| 130 |
| 131 @Override |
| 132 public void onDismissNoAction(Object actionData) { |
| 133 DataReductionProxyUma.dataReductionProxyUIAction( |
| 134 DataReductionProxyUma.ACTION_SNACKBAR_DISMISSED); |
| 135 } |
| 136 } |
OLD | NEW |