About Monkey 2 › Forums › Monkey 2 Programming Help › Android Permissions
This topic contains 19 replies, has 6 voices, and was last updated by
PhatPeter 1 year, 5 months ago.
-
AuthorPosts
-
October 5, 2017 at 8:03 pm #10998
I am having trouble figuring out why the permissions for storage aren’t being set at install. When the app installs it says that there are no special permissions, and then I have to go into settings and manually set the storage permission. After that everything works fine. I have the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE set in the manifest, so I’m not sure why it isn’t being set at install. Any ideas?
October 6, 2017 at 4:37 am #11004Can you post/attach the manifest file here?
October 6, 2017 at 5:06 pm #11024Here is the manifest.
October 6, 2017 at 5:07 pm #11025<?xml version=”1.0″ encoding=”utf-8″?>
<manifest
xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.sscalculator.bighatgames”><uses-sdk android:minSdkVersion=”10″ android:targetSdkVersion=”23″ />
<uses-feature android:glEsVersion=”0x00020000″ />
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” /><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity
android:name=”.SSCalculator”
android:label=”SSCalculator”
android:screenOrientation=”portrait”
android:configChanges=”keyboardHidden|orientation|screenSize”
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
android:launchMode=”singleTop”>android:configChanges=”orientation”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter></activity>
</application></manifest>
October 6, 2017 at 5:47 pm #11026HI, if you are building for Android SDK 23+ you have to request permissions from the user not just in the manifest.
This is the java module code I created for Monkey1 to deal with this:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import android.support.v4.content.ContextCompat;import android.support.v4.app.ActivityCompat;import android.Manifest;import android.content.pm.PackageManager;class PermissionNative extends ActivityDelegate implements ActivityCompat.OnRequestPermissionsResultCallback {private boolean done;private boolean result;public void CheckPermissionsNative(boolean calendar, boolean camera, boolean location, boolean storage){done = false;if(Build.VERSION.SDK_INT >= 23){BBAndroidGame.AndroidGame().AddActivityDelegate(this);AskForPermissionsNative(calendar, camera, location, storage);}else {done = true;result = true;}}public void AskForPermissionsNative(boolean calendar, boolean camera, boolean location, boolean storage){Activity activity = BBAndroidGame.AndroidGame().GetActivity();List<String> requestedPermissionsList = new ArrayList<String>();if(calendar == true && ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED){requestedPermissionsList.add(Manifest.permission.READ_CALENDAR);}if(camera == true && ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){requestedPermissionsList.add(Manifest.permission.CAMERA);}if(location == true && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){requestedPermissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);}if(storage == true && ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){requestedPermissionsList.add(Manifest.permission.READ_EXTERNAL_STORAGE);}if(requestedPermissionsList.size() >0){String[] requestedPermissions = new String[requestedPermissionsList.size()];requestedPermissions = requestedPermissionsList.toArray(requestedPermissions);ActivityCompat.requestPermissions( activity, requestedPermissions, 0);}else {done = true;result = true;}}public boolean CheckPermissionsDoneNative(){return done;}public boolean CheckPermissionsResultNative(){return result;}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {switch (requestCode) {case 0: {done = true;if (grantResults.length > 0) {result = true;for (int i=0; i<permissions.length; i++) {if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {result = false;}}} else {result = false;}return;}}}}October 6, 2017 at 8:49 pm #11028Ok, just adding a permission module now.
Thanks for the sample code coppercircle. Am I correct in thinking you also has to add the permissions callback to AppDelagate and main Actitiy class?
October 7, 2017 at 6:00 pm #11033Hi, yeah just a delagate and callback for the response
Monkey123456789101112131415161718192021222324Class MyApp Extends App Implements PermissionDelegateMethod OnCreate:Int()Permission.CheckPermissions(Self, True, True, True, False)Return 0End MethodMethod OnRender:Int()Cls 255, 255, 200Return 0End MethodMethod OnUpdate:Int()Permission.Update()Return 0End MethodMethod OnPermissionsReply:Void(allAllowed:Bool)Print "OnPermissionsReply: " + Int(allAllowed)EndEndOctober 7, 2017 at 9:39 pm #11035I actually meant the native target MonkeyActivity java class – my version doesn’t have the onPermissionsReply method.
Ditto it’s not supported via the delegate mechanism. It’s easy enough to add, just wondering if I missed a trick somewhere. I hate having to continually mung up the core Activity class each time we find something else that needs to be delegated out. It’s a pretty crappy design on the part of Android IMO.
October 8, 2017 at 9:24 am #11039Yes unfortunately you have to add it to the target, I haven’t found anyway of of creating the delegate on the fly.
October 11, 2017 at 6:09 pm #11073Thanks for the answers, guys. Greatly Appreciated.
So how would I use this in Monkey2?
October 11, 2017 at 8:08 pm #11075A version of this has been pushed to the develop branch of github. It will also be included in the v1.1.08 release due in a few days, so you can just wait for that if you want.
Here it is in action, hopefully pretty straightforward:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )Print "Hello World from permissions demo!"Local permissions:=New String[]("android.permission.INTERNET","android.permission.ACCESS_NETWORK_STATE","android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE","com.android.vending.BILLING" )For Local i:=0 Until permissions.LengthPrint "CheckPermission(~q"+permissions[i]+"~q)="+CheckPermission( permissions[i] )NextRequestPermissions( permissions,Lambda( results:int[] )If Not resultsPrint "RequestPermissions cancelled"ReturnEndifFor Local i:=0 Until results.LengthIf results[i]Print "Permission granted for "+permissions[i]ElsePrint "Permission NOT granted for "+permissions[i]EndifNextEnd )EndMethod OnRender( canvas:Canvas ) OverrideGlobal n:Intn+=1App.RequestRender()canvas.DrawText( "Hello World! n="+n,Width/2,Height/2,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndYou might also want to read up on runtime permissions – was certainly news to me!
https://developer.android.com/training/permissions/requesting.html
You apparently only need to do this if targetSDK is >=23, which it is in the default manifest for monkey2 projects. So one way to avoid all this I guess is to just change targetSDK to 22!
Thanks to CopperCircle for pointing me in the right direction.
October 11, 2017 at 9:12 pm #11077Awesome!
Thanks Mark.
and Thanks CopperCircleOctober 11, 2017 at 9:43 pm #11079Also, can you let me know what you want to do with external storage?
There are a lot of directories and APIS involved and I just wanna know what to concentrate on first.
October 12, 2017 at 6:30 pm #11085Right now, I’m just using it to store user settings in a text file. Later, in a created directory, I may have some records(also in a text file) readable by other apps. So just text files at this point.
October 12, 2017 at 7:19 pm #11087I would like to save some generated images and svg files to a user specified folder, maybe just “downloads” on Android. Not sure where to put them on iOS, but the new 11, might have some pointeres, because theres a file manager in that.
-
AuthorPosts
You must be logged in to reply to this topic.