Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 3df50a3

Browse files
committedNov 28, 2021
add pending intent flag if sdk version >= 31
1 parent 524731a commit 3df50a3

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed
 

‎sampleapp/src/main/java/io/matthewnelson/sampleapp/App.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ package io.matthewnelson.sampleapp
7373

7474
import android.app.Application
7575
import android.app.PendingIntent
76-
import android.content.Intent
76+
import android.os.Build
7777
import android.os.Process
7878
import io.matthewnelson.encrypted_storage.Prefs
7979
import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
8080
import io.matthewnelson.sampleapp.topl_android.MyServiceExecutionHooks
8181
import io.matthewnelson.sampleapp.topl_android.MyTorSettings
82-
import io.matthewnelson.sampleapp.ui.MainActivity
8382
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
8483
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
8584
import io.matthewnelson.sampleapp.ui.fragments.settings.library.components.LibraryPrefs
@@ -195,8 +194,14 @@ class App: Application() {
195194
super.onCreate()
196195
val prefs = Prefs.createUnencrypted(PREFS_NAME, this)
197196

197+
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
198+
PendingIntent.FLAG_IMMUTABLE
199+
} else {
200+
0
201+
}
202+
198203
packageManager?.getLaunchIntentForPackage(packageName)?.let { intent ->
199-
contentIntent = PendingIntent.getActivity(this.applicationContext, 0, intent, 0)
204+
contentIntent = PendingIntent.getActivity(this.applicationContext, 0, intent, flags)
200205
}
201206

202207
val serviceNotificationBuilder = generateTorServiceNotificationBuilder(

‎sampleapp/src/main/java/io/matthewnelson/sampleapp/topl_android/CodeSamples.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import android.app.Application
7575
import android.app.PendingIntent
7676
import android.content.Context
7777
import android.content.Intent
78+
import android.os.Build
7879
import androidx.core.app.NotificationCompat
7980
import io.matthewnelson.sampleapp.App
8081
import io.matthewnelson.sampleapp.BuildConfig
@@ -125,12 +126,18 @@ class CodeSamples {
125126
//
126127
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
127128

129+
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
130+
PendingIntent.FLAG_IMMUTABLE
131+
} else {
132+
0
133+
}
134+
128135
builder.setContentIntent(
129136
PendingIntent.getActivity(
130137
context.applicationContext,
131138
0, // Your desired request code
132139
intent,
133-
0 // flags
140+
flags
134141
// can also include a bundle if desired
135142
)
136143
)

‎sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/dashboard/DashboardFragment.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,14 @@ class DashboardFragment : Fragment() {
262262
private fun killApplication(context: Context) {
263263
// Build notification to restart the Application
264264
val intent = Intent(context.applicationContext, MainActivity::class.java)
265+
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
266+
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
267+
} else {
268+
PendingIntent.FLAG_ONE_SHOT
269+
}
270+
265271
val pendingIntent =
266-
PendingIntent.getActivity(context.applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
272+
PendingIntent.getActivity(context.applicationContext, 0, intent, flags)
267273

268274
val notificationManager =
269275
context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?

‎topl-service/src/main/java/io/matthewnelson/topl_service/notification/ServiceNotification.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,17 @@ class ServiceNotification private constructor(
646646
intent.putExtra(TorServiceReceiver.getServiceIntentFilter(), action)
647647
intent.setPackage(torService.getContext().packageName)
648648

649+
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
650+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
651+
} else {
652+
PendingIntent.FLAG_UPDATE_CURRENT
653+
}
654+
649655
return PendingIntent.getBroadcast(
650656
torService.getContext(),
651657
requestCode,
652658
intent,
653-
PendingIntent.FLAG_UPDATE_CURRENT
659+
flags
654660
)
655661
}
656662

0 commit comments

Comments
 (0)
This repository has been archived.