Set default browser notification at 11:22
This commit is contained in:
@@ -13,6 +13,7 @@ brave_java_sources = [
|
||||
"../../brave/android/java/org/chromium/chrome/browser/appmenu/BraveShieldsMenuHandler.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/appmenu/BraveShieldsMenuObserver.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/document/BraveLauncherActivity.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/notifications/BraveSetDefaultBrowserNotificationService.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/ntp/BraveDuckDuckGoOfferView.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java",
|
||||
"../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageView.java",
|
||||
|
||||
@@ -5,10 +5,13 @@
|
||||
|
||||
package org.chromium.chrome.browser;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
@@ -23,6 +26,7 @@ import org.chromium.base.Log;
|
||||
import org.chromium.base.annotations.JNINamespace;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.ChromeSwitches;
|
||||
import org.chromium.chrome.browser.notifications.BraveSetDefaultBrowserNotificationService;
|
||||
import org.chromium.chrome.browser.preferences.BraveSearchEngineUtils;
|
||||
import org.chromium.chrome.browser.preferences.Pref;
|
||||
import org.chromium.chrome.browser.preferences.PrefServiceBridge;
|
||||
@@ -41,6 +45,7 @@ public abstract class BraveActivity extends ChromeActivity {
|
||||
*/
|
||||
public static final String BRAVE_PRODUCTION_PACKAGE_NAME = "com.brave.browser";
|
||||
public static final String BRAVE_DEVELOPMENT_PACKAGE_NAME = "com.brave.browser_default";
|
||||
public static final String CHANNEL_ID = "com.brave.browser";
|
||||
|
||||
@Override
|
||||
public void onResumeWithNative() {
|
||||
@@ -116,17 +121,43 @@ public abstract class BraveActivity extends ChromeActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNoRestoreState() {
|
||||
return ContextUtils.getAppSharedPreferences().getBoolean(PREF_CLOSE_TABS_ON_EXIT, false);
|
||||
@Override
|
||||
public void performPostInflationStartup() {
|
||||
super.performPostInflationStartup();
|
||||
|
||||
createNotificationChannel();
|
||||
setupBraveSetDefaultBrowserNotification();
|
||||
}
|
||||
|
||||
public boolean isBraveSetAsDefaultBrowser() {
|
||||
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
|
||||
boolean supportsDefault = Build.VERSION.SDK_INT >= 24;
|
||||
ResolveInfo resolveInfo = getPackageManager().resolveActivity(
|
||||
browserIntent, supportsDefault ? PackageManager.MATCH_DEFAULT_ONLY : 0);
|
||||
return resolveInfo.activityInfo.packageName.equals(BRAVE_PRODUCTION_PACKAGE_NAME)
|
||||
|| resolveInfo.activityInfo.packageName.equals(BRAVE_DEVELOPMENT_PACKAGE_NAME);
|
||||
private void createNotificationChannel() {
|
||||
Context context = ContextUtils.getApplicationContext();
|
||||
// Create the NotificationChannel, but only on API 26+ because
|
||||
// the NotificationChannel class is new and not in the support library
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
CharSequence name = "Brave Browser";
|
||||
String description = "Notification channel for Brave Browser";
|
||||
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
||||
channel.setDescription(description);
|
||||
// Register the channel with the system; you can't change the importance
|
||||
// or other notification behaviors after this
|
||||
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
}
|
||||
}
|
||||
|
||||
private void setupBraveSetDefaultBrowserNotification() {
|
||||
Context context = ContextUtils.getApplicationContext();
|
||||
if (BraveSetDefaultBrowserNotificationService.isBraveSetAsDefaultBrowser(this)) {
|
||||
// Don't ask again
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(context, BraveSetDefaultBrowserNotificationService.class);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
private boolean isNoRestoreState() {
|
||||
return ContextUtils.getAppSharedPreferences().getBoolean(PREF_CLOSE_TABS_ON_EXIT, false);
|
||||
}
|
||||
|
||||
private void handleBraveSetDefaultBrowserDialog() {
|
||||
@@ -138,7 +169,7 @@ public abstract class BraveActivity extends ChromeActivity {
|
||||
ResolveInfo resolveInfo = getPackageManager().resolveActivity(
|
||||
browserIntent, supportsDefault ? PackageManager.MATCH_DEFAULT_ONLY : 0);
|
||||
Context context = ContextUtils.getApplicationContext();
|
||||
if (isBraveSetAsDefaultBrowser()) {
|
||||
if (BraveSetDefaultBrowserNotificationService.isBraveSetAsDefaultBrowser(this)) {
|
||||
Toast toast = Toast.makeText(
|
||||
context, R.string.brave_already_set_as_default_browser, Toast.LENGTH_LONG);
|
||||
toast.show();
|
||||
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
/* Copyright (c) 2019 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.notifications;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.provider.Settings;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.NotificationManagerCompat;
|
||||
|
||||
import org.chromium.base.Log;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.base.ContextUtils;
|
||||
import org.chromium.chrome.browser.BraveActivity;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public class BraveSetDefaultBrowserNotificationService extends BroadcastReceiver {
|
||||
public Context mContext;
|
||||
private Intent mIntent;
|
||||
|
||||
public static final String HAS_ASKED_AT_11_22 = "brave_set_default_browser_has_at_11_22";
|
||||
|
||||
public static final int NOTIFICATION_ID = 10;
|
||||
|
||||
// Deep links
|
||||
public static final String DEEP_LINK = "deep_link";
|
||||
public static final String SHOW_DEFAULT_APP_SETTINGS = "SHOW_DEFAULT_APP_SETTINGS";
|
||||
|
||||
// Intent types
|
||||
public static final String INTENT_TYPE = "intent_type";
|
||||
public static final String AT_11_22 = "at_11_22";
|
||||
|
||||
public static final String CANCEL_NOTIFICATION = "cancel_notification";
|
||||
|
||||
// Startup notification data
|
||||
public static final String NOTIFICATION_ID_EXTRA = "notification_id_extra";
|
||||
|
||||
public static boolean isBraveSetAsDefaultBrowser(Context context) {
|
||||
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
|
||||
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(browserIntent, supportsDefault() ? PackageManager.MATCH_DEFAULT_ONLY : 0);
|
||||
return resolveInfo.activityInfo.packageName.equals(BraveActivity.BRAVE_PRODUCTION_PACKAGE_NAME) || resolveInfo.activityInfo.packageName.equals(BraveActivity.BRAVE_DEVELOPMENT_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
private boolean shouldShowNotification() {
|
||||
if (isBraveSetAsDefaultBrowser(mContext) || mIntent.getStringExtra(INTENT_TYPE) == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mIntent.getStringExtra(INTENT_TYPE).equals(AT_11_22)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Shouldn't reach here. Just out of safety, don't annoy users
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean supportsDefault() {
|
||||
return Build.VERSION.SDK_INT >= 24;
|
||||
}
|
||||
|
||||
private boolean hasAlternateDefaultBrowser() {
|
||||
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
|
||||
ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(browserIntent, supportsDefault() ? PackageManager.MATCH_DEFAULT_ONLY : 0);
|
||||
return !(resolveInfo.activityInfo.packageName.equals("com.google.android.setupwizard") || resolveInfo.activityInfo.packageName.equals("android"));
|
||||
}
|
||||
|
||||
private void showNotification() {
|
||||
NotificationCompat.Builder b = new NotificationCompat.Builder(mContext, BraveActivity.CHANNEL_ID);
|
||||
|
||||
b.setSmallIcon(R.drawable.ic_chrome)
|
||||
.setAutoCancel(false)
|
||||
.setContentTitle("⚡" + mContext.getString(R.string.brave_default_browser_title) + "⚡")
|
||||
.setContentText(mContext.getString(R.string.brave_default_browser_body))
|
||||
.setStyle(new NotificationCompat.BigTextStyle().bigText(mContext.getString(R.string.brave_default_browser_body)))
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setCategory(NotificationCompat.CATEGORY_MESSAGE);
|
||||
|
||||
if (supportsDefault()) {
|
||||
PendingIntent intentYes = getDefaultAppSettingsIntent(mContext);
|
||||
NotificationCompat.Action actionYes = new NotificationCompat.Action.Builder(0, mContext.getString(R.string.brave_make_default_text), intentYes).build();
|
||||
NotificationCompat.Action actionNo = new NotificationCompat.Action.Builder(0, mContext.getString(R.string.brave_make_default_no), getDismissIntent(mContext, NOTIFICATION_ID)).build();
|
||||
b.addAction(actionNo);
|
||||
b.addAction(actionYes);
|
||||
b.setContentIntent(intentYes);
|
||||
}
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.notify(NOTIFICATION_ID, b.build());
|
||||
}
|
||||
|
||||
public static PendingIntent getDefaultAppSettingsIntent(Context context) {
|
||||
Intent intent = new Intent(context, BraveSetDefaultBrowserNotificationService.class);
|
||||
intent.setAction(DEEP_LINK);
|
||||
intent.putExtra(DEEP_LINK, SHOW_DEFAULT_APP_SETTINGS);
|
||||
intent.putExtra(NOTIFICATION_ID_EXTRA, NOTIFICATION_ID);
|
||||
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
|
||||
public static PendingIntent getDismissIntent(Context context, int notification_id) {
|
||||
Intent intent = new Intent(context, BraveSetDefaultBrowserNotificationService.class);
|
||||
intent.setAction(CANCEL_NOTIFICATION);
|
||||
intent.putExtra(NOTIFICATION_ID_EXTRA, notification_id);
|
||||
|
||||
return PendingIntent.getBroadcast(context, notification_id, intent, 0);
|
||||
}
|
||||
|
||||
private boolean hasAskedAt1122() {
|
||||
SharedPreferences sharedPref = ContextUtils.getAppSharedPreferences();
|
||||
// Check to see if we already asked at 11:22
|
||||
return sharedPref.getBoolean(HAS_ASKED_AT_11_22, false);
|
||||
}
|
||||
|
||||
private void setAlarmFor1122() {
|
||||
SharedPreferences sharedPref = ContextUtils.getAppSharedPreferences();
|
||||
SharedPreferences.Editor editor = sharedPref.edit();
|
||||
editor.putBoolean(HAS_ASKED_AT_11_22, true);
|
||||
editor.apply();
|
||||
|
||||
Calendar currentTime = Calendar.getInstance();
|
||||
if (currentTime.get(Calendar.HOUR_OF_DAY) > 11 ||
|
||||
(currentTime.get(Calendar.HOUR_OF_DAY) == 11 && currentTime.get(Calendar.MINUTE) >= 22)) {
|
||||
// Current time is after 11:22, so set alarm on tomorrow
|
||||
currentTime.add(Calendar.DAY_OF_YEAR, 1);
|
||||
}
|
||||
currentTime.set(Calendar.HOUR_OF_DAY, 11);
|
||||
currentTime.set(Calendar.MINUTE, 22);
|
||||
currentTime.set(Calendar.SECOND, 0);
|
||||
currentTime.set(Calendar.MILLISECOND, 0);
|
||||
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(mContext, BraveSetDefaultBrowserNotificationService.class);
|
||||
intent.putExtra(INTENT_TYPE, AT_11_22);
|
||||
intent.setAction(AT_11_22);
|
||||
am.setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
currentTime.getTimeInMillis(),
|
||||
PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
);
|
||||
}
|
||||
|
||||
private void handleBraveSetDefaultBrowserDeepLink(Intent intent) {
|
||||
Bundle bundle = intent.getExtras();
|
||||
if (bundle.getString(BraveSetDefaultBrowserNotificationService.DEEP_LINK).equals(BraveSetDefaultBrowserNotificationService.SHOW_DEFAULT_APP_SETTINGS)) {
|
||||
Intent settingsIntent = hasAlternateDefaultBrowser() ?
|
||||
new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS) :
|
||||
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.brave.com/blog"));
|
||||
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
mContext.startActivity(settingsIntent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
mContext = context;
|
||||
mIntent = intent;
|
||||
boolean deepLinkIsHandled = false;
|
||||
if (intent != null && intent.hasExtra(BraveSetDefaultBrowserNotificationService.DEEP_LINK)) {
|
||||
handleBraveSetDefaultBrowserDeepLink(intent);
|
||||
deepLinkIsHandled = true;
|
||||
}
|
||||
|
||||
if (deepLinkIsHandled ||
|
||||
(intent != null && intent.getAction() != null && intent.getAction().equals(CANCEL_NOTIFICATION))) {
|
||||
int notification_id = intent.getIntExtra(NOTIFICATION_ID_EXTRA, 0);
|
||||
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.cancel(notification_id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (shouldShowNotification()) {
|
||||
showNotification();
|
||||
} else if (!hasAskedAt1122()) {
|
||||
setAlarmFor1122();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,6 +240,18 @@ This file contains all "about" strings. It is set to NOT be translated, in tran
|
||||
<message name="IDS_BRAVE_DEFAULT_BROWSER_GO_TO_SETTINGS" desc="Toast message for those without 7.0 to go to settings to change default browser">
|
||||
You've already set a default browser. You may change the default browser in your app settings.
|
||||
</message>
|
||||
<message name="IDS_BRAVE_DEFAULT_BROWSER_TITLE" desc="Title for default browser notification.">
|
||||
Brave is lightning fast
|
||||
</message>
|
||||
<message name="IDS_BRAVE_DEFAULT_BROWSER_BODY" desc="Body for default browser notification.">
|
||||
Make Brave your default browser!
|
||||
</message>
|
||||
<message name="IDS_BRAVE_MAKE_DEFAULT_TEXT" desc="Text for positive default browser notification button.">
|
||||
Make default
|
||||
</message>
|
||||
<message name="IDS_BRAVE_MAKE_DEFAULT_NO" desc="Negative button text for making default browser.">
|
||||
No
|
||||
</message>
|
||||
<message name="IDS_MENU_BRAVE_REWARDS" desc="Title for brave rewards menu item">
|
||||
Brave Rewards
|
||||
</message>
|
||||
|
||||
Reference in New Issue
Block a user