Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

[Android API 23+] Request for permissions on runtime - #6614

Merged
livecodepanos merged 13 commits into
livecode:release-9.0.1from
livecodepanos:bugfix-20427
Jul 31, 2018
Merged

[Android API 23+] Request for permissions on runtime#6614
livecodepanos merged 13 commits into
livecode:release-9.0.1from
livecodepanos:bugfix-20427

Conversation

@livecodepanos

Copy link
Copy Markdown
Contributor

No description provided.

@livecodepanos livecodepanos added this to the 9.0.1-rc-2 milestone Jul 26, 2018
@livecodepanos livecodepanos changed the title [Android API 23+][mobilePickPhoto] Request for camera permissions on runtime [Android API 23+] Request for permissions on runtime Jul 26, 2018
@montegoulding

Copy link
Copy Markdown
Contributor

@livecodepanos I think it would make things easier to have an api and callback handler in mblandroiddc.cpp and call it before calling anything that needs the permission. That way it's easier to setup the a static sentinel variable and a wait loop while waiting for the callback. Also it would hopefully be easier to reuse the same code in all the places required.

@montegoulding

Copy link
Copy Markdown
Contributor

MCScreenDC::popupanswerdialog & Java_com_runrev_android_Engine_doAnswerDialogDone is an example of what I mean with the wait loop:

while(s_in_popup_dialog)
		MCscreen -> wait(60.0, !p_blocking, True);

@livecodepanos

Copy link
Copy Markdown
Contributor Author

still WIP until adding a androidRequestPermission command so apps can explicitly request permission at an appropriate time

Comment thread engine/src/mblandroiddc.cpp Outdated
MCAndroidEngineRemoteCall("askPermission", "bx", &t_result, p_permission);

while (s_in_permission_dialog)
MCscreen -> wait(60.0, True, True);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos I think it might be simpler to make this a blocking wait MCscreen -> wait(60.0, False, True); as if we are using a dispatching wait it's feasible that there could be another request during it and the implementation would need to be much more complicated to handle that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do need dispatching wait then I think we would need to generate a request code from a sequence static uint and have s_in_permission_dialog and s_permission_granted be maps of request code -> bool and include the request code as a param in Java_com_runrev_android_Engine_doAskPermissionDone the correct wait loop can be broken.

}
}

public static final int CAMERA_PERMISSION_REQUEST_CODE = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we aren't actually using the request code then we could just call all requests with 1. If we do need dispatching waits (see the other comment) then we probably need a unique code to come from the engine and map to the actual request.


Introduced: 9.0.1

OS: mobile

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos OS and Platform entries here need to be swapped

local tCameraPermissionGranted
put androidRequestPermission("android.permission.CAMERA") into tCameraPermissionGranted
if not tCameraPermissionGranted then
answer "This app is not permitted to access the device camera. You can change this \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos this does not compile. Should be:

answer "This app is not permitted to access the device camera. You can change this" & \
       "in the Settings app."

Interestingly enough I chatted to @runrevmark once about \ escaping newlines in string literals and it's both feasible and backwards compatible to do at some point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @montegoulding :)

I guess I need to use && instead of & (or add a space at the end of the first string)

Comment thread engine/src/mblandroidfs.cpp Outdated
Boolean MCAndroidSystem::GetStandardFolder(MCNameRef p_folder, MCStringRef &r_folder)
{
// accessing "external documents", "external cache" etc requires Write External Storage permission
if (MCStringBeginsWith(MCNameGetString(p_folder), MCSTR("external"), kMCStringOptionCompareCaseless) && !MCAndroidCheckRuntimePermission(MCSTR("android.permission.WRITE_EXTERNAL_STORAGE")))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos can you put everything after && onto a new line here

Comment thread engine/src/mblandroidsensor.cpp Outdated
{
if (p_sensor == kMCSensorTypeLocation)
{
bool t_success = MCAndroidCheckRuntimePermission(MCSTR("android.permission.ACCESS_COARSE_LOCATION")) && MCAndroidCheckRuntimePermission(MCSTR("android.permission.ACCESS_FINE_LOCATION"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos again with the newline

@montegoulding

Copy link
Copy Markdown
Contributor

@panos.merakos there’s a couple of minor patches to make but I’m also wondering if the LCS api should use a case insensitive enum camera, coarselocation, ... rather than the permission strings directly. I guess the advantage of the way it is implemented is any new permissions will work with it without patching. The disadvantage is at the moment there's no checking if the permission string is valid. What happens if you try to request foo? Perhaps there's an exception we need to handle if we expose the API this way.

@livecodepanos

Copy link
Copy Markdown
Contributor Author

Hehe I was thinking the same (i.e. to request just camera rather than the exact permission string), but I left it as it is now for being able to add more permissions in the future. Currently if you request permission for foo the function will just return false

@montegoulding

Copy link
Copy Markdown
Contributor

OK, I think we had better check with @runrevmark on the API to see what he thinks before reviewing OK. I'm not sure we expose many things in a low level way like this in the engine although I agree it does reduce longer term maintenance.

@runrevmark

Copy link
Copy Markdown
Contributor

I suggest:

function androidPermissionExists pTag -- returns true if pTag identifies a known permission
function androidHasPermission pTag -- returns true if the permission identified by pTag is granted, throws if pTag is not an existing permission
command androidRequestPermission pTag -- requests the given permission, throwing if pTag is not a known permission

Note: RequestPermission does not return anything. Grant of permission is done with HasPermission subsequently.

I suggest pTag is either a full permissions name (which is done case-sensitively) or if there is no '.' then it builds it as .toUpper(pTag).

put androidRequestPermission("android.permission.CAMERA") into tCameraPermissionGranted
put androidHasPermission("android.permission.CAMERA") into tCameraPermissionGranted
if not tCameraPermissionGranted then
androidRequestPermission("android.permission.CAMERA")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos you should drop the parentheses here and from the syntax element

local tLocationPermissionGranted
put androidHasPermission("android.permission.ACCESS_FINE_LOCATION") into tLocationPermissionGranted
if not tLocationPermissionGranted then
androidRequestPermission("android.permission.ACCESS_FINE_LOCATION")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parentheses again


bool MCSystemRequestPermission(MCStringRef p_permission, bool& r_granted)
{
r_granted = MCAndroidCheckRuntimePermission(p_permission);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@livecodepanos you need to add do-nothing stubs for these to mbliphonemisc.mm I think

permissionName (enum):
The name of the permission to request.

- "android.permission.CAMERA": permission to access the device camera.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@montegoulding

Copy link
Copy Markdown
Contributor

@livecode-vulcan review ok 39b59ee

@livecode-vulcan

Copy link
Copy Markdown
Contributor

💙 review by @montegoulding ok 39b59ee

livecode-vulcan added a commit that referenced this pull request Jul 31, 2018
[Android API 23+] Request for permissions on runtime
@livecode-vulcan

Copy link
Copy Markdown
Contributor

😎 test success 39b59ee

  • try-community-armv6-android-sdk26_ndk16r15: success
  • try-community-armv7-android-sdk26_ndk16r15: success
  • try-community-arm64-android-sdk26_ndk16r15: success
  • try-community-x86-android-sdk26_ndk16r15: success
  • try-community-x86_64-android-sdk26_ndk16r15: success
  • try-community-js-emscripten-sdk1.35: success
  • try-community-universal-ios-iphoneos11.4: success
  • try-community-universal-ios-iphonesimulator11.4: success
  • try-community-universal-mac-macosx10.9: success
  • try-community-x86-linux-debian8: success
  • try-community-x86_64-linux-debian8: success
  • try-community-x86-win32: success
  • try-community-x86_64-win32: success

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants