From 51dfeed1191dc7f0f467a607a54931c7e3d87306 Mon Sep 17 00:00:00 2001 From: Peter Lewis Date: Fri, 27 Nov 2020 11:57:52 -0800 Subject: [PATCH] ANDROID: request permissions from Manifest This reads permissions from the manifest and requests all that are needed. Apps and modules should add necessary permissions to ANDROID_xml_permissions. Moved the initial setContentView back to before @ANDROID_JAVA_ONCREATE@ so that apps can set a content view in ANDROID_java_oncreate --- loaders/android/AndroidManifest.xml.in | 2 +- loaders/android/bootstrap.java.in | 34 +++++++++++++++++-- modules/camera/ANDROID_java_oncreate | 2 -- modules/gps/ANDROID_java_oncreate | 2 -- modules/hybridapp-xwalk/ANDROID_java_oncreate | 3 -- modules/hybridapp/ANDROID_java_oncreate | 3 -- 6 files changed, 32 insertions(+), 14 deletions(-) delete mode 100644 modules/camera/ANDROID_java_oncreate diff --git a/loaders/android/AndroidManifest.xml.in b/loaders/android/AndroidManifest.xml.in index 28414f23..1c663a7d 100644 --- a/loaders/android/AndroidManifest.xml.in +++ b/loaders/android/AndroidManifest.xml.in @@ -21,7 +21,7 @@ @ANDROID_XML_RECEIVERS@ @ANDROID_XML_SERVICES@ -@ANDROID_XML_PERMISSIONS@ +@ANDROID_XML_PERMISSIONS@ # eof diff --git a/loaders/android/bootstrap.java.in b/loaders/android/bootstrap.java.in index 89030701..bf84f08d 100644 --- a/loaders/android/bootstrap.java.in +++ b/loaders/android/bootstrap.java.in @@ -45,6 +45,8 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.BroadcastReceiver; import android.content.res.Configuration; +import android.content.pm.PackageManager; +import android.content.pm.PackageInfo; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.os.PowerManager; @@ -107,6 +109,29 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@ return true; } + public boolean requestAllPermissions() { + @IF_ANDROIDAPI_GT_22@ + PackageManager pm = getPackageManager(); + try + { + PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS); + String[] requestedPermissions = null; + if (packageInfo != null) + requestedPermissions = packageInfo.requestedPermissions; + + if (requestedPermissions != null && requestedPermissions.length > 0) + requestPermissions(requestedPermissions, 1); + } + catch (PackageManager.NameNotFoundException e) + { + System.err.print("requestAllPermissions error: " + e); + e.printStackTrace(System.err); + return false; + } + /* end of IF_ANDROIDAPI_GT_22 */ + return true; + } + @Override public void startActivityForResult(Intent intent, int cont) { try { @@ -176,16 +201,19 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@ mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); - checkOrRequestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE); - // Additions and permissions needed by modules, e.g. gps - @ANDROID_JAVA_ONCREATE@ + requestAllPermissions(); // MUST NOT run before nativeInstanceInit completed // and MUST NOT run before permission checks setContentView(current_ContentView==null ? mGLView : current_ContentView); + + // Additions needed by modules, e.g. gps + @ANDROID_JAVA_ONCREATE@ + // start EVENT_IDLE if(idle_tmScheduleRate > 0) idle_tm.scheduleAtFixedRate(idle_task, 0, idle_tmScheduleRate); } + @Override protected void onDestroy() { setContentView(mGLView); diff --git a/modules/camera/ANDROID_java_oncreate b/modules/camera/ANDROID_java_oncreate deleted file mode 100644 index efedc131..00000000 --- a/modules/camera/ANDROID_java_oncreate +++ /dev/null @@ -1,2 +0,0 @@ -checkOrRequestPermission(android.Manifest.permission.CAMERA); -//checkOrRequestPermission(android.Manifest.permission.RECORD_AUDIO); diff --git a/modules/gps/ANDROID_java_oncreate b/modules/gps/ANDROID_java_oncreate index 9f2acc42..ac50c0d8 100644 --- a/modules/gps/ANDROID_java_oncreate +++ b/modules/gps/ANDROID_java_oncreate @@ -2,11 +2,9 @@ locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ criteria.setAccuracy(Criteria.ACCURACY_FINE); - checkOrRequestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); } else { criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAccuracy(Criteria.ACCURACY_COARSE); - checkOrRequestPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION); } provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); diff --git a/modules/hybridapp-xwalk/ANDROID_java_oncreate b/modules/hybridapp-xwalk/ANDROID_java_oncreate index 39c14059..10d9818f 100755 --- a/modules/hybridapp-xwalk/ANDROID_java_oncreate +++ b/modules/hybridapp-xwalk/ANDROID_java_oncreate @@ -1,6 +1,3 @@ -// permissions - -checkOrRequestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); // webview diff --git a/modules/hybridapp/ANDROID_java_oncreate b/modules/hybridapp/ANDROID_java_oncreate index 6bf7e206..8e8d0780 100755 --- a/modules/hybridapp/ANDROID_java_oncreate +++ b/modules/hybridapp/ANDROID_java_oncreate @@ -1,6 +1,3 @@ -// permissions - -checkOrRequestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); // webview