Skip to content

Instantly share code, notes, and snippets.

@LongClipeus
Last active September 25, 2021 07:41
Show Gist options
  • Save LongClipeus/851a1359950326b5079a78e2b97b5809 to your computer and use it in GitHub Desktop.
Save LongClipeus/851a1359950326b5079a78e2b97b5809 to your computer and use it in GitHub Desktop.
How to know if user has disabled Picture in Picture feature permission?
const val ACTION_PIP_SETTING = "android.settings.PICTURE_IN_PICTURE_SETTINGS"
@RequiresApi(Build.VERSION_CODES.O)
fun AppCompatActivity.hasPictureInPicturePermission(): Boolean {
val appOps = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
val uid = android.os.Process.myUid()
val mode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
appOps.unsafeCheckOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, uid, packageName)
} else {
@Suppress("DEPRECATION")
appOps.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, uid, packageName)
}
return mode == AppOpsManager.MODE_ALLOWED
}
@RequiresApi(Build.VERSION_CODES.O)
fun AppCompatActivity.requestPictureInPicturePermission() {
val packageUri = Uri.parse("$PACKAGE$COLON$packageName")
val intent = Intent(ACTION_PIP_SETTING, packageUri)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment