Chromium Code Reviews| 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.infobar; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.pm.PackageInfo; | |
| 9 import android.content.pm.PackageManager.NameNotFoundException; | |
| 10 | |
| 11 import org.chromium.chrome.R; | |
| 12 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | |
| 13 import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoU tils; | |
| 14 import org.chromium.content_public.browser.WebContents; | |
| 15 | |
| 16 import java.util.Calendar; | |
| 17 import java.util.TimeZone; | |
| 18 | |
| 19 /** | |
| 20 * Generates an InfoBar for data reduction proxy promotion that contains a messa ge and button to | |
| 21 * enable the proxy. | |
| 22 */ | |
| 23 public class DataReductionPromoInfoBar { | |
| 24 private static final String HTTP_SCHEME = "http://"; | |
|
bengr
2016/06/02 23:02:11
Use: https://code.google.com/p/chromium/codesearch
megjablon
2016/06/03 23:04:56
Done.
| |
| 25 | |
| 26 private static String sTitle; | |
| 27 private static String sText; | |
| 28 private static String sPrimaryButtonText; | |
| 29 private static String sSecondaryButtonText; | |
| 30 | |
| 31 /** | |
| 32 * Launch the data reduction InfoBar Promo, if it needs to be displayed. | |
| 33 * | |
| 34 * @param context An Android context. | |
| 35 * @param webContents The WebContents of the tab on which the InfoBar should show. | |
| 36 * @param url The URL of the page on which the InfoBar should show. | |
| 37 */ | |
| 38 public static void maybeLaunchDataReductionPromoInfoBar(Context context, | |
| 39 WebContents webContents, String url) { | |
| 40 if (!DataReductionPromoUtils.canShowDataReductionPromos()) return; | |
| 41 | |
| 42 // Don't show the promo if the user is not part of the field trial. | |
| 43 if (!DataReductionPromoUtils.getDataReductionInfoBarPromoFieldTrialEnabl ed()) return; | |
|
bengr
2016/06/02 23:02:11
Shouldn't this just be part of the second run prom
megjablon
2016/06/03 23:04:56
Done.
| |
| 44 | |
| 45 // Only show the promo on HTTP pages. | |
| 46 if (!url.startsWith(HTTP_SCHEME)) return; | |
|
bengr
2016/06/02 23:02:11
Use this instead:
https://code.google.com/p/chromi
megjablon
2016/06/03 23:04:56
Done.
| |
| 47 | |
| 48 // Don't show the InfoBar promo if neither the FRE or second run promo h ave been shown. | |
| 49 if (!DataReductionPromoUtils.getDisplayedDataReductionPromo()) return; | |
| 50 | |
| 51 // Don't show the promo if the user opted out on the FRE card. | |
| 52 if (DataReductionPromoUtils.getDataReductionFrePromoOptOut()) return; | |
| 53 | |
| 54 // Don't show the promo if the user has opted out of this InfoBar promo before. | |
| 55 if (DataReductionPromoUtils.getDataReductionInfoBarPromoOptOut()) return ; | |
| 56 | |
| 57 int current_build = getBuildFromVersionNumber( | |
| 58 PrefServiceBridge.getInstance().getAboutVersionStrings().getAppl icationVersion()); | |
| 59 int promo_build = getBuildFromVersionNumber( | |
| 60 DataReductionPromoUtils.getDisplayedDataReductionPromoVersion()) ; | |
| 61 int infobar_promo_build = getBuildFromVersionNumber( | |
| 62 DataReductionPromoUtils.getDisplayedDataReductionInfoBarPromoVer sion()); | |
| 63 | |
| 64 PackageInfo packageInfo; | |
| 65 try { | |
| 66 packageInfo = context.getPackageManager().getPackageInfo(context.get PackageName(), 0); | |
| 67 } catch (NameNotFoundException e) { | |
| 68 packageInfo = null; | |
| 69 } | |
| 70 Calendar m48ReleaseDateCal = Calendar.getInstance(TimeZone.getTimeZone(" UTC")); | |
| 71 m48ReleaseDateCal.set(2016, 1, 26); | |
| 72 long m48ReleaseDate = m48ReleaseDateCal.getTimeInMillis(); | |
| 73 long firstInstallTime = packageInfo == null ? 0 : packageInfo.firstInsta llTime; | |
| 74 | |
| 75 // The boolean pref storing if the user opted out on the FRE promo was a dded in 51. Don't | |
| 76 // show the infobar for FRE promos that were shown for builds 48 to 51. | |
| 77 if (promo_build < 51 && firstInstallTime > m48ReleaseDate) return; | |
| 78 | |
| 79 // Only show the promo if the current version is at least two builds aft er the last promo | |
| 80 // was displayed. | |
| 81 if (current_build < promo_build + 2) return; | |
| 82 | |
| 83 // Only show the promo if the current version is at least two builds aft er the last infobar | |
| 84 // promo was displayed. | |
| 85 if (DataReductionPromoUtils.getDisplayedDataReductionInfoBarPromo() | |
| 86 && (current_build < infobar_promo_build + 2)) { | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 DataReductionPromoInfoBar.launch(webContents, | |
| 91 context.getString(R.string.data_reduction_promo_infobar_title), | |
| 92 context.getString(R.string.data_reduction_promo_infobar_text), | |
| 93 context.getString(R.string.data_reduction_promo_infobar_button), | |
| 94 context.getString(R.string.no_thanks)); | |
| 95 } | |
| 96 | |
| 97 private static int getBuildFromVersionNumber(String version) { | |
| 98 if (version.isEmpty()) { | |
| 99 return -1; | |
| 100 } | |
| 101 | |
| 102 version = version.replaceAll("[^\\d.]", ""); | |
| 103 | |
| 104 // Parse out the version numbers. | |
| 105 String[] pieces = version.split("\\."); | |
| 106 if (pieces.length != 4) { | |
| 107 return -1; | |
| 108 } | |
| 109 | |
| 110 try { | |
| 111 return Integer.parseInt(pieces[0]); | |
| 112 } catch (NumberFormatException e) { | |
| 113 return -1; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Launch a data reduction proxy {@link InfoBar} with the specified title an d link | |
| 119 * text. Clicking the link will open the specified settings page. | |
| 120 * @param webContents The {@link WebContents} in which to open the {@link In foBar}. | |
| 121 * @param title The text to display in the {@link InfoBar}. | |
| 122 * @param primaryButtonText | |
| 123 * @param secondaryButtonText | |
| 124 */ | |
| 125 public static void launch(WebContents webContents, | |
| 126 String title, | |
| 127 String text, | |
| 128 String primaryButtonText, | |
| 129 String secondaryButtonText) { | |
| 130 sTitle = title; | |
| 131 sText = text; | |
| 132 sPrimaryButtonText = primaryButtonText; | |
| 133 sSecondaryButtonText = secondaryButtonText; | |
| 134 // TODO(megjablon): Show infobar. | |
| 135 DataReductionPromoUtils.saveDataReductionInfoBarPromoDisplayed(); | |
| 136 } | |
| 137 } | |
| OLD | NEW |