Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: permission related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 7, 2024
1 parent adc5de9 commit 302b910
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CanvasOverlay(context: Context) {
y = 0
}

init {
fun show() {
hideCanvas()
try {
if (canvasView.windowToken == null && canvasView.parent == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bnyro.recorder.services

import android.app.PendingIntent
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.os.Build
import android.service.quicksettings.TileService
import androidx.annotation.RequiresApi
Expand All @@ -14,6 +15,9 @@ class AudioRecorderTile : TileService() {
super.onClick()
val intent = Intent(this, MainActivity::class.java)
.putExtra(MainActivity.EXTRA_ACTION_KEY, RecorderType.AUDIO.name)
.apply {
flags = FLAG_ACTIVITY_NEW_TASK
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(
PendingIntent.getActivity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ScreenRecorderTile : TileService() {
super.onClick()
val intent = Intent(this, MainActivity::class.java)
.putExtra(MainActivity.EXTRA_ACTION_KEY, RecorderType.VIDEO.name)
.apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/bnyro/recorder/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class MainActivity : ComponentActivity() {
if (initialRecorderType == RecorderType.AUDIO) {
recorderModel.startAudioRecorder(this)
} else if (initialRecorderType == RecorderType.VIDEO) {
launcher.launch(mProjectionManager.createScreenCaptureIntent())
if (recorderModel.hasScreenRecordingPermissions(this)) {
launcher.launch(mProjectionManager.createScreenCaptureIntent())
}
}
intent.removeExtra(EXTRA_ACTION_KEY)
}
Expand Down
24 changes: 7 additions & 17 deletions app/src/main/java/com/bnyro/recorder/ui/models/RecorderModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.provider.Settings
import androidx.activity.result.ActivityResult
import androidx.annotation.RequiresApi
import androidx.compose.runtime.getValue
Expand All @@ -31,7 +29,6 @@ import com.bnyro.recorder.util.PermissionHelper
import com.bnyro.recorder.util.Preferences

class RecorderModel : ViewModel() {
private val audioPermission = arrayOf(Manifest.permission.RECORD_AUDIO)
private val supportsOverlay = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O

var recorderState by mutableStateOf(RecorderState.IDLE)
Expand All @@ -52,6 +49,7 @@ class RecorderModel : ViewModel() {
recorderState = it
}
(recorderService as? ScreenRecorderService)?.prepare(activityResult!!)
if (supportsOverlay) canvasOverlay?.show()
recorderService?.start()
}

Expand All @@ -73,7 +71,12 @@ class RecorderModel : ViewModel() {

@SuppressLint("NewApi")
fun startAudioRecorder(context: Context) {
if (!PermissionHelper.checkPermissions(context, audioPermission)) return
val audioPermission = mutableListOf(Manifest.permission.RECORD_AUDIO)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
audioPermission.add(Manifest.permission.POST_NOTIFICATIONS)
}

if (!PermissionHelper.checkPermissions(context, audioPermission.toTypedArray())) return

val serviceIntent =
if (Preferences.prefs.getBoolean(Preferences.losslessRecorderKey, false)) {
Expand Down Expand Up @@ -152,19 +155,6 @@ class RecorderModel : ViewModel() {

@SuppressLint("NewApi")
fun hasScreenRecordingPermissions(context: Context): Boolean {
val overlayEnabled = Preferences.prefs.getBoolean(
Preferences.showOverlayAnnotationToolKey,
false
)
if (supportsOverlay && overlayEnabled && !Settings.canDrawOverlays(context)) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + context.packageName)
)
context.startActivity(intent)
return false
}

val requiredPermissions = arrayListOf<String>()

val recordAudio =
Expand Down

0 comments on commit 302b910

Please sign in to comment.