Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android 13] Notifications - user manually disable notifications #15407

Merged
merged 6 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ brave_java_resources = [
"java/res/drawable/ic_baseline_more_vert_24.xml",
"java/res/drawable/ic_basic_check.xml",
"java/res/drawable/ic_bat_logo.xml",
"java/res/drawable/ic_bell_icon.xml",
"java/res/drawable/ic_bg.xml",
"java/res/drawable/ic_bg_video_playback.xml",
"java/res/drawable/ic_brave.xml",
Expand Down Expand Up @@ -654,6 +655,7 @@ 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_on_button_background.xml",
"java/res/drawable/orange_rounded_button.xml",
"java/res/drawable/progress_indeterminate.xml",
"java/res/drawable/progress_indeterminate_orange.xml",
Expand Down Expand Up @@ -754,6 +756,7 @@ brave_java_resources = [
"java/res/layout/brave_news_load_new_content.xml",
"java/res/layout/brave_news_row.xml",
"java/res/layout/brave_notification_permission_rationale_dialog.xml",
"java/res/layout/brave_notification_warning_dialog.xml",
"java/res/layout/brave_onboarding_searchbox.xml",
"java/res/layout/brave_permission_prompt_dialog.xml",
"java/res/layout/brave_rewards_creator_panel.xml",
Expand Down
2 changes: 2 additions & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/night_mode/settings/BraveThemePreferences.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/BraveNotificationPlatformBridge.java",
"../../brave/android/java/org/chromium/chrome/browser/notifications/BraveNotificationSettingsBridge.java",
"../../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/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
33 changes: 31 additions & 2 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.fullscreen.BrowserControlsManager;
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.NotificationPermissionController;
import org.chromium.chrome.browser.notifications.permissions.NotificationPermissionRationaleDialogController;
import org.chromium.chrome.browser.notifications.retention.RetentionNotificationUtil;
Expand Down Expand Up @@ -185,6 +187,7 @@
import org.chromium.content_public.browser.WebContents;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;
import org.chromium.ui.permissions.PermissionConstants;
import org.chromium.ui.widget.Toast;

import java.util.ArrayList;
Expand Down Expand Up @@ -860,7 +863,7 @@ public void finishNativeInitialization() {
BraveSyncWorker.get();
}

checkForNotificationData();
checkAndshowNotificationWarningDialog();

if (!RateUtils.getInstance(this).getPrefRateEnabled()) {
RateUtils.getInstance(this).setPrefRateEnabled(true);
Expand Down Expand Up @@ -1223,6 +1226,32 @@ private void checkForYandexSE() {
}
}

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

private BraveNotificationWarningDialog.DismissListener closeDialogListener =
new BraveNotificationWarningDialog.DismissListener() {
@Override
public void onDismiss() {
checkForNotificationData();
}
};

private void checkAndshowNotificationWarningDialog() {
if (BraveNotificationWarningDialog.shouldShowNotificationWarningDialog(this)) {
showNotificationWarningDialog();
} else {
checkForNotificationData();
}
}

private void checkForNotificationData() {
Intent notifIntent = getIntent();
if (notifIntent != null && notifIntent.getStringExtra(RetentionNotificationUtil.NOTIFICATION_TYPE) != null) {
Expand Down Expand Up @@ -1591,7 +1620,7 @@ public void onNewIntent(Intent intent) {
openNewOrSelectExistingTab(openUrl);
}
}
checkForNotificationData();
checkAndshowNotificationWarningDialog();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.chromium.chrome.browser.local_database.DatabaseHelper;
import org.chromium.chrome.browser.local_database.SavedBandwidthTable;
import org.chromium.chrome.browser.night_mode.GlobalNightModeStateProviderHolder;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.util.ConfigurationUtils;
import org.chromium.ui.base.DeviceFormFactor;
Expand Down Expand Up @@ -210,7 +211,8 @@ public void onRequestPermissionsResult(
@Override
public void onResume() {
super.onResume();
if (hasPermission(getActivity(), PermissionConstants.NOTIFICATION_PERMISSION)) {
if (BravePermissionUtils.hasPermission(
getContext(), PermissionConstants.NOTIFICATION_PERMISSION)) {
statsNotificationView.setVisibility(View.GONE);
} else {
statsNotificationView.setVisibility(View.VISIBLE);
Expand All @@ -228,7 +230,7 @@ private void updateNotificationView(View view) {
// 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
redirectToSettingPage();
BravePermissionUtils.notificationSettingPage(getContext());
} else {
// 1st time request permission
ActivityCompat.requestPermissions(getActivity(),
Expand All @@ -237,29 +239,6 @@ private void updateNotificationView(View view) {
});
}

private void redirectToSettingPage() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// for Android 5-7
intent.putExtra(APP_PACKAGE, getActivity().getPackageName());
intent.putExtra(APP_UID, getActivity().getApplicationInfo().uid);

// for Android 8 and above
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());

startActivity(intent);
}

private Boolean hasPermission(Context context, String permission) {
if (ContextCompat.checkSelfPermission(context, permission)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
return true;
}

@Override
public void onDestroyView() {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/**
* 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;

import android.content.Context;
import android.content.DialogInterface;
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 android.widget.ImageView;
import android.widget.TextView;

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.BraveDialogFragment;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.ui.permissions.PermissionConstants;

/**
* This dialog is used to show different messages when notification permission is off and
* if rewards or privacy is on OR both on
* */
public class BraveNotificationWarningDialog extends BraveDialogFragment {
public static final String NOTIFICATION_WARNING_DIALOG_TAG = "NotificationWarningDialog";

public static final int FROM_LAUNCHED_BRAVE_SETTINGS = 1;
public static final int FROM_LAUNCHED_BRAVE_ACTIVITY = 2;
public static final int FROM_LAUNCHED_BRAVE_PANEL = 3;
private static final String LAUNCHED_FROM = "launched_from";

private TextView mTitleTextView;
private TextView mDescriptionTextView;
private Button mPrimaryButton;
private int mLaunchedFrom;

public interface DismissListener {
void onDismiss();
}
private DismissListener mListener;
public void setDismissListener(DismissListener listener) {
mListener = listener;
}

@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
if (mListener != null && mLaunchedFrom == FROM_LAUNCHED_BRAVE_ACTIVITY)
mListener.onDismiss();
}

public static BraveNotificationWarningDialog newInstance(int launchedFrom) {
BraveNotificationWarningDialog fragment = new BraveNotificationWarningDialog();
Bundle args = new Bundle();
args.putInt(LAUNCHED_FROM, launchedFrom);
fragment.setArguments(args);
return fragment;
}

/**
* If no notification permission and if any privacy or rewards state is on then return true
* */
public static boolean shouldShowNotificationWarningDialog(Context context) {
if (!BravePermissionUtils.hasPermission(
context, PermissionConstants.NOTIFICATION_PERMISSION)) {
return OnboardingPrefManager.getInstance().isBraveStatsEnabled()
|| OnboardingPrefManager.getInstance().isBraveAdsEnabled();
}
return false;
}

@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_warning_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);
init(view);
}

private void init(View view) {
mTitleTextView = view.findViewById(R.id.notification_title_tv);
mDescriptionTextView = view.findViewById(R.id.notification_description_tv);
mPrimaryButton = view.findViewById(R.id.notification_warning_primary_button);
updateTitleDescriptionText(view);
clickOnPrimaryButton(view);
clickOnCloseButton(view);
clickOnNotNow(view);
}

public boolean isBraveRewardsEnabled() {
return OnboardingPrefManager.getInstance().isBraveAdsEnabled();
}

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

private void updateTitleDescriptionText(View view) {
if (getArguments() != null) {
mLaunchedFrom = getArguments().getInt(LAUNCHED_FROM);
if (mLaunchedFrom == FROM_LAUNCHED_BRAVE_ACTIVITY) {
launchedFromBraveActivity(view);
} else if (mLaunchedFrom == FROM_LAUNCHED_BRAVE_SETTINGS) {
launchedFromBraveSettings(view);
view.findViewById(R.id.btn_not_now).setVisibility(View.GONE);
} else if (mLaunchedFrom == FROM_LAUNCHED_BRAVE_PANEL) {
launchedFromBravePanel(view);
}
}
}

private void launchedFromBravePanel(View view) {
ImageView icon = view.findViewById(R.id.warning_imageview);
icon.setImageDrawable(
ResourcesCompat.getDrawable(view.getResources(), R.drawable.ic_bell_icon, null));
mTitleTextView.setText(R.string.enable_notifications_from_brave_to_earn_brave_rewards);
mDescriptionTextView.setText(
R.string.open_settings_and_turn_on_device_notifications_for_brave_ads);
view.findViewById(R.id.btn_not_now).setVisibility(View.GONE);
mPrimaryButton.setText(R.string.brave_open_system_sync_settings);
mPrimaryButton.setBackground(ResourcesCompat.getDrawable(
view.getResources(), R.drawable.blue_48_rounded_bg, null));
}

private void launchedFromBraveActivity(View view) {
mPrimaryButton.setText(R.string.turn_on_brave_notifications);

if (isBraveRewardsEnabled() && isPrivacyReportsEnabled()) {
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()) {
mTitleTextView.setText(R.string.notification_os_dialog_header_only_rewards);
mDescriptionTextView.setText(R.string.notification_os_dialog_description_only_rewards);
} else if (isPrivacyReportsEnabled()) {
mTitleTextView.setText(R.string.notification_os_dialog_header_only_privacy);
mDescriptionTextView.setText(R.string.notification_os_dialog_description_only_privacy);
}
}

private void launchedFromBraveSettings(View view) {
mPrimaryButton.setText(R.string.got_it);

if (isBraveRewardsEnabled() && isPrivacyReportsEnabled()) {
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()) {
mTitleTextView.setText(R.string.notification_brave_dialog_header_only_rewards);
mDescriptionTextView.setText(
R.string.notification_brave_dialog_description_only_rewards);
} else if (isPrivacyReportsEnabled()) {
mTitleTextView.setText(R.string.notification_brave_dialog_header_only_privacy);
mDescriptionTextView.setText(
R.string.notification_brave_dialog_description_only_privacy);
}
}

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

private void clickOnCloseButton(View view) {
ImageView btnClose = view.findViewById(R.id.notification_dialog_close);
btnClose.setOnClickListener(v -> { dismiss(); });
}

private void clickOnNotNow(View view) {
Button notNowButton = view.findViewById(R.id.btn_not_now);
notNowButton.setOnClickListener(v -> { dismiss(); });
}
}
Loading