Last active
September 25, 2021 07:41
-
-
Save LongClipeus/851a1359950326b5079a78e2b97b5809 to your computer and use it in GitHub Desktop.
How to know if user has disabled Picture in Picture feature permission?
This file contains hidden or 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
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