confused about android runtime permissions - android

So this whole new android runtime permissions has gotten me confused. My app is currently compiling and targetting version 23 which means I have to use runtime permissions. My app primarily uses the camera api which needs the camera permission so I added the runtime permissions before opening the camera as such:
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
{//ask permissions for camera
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CameraPermissions);
}
else
{//permissions attained now you can open the camera
camera=Camera.open(getCid());
camera.setPreviewCallback(this);
initPreview(width, height);
startPreview();
startTimer();
}
I also check when I stop the camera:
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
camera.setPreviewCallback(null);
camera.release();
faceProc.release();
faceProc = null;
camera = null;
inPreview = false;
cameraConfigured = false;
}
The permission request is handled as such:
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case CameraPermissions: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
StartUpCam();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("How is this app going to work if you rejected the camera permission.... DUHHHH!!")
.setTitle("Rejected");
builder.setPositiveButton("Exit App", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//close application
closeApp();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return;
}
}
}
So when the request is given it calls the StartUpCam which then tries to open the camera if the permissions is given. So here comes my questions, if I add this runtime permission checks how does this affect android devices lower than 6.0?? So a phone with version 5.0.1 will also get a prompt to give camera permissions? If I use runtime permissions, do I have to remove the camera permissions in the manifest file? Currently, I keep the camera permissions in the manifest along with the runtime permissions I don't know if that is correct or not. What if I lower the target and compiling sdk to 22 instead of 23, will android devices above 6.0 won't be able to download my app??? If I lower it to version 22 then I avoid all this headache...

I also check when I stop the camera
That is not needed, assuming that you do not try to stop a camera that you never opened. If the user revokes the permission while your app is running, your process is immediately terminated. As a result, you can never lose permissions in a running app. Since you checked for and had permission to open the camera, you already have permission to close it.
if I add this runtime permission checks how does this affect android devices lower than 6.0?
ContextCompat.checkSelfPermission() will return PackageManager.PERMISSION_GRANTED on older devices, assuming that you have the permission listed in the manifest.
So a phone with version 5.0.1 will also get a prompt to give camera permissions?
No.
If I use runtime permissions, do I have to remove the camera permissions in the manifest file?
No. Those elements are necessary on all Android versions.
What if I lower the target and compiling sdk to 22 instead of 23, will android devices above 6.0 won't be able to download my app?
Your compileSdkVersion has no impact on what versions of Android you support. Android 6.0 users will still be able to download your app.
If you lower your targetSdkVersion to 22 or lower, that too has no impact on what versions of Android that you support. Android 6.0 users will still be able to download your app. Doing this would mean that you could skip the runtime permission code. However, bear in mind that you still may not have permission. Users of Android 6.0 devices, running your targetSdkVersion 22 app, will grant the CAMERA permission by default. But, those users can still go into Settings > Apps, find your app, and revoke the permission. With the camera API, you basically cannot open the camera.
Tactically, going with targetSdkVersion of 22 or lower is certainly possible. Eventually, though, something is going to "force your hand" and require you to move to a targetSdkVersion of 23 or higher. So, someday, you will need to deal with the runtime permissions. Whether that is today or some day in the future is up to you do decide.

No if the Permission is in Menifest file. Android having less than 23 sdk won't receive a prompt about the permission, It will be the same like before.

Related

Unable to retrieve if REQUEST_INSTALL_PACKAGES permission is granted for other apps

I'm trying to get a list of apps (on Android 8.0+) that have REQUEST_INSTALL_PACKAGES permissions marked as granted.
context.packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS or PackageManager.GET_META_DATA).forEach { pi ->
if (pi.requestedPermissions != null) {
for (i in pi.requestedPermissions.indices) {
if (pi.requestedPermissions[i] == android.Manifest.permission.REQUEST_INSTALL_PACKAGES) {
if ((pi.requestedPermissionsFlags[i] and PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) {
// permission is granted, do stuff here
}
}
}
}
}
The problem is, that corresponding requestedPermissionsFlags entry is always 1, which means REQUESTED_PERMISSION_REQUIRED and that is obviously wrong.
I tried different api: context.packageManager.checkPermission(android.Manifest.permission.REQUEST_INSTALL_PACKAGES, pi.packageName) == PackageManager.PERMISSION_GRANTED with the same result.
In tried this on Android 8, 9 and 10 on different devices and the only success I had was on Pixel 3a that came positive for com.android.nfc. Others were always false even when I can see in system settings that chrome and other apps have this permission granted.
Am I missing something for this API? Is it private? There is not much to find about it.
Because this permmission has a signature protection level. According to documentation:
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission
So requestedPermissionsFlags is correct - only com.android.nfc is granted.
Actually REQUEST_INSTALL_PACKAGES has a signature|appop protection level, that's why not only system apps can use it although this perrmission is not granted for them. For a little bit more details - Acquiring Android Permission with Signature Protection Level.

Displaying images from SDCard in a Widget from MarshMallow

In a widget I display images from SDCard using remoteView.setImageViewUri(). This strategy works correctly except with MarshMallow:
The error is:
Unable to open content: file:///storage/emulated/0/Android/data/applicaitonPackage/files/file.png
open failed: EACCESS (Permission denied)
It's clear that this is a permission problem, but I don't know how to give permissions to the widget container and in theory (see Note 1) the images are already stored in shared storage.
Note 1: The directory where images are stored is shared storage under Environment.getExternalStorageDirectory()
Note 2: Application is not adapted to MarshMallow and uses targetSdkVersion=15
Note 3: Don't just reply explaining about new runtime permissions in MarshMallow. I already know permissions changes and that is not the problem because application is targeted SDKVersion 15 and the app hasn't any problem accessing the external storage, the problem is with the widget container that is the one that I suspect that doesn't has the permissions.
#Docs says
Requesting Permissions at Run Time
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality;
Therefore this make sense although you declare permission in manifest but still getting permission denied
I suggest you to read how to get runtime permission
http://developer.android.com/training/permissions/requesting.html
Here is the example provided by Docs
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Change permission as per you requirement
Hope it leads you to right direction
Did you have this permisstion in your Manifest file?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If I set the targetSdkVersion to 15 instead of 23 - will the android M users see the app and be able to download it (granting all permissions at runtime)?
Yes, the app will be available to M users and every permission is granted at install time.
`targetSdkVersion=15`
which is less than 23.User will able to download the app and use it it grants all the permission at run time. If you want to check the permission go to settings and grant the permission. If you want to use the android M permission module at run time you have to set the target SDK version to 23.
If set the targetSdkVersion=15 on android M and higher devices app getting crashed.
If you want to support android M users set targetSdkVersion=23and
handle permission runtime.

Android not requiring permissions - Marshmallow 6.0.1 Unity

I'm building a Cardboard app and testing it on a Nexus 6P.
The problem I have is that when I install the app, it doesn't ask for any permissions.
In my manifest I have this:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then if I want to download some file from the internet in my app, it doesn't work. It doesn't even create the com. folder.
I have to go manually to the app info and check the storage permission.
It's weird because when building the app for GearVR and testing it on a Note 4, it asks for permission (in the Note 4 I use the sd card, in Nexus 6P the internal)
Why is this happening?
Thank you.
For Android 6+ you need to request permissions at runtime, not startup. This is especially the case if you are considering your app for Google Play featuring,
In short, you need to specify the permission in tour manifest, and also include the following in your application tag
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
Then, request the permission(s) at runtime via Android.
There is a great little plugin for this at https://www.assetstore.unity3d.com/#!/content/62735
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. Thats a reason why you didn't have request dialog.
If you need to request permission you need to show request rationale and handle user iteration result.
From docs :
Request permission :
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Handle User Choosed Option
In ActivityCompat :
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Starting with Android 6.0 (Marshmallow) apps no longer get all permissions on install like they used to. You now have to request the permissions at runtime (which the user can decline) and have to be able to handle the permission getting revoked any time after it has been granted.
If you want to use the old behavior for now, I believe you should be able to set your target API level to 22.
Read more here: http://developer.android.com/training/permissions/requesting.html
for a quick fix, you can target android 5 (api level 22 instead of 23) and the permissions will keep using the old behavior.
note, if you publish an app targeting android 6 (level 23) api, you can't deprecate the target api level in an update.
https://developer.android.com/training/permissions/requesting.html

How can I know if my app's camera permission is granted?

I want to know if my app's camera permission is granted or not. Then I written the below functions, but they are always return incorrect result. Not matter how can I disabled the camera permission of my app in system settings, the both functions always return TRUE. Do you know something wrong in my codes?
public static boolean checkCameraPermission() {
String permission = "android.permission.CAMERA";
int res = App.getInstance().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
public static boolean checkCameraPermission(Context context) {
PackageManager pm = context.getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED ==
pm.checkPermission("android.permission.CAMERA", pm.getNameForUid(Binder.getCallingUid())));
return permission;
}
I want to know if my app's camera permission is granted or not
If your targetSdkVersion is 23 or higher, and you are running on an Android 6.0+ device, use checkSelfPermission(). The native implementation of checkSelfPermission() is on Context; ContextCompat has a version that will not crash on older Android devices.
If your targetSdkVersion is 22 or lower, or you are running on an Android 5.1 or older device, you always have your requested permissions (exception: possibly some custom ROMs).
If your targetSdkVersion is 22 or lower, and you are running on an Android 6.0 or higher device, you also always have your requested permissions, in terms of the methods that you are calling as outlined in your question. The user, in Settings, can block your access to data associated with those permissions, but technically the user is not actually revoking the permissions themselves (though we often say they are just to keep the explanation simple). I know of no way for you to determine that the user is blocking your camera access, other than by catching the relevant exceptions when you attempt to access the camera. Note that those exceptions will be raised in other scenarios (e.g., camera access is blocked by device policy).

Grant permission required for EXTERNAL_STORAGE in Android M?

Will the Android permissions WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE trigger the new grant permission dialog of Android M?
I agree with Guillaume Perrot 's answer. I have met the similar question when I write the permission of READ_WRITE_EXTERNAL_STORAGE in AndroidManifest.xml
with no permissions showing up in the app by default , people need to switch the toggle button of storage in the app permissions.Then I modify my targetSdkVersion in build.gradle to less than 23(MNC) and other number related with sdkVersion, the app installed with the permissions on.
The other way is to write requestpermission function in the place that you need the permisson. The code is as follow:
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED) {
//do the things} else {
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
AnyNumber);
Because I have less than 15 reputation so I can't vote for the Guillaume Perrot 's answer.Just use this way to show my idea.
I solved add this if check version for Android M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
My answer is based on my tests on M Preview SDK version 2, using an emulator.
If you target MNC preview API level, WRITE_EXTERNAL_STORAGE is not granted by default and will be part of the new dynamic permission API.
You will see the storage permission as a toggle button in the new app permissions menu in device settings, and you can use Activity.requestPermissions to show the popup for that permission.
However if you target api level < MNC, it won't be classified as a dangerous permission, and thus will be granted without a way for the user to disable it (not showing up in permission settings), and you will not be able to compile code using Activity.requestPermissions anyway as the preview SDK enforces minSdkVersion="MNC" to use the new APIs.
This is a different behavior than location permissions: whatever the API level you target, the user will be able to turn location off in permission menu.
For the permission menu itself, the permission toggle state is ON by default if:
Target API level < MNC.
Target API level = MNC but you upgrade app on device from a previous install where target API level was less than MNC.
Otherwise you will see the toggle as OFF by default.
Hope it helps.
According to the docs:
Limited Permissions Granted at Install Time: When the user installs or updates the app, the system grants the app all permissions that the app requests that fall under PROTECTION_NORMAL.
So because READ_EXTERNAL_STORAGE is falling under PROTECTION_NORMAL , it won't trigger the dialog.
But because the level of WRITE_EXTERNAL_STORAGE is PROTECTION_DANGEROUS, it will fall under this behavior as described in docs:
User Grants Permissions at Run-Time: When the app requests a permission, the system shows a dialog to the user, then calls the app's callback function to notify it whether the permission was granted. If a user grants a permission, the app is given all permissions in that permission's functional area that were declared in the app manifest
Here is the sources for the protection level:
detailed list
According to Android docs you don't need to request permission about read and write external storage.
Edit: in the latest Android M release you need to ask for both read and write permissions
Storage permission falls under dangerous protection level, So all the dangerous protection level permissions will not be granted at install time in Android M, if App target SDK is set to 23. They will be given at run time.
And yes these permissions can be revoked at run time also.
No permission dialog will not be triggered automatically, you need to do a request by using API such as requestPermissions() method to show that native dialog.
Please check the dangerous level permission list here

Categories

Resources