Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Fuse.Android.Permissions (from Uno.Permissions) #1387

Merged
merged 1 commit into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
773 changes: 773 additions & 0 deletions Source/Fuse.Android.Permissions/AndroidPermissionsInternal.uno

Large diffs are not rendered by default.

614 changes: 614 additions & 0 deletions Source/Fuse.Android.Permissions/AndroidPermissionsInternal.uxl

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Source/Fuse.Android.Permissions/Fuse.Android.Permissions.unoproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Copyright": "Copyright (c) Fuse Open 2018",
"Description": "Asynchronous feature permission requests",
"Publisher": "Fuse Open",
"Packages": [
"Uno.Threading"
],
"Includes": [
"Permissions.uno:Source",
"AndroidPermissionsInternal.uno:Source",
"AndroidPermissionsInternal.uxl:Extensions",
"Permissions.java:File",
]
}
106 changes: 106 additions & 0 deletions Source/Fuse.Android.Permissions/Permissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.fuse;

import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.PermissionChecker;
import java.util.ArrayList;
import com.uno.UnoObject;
import com.fuse.Activity;

class PermissionsRequest
{
public UnoObject promise;
public String[] permissions;
public int requestID;
public PermissionsRequest(UnoObject inPromise, String[] inPermissions, int inId)
{
promise = inPromise;
permissions = inPermissions;
requestID = inId;
}
}

public final class Permissions {

public static boolean hasPermissions(String[] permissions)
{
for(String p : permissions)
{
if(!hasPermission(p)) return false;
}
return true;
}

public static boolean hasPermission(String permission)
{
return ContextCompat.checkSelfPermission (Activity.getRootActivity(), permission) == PackageManager.PERMISSION_GRANTED;
}

public static boolean shouldShowInformation(String permission)
{
return ActivityCompat.shouldShowRequestPermissionRationale(Activity.getRootActivity(), permission);
}

// _permissionRequestID is an int that the callback method gets the given
// with result of the request.
private static int _permissionRequestID = 0;
private static ArrayList<PermissionsRequest> _requests = new ArrayList<PermissionsRequest>();
private static PermissionsRequest _currentRequest = null;

public static void startPermissionRequest(UnoObject promise, String permission)
{
startPermissionRequest(promise, new String[]{permission});
}

public static void startPermissionRequest(UnoObject promise, String[] permissions)
{
if (hasPermissions(permissions)) {
com.foreign.ExternedBlockHost.permissionRequestSucceeded(promise);
}else{
_requests.add(new PermissionsRequest(promise, permissions, _permissionRequestID++));
if(_currentRequest == null)
nextRequest();
}
}

private static void nextRequest()
{
if( _currentRequest != null || _requests.size() == 0)
return;

_currentRequest = _requests.remove(0);
ActivityCompat.requestPermissions(
Activity.getRootActivity(),
_currentRequest.permissions,
_currentRequest.requestID);
}

public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if( _currentRequest == null)
return;

if (_currentRequest.requestID == requestCode && _currentRequest.promise != null && grantResults.length > 0)
{
boolean ok = true;
for(int result : grantResults)
{
if(result != PackageManager.PERMISSION_GRANTED)
{
ok = false;
break;
}
}
if (ok) {
android.util.Log.d("Permissions", "Permissions granted");
com.foreign.ExternedBlockHost.permissionRequestSucceeded(_currentRequest.promise);
} else {
android.util.Log.d("Permissions", "Permissions denied");
com.foreign.ExternedBlockHost.permissionRequestFailed(_currentRequest.promise);
}
}
_currentRequest = null;
nextRequest();
}
}
298 changes: 298 additions & 0 deletions Source/Fuse.Android.Permissions/Permissions.uno

Large diffs are not rendered by default.

Loading