Skip to content

Commit

Permalink
Fix the IllegalArgumentException that the pending intent should expli…
Browse files Browse the repository at this point in the history
…citly declared as mutable or immutable when targetSDK is 31 or above
  • Loading branch information
benjamin-cheng committed Jan 2, 2023
1 parent eceeb06 commit 523ce94
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/mozilla/focus/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ class MainActivity :
)
val pendingIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)

val builder = NotificationUtil.importantBuilder(this)
Expand Down Expand Up @@ -968,7 +968,7 @@ class MainActivity :
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val builder = NotificationUtil.baseBuilder(this, NotificationUtil.Channel.LOW_PRIORITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,22 @@ class RocketMessagingService : FirebaseMessagingServiceWrapper() {
command,
deepLink
)
return PendingIntent.getBroadcast(appContext, RocketMessagingService.REQUEST_CODE_CLICK_NOTIFICATION, clickIntent, PendingIntent.FLAG_ONE_SHOT)
return PendingIntent.getBroadcast(
appContext,
REQUEST_CODE_CLICK_NOTIFICATION,
clickIntent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
}

private fun addDeleteTelemetry(appContext: Context, builder: NotificationCompat.Builder, messageId: String?, link: String?) {
val intent = IntentUtils.genDeleteFirebaseNotificationActionForBroadcastReceiver(appContext, messageId, link)
val pendingIntent = PendingIntent.getBroadcast(appContext, RocketMessagingService.REQUEST_CODE_DELETE_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT)
val pendingIntent = PendingIntent.getBroadcast(
appContext,
REQUEST_CODE_DELETE_NOTIFICATION,
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
builder.setDeleteIntent(pendingIntent)
}
}
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/org/mozilla/focus/utils/DialogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.focus.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.app.PendingIntent
Expand Down Expand Up @@ -153,12 +154,13 @@ object DialogUtils {
}
}

@SuppressLint("LaunchActivityFromNotification")
@JvmStatic
fun showRateAppNotification(context: Context) { // Brings up Rocket and display full screen "Love Rocket" dialog
val openRocket = IntentUtils.genFeedbackNotificationClickForBroadcastReceiver(context)
val openRocketPending = PendingIntent.getBroadcast(
context, REQUEST_RATE_CLICK, openRocket,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val string = context.getString(
R.string.rate_app_dialog_text_title,
Expand All @@ -172,7 +174,7 @@ object DialogUtils {
val rateStar = IntentUtils.genRateStarNotificationActionForBroadcastReceiver(context)
val rateStarPending = PendingIntent.getBroadcast(
context, REQUEST_RATE_RATE, rateStar,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
builder.addAction(
R.drawable.notification_rating,
Expand All @@ -184,7 +186,7 @@ object DialogUtils {
val feedback = IntentUtils.genFeedbackNotificationActionForBroadcastReceiver(context)
val feedbackPending = PendingIntent.getBroadcast(
context, REQUEST_RATE_FEEDBACK, feedback,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
builder.addAction(
R.drawable.notification_feedback,
Expand All @@ -196,6 +198,7 @@ object DialogUtils {
Settings.getInstance(context).setRateAppNotificationDidShow()
}

@SuppressLint("LaunchActivityFromNotification")
@JvmStatic
@JvmOverloads
fun showDefaultSettingNotification(
Expand All @@ -206,7 +209,7 @@ object DialogUtils {
IntentUtils.genDefaultBrowserSettingIntentForBroadcastReceiver(context)
val openRocketPending = PendingIntent.getBroadcast(
context, REQUEST_DEFAULT_CLICK, openDefaultBrowserSetting,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val title: String? = if (TextUtils.isEmpty(message)) {
context.getString(R.string.preference_default_browser) + "?\uD83D\uDE0A"
Expand All @@ -221,13 +224,14 @@ object DialogUtils {
Settings.getInstance(context).setDefaultBrowserSettingDidShow()
}

@SuppressLint("LaunchActivityFromNotification")
@JvmStatic
fun showPrivacyPolicyUpdateNotification(context: Context) {
val privacyPolicyUpdateNotice =
IntentUtils.genPrivacyPolicyUpdateNotificationActionForBroadcastReceiver(context)
val openRocketPending = PendingIntent.getBroadcast(
context, REQUEST_PRIVACY_POLICY_CLICK, privacyPolicyUpdateNotice,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationUtil.importantBuilder(context)
.setContentTitle(context.getString(R.string.privacy_policy_update_notification_title))
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/mozilla/focus/utils/IntentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static Intent getLauncherHomeIntent() {

public static PendingIntent getLauncherHomePendingIntent(Context context) {
return PendingIntent.getActivity(context, 0, getLauncherHomeIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}

public interface OpenGooglePlayFallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PrivateSessionNotificationService : Service() {
applicationContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mozilla.rocket.periodic

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.SharedPreferences
Expand Down Expand Up @@ -60,11 +61,12 @@ class FirstLaunchWorker(context: Context, workerParams: WorkerParameters) : Work
return Result.success()
}

@SuppressLint("LaunchActivityFromNotification")
private fun showNotification(context: Context, messageId: String, title: String?, message: String, openUrl: String?, command: String?, deepLink: String?) {
val intent = IntentUtils.genFirstrunNotificationClickForBroadcastReceiver(context, messageId, openUrl, command, deepLink)
val openRocketPending = PendingIntent.getBroadcast(
context, REQUEST_CODE_CLICK_NOTIFICATION, intent,
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationUtil.importantBuilder(context)
.also {
Expand All @@ -87,7 +89,12 @@ class FirstLaunchWorker(context: Context, workerParams: WorkerParameters) : Work

private fun addDeleteTelemetry(appContext: Context, builder: NotificationCompat.Builder, messageId: String, link: String?) {
val intent = IntentUtils.genDeleteFirstrunNotificationActionForBroadcastReceiver(appContext, messageId, link)
val pendingIntent = PendingIntent.getBroadcast(appContext, REQUEST_CODE_DELETE_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT)
val pendingIntent = PendingIntent.getBroadcast(
appContext,
REQUEST_CODE_DELETE_NOTIFICATION,
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
builder.setDeleteIntent(pendingIntent)
}
}
Expand Down

0 comments on commit 523ce94

Please sign in to comment.