MAUI app under Android hot to give write permission to BlazorWebView - android

I'm developing a MAUI Blazor app that should run under windows and android
On Android I get an error from a component that tries to write on the clipboard
blazor.webview.js:1 Write permission denied.
Reading this doc I cannot figure out where and how to call the BlazorWebViewInizializing event to allow this permission
Or should I do something also in the AndroidManifest.xml?
Thanks

For this, you can check document Permissions.
In android, Permissions must have the matching attributes set in the Android Manifest file. Permission status defaults to Denied.
Above article describes how you can use the .NET Multi-platform App UI (.NET MAUI) Permissions class. This class allows you to check and request permissions at run-time. The Permissions type is available in the Microsoft.Maui.ApplicationModel namespace.
Checking permissions:
To check the current status of a permission, use the Permissions.CheckStatusAsync method along with the specific permission to get the status for. The following example checks the status of the LocationWhenInUse permission:
PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
Requesting permissions:
To request a permission from the users, use the Permissions.RequestAsync method along with the specific permission to request. If the user previously granted permission, and hasn't revoked it, then this method will return Granted without showing a dialog to the user. The following example requests the LocationWhenInUse permission:
PermissionStatus status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
For more information, you can check document Permissions.

Related

Requesting app runtime permissions (Android)

I want to write a simple app which accesses the device's location. Only I will ever use the app. This is my first attempt in about 10 years to write an Android app, so it is the first time I've had to deal with runtime permissions.
My first question is, given that the app is solely for my use, is it possible to by-pass the need for runtime permission code?
Failing that, is there any simple example code that fills in the numerous gaps in the Android documentation?
To take one example: the doc includes the following:
when {
ContextCompat.checkSelfPermission(
CONTEXT,
Manifest.permission.REQUESTED_PERMISSION
) == PackageManager.PERMISSION_GRANTED -> {
// You can use the API that requires the permission.
performAction(...)
What does this mean? What "API that requires the permission"? What replaces the "..."?
There are several other similar gaps on the page.
You have mentioned that app is solely for your use then you don't have to write the code for the runtime permission you can skip it...
How to do this... ?
Step 1 : Just put all the permission you need inside the app manifest file and install the app
Step 2 : Go to the app settings or app info in the phone and check for app permissions all the permission that you mentioned will be displayed there just toogle them manually
That's it now write code to access the things which you supposed to write after getting permission
If you want to avoid run time permission request you can build your app with Android SDK version below 23 (Android 6 Marshmellow)
For Android API Level 23 or above (after adding permission in your manifest file):
First, add your permission to AndroidManifest.xml file:
<uses-permission android:name="android.permission.THE_PERMISSION" />
Then in your Activity:
Check Permission:
fun checkPermission(permission: String): Boolean {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}
Get Permission:
fun getPermission(permission: String) {
ActivityCompat.requestPermissions(this, arrayOf(permission), REQ_CODE_PERMISSION)
}
Get Permission Result:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == REQ_CODE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
...
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
If you are using Fragments:
A fragment contains a method for requesting permission and getting the result back to its onRequestPermissionsResult:
fun getPermission(permission: String) {
requestPermissions(arrayOf(permission), REQ_CODE_PERMISSION)
}
REQ_CODE_PERMISSION: is some random number like 123 you use to identify your request with.
is it possible to by-pass the need for runtime permission code?
You still need the <uses-permission> element. But you could manually grant the permission to your app via the Settings app.
The point of runtime permission code is to request the permission from the user and defend against cases where that permission has not been granted. In your case, if your app crashes because you revoked the permission, you can yell the developer. You, as the developer, in turn, can yell at you, as the user, for failing to manually grant the permission. Since you will be yelling at yourself, it is recommended to do this in a private location, or perhaps have a Bluetooth headset in your ear as cover. :-)
What does this mean?
We request runtime permissions because we want to use some Android API that is defended by such a permission. We usually do not request runtime permissions because we woke up one morning and decided that requesting runtime permissions sounds like a really fun thing to do.
What "API that requires the permission"? What replaces the "..."?
In your case, it would appear to be either methods on LocationManager or stuff using the fused location API from Google Play Services.
is there any simple example code that fills in the numerous gaps in the Android documentation?
The problem is that "example code" is 5% permission-related and 95% whatever it is that you are using that requires the permission. Any sample code that only shows permissions is going to have the same hand-wavy stuff that you don't like from the documentation. In your case, any up-to-date examples of using location APIs should also show the runtime permission elements.
FWIW, this directory contains several sample projects from this book that show getting the location. They are a bit old but do show requesting runtime permissions (mostly contained in an AbstractPermissionActivity). This sample is newer and in Kotlin, but it is for file-access permissions, not for locations (and is covered in this other book).

Can not read a file using in Xamarin forms using Pcl Storage

I am trying to read a video file which is in byte array using PCL Storage. So I have added the video file in the folder named File using android studio tool. Trying to read from there. The thing is that in my code it can find out that the file exists in the folder but whenever I am trying to read it using the following code I am getting an exception: "Access denied" but I have already enabled the permission in android manifest for reading and writing from external storage
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFile files = await rootFolder.GetFileAsync("myVideo.Mp4");
var text = await files.ReadAllTextAsync();
I am getting an exception: "Access denied" but I have already enabled the permission in android manifest
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. You should request permissions at runtime.
System permissions are divided into two categories, normal and dangerous:
Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
Dangerous permissions can give the app access to the user's confidential data. If you list a dangerous permission, the user has to explicitly give approval to your app during the runtime of the app.
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider ... requires android.permission.READ_EXTERNAL_STORAGE
READ_EXTERNAL_STORAGE is categorized as Dangerous permissions, so you should check this permissions manually for API level 23 and above.
Update:
Here is an example that request permissions at runtime.

android registerContentObserver for Contacts requires READ_CONTACTS permission

I just discovered that, during app initialization my registerContentObserver for Contacts requires READ_CONTACTS permission. Obviously, for a new user on Android 6 and later, this permission won't yet be granted.
It seems to me it would be sufficient to ignore the permission during registration and check permission when the app listener tries to access contacts - which I'm sure it already does.
Same for Calendar.
Should I make an Android change request - why is this coding "penalty" being imposed?

Why is READ_EXTERNAL_STORAGE permission request not needed to read file from external storage?

I'm testing my app on an emulator. I have an export function where I create and write to a file in the external storage's downloads directory. And I also have an import function where I read a file from the external storage's downloads directory.
From Android documentation:
If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all.
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
My emulator is running on Android 6.0 and my app's target SDK is 25, therefore I must also request each dangerous permission it needs while the app is running. I did so for the export functionality and everything works properly. However, when I'm implementing the import function I didn't request a permission during runtime. And the strange thing is I'm still able to read from my external storage's permission without READ_EXTERNAL_STORAGE being requested and granted at runtime. READ_EXTERNAL_STORAGE is a dangerous permission according to this Android documentation .
To verify, I made sure to disable permissions before I started using the feature and after it is completed, I verified again that the permission still wasn't granted. Although I'm happy with the behaviour since it's working without me requesting permission at runtime, but according to the documentations I don't believe this behaviour is expected. That's why I will like to know what's causing this and to figure out the problem before I publish any changes for the app.
Here's a code snippet of my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The code snippet where I pick a file to read:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent, GET_FILE_RESULT_CODE);
The code snippet where I read the file chosen from the code snippet above (exportFile is simply the URI from onActivityResult):
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(context.getContentResolver().openInputStream(exportFile)));
String line;
// Skip first header line
br.readLine();
while ((line = br.readLine()) != null) {...}
Thanks!
There's a well explanation here,
READ_EXTERNAL_STORAGE
Provides protected read access to external storage. In Android 4.1 by
default all applications still have read access. This will be changed
in a future release to require that applications explicitly request
read access using this permission. If your application already
requests write access, it will automatically get read access as well.
There is a new developer option to turn on read access restriction,
for developers to test their applications against how Android will
behave in the future.
In short, READ_EXTERNAL_STORAGE only exists as of Jelly Bean (Level 16). So, unless you're using a Jelly Bean phone and set the developer option "Protect USB storage" it won't be a problem.
You know,Android Runtime Permissions are grouped, since you applied for WRITE_EXTERNAL_STORAGE permission in the manifest already, so there's no need to apply for READ_EXTERNAL_STORAGE permissions.Both of them are the same group.

neither user 10632 nor current process has android.permission.READ_PHONE_STATE error in Android 6

I am developing an Android application in Android 6 and my application in a service needs to get device ID(getDeviceId). I have set android.permission.READ_PHONE_STATE in the manifest file, but at run time I got this error:
neither user 10632 nor current process has android.permission.READ_PHONE_STATE. java.lang.SecurityException:getDeviceId need android.permission.Read_Phone_state
I install my apk file using adb install myapk.apk and I suppose that all of application's required permissions are granted at install time.
You also need to request permission at run time.
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_PHONE_STATE);
If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission.
If you dont get it , you should request it
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
You need to ask for permissions at runtime in android 6+. Visit android developer page to learn how to ask for permission for android M and above visit : https://developer.android.com/training/permissions/requesting.html

Categories

Resources