Skip to content

Commit

Permalink
ANDROID: request permissions from Manifest
Browse files Browse the repository at this point in the history
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
  • Loading branch information
peterlew committed Nov 27, 2020
1 parent 1e4c9f3 commit 51dfeed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion loaders/android/AndroidManifest.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@ANDROID_XML_RECEIVERS@
@ANDROID_XML_SERVICES@
</application>
@ANDROID_XML_PERMISSIONS@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
@ANDROID_XML_PERMISSIONS@
</manifest>
# eof
34 changes: 31 additions & 3 deletions loaders/android/bootstrap.java.in
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions modules/camera/ANDROID_java_oncreate

This file was deleted.

2 changes: 0 additions & 2 deletions modules/gps/ANDROID_java_oncreate
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions modules/hybridapp-xwalk/ANDROID_java_oncreate
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// permissions

checkOrRequestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);

// webview

Expand Down
3 changes: 0 additions & 3 deletions modules/hybridapp/ANDROID_java_oncreate
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// permissions

checkOrRequestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);

// webview

Expand Down

0 comments on commit 51dfeed

Please sign in to comment.