-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to play music while on and other fixes
- Loading branch information
1 parent
56c3fc0
commit 1a755ce
Showing
8 changed files
with
57 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 45 additions & 17 deletions
62
app/src/main/java/dev/brahmkshatriya/echo/utils/CheckPermissions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,64 @@ | ||
package dev.brahmkshatriya.echo.utils | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.provider.Settings | ||
import android.widget.Toast | ||
import androidx.activity.result.ActivityResultCaller | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import dev.brahmkshatriya.echo.R | ||
|
||
fun checkPermissions(activity: AppCompatActivity) { | ||
|
||
fun AppCompatActivity.checkAudioPermissions() { | ||
val perm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) | ||
Manifest.permission.READ_MEDIA_AUDIO | ||
else | ||
Manifest.permission.READ_EXTERNAL_STORAGE | ||
val permStatus = ContextCompat.checkSelfPermission(activity, perm) | ||
if (permStatus != PackageManager.PERMISSION_GRANTED) | ||
activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) { | ||
if (!it) | ||
AlertDialog.Builder(activity) | ||
.setTitle("Permission Required") | ||
.setMessage("This permission is required to access your music library") | ||
.setPositiveButton("Ok") { _, _ -> | ||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||
data = Uri.fromParts("package", activity.packageName, null) | ||
activity.startActivity(this) | ||
} | ||
} | ||
.setNegativeButton("Cancel") { _, _ -> activity.finish() } | ||
.show() | ||
checkPermissions( | ||
this, | ||
perm, | ||
R.string.permission_required, | ||
R.string.music_permission_required_summary, | ||
{ finish() } | ||
)?.launch(perm) | ||
} | ||
|
||
}.launch(perm) | ||
fun ActivityResultCaller.checkPermissions( | ||
context: Context, | ||
perm: String, | ||
title: Int, | ||
message: Int, | ||
onCancel: () -> Unit, | ||
onGranted: () -> Unit = {}, | ||
onRequest: () -> Unit = { | ||
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||
data = Uri.fromParts("package", context.packageName, null) | ||
context.startActivity(this) | ||
} | ||
} | ||
): ActivityResultLauncher<String>? = with(context) { | ||
val permStatus = ContextCompat.checkSelfPermission(this, perm) | ||
val contract = ActivityResultContracts.RequestPermission() | ||
return if (permStatus != PackageManager.PERMISSION_GRANTED) registerForActivityResult(contract) { | ||
if (!it) AlertDialog.Builder(this) | ||
.setTitle(getString(title)) | ||
.setMessage(getString(message)) | ||
.setPositiveButton(getString(R.string.ok)) { _, _ -> onRequest() } | ||
.setNegativeButton(getString(R.string.cancel)) { _, _ -> | ||
Toast.makeText( | ||
this, getString(R.string.permission_denied), Toast.LENGTH_SHORT | ||
).show() | ||
onCancel() | ||
} | ||
.show() | ||
else onGranted() | ||
} else null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters