Fix permission for SDK<26

This commit is contained in:
sim 2024-11-11 22:17:12 +00:00
parent 94cd3badba
commit 1d206c92f8
1 changed files with 28 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.R
import org.unifiedpush.distributor.nextpush.activities.PermissionsRequest.requestAppPermissions
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
object PermissionsRequest { object PermissionsRequest {
@ -35,6 +36,33 @@ object PermissionsRequest {
Manifest.permission.POST_NOTIFICATIONS Manifest.permission.POST_NOTIFICATIONS
) )
} }
} else if (Build.VERSION.SDK_INT < 26) {
this.requestAccountPermission()
}
}
@Deprecated("For SDK<26 only")
fun AppCompatActivity.requestAccountPermission() {
if (checkSelfPermission(Manifest.permission.GET_ACCOUNTS)
!= PackageManager.PERMISSION_GRANTED
) {
Log.d(TAG, "Requesting GET_ACCOUNT permission")
this.registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted ->
Log.d(TAG, "GET_ACCOUNT permission granted: $granted")
if (!granted) {
if (shouldShowRequestPermissionRationale(Manifest.permission.GET_ACCOUNTS)) {
Log.d(TAG, "Show GET_ACCOUNTS permission rationale")
AlertDialog.Builder(this)
.setTitle(getString(R.string.no_notification_dialog_title))
.setMessage(R.string.no_notification_dialog_message)
.show()
}
}
}.launch(
Manifest.permission.GET_ACCOUNTS
)
} }
} }
} }