Skip to content

Commit de3874e

Browse files
committed
Fix lint warning: check permission before notifying with notification
1 parent cf01890 commit de3874e

File tree

4 files changed

+131
-80
lines changed

4 files changed

+131
-80
lines changed

vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
package im.vector.app.core.services
1010

11+
import android.Manifest
1112
import android.content.Context
1213
import android.content.Intent
14+
import android.content.pm.PackageManager
1315
import android.os.Binder
1416
import android.support.v4.media.session.MediaSessionCompat
1517
import android.view.KeyEvent
18+
import androidx.core.app.ActivityCompat
1619
import androidx.core.app.NotificationManagerCompat
1720
import androidx.core.content.ContextCompat
1821
import androidx.media.session.MediaButtonReceiver
@@ -150,7 +153,8 @@ class CallAndroidService : VectorAndroidService() {
150153
val isVideoCall = call.mxCall.isVideoCall
151154
val fromBg = intent.getBooleanExtra(EXTRA_IS_IN_BG, false)
152155
Timber.tag(loggerTag.value).v("displayIncomingCallNotification : display the dedicated notification")
153-
val incomingCallAlert = IncomingCallAlert(callId,
156+
val incomingCallAlert = IncomingCallAlert(
157+
callId,
154158
shouldBeDisplayedIn = { activity ->
155159
if (activity is VectorCallActivity) {
156160
activity.intent.getParcelableExtraCompat<CallArgs>(Mavericks.KEY_ARG)?.callId != call.callId
@@ -176,7 +180,11 @@ class CallAndroidService : VectorAndroidService() {
176180
if (knownCalls.isEmpty()) {
177181
startForegroundCompat(callId.hashCode(), notification)
178182
} else {
179-
notificationManager.notify(callId.hashCode(), notification)
183+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
184+
Timber.w("Not allowed to notify.")
185+
} else {
186+
notificationManager.notify(callId.hashCode(), notification)
187+
}
180188
}
181189
knownCalls[callId] = callInformation
182190
}
@@ -234,7 +242,11 @@ class CallAndroidService : VectorAndroidService() {
234242
if (knownCalls.isEmpty()) {
235243
startForegroundCompat(callId.hashCode(), notification)
236244
} else {
237-
notificationManager.notify(callId.hashCode(), notification)
245+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
246+
Timber.w("Not allowed to notify.")
247+
} else {
248+
notificationManager.notify(callId.hashCode(), notification)
249+
}
238250
}
239251
knownCalls[callId] = callInformation
240252
}
@@ -258,7 +270,11 @@ class CallAndroidService : VectorAndroidService() {
258270
if (knownCalls.isEmpty()) {
259271
startForegroundCompat(callId.hashCode(), notification)
260272
} else {
261-
notificationManager.notify(callId.hashCode(), notification)
273+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
274+
Timber.w("Not allowed to notify.")
275+
} else {
276+
notificationManager.notify(callId.hashCode(), notification)
277+
}
262278
}
263279
knownCalls[callId] = callInformation
264280
}

vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
package im.vector.app.features.location.live.tracking
99

10+
import android.Manifest
1011
import android.content.Intent
12+
import android.content.pm.PackageManager
1113
import android.os.IBinder
1214
import android.os.Parcelable
15+
import androidx.core.app.ActivityCompat
1316
import androidx.core.app.NotificationManagerCompat
1417
import dagger.hilt.android.AndroidEntryPoint
1518
import im.vector.app.core.di.ActiveSessionHolder
@@ -95,7 +98,11 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
9598
// Show a sticky notification
9699
val notification = liveLocationNotificationBuilder.buildLiveLocationSharingNotification(roomArgs.roomId)
97100
if (foregroundModeStarted) {
98-
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
101+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
102+
Timber.w("Not allowed to notify.")
103+
} else {
104+
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
105+
}
99106
} else {
100107
startForegroundCompat(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
101108
foregroundModeStarted = true
@@ -146,10 +153,14 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
146153
}
147154

148155
private fun updateNotification() {
149-
if (liveInfoSet.isNotEmpty()) {
150-
val roomId = liveInfoSet.last().roomArgs.roomId
151-
val notification = liveLocationNotificationBuilder.buildLiveLocationSharingNotification(roomId)
152-
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
156+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
157+
Timber.w("Not allowed to notify.")
158+
} else {
159+
if (liveInfoSet.isNotEmpty()) {
160+
val roomId = liveInfoSet.last().roomArgs.roomId
161+
val notification = liveLocationNotificationBuilder.buildLiveLocationSharingNotification(roomId)
162+
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
163+
}
153164
}
154165
}
155166

vector/src/main/java/im/vector/app/features/notifications/NotificationDisplayer.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@
77

88
package im.vector.app.features.notifications
99

10+
import android.Manifest
1011
import android.app.Notification
1112
import android.content.Context
13+
import android.content.pm.PackageManager
14+
import androidx.core.app.ActivityCompat
1215
import androidx.core.app.NotificationManagerCompat
1316
import timber.log.Timber
1417
import javax.inject.Inject
1518

16-
class NotificationDisplayer @Inject constructor(context: Context) {
19+
class NotificationDisplayer @Inject constructor(
20+
private val context: Context,
21+
) {
1722

1823
private val notificationManager = NotificationManagerCompat.from(context)
1924

2025
fun showNotificationMessage(tag: String?, id: Int, notification: Notification) {
21-
notificationManager.notify(tag, id, notification)
26+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
27+
Timber.w("Not allowed to notify.")
28+
} else {
29+
notificationManager.notify(tag, id, notification)
30+
}
2231
}
2332

2433
fun cancelNotificationMessage(tag: String?, id: Int) {

vector/src/main/java/im/vector/app/features/notifications/NotificationUtils.kt

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
package im.vector.app.features.notifications
1111

12+
import android.Manifest
1213
import android.annotation.SuppressLint
1314
import android.app.Notification
1415
import android.app.NotificationChannel
1516
import android.app.NotificationManager
1617
import android.app.PendingIntent
1718
import android.content.Context
1819
import android.content.Intent
20+
import android.content.pm.PackageManager
1921
import android.graphics.Bitmap
2022
import android.graphics.Canvas
2123
import android.net.Uri
@@ -27,6 +29,7 @@ import androidx.annotation.AttrRes
2729
import androidx.annotation.ChecksSdkIntAtLeast
2830
import androidx.annotation.DrawableRes
2931
import androidx.annotation.StringRes
32+
import androidx.core.app.ActivityCompat
3033
import androidx.core.app.NotificationCompat
3134
import androidx.core.app.NotificationManagerCompat
3235
import androidx.core.app.RemoteInput
@@ -153,55 +156,59 @@ class NotificationUtils @Inject constructor(
153156
* Default notification importance: shows everywhere, makes noise, but does not visually
154157
* intrude.
155158
*/
156-
notificationManager.createNotificationChannel(NotificationChannel(
157-
NOISY_NOTIFICATION_CHANNEL_ID,
158-
stringProvider.getString(CommonStrings.notification_noisy_notifications).ifEmpty { "Noisy notifications" },
159-
NotificationManager.IMPORTANCE_DEFAULT
160-
)
161-
.apply {
162-
description = stringProvider.getString(CommonStrings.notification_noisy_notifications)
163-
enableVibration(true)
164-
enableLights(true)
165-
lightColor = accentColor
166-
})
159+
notificationManager.createNotificationChannel(
160+
NotificationChannel(
161+
NOISY_NOTIFICATION_CHANNEL_ID,
162+
stringProvider.getString(CommonStrings.notification_noisy_notifications).ifEmpty { "Noisy notifications" },
163+
NotificationManager.IMPORTANCE_DEFAULT
164+
)
165+
.apply {
166+
description = stringProvider.getString(CommonStrings.notification_noisy_notifications)
167+
enableVibration(true)
168+
enableLights(true)
169+
lightColor = accentColor
170+
})
167171

168172
/**
169173
* Low notification importance: shows everywhere, but is not intrusive.
170174
*/
171-
notificationManager.createNotificationChannel(NotificationChannel(
172-
SILENT_NOTIFICATION_CHANNEL_ID,
173-
stringProvider.getString(CommonStrings.notification_silent_notifications).ifEmpty { "Silent notifications" },
174-
NotificationManager.IMPORTANCE_LOW
175-
)
176-
.apply {
177-
description = stringProvider.getString(CommonStrings.notification_silent_notifications)
178-
setSound(null, null)
179-
enableLights(true)
180-
lightColor = accentColor
181-
})
182-
183-
notificationManager.createNotificationChannel(NotificationChannel(
184-
LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID,
185-
stringProvider.getString(CommonStrings.notification_listening_for_events).ifEmpty { "Listening for events" },
186-
NotificationManager.IMPORTANCE_MIN
187-
)
188-
.apply {
189-
description = stringProvider.getString(CommonStrings.notification_listening_for_events)
190-
setSound(null, null)
191-
setShowBadge(false)
192-
})
193-
194-
notificationManager.createNotificationChannel(NotificationChannel(
195-
CALL_NOTIFICATION_CHANNEL_ID,
196-
stringProvider.getString(CommonStrings.call).ifEmpty { "Call" },
197-
NotificationManager.IMPORTANCE_HIGH
198-
)
199-
.apply {
200-
description = stringProvider.getString(CommonStrings.call)
201-
setSound(null, null)
202-
enableLights(true)
203-
lightColor = accentColor
204-
})
175+
notificationManager.createNotificationChannel(
176+
NotificationChannel(
177+
SILENT_NOTIFICATION_CHANNEL_ID,
178+
stringProvider.getString(CommonStrings.notification_silent_notifications).ifEmpty { "Silent notifications" },
179+
NotificationManager.IMPORTANCE_LOW
180+
)
181+
.apply {
182+
description = stringProvider.getString(CommonStrings.notification_silent_notifications)
183+
setSound(null, null)
184+
enableLights(true)
185+
lightColor = accentColor
186+
})
187+
188+
notificationManager.createNotificationChannel(
189+
NotificationChannel(
190+
LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID,
191+
stringProvider.getString(CommonStrings.notification_listening_for_events).ifEmpty { "Listening for events" },
192+
NotificationManager.IMPORTANCE_MIN
193+
)
194+
.apply {
195+
description = stringProvider.getString(CommonStrings.notification_listening_for_events)
196+
setSound(null, null)
197+
setShowBadge(false)
198+
})
199+
200+
notificationManager.createNotificationChannel(
201+
NotificationChannel(
202+
CALL_NOTIFICATION_CHANNEL_ID,
203+
stringProvider.getString(CommonStrings.call).ifEmpty { "Call" },
204+
NotificationManager.IMPORTANCE_HIGH
205+
)
206+
.apply {
207+
description = stringProvider.getString(CommonStrings.call)
208+
setSound(null, null)
209+
enableLights(true)
210+
lightColor = accentColor
211+
})
205212
}
206213

207214
fun getChannel(channelId: String): NotificationChannel? {
@@ -997,7 +1004,11 @@ class NotificationUtils @Inject constructor(
9971004
}
9981005

9991006
fun showNotificationMessage(tag: String?, id: Int, notification: Notification) {
1000-
notificationManager.notify(tag, id, notification)
1007+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
1008+
Timber.w("Not allowed to notify.")
1009+
} else {
1010+
notificationManager.notify(tag, id, notification)
1011+
}
10011012
}
10021013

10031014
fun cancelNotificationMessage(tag: String?, id: Int) {
@@ -1025,30 +1036,34 @@ class NotificationUtils @Inject constructor(
10251036

10261037
@SuppressLint("LaunchActivityFromNotification")
10271038
fun displayDiagnosticNotification() {
1028-
val testActionIntent = Intent(context, TestNotificationReceiver::class.java)
1029-
testActionIntent.action = actionIds.diagnostic
1030-
val testPendingIntent = PendingIntent.getBroadcast(
1031-
context,
1032-
0,
1033-
testActionIntent,
1034-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE
1035-
)
1039+
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
1040+
Timber.w("Not allowed to notify.")
1041+
} else {
1042+
val testActionIntent = Intent(context, TestNotificationReceiver::class.java)
1043+
testActionIntent.action = actionIds.diagnostic
1044+
val testPendingIntent = PendingIntent.getBroadcast(
1045+
context,
1046+
0,
1047+
testActionIntent,
1048+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntentCompat.FLAG_IMMUTABLE
1049+
)
10361050

1037-
notificationManager.notify(
1038-
"DIAGNOSTIC",
1039-
888,
1040-
NotificationCompat.Builder(context, NOISY_NOTIFICATION_CHANNEL_ID)
1041-
.setContentTitle(buildMeta.applicationName)
1042-
.setContentText(stringProvider.getString(CommonStrings.settings_troubleshoot_test_push_notification_content))
1043-
.setSmallIcon(R.drawable.ic_notification)
1044-
.setLargeIcon(getBitmap(context, im.vector.lib.ui.styles.R.drawable.element_logo_green))
1045-
.setColor(ContextCompat.getColor(context, im.vector.lib.ui.styles.R.color.notification_accent_color))
1046-
.setPriority(NotificationCompat.PRIORITY_MAX)
1047-
.setCategory(NotificationCompat.CATEGORY_STATUS)
1048-
.setAutoCancel(true)
1049-
.setContentIntent(testPendingIntent)
1050-
.build()
1051-
)
1051+
notificationManager.notify(
1052+
"DIAGNOSTIC",
1053+
888,
1054+
NotificationCompat.Builder(context, NOISY_NOTIFICATION_CHANNEL_ID)
1055+
.setContentTitle(buildMeta.applicationName)
1056+
.setContentText(stringProvider.getString(CommonStrings.settings_troubleshoot_test_push_notification_content))
1057+
.setSmallIcon(R.drawable.ic_notification)
1058+
.setLargeIcon(getBitmap(context, im.vector.lib.ui.styles.R.drawable.element_logo_green))
1059+
.setColor(ContextCompat.getColor(context, im.vector.lib.ui.styles.R.color.notification_accent_color))
1060+
.setPriority(NotificationCompat.PRIORITY_MAX)
1061+
.setCategory(NotificationCompat.CATEGORY_STATUS)
1062+
.setAutoCancel(true)
1063+
.setContentIntent(testPendingIntent)
1064+
.build()
1065+
)
1066+
}
10521067
}
10531068

10541069
private fun getBitmap(context: Context, @DrawableRes drawableRes: Int): Bitmap? {

0 commit comments

Comments
 (0)