Skip to content

Commit

Permalink
Android 13 showWarning modal condition update
Browse files Browse the repository at this point in the history
Notification permission dialog dark mode
  • Loading branch information
sujitacharya2005 committed Dec 7, 2022
1 parent 99288a5 commit 1a27a1a
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 59 deletions.
2 changes: 2 additions & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,9 @@ brave_java_resources = [
"java/res/drawable/modern_toolbar_background_grey_middle_segment.xml",
"java/res/drawable/monthly_selector.xml",
"java/res/drawable/news_button_bg.xml",
"java/res/drawable/notification_button_rounded_bg.xml",
"java/res/drawable/notification_on_button_background.xml",
"java/res/drawable/notification_rationale_dialog_background.xml",
"java/res/drawable/orange_rounded_button.xml",
"java/res/drawable/progress_indeterminate.xml",
"java/res/drawable/progress_indeterminate_orange.xml",
Expand Down
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
25 changes: 18 additions & 7 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import org.chromium.chrome.browser.informers.BraveAndroidSyncDisabledInformer;
import org.chromium.chrome.browser.notifications.BraveNotificationWarningDialog;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;
import org.chromium.chrome.browser.notifications.permissions.BraveNotificationPermissionRationaleDialog;
import org.chromium.chrome.browser.notifications.permissions.NotificationPermissionController;
import org.chromium.chrome.browser.notifications.permissions.NotificationPermissionRationaleDialogController;
import org.chromium.chrome.browser.notifications.retention.RetentionNotificationUtil;
Expand Down Expand Up @@ -1260,11 +1261,17 @@ private void checkForYandexSE() {
}
}

public void showNotificationRationale() {
BraveNotificationPermissionRationaleDialog notificationRationaleDialog =
BraveNotificationPermissionRationaleDialog.newInstance();
notificationRationaleDialog.show(getSupportFragmentManager(),
BraveNotificationPermissionRationaleDialog.NOTIFICATION_RATIONALE_DIALOG_TAG);
}

private void showNotificationWarningDialog() {
BraveNotificationWarningDialog notificationWarningDialog =
BraveNotificationWarningDialog.newInstance(
BraveNotificationWarningDialog.FROM_LAUNCHED_BRAVE_ACTIVITY);
notificationWarningDialog.setCancelable(false);
notificationWarningDialog.setDismissListener(closeDialogListener);
notificationWarningDialog.show(getSupportFragmentManager(),
BraveNotificationWarningDialog.NOTIFICATION_WARNING_DIALOG_TAG);
Expand All @@ -1279,11 +1286,15 @@ public void onDismiss() {
};

private void checkAndshowNotificationWarningDialog() {
if (BraveNotificationWarningDialog.shouldShowNotificationWarningDialog(this)
&& !OnboardingPrefManager.getInstance()
.isNotificationPermissionEnablingDialogShown()) {
showNotificationWarningDialog();
OnboardingPrefManager.getInstance().setNotificationPermissionEnablingDialogShown(true);
OnboardingPrefManager.getInstance().updateLaunchCount();
if (OnboardingPrefManager.getInstance().launchCount() == 3
&& BraveNotificationWarningDialog.shouldShowNotificationWarningDialog(this)) {
if (BraveNotificationWarningDialog.shouldShowRewardWarningDialog(this)
|| BraveNotificationWarningDialog.shouldShowPrivacyWarningDialog(this)) {
showNotificationWarningDialog();
} else {
showNotificationRationale();
}
} else {
checkForNotificationData();
}
Expand Down Expand Up @@ -1657,7 +1668,7 @@ public void onNewIntent(Intent intent) {
openNewOrSelectExistingTab(openUrl);
}
}
checkAndshowNotificationWarningDialog();
checkForNotificationData();
}

@Override
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,38 @@ 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;
}

public static boolean shouldShowRewardWarningDialog(Context context) {
return (!BravePermissionUtils.hasNotificationPermission(context)
|| BravePermissionUtils.isBraveAdsNotificationPermissionBlocked(context))
&& isBraveRewardsEnabled();
}

public static boolean shouldShowPrivacyWarningDialog(Context context) {
return (!BravePermissionUtils.hasNotificationPermission(context)
|| BravePermissionUtils.isGeneralNotificationPermissionBlocked(context))
&& isPrivacyReportsEnabled();
}

private static boolean shouldShowBothWarningDialog(Context context) {
return shouldShowRewardWarningDialog(context) && shouldShowPrivacyWarningDialog(context);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -117,11 +138,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 +176,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 +192,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 +211,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,73 @@
/**
* 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/.
*/

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

import android.content.Context;
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 final String NOTIFICATION_RATIONALE_DIALOG_TAG =
"notification_rationale_dialog_tag";

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(); });
}
}
Loading

0 comments on commit 1a27a1a

Please sign in to comment.