Getting phone permissions
Make sure READ_PHONE_STATE
permission is declared in the application’s AndroidManifest.xml.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Before initialising the PAK, the READ_PHONE_STATE
permission must be requested at runtime when all of the following conditions are true:
- The application will run on Android P devices.
- The application’s
targetSdkVersion
is 28 (Android P) or higher. - The OpVault file’s
EnforceSingleDeviceUniqueIDPerDeviceFlag
is set totrue
.
The following code will, if necessary, show a system dialogue box requesting approval from the user. If permission has not already been granted, the user’s approval comes back asynchronously in the onRequestPermissionsResult()
callback (which is an Activity
override).
if (checkReadPhoneStatePermission()) {
// Permission granted. Can continue.
} else {
// Permission not granted yet. Need to wait for user's approval.
}
public boolean checkReadPhoneStatePermission() {
boolean hasPermission = (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
requestReadPhoneStatePermission();
}
return hasPermission;
}
@TargetApi(23)
public void requestReadPhoneStatePermission() {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
// Show our own request permission dialog before popping the system one.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("External permission required for Read Phone State");
dialogBuilder.setPositiveButton("Ok", (dialog, which) ->
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1))
.show();
} else {
// Show system dialog to request permission
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// Dialogue results callback
if (requestCode != 1) {
// Not coming from our request, tagged as 1
return;
}
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
// Permission refused/not granted by user
} else {
// Permission granted by user
}
}
Next step: Using the DRMHandler class