Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/DataReductionPromoInfoBar.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/DataReductionPromoInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DataReductionPromoInfoBar.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2e8c9e3c392d5e579cce4233d9712a008494ab34 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/DataReductionPromoInfoBar.java |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.infobar; |
| + |
| +import android.content.Context; |
| +import android.content.pm.PackageInfo; |
| +import android.content.pm.PackageManager.NameNotFoundException; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| +import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoUtils; |
| +import org.chromium.content_public.browser.WebContents; |
| + |
| +import java.util.Calendar; |
| +import java.util.TimeZone; |
| + |
| +/** |
| + * Generates an InfoBar for data reduction proxy promotion that contains a message and button to |
| + * enable the proxy. |
| + */ |
| +public class DataReductionPromoInfoBar { |
| + 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.
|
| + |
| + private static String sTitle; |
| + private static String sText; |
| + private static String sPrimaryButtonText; |
| + private static String sSecondaryButtonText; |
| + |
| + /** |
| + * Launch the data reduction InfoBar Promo, if it needs to be displayed. |
| + * |
| + * @param context An Android context. |
| + * @param webContents The WebContents of the tab on which the InfoBar should show. |
| + * @param url The URL of the page on which the InfoBar should show. |
| + */ |
| + public static void maybeLaunchDataReductionPromoInfoBar(Context context, |
| + WebContents webContents, String url) { |
| + if (!DataReductionPromoUtils.canShowDataReductionPromos()) return; |
| + |
| + // Don't show the promo if the user is not part of the field trial. |
| + if (!DataReductionPromoUtils.getDataReductionInfoBarPromoFieldTrialEnabled()) 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.
|
| + |
| + // Only show the promo on HTTP pages. |
| + 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.
|
| + |
| + // Don't show the InfoBar promo if neither the FRE or second run promo have been shown. |
| + if (!DataReductionPromoUtils.getDisplayedDataReductionPromo()) return; |
| + |
| + // Don't show the promo if the user opted out on the FRE card. |
| + if (DataReductionPromoUtils.getDataReductionFrePromoOptOut()) return; |
| + |
| + // Don't show the promo if the user has opted out of this InfoBar promo before. |
| + if (DataReductionPromoUtils.getDataReductionInfoBarPromoOptOut()) return; |
| + |
| + int current_build = getBuildFromVersionNumber( |
| + PrefServiceBridge.getInstance().getAboutVersionStrings().getApplicationVersion()); |
| + int promo_build = getBuildFromVersionNumber( |
| + DataReductionPromoUtils.getDisplayedDataReductionPromoVersion()); |
| + int infobar_promo_build = getBuildFromVersionNumber( |
| + DataReductionPromoUtils.getDisplayedDataReductionInfoBarPromoVersion()); |
| + |
| + PackageInfo packageInfo; |
| + try { |
| + packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); |
| + } catch (NameNotFoundException e) { |
| + packageInfo = null; |
| + } |
| + Calendar m48ReleaseDateCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
| + m48ReleaseDateCal.set(2016, 1, 26); |
| + long m48ReleaseDate = m48ReleaseDateCal.getTimeInMillis(); |
| + long firstInstallTime = packageInfo == null ? 0 : packageInfo.firstInstallTime; |
| + |
| + // The boolean pref storing if the user opted out on the FRE promo was added in 51. Don't |
| + // show the infobar for FRE promos that were shown for builds 48 to 51. |
| + if (promo_build < 51 && firstInstallTime > m48ReleaseDate) return; |
| + |
| + // Only show the promo if the current version is at least two builds after the last promo |
| + // was displayed. |
| + if (current_build < promo_build + 2) return; |
| + |
| + // Only show the promo if the current version is at least two builds after the last infobar |
| + // promo was displayed. |
| + if (DataReductionPromoUtils.getDisplayedDataReductionInfoBarPromo() |
| + && (current_build < infobar_promo_build + 2)) { |
| + return; |
| + } |
| + |
| + DataReductionPromoInfoBar.launch(webContents, |
| + context.getString(R.string.data_reduction_promo_infobar_title), |
| + context.getString(R.string.data_reduction_promo_infobar_text), |
| + context.getString(R.string.data_reduction_promo_infobar_button), |
| + context.getString(R.string.no_thanks)); |
| + } |
| + |
| + private static int getBuildFromVersionNumber(String version) { |
| + if (version.isEmpty()) { |
| + return -1; |
| + } |
| + |
| + version = version.replaceAll("[^\\d.]", ""); |
| + |
| + // Parse out the version numbers. |
| + String[] pieces = version.split("\\."); |
| + if (pieces.length != 4) { |
| + return -1; |
| + } |
| + |
| + try { |
| + return Integer.parseInt(pieces[0]); |
| + } catch (NumberFormatException e) { |
| + return -1; |
| + } |
| + } |
| + |
| + /** |
| + * Launch a data reduction proxy {@link InfoBar} with the specified title and link |
| + * text. Clicking the link will open the specified settings page. |
| + * @param webContents The {@link WebContents} in which to open the {@link InfoBar}. |
| + * @param title The text to display in the {@link InfoBar}. |
| + * @param primaryButtonText |
| + * @param secondaryButtonText |
| + */ |
| + public static void launch(WebContents webContents, |
| + String title, |
| + String text, |
| + String primaryButtonText, |
| + String secondaryButtonText) { |
| + sTitle = title; |
| + sText = text; |
| + sPrimaryButtonText = primaryButtonText; |
| + sSecondaryButtonText = secondaryButtonText; |
| + // TODO(megjablon): Show infobar. |
| + DataReductionPromoUtils.saveDataReductionInfoBarPromoDisplayed(); |
| + } |
| +} |