Skip to content

Commit

Permalink
Merge pull request #5937 from brave/android_upgrade_notification
Browse files Browse the repository at this point in the history
Android upgrade notification
  • Loading branch information
samartnik authored Jun 25, 2020
2 parents 9fb7b9d + 97620df commit 7023925
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/toolbar/bottom/BraveSearchAccelerator.java",
"../../brave/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java",
"../../brave/android/java/org/chromium/chrome/browser/upgrade/BravePackageReplacedBroadcastReceiver.java",
"../../brave/android/java/org/chromium/chrome/browser/upgrade/NotificationIntent.java",
"../../brave/android/java/org/chromium/chrome/browser/util/BraveDbUtil.java",
"../../brave/android/java/org/chromium/chrome/browser/util/BraveReferrer.java",
"../../brave/android/java/org/chromium/chrome/browser/util/PackageUtils.java",
Expand Down
30 changes: 30 additions & 0 deletions android/java/org/chromium/chrome/browser/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
Expand All @@ -26,6 +27,7 @@
import org.chromium.base.ApplicationStatus;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.IntentUtils;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.task.PostTask;
import org.chromium.base.task.TaskTraits;
Expand All @@ -39,6 +41,7 @@
import org.chromium.chrome.browser.notifications.BraveSetDefaultBrowserNotificationService;
import org.chromium.chrome.browser.onboarding.OnboardingActivity;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
import org.chromium.chrome.browser.settings.BraveRewardsPreferences;
Expand All @@ -62,6 +65,8 @@
import org.chromium.chrome.browser.rate.RateUtils;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;

import java.util.Calendar;

/**
* Brave's extension for ChromeActivity
*/
Expand Down Expand Up @@ -481,4 +486,29 @@ public void performPreInflationStartup() {
}
super.performPreInflationStartup();
}

@Override
protected @LaunchIntentDispatcher.Action int maybeDispatchLaunchIntent(
Intent intent, Bundle savedInstanceState) {
boolean notificationUpdate = IntentUtils.safeGetBooleanExtra(
intent, BravePreferenceKeys.BRAVE_UPDATE_EXTRA_PARAM, false);
if (notificationUpdate) {
SetUpdatePreferences();
}

return super.maybeDispatchLaunchIntent(intent, savedInstanceState);
}

private void SetUpdatePreferences() {
Calendar currentTime = Calendar.getInstance();
long milliSeconds = currentTime.getTimeInMillis();

SharedPreferences sharedPref =
getApplicationContext().getSharedPreferences(
BravePreferenceKeys.BRAVE_NOTIFICATION_PREF_NAME, 0);
SharedPreferences.Editor editor = sharedPref.edit();

editor.putLong(BravePreferenceKeys.BRAVE_MILLISECONDS_NAME, milliSeconds);
editor.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ public final class BravePreferenceKeys {
public static final String BRAVE_BOTTOM_TOOLBAR_SET_KEY = "brave_bottom_toolbar_enabled";
public static final String BRAVE_USE_CUSTOM_TABS = "use_custom_tabs";
public static final String BRAVE_APP_OPEN_COUNT = "brave_app_open_count";
public static final String BRAVE_UPDATE_EXTRA_PARAM =
"org.chromium.chrome.browser.upgrade.UPDATE_NOTIFICATION_NEW";
public static final String BRAVE_NOTIFICATION_PREF_NAME =
"org.chromium.chrome.browser.upgrade.NotificationUpdateTimeStampPreferences_New";
public static final String BRAVE_MILLISECONDS_NAME =
"org.chromium.chrome.browser.upgrade.Milliseconds_New";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public final class BravePackageReplacedBroadcastReceiver extends BroadcastReceiv
public void onReceive(final Context context, Intent intent) {
if (!Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) return;
SharedPreferencesManager.getInstance().writeInt(BravePreferenceKeys.BRAVE_APP_OPEN_COUNT, 0);
try {
NotificationIntent.fireNotificationIfNecessary(context);
} catch (Exception exc) {
// Just ignore if we could not send a notification
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.upgrade;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.view.View;

import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
* Fires notification about new features.
*/
public class NotificationIntent {
private static final String TAG = "NotificationIntent";
private static final String NOTIFICATION_TAG = "16c570a4-da7d-4c4e-8518-d2b7d6e41615";
private static final String NOTIFICATION_CHANNEL_ID = "a79c3102-4183-4001-a553-ec3041bd0f49";
private static final String URL = "https://support.brave.com/hc/en-us/articles/360045401211/";
private static final List<String> mWhitelistedRegionalLocales = Arrays.asList("en", "ru", "uk", "de", "pt", "pl", "ja", "es", "fr");
private static final int NOTIFICATION_ID = 732;
//private static final String NOTIFICATION_TITLE = "Brave update";
//private static final String NOTIFICATION_TEXT = "The new Brave browser is 22% faster";

public static void fireNotificationIfNecessary(Context context) {
String notification_text = context.getString(R.string.update_notification_text);
if (!ShouldNotify(context)) {
return;
}
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
"Channel for notifications",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);

// Create an intent that’ll be fired when a user taps the notification//
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
intent.putExtra(BravePreferenceKeys.BRAVE_UPDATE_EXTRA_PARAM, true);
intent.setPackage(context.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

mBuilder.setContentIntent(pendingIntent);
mBuilder.setAutoCancel(true);

mBuilder.setSmallIcon(R.drawable.ic_chrome);
mBuilder.setContentTitle(context.getString(R.string.update_notification_title));
mBuilder.setContentText(notification_text);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notification_text));

mNotificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, mBuilder.build());
}

public static boolean ShouldNotify(Context context) {
String deviceLanguage = Locale.getDefault().getLanguage();
if (GetPreferences(context) != 0
|| !mWhitelistedRegionalLocales.contains(new Locale(deviceLanguage).getLanguage())) {
return false;
}

return true;
}

public static long GetPreferences(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(BravePreferenceKeys.BRAVE_NOTIFICATION_PREF_NAME, 0);

return sharedPref.getLong(BravePreferenceKeys.BRAVE_MILLISECONDS_NAME, 0);
}
}
6 changes: 6 additions & 0 deletions browser/ui/android/strings/android_brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,12 @@ until they verify, or until 90 days have passed.
<message name="IDS_AUTOCOMPLETE_BRAVE_SUGGESTED_SITES_TITLE" desc="The label for settings switch controlling whether or not Brave suggested sites show up in autocomplete">
Show suggested sites in autocomplete
</message>
<message name="IDS_UPDATE_NOTIFICATION_TITLE" desc="Title of notification on application update.">
Save Data Online
</message>
<message name="IDS_UPDATE_NOTIFICATION_TEXT" desc="Text of notification on application update.">
Browse faster and block ads with Brave
</message>
</messages>
</release>
</grit>
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,6 @@ um das nächste Internet zu erleben.</translation>
<translation id="76550953340008122">Vielen Dank, dass Sie Braves Entwickler darüber informiert haben, dass auf dieser Website ein Fehler auftritt. Wir werden unser Bestes tun, um ihn zu beheben!</translation>
<translation id="650318023204990401">Fortgeschrittene Steuerung</translation>
<translation id="7182293319417450928">Ein Minimumguthaben von 25 BAT ist für die Verifizierung erforderlich.</translation>
<translation id="5370478469168390143">Daten online speichern</translation>
<translation id="1118508688433541158">Schneller surfen und Anzeigen blockieren mit Brave</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,6 @@ para apoyarlos.</translation>
<translation id="76550953340008122">Gracias por informar a los desarrolladores de Brave sobre posibles errores en este sitio. ¡Haremos todo lo posible para arreglarlo!</translation>
<translation id="650318023204990401">Controles avanzados</translation>
<translation id="7182293319417450928">Se requiere un saldo mínimo de 25 BAT para hacer la verificación.</translation>
<translation id="5370478469168390143">Guardar datos en línea</translation>
<translation id="1118508688433541158">Navegar más rápido y bloquear anuncios con Brave</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,6 @@ mois-ci...</translation>
<translation id="76550953340008122">Merci d'avoir fait savoir aux développeurs de Brave qu'il y a quelque chose qui ne marche pas sur ce site. Nous ferons de notre mieux pour le réparer !</translation>
<translation id="650318023204990401">Commandes avancées</translation>
<translation id="7182293319417450928">Un solde minimum de 25 BAT est requis pour la vérification.</translation>
<translation id="5370478469168390143">Economisez vos données mobiles</translation>
<translation id="1118508688433541158">Naviguez plus rapidement et bloquez les pubs avec Brave</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,6 @@
<translation id="76550953340008122">このサイトの問題をBraveの開発者にお知らせいただき、ありがとうございます。最善を尽くしてその修正を行います!</translation>
<translation id="650318023204990401">高度なコントロール</translation>
<translation id="7182293319417450928">照会するには最低25BATの残高が必要です。</translation>
<translation id="5370478469168390143">オンラインデータを保存</translation>
<translation id="1118508688433541158">Braveで高速ブラウジングし, 広告をブロックできる</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,6 @@ przez kolejne 90 dni lub do momentu zarejestrowania się twórców.</translation
<translation id="76550953340008122">Dziękujemy za poinformowanie deweloperów Brave, że coś jest nie tak z tą witryną. Dołożymy wszelkich starań, aby ją naprawić!</translation>
<translation id="650318023204990401">Zaawansowane sterowanie</translation>
<translation id="7182293319417450928">Do weryfikacji wymagane jest minimalne saldo w wysokości 25 BAT.</translation>
<translation id="5370478469168390143">Oszczędzaj transfer danych</translation>
<translation id="1118508688433541158">Przeglądaj szybciej i blokuj reklamy z Brave</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,6 @@ até que a verificação ocorra ou tenham decorrido 90 dias.</translation>
<translation id="76550953340008122">Obrigado por comunicar aos desenvolvedores da Brave que há algo de errado com o site. Daremos o nosso melhor para o consertar.</translation>
<translation id="650318023204990401">Controlos avançados</translation>
<translation id="7182293319417450928">É necessário um saldo mínimo de 25 MTD para a verificação.</translation>
<translation id="5370478469168390143">Economize Dados Online</translation>
<translation id="1118508688433541158">Navegue mais rápido e bloqueie anúncios online</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,6 @@
<translation id="76550953340008122">Спасибо, что сообщили разработчикам Brave о неполадке на этом сайте. Мы сделаем все возможное, чтобы исправить ее!</translation>
<translation id="650318023204990401">Расширенное управление</translation>
<translation id="7182293319417450928">Для проверки на балансе должно быть не менее 25 BAT.</translation>
<translation id="5370478469168390143">Экономь на трафике</translation>
<translation id="1118508688433541158">Загружай сайты быстрее и блокируй рекламу с Brave</translation>
</translationbundle>
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,6 @@
<translation id="76550953340008122">Дякуємо за повідомлення розробникам Brave щодо проблеми із цим сайтом. Ми постараємося її виправити!</translation>
<translation id="650318023204990401">Розширені налаштування</translation>
<translation id="7182293319417450928">Для підтвердження необхідний мінімальний баланс у 25 BAT.</translation>
<translation id="5370478469168390143">Заощаджуй на трафіку</translation>
<translation id="1118508688433541158">Завантажуй сайти швидше та блокуй рекламу з Brave</translation>
</translationbundle>

0 comments on commit 7023925

Please sign in to comment.