Checking permission of write to external storage in Android application - android

Normal way to do that is checking whether the manifest uses WRITE_EXTERNAL_STORAGE permission:
String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
My question is in about using canWrite() method.Docs say:
public boolean canWrite ()
Added in API level 1
Indicates whether the
current context is allowed to write to this file.
So we can use this method to check permission like this:
Environment.getExternalStorageDirectory().canWrite();
This way seems to be simpler than first way and also does not need to use Context.Also we can use same to check read permission.But is this a good way to check those?Also is it a good way to check if manifest uses read/write permissions?

Related

How to check whether an EXTERNAL app has draw-over permissions in Android 7?

I am trying to check whether an external application (i.e. not the one I am writing, but another one running on the debug device) has draw-over permissions. However, regardless of the method I use, I always get "false" as answer, whence I know for certain that said application can draw over other apps. In fact, not only I explicitly gave it permissions through the Settings interface, but I actually witnessed it performing a view overlay.
This is the first method I used:
pm.checkPermission(Manifest.permission.SYSTEM_ALERT_WINDOW, "target.package")
This is the second method I used (packageInfo being the PackageInfo object associated with "target.package"):
boolean requestedPermissionGranted = false;
for (int i = 0; i < packageInfo.requestedPermissions.length; ++i) {
if (packageInfo.requestedPermissions[i].equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
requestedPermissionGranted =
(packageInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;
break;
}
}
hasPermission = requestedPermissionGranted;
Both expressions return false. By the way, I verified that packageInfo.requestedPermissions actually contains SYSTEM_ALERT_WINDOW.
I searched far and wide, but I could not find any topic that would answer my doubts. I only managed to find threads about how to check an app's own permissions, which I am not interested in.
P.S: I cannot use the shell through getRuntime().exec() and list active windows, as I get permission denied.

How to check if user has given camera or location permissions (android) UNITY

I really struggle with this since a while :( as I need an solution that works within UNITY3D.
I need to check if the user has given the permission to access the Android device camera (and location on a second level).
Normally the app start by asking for this permissions at launch, but if the user denies the access for the camera I need to know and check that later.
Otherwise the user could hit the camera UI button I made and try to access the camera via webcamtexture... and that leads into a crash of the app.
Since Android API 23 you cannot ignore or already grant permissions by changing the android manifest like I tried after reading several posts about that.
Thank's to everyone who has an idea to solve this.
Check this library: https://github.com/sanukin39/UniAndroidPermission
In that library I got these methods to check and request Permission.
public static void requestPermission(String permissionStr){
if(!hasPermission(permissionStr)) {
UnityPlayer.currentActivity.requestPermissions(new String[]{permissionStr}, 0);
}
}
public static boolean hasPermission(String permissionStr) {
if(Build.VERSION.SDK_INT < 23) {
return true;
}
Context context = UnityPlayer.currentActivity.getApplicationContext();
return context.checkCallingOrSelfPermission(permissionStr) == PackageManager.PERMISSION_GRANTED;
}
Hope it helps:)

Getting permission not granted in GingerBread

I am using following function to check whether read from external storage permission has been granted or not.
public static boolean checkWritePermissions(Context context)
{ String permission = "android.permission.READ_EXTERNAL_STORAGE";
int res = context.checkCallingOrSelfPermission(permission);
return (res==PackageManager.PERMISSION_GRANTED);
}
The function is working correctly for post GingerBread devices, however for GingerBread devices the returned value is false. Ignoring the function returned value shows that permission has been granted. Is there any alternative for GingerBread devices.
P.S.: The application isn't using Support Library so new permission methods aren't being used.

Is ACCESS_COARSE_LOCATION necessary if the user has already granted ACCESS_FINE_LOCATION?

I am updating my app to work with the new Android Marshmallow permission framework and it looks like it's enough for the user to grant the ACCESS_FINE_LOCATION permission at runtime for the app to work fine. This is what I do:
public static final String[] runtimePermissions = { permission.ACCESS_FINE_LOCATION };
public static final int LOCATION_PERMISSION_IDENTIFIER = 1;
and further down the class:
public static boolean checkConnectionPermission(final Context context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (context.checkSelfPermission(permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
}
else {
((Activity) context).requestPermissions(runtimePermissions,
LOCATION_PERMISSION_IDENTIFIER);
return false;
}
}
// since in API levels below M the permission is granted on
// installation,
// it is considered a given that the permission has been granted since
// the
// app is running. So we return true by default
else {
return true;
}
}
I am just concerned that I am overlooking something that could cause trouble in the future (the app is near-production) with Security Exception(s).
I guess my ultimate question is: does granting FINE_LOCATION somehow auto-grant COARSE_LOCATION too?
Sort of already answered here.
If I have ACCESS_FINE_LOCATION already, can I omit ACCESS_COARSE_LOCATION?
Some additional information, you should look at permission group.
https://developer.android.com/preview/features/runtime-permissions.html
They are in the same permission group. If you really want to play safe, just include both in your manifest, and request them on need. It would be transparent to user.
According to this blog post, if you have specified both in the manifest and the user has granted you one, then when you ask for the other it will be automatically granted (since they're both in the same permission group).
That means that if you have been granted COARSE_LOCATION and then ask for FINE_LOCATION you can get it without the user being prompted, but the catch is that you still have to specify both in the manifest.
Apps can actually register for two types of location updates. Fine grained, which uses the Global Positioning System to get the device’s location accurate to within a few meters, vs coarse-grained, which uses the Wifi, Cell towers and other data available to the device to get a rough (accurate to 10s of meters) location of the device.
Choice is yours.

Can I detect that a particular permission has been removed for my android app?

So with the arrival of Android 4.3 comes the ability to selectively disable particular permissions for apps.
This could obviously cause problems where an app loses a permission it needs to operate correctly.
Does the system broadcast a message when a permission is revoked, or how could I tell that my app no longer has a certain permission? Ideally I would like to inform the user that turning off permission A will break xyz in the application.
There is no broadcast, but you can check permissions yourself on startup(or resume, etc). Context#checkCallingOrSelfPermission() is provided for this.
If you want to check all your permissions, you could do something like this:
public static boolean[] getPermissionsGranted(Context context, String[] permissions){
boolean[] permissionsGranted = new boolean[permissions.length];
for(int i=0;i<permissions.length;i++){
int check = context.checkCallingOrSelfPermission(permissions[i]);
permissionsGranted[i] = (check == PackageManager.PERMISSION_GRANTED);
}
return permissionsGranted;
}
Where the input Strings are the permissions' names, such as "android.permission.INTERNET".

Categories

Resources