Skip to content

Commit

Permalink
Request notification permission in Mirroring app
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed Feb 18, 2024
1 parent 6e1263a commit 81a9c36
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)
}

override fun onResume() {
super.onResume()
viewModel.updatePermissions(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
package io.bimmergestalt.idriveconnectaddons.screenmirror

import android.content.Context
import android.Manifest
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.core.app.ActivityCompat

class MainController(val activity: Activity) {

companion object {
const val REQUEST_POST_NOTIFICATIONS = 60
}

private fun tryOpenActivity(intent: Intent): Boolean {
if (activity.packageManager.resolveActivity(intent, 0) != null) {
try {
activity.startActivity(intent)
return true
} catch (e: ActivityNotFoundException) {
} catch (e: IllegalArgumentException) {}
}
return false
}

fun openApplicationPermissions(packageName: String) {
run {
val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.setClassName("com.miui.securitycenter",
"com.miui.permcenter.permissions.PermissionsEditorActivity")
intent.putExtra("extra_pkgname", packageName)
if (tryOpenActivity(intent)) return
}
run {
val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
// try an implicit intent without a classname
intent.putExtra("extra_pkgname", packageName)
if (tryOpenActivity(intent)) return
}
run {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.data = Uri.fromParts("package", packageName, null)
if (tryOpenActivity(intent)) return
}
run {
val intent = Intent(Settings.ACTION_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
if (tryOpenActivity(intent)) return
}
}

class MainController(val context: Context) {
fun promptPermission() {
val intent = Intent(context, RequestActivity::class.java)
context.startActivity(intent)
val intent = Intent(activity, RequestActivity::class.java)
activity.startActivity(intent)
}

fun promptPostNotificationsPermission() {
if (android.os.Build.VERSION.SDK_INT >= 33 && activity.applicationInfo.targetSdkVersion >= 33) { // Android 13+
ActivityCompat.requestPermissions(activity,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
REQUEST_POST_NOTIFICATIONS)
} else {
openApplicationPermissions(activity.packageName)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.bimmergestalt.idriveconnectaddons.screenmirror

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.bimmergestalt.idriveconnectaddons.lib.LiveDataHelpers.map

class MainModel: ViewModel() {
val notificationPermission = MutableLiveData(false)
val mirroringState = ScreenMirrorProvider.state
val mirroringStateText: LiveData<Context.() -> String> = ScreenMirrorProvider.state.map({getString(R.string.lbl_status_not_ready)}) {
when (it) {
Expand All @@ -23,4 +28,9 @@ class MainModel: ViewModel() {
}
}
}

fun updatePermissions(context: Context) {
notificationPermission.value = Build.VERSION.SDK_INT < 33
|| context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class NotificationService: Service() {
// or an in-app request to tap to open the prompt for recording permission
if (ScreenMirrorProvider.state.value == MirroringState.NOT_ALLOWED) {
notificationBuilder
.setContentText(getText(R.string.btn_grant_auth))
.setContentText(getText(R.string.btn_grant_mirror_auth))
.setContentIntent(PendingIntent.getActivity(applicationContext, 0, requestActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT))
if (!foreground) {
// a background request to tap and start the grant flow
Expand Down

0 comments on commit 81a9c36

Please sign in to comment.