Skip to content

Commit

Permalink
refactor permission setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Oct 21, 2024
1 parent 58c8721 commit 575b76e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<application
android:name=".BOApplication"
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
package org.blitzortung.android.app

import android.app.AlarmManager
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.ServiceCompat
import dagger.android.AndroidInjection
import org.blitzortung.android.alert.handler.AlertHandler
import org.blitzortung.android.app.controller.NotificationHandler.Companion.CHANNEL_ID
import org.blitzortung.android.app.view.OnSharedPreferenceChangeListener
import org.blitzortung.android.app.view.PreferenceKey
import org.blitzortung.android.app.view.get
Expand Down Expand Up @@ -127,9 +131,30 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
Log.d(Main.LOG_TAG, "AppService.onStartCommand() non matching intent ${intent?.action}")
}

val intent = Intent(this, Main::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val flags = PendingIntent.FLAG_CANCEL_CURRENT or (if (isAtLeast(Build.VERSION_CODES.M)) {
PendingIntent.FLAG_IMMUTABLE
} else {
0
})
val contentIntent = PendingIntent.getActivity(this, 0, intent, flags)

val notification = Notification.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(this.resources.getText(R.string.app_name))
.setContentText("bla")
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setShowWhen(true).build()

ServiceCompat.startForeground(this, 0, notification, FOREGROUND_SERVICE_TYPE_MANIFEST)

return START_STICKY
}


override fun onBind(intent: Intent): IBinder? {
Log.i(Main.LOG_TAG, "AppService.onBind() $intent")

Expand All @@ -139,6 +164,8 @@ class AppService : Service(), OnSharedPreferenceChangeListener {
override fun onDestroy() {
super.onDestroy()

ServiceCompat.stopForeground(this, 0)

preferences.unregisterOnSharedPreferenceChangeListener(this)
isEnabled = false

Expand Down
40 changes: 28 additions & 12 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,18 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {

val requestCode = (LocationProviderRelation.byProviderName[locationProviderName]?.ordinal ?: Int.MAX_VALUE)
if (requiresPermission) {
val locationText = resources.getString(R.string.location_permission_background_disclosure)
val locationText = this.resources.getString(R.string.location_permission_background_disclosure)
AlertDialog.Builder(this).setMessage(locationText).setCancelable(false)
.setNeutralButton(android.R.string.ok) { dialog, count ->
requestPermission(
arrayOf(permission), requestCode, R.string.location_permission_required
permission, requestCode,
R.string.location_permission_required
)
}.show()
} else {
if (requiresBackgroundPermission) {
if (requiresBackgroundPermission && isAtLeast(Build.VERSION_CODES.Q)) {
requestPermission(
arrayOf(ACCESS_BACKGROUND_LOCATION),
ACCESS_BACKGROUND_LOCATION,
requestCode,
R.string.location_permission_background_required
)
Expand All @@ -662,23 +663,38 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
}
}


@TargetApi(Build.VERSION_CODES.M)
private fun requestPermission(permission: Array<String>, requestCode: Int, permissionRequiredStringId: Int) {
val shouldShowPermissionRationale = shouldShowRequestPermissionRationale(permission[0])
val permissionStatus = checkSelfPermission(permission[0])
private fun requestPermission(permission: String, requestCode: Int, permissionRequiredStringId: Int) {
val shouldShowPermissionRationale = shouldShowRequestPermissionRationale(permission)
val permissionStatus = checkSelfPermission(permission)
Log.v(
LOG_TAG,
"Main.requestPermission() permission: ${permission.joinToString()}, status: $permissionStatus, shouldRequest: ${!shouldShowPermissionRationale}"
"Main.requestPermission() permission: $permission, status: $permissionStatus, shouldRequest: ${!shouldShowPermissionRationale}"
)
if (!shouldShowPermissionRationale && permissionStatus != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permission, requestCode)
} else {

if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
if (shouldShowPermissionRationale) {
Toast.makeText(baseContext, permissionRequiredStringId, Toast.LENGTH_LONG).show()
requestPermissionsAfterDialog(permissionRequiredStringId, permission, requestCode)
} else {
requestPermissions(arrayOf(permission), requestCode)
}
}
}

@TargetApi(Build.VERSION_CODES.M)
private fun requestPermissionsAfterDialog(
dialogTextResource: Int,
permission: String,
requestCode: Int,
) {
val locationText = resources.getString(dialogTextResource)
AlertDialog.Builder(this).setMessage(locationText).setCancelable(false)
.setNeutralButton(android.R.string.ok) { dialog, count ->
requestPermissions( arrayOf(permission), requestCode )
}.show()
}

@TargetApi(Build.VERSION_CODES.M)
private fun requestWakeupPermissions(context: Context) {
Log.v(LOG_TAG, "requestWakeupPermissions() background alerts: $backgroundAlertEnabled")
Expand Down

0 comments on commit 575b76e

Please sign in to comment.