Skip to content

Commit

Permalink
Uplift of #16128 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Dec 10, 2022
1 parent e068800 commit ce206c1
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 34 deletions.
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/notifications/BraveNotificationWarningDialog.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/BraveOnboardingNotification.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/BravePermissionUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/permissions/BraveNotificationPermissionRationaleDialog.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/permissions/BraveNotificationPermissionRationaleDialogController.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/retention/RetentionNotification.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/retention/RetentionNotificationPublisher.java",
Expand Down
10 changes: 8 additions & 2 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1245,10 +1245,16 @@ public void onDismiss() {
};

private void checkAndshowNotificationWarningDialog() {
if (BraveNotificationWarningDialog.shouldShowNotificationWarningDialog(this)
OnboardingPrefManager.getInstance().updateLaunchCount();
if (OnboardingPrefManager.getInstance().launchCount() >= 3
&& BraveNotificationWarningDialog.shouldShowNotificationWarningDialog(this)
&& !OnboardingPrefManager.getInstance()
.isNotificationPermissionEnablingDialogShown()) {
showNotificationWarningDialog();
if (BravePermissionUtils.hasNotificationPermission(this)) {
showNotificationWarningDialog();
} else {
maybeShowNotificationPermissionRetionale();
}
OnboardingPrefManager.getInstance().setNotificationPermissionEnablingDialogShown(true);
} else {
checkForNotificationData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.res.ResourcesCompat;

import org.chromium.base.BuildInfo;
import org.chromium.base.Log;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveAdsNativeHelper;
import org.chromium.chrome.browser.BraveDialogFragment;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.ui.permissions.PermissionConstants;

/**
Expand Down Expand Up @@ -74,17 +74,39 @@ public static BraveNotificationWarningDialog newInstance(int launchedFrom) {
}

/**
* If no notification permission and if any privacy or rewards state is on then return true
* Should show dialog if any one is true
* 1. No notification permission
* 2. Notification permission is there but general or ads group is blocked
*
* if above any case is there and rewards / privacy / both enabled.
* */
public static boolean shouldShowNotificationWarningDialog(Context context) {
if (!BravePermissionUtils.hasPermission(
context, PermissionConstants.NOTIFICATION_PERMISSION)) {
return OnboardingPrefManager.getInstance().isBraveStatsEnabled()
|| OnboardingPrefManager.getInstance().isBraveAdsEnabled();
if (!BravePermissionUtils.hasNotificationPermission(context)) {
return true;
} else if (shouldShowRewardWarningDialog(context)
|| shouldShowPrivacyWarningDialog(context)) {
return true;
}
return false;
}

private static boolean shouldShowRewardWarningDialog(Context context) {
return BravePermissionUtils.isBraveAdsNotificationPermissionBlocked(context)
&& isBraveRewardsEnabled();
}

private static boolean shouldShowPrivacyWarningDialog(Context context) {
return BravePermissionUtils.isGeneralNotificationPermissionBlocked(context)
&& isPrivacyReportsEnabled();
}

private static boolean shouldShowBothWarningDialog(Context context) {
if (!BravePermissionUtils.hasNotificationPermission(context)) {
return true;
}
return shouldShowRewardWarningDialog(context) && shouldShowPrivacyWarningDialog(context);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -117,11 +139,11 @@ private void init(View view) {
clickOnNotNow(view);
}

public boolean isBraveRewardsEnabled() {
return OnboardingPrefManager.getInstance().isBraveAdsEnabled();
public static boolean isBraveRewardsEnabled() {
return BraveAdsNativeHelper.nativeIsBraveAdsEnabled(Profile.getLastUsedRegularProfile());
}

public boolean isPrivacyReportsEnabled() {
public static boolean isPrivacyReportsEnabled() {
return OnboardingPrefManager.getInstance().isBraveStatsEnabled();
}

Expand Down Expand Up @@ -155,14 +177,14 @@ private void launchedFromBravePanel(View view) {
private void launchedFromBraveActivity(View view) {
mPrimaryButton.setText(R.string.turn_on_brave_notifications);

if (isBraveRewardsEnabled() && isPrivacyReportsEnabled()) {
if (shouldShowBothWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_os_dialog_header_both_rewards_privacy);
mDescriptionTextView.setText(
R.string.notification_os_dialog_description_both_rewards_privacy);
} else if (isBraveRewardsEnabled()) {
} else if (shouldShowRewardWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_os_dialog_header_only_rewards);
mDescriptionTextView.setText(R.string.notification_os_dialog_description_only_rewards);
} else if (isPrivacyReportsEnabled()) {
} else if (shouldShowPrivacyWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_os_dialog_header_only_privacy);
mDescriptionTextView.setText(R.string.notification_os_dialog_description_only_privacy);
}
Expand All @@ -171,15 +193,15 @@ private void launchedFromBraveActivity(View view) {
private void launchedFromBraveSettings(View view) {
mPrimaryButton.setText(R.string.got_it);

if (isBraveRewardsEnabled() && isPrivacyReportsEnabled()) {
if (shouldShowBothWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_brave_dialog_header_both_rewards_privacy);
mDescriptionTextView.setText(
R.string.notification_brave_dialog_description_both_rewards_privacy);
} else if (isBraveRewardsEnabled()) {
} else if (shouldShowRewardWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_brave_dialog_header_only_rewards);
mDescriptionTextView.setText(
R.string.notification_brave_dialog_description_only_rewards);
} else if (isPrivacyReportsEnabled()) {
} else if (shouldShowPrivacyWarningDialog(getContext())) {
mTitleTextView.setText(R.string.notification_brave_dialog_header_only_privacy);
mDescriptionTextView.setText(
R.string.notification_brave_dialog_description_only_privacy);
Expand All @@ -190,18 +212,7 @@ private void clickOnPrimaryButton(View view) {
Button primaryButton = view.findViewById(R.id.notification_warning_primary_button);
primaryButton.setOnClickListener(v -> {
dismiss();
if (getActivity().shouldShowRequestPermissionRationale(
PermissionConstants.NOTIFICATION_PERMISSION)
|| (!BuildInfo.isAtLeastT() || !BuildInfo.targetsAtLeastT())) {
// other than android 13 redirect to
// setting page and for android 13 Last time don't allow selected in permission
// dialog, then enable through setting
BravePermissionUtils.notificationSettingPage(getContext());
} else {
// 1st time request permission
ActivityCompat.requestPermissions(getActivity(),
new String[] {PermissionConstants.NOTIFICATION_PERMISSION}, 1);
}
BravePermissionUtils.requestPermission(getActivity());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,34 @@

package org.chromium.chrome.browser.notifications;

import android.app.Activity;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

import org.chromium.base.BuildInfo;
import org.chromium.chrome.browser.notifications.channels.BraveChannelDefinitions;
import org.chromium.ui.permissions.PermissionConstants;

/**
* This class is for settings permission related checks
*/
public class BravePermissionUtils {
private static final String APP_PACKAGE = "app_package";
private static final String APP_UID = "app_uid";

public static boolean hasNotificationPermission(Context context) {
return hasPermission(context, PermissionConstants.NOTIFICATION_PERMISSION);
}

public static Boolean hasPermission(Context context, String permission) {
return ContextCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED;
Expand All @@ -40,4 +54,40 @@ public static void notificationSettingPage(Context context) {

context.startActivity(intent);
}

// When in OS notification permission particular group switch is off means that group is blocked
public static boolean channeGroupIsBlocked(Context context, String channelGroupName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannelGroup notificationChannelGroup =
notificationManager.getNotificationChannelGroup(channelGroupName);
return notificationChannelGroup.isBlocked();
} else {
return NotificationManagerCompat.from(context).areNotificationsEnabled();
}
}

public static boolean isBraveAdsNotificationPermissionBlocked(Context context) {
return channeGroupIsBlocked(context, BraveChannelDefinitions.ChannelGroupId.BRAVE_ADS);
}

public static boolean isGeneralNotificationPermissionBlocked(Context context) {
return channeGroupIsBlocked(context, BraveChannelDefinitions.ChannelGroupId.GENERAL);
}

public static void requestPermission(Activity activity) {
if (activity.shouldShowRequestPermissionRationale(
PermissionConstants.NOTIFICATION_PERMISSION)
|| (!BuildInfo.isAtLeastT() || !BuildInfo.targetsAtLeastT())) {
// other than android 13 redirect to
// setting page and for android 13 Last time don't allow selected in permission
// dialog, then enable through setting
BravePermissionUtils.notificationSettingPage(activity);
} else {
// 1st time request permission
ActivityCompat.requestPermissions(
activity, new String[] {PermissionConstants.NOTIFICATION_PERMISSION}, 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright (c) 2022 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 https://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.notifications.permissions;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveDialogFragment;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;

public class BraveNotificationPermissionRationaleDialog extends BraveDialogFragment {
public static BraveNotificationPermissionRationaleDialog newInstance() {
BraveNotificationPermissionRationaleDialog fragment =
new BraveNotificationPermissionRationaleDialog();
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.brave_notification_permission_rationale_dialog, container, false);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
clickOnContinueButton(view);
clickOnNotNow(view);
}

private void clickOnContinueButton(View view) {
Button primaryButton = view.findViewById(R.id.notification_continue_button);
primaryButton.setOnClickListener(v -> {
dismiss();
BravePermissionUtils.requestPermission(getActivity());
});
}

private void clickOnNotNow(View view) {
Button notNowButton = view.findViewById(R.id.notification_not_now_button);
notNowButton.setOnClickListener(v -> { dismiss(); });
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2022 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/. */
* You can obtain one at https://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.notifications.permissions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class OnboardingPrefManager {
private static final String PREF_NOTIFICATION_PERMISSION_ENABLING_DIALOG =
"notification_permission_enabling_dialog";

private static final String PREF_NOTIFICATION_PERMISSION_ENABLING_DIALOG_FROM_SETTING =
"notification_permission_enabling_dialog_from_setting";

private static final String PREF_APP_LAUNCH_COUNT = "APP_LAUNCH_COUNT";

private static OnboardingPrefManager sInstance;

private final SharedPreferences mSharedPreferences;
Expand Down Expand Up @@ -359,4 +364,40 @@ public void setNotificationPermissionEnablingDialogShown(boolean isShown) {
sharedPreferencesEditor.putBoolean(PREF_NOTIFICATION_PERMISSION_ENABLING_DIALOG, isShown);
sharedPreferencesEditor.apply();
}

/**
* Returns the user preference for whether the Notification Permission Enabling dialog is shown
* From setting.
*/
public boolean isNotificationPermissionEnablingDialogShownFromSetting() {
return mSharedPreferences.getBoolean(
PREF_NOTIFICATION_PERMISSION_ENABLING_DIALOG_FROM_SETTING, false);
}

/**
* Sets the user preference for whether the Notification Permission Enabling dialog is shown
* From setting.
*/
public void setNotificationPermissionEnablingDialogShownFromSetting(boolean isShown) {
SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
sharedPreferencesEditor.putBoolean(
PREF_NOTIFICATION_PERMISSION_ENABLING_DIALOG_FROM_SETTING, isShown);
sharedPreferencesEditor.apply();
}

/**
* Returns the user preference for application launch count
*/
public int launchCount() {
return mSharedPreferences.getInt(PREF_APP_LAUNCH_COUNT, 0);
}

/**
* Sets the user preference for application launch count
*/
public void updateLaunchCount() {
SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit();
sharedPreferencesEditor.putInt(PREF_APP_LAUNCH_COUNT, launchCount() + 1);
sharedPreferencesEditor.apply();
}
}
Loading

0 comments on commit ce206c1

Please sign in to comment.