diff --git a/sensorsframework_app/src/main/AndroidManifest.xml b/sensorsframework_app/src/main/AndroidManifest.xml index 5b2ae36..d959033 100644 --- a/sensorsframework_app/src/main/AndroidManifest.xml +++ b/sensorsframework_app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + diff --git a/sensorsframework_app/src/main/java/org/opendatakit/sensors/ui/activity/AddSensorActivity.java b/sensorsframework_app/src/main/java/org/opendatakit/sensors/ui/activity/AddSensorActivity.java index c8262b2..8172a1c 100644 --- a/sensorsframework_app/src/main/java/org/opendatakit/sensors/ui/activity/AddSensorActivity.java +++ b/sensorsframework_app/src/main/java/org/opendatakit/sensors/ui/activity/AddSensorActivity.java @@ -15,15 +15,22 @@ */ package org.opendatakit.sensors.ui.activity; +import android.Manifest; import android.app.Activity; +import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; import android.util.Log; import android.view.View; +import android.widget.Toast; import org.opendatakit.sensors.Constants; import org.opendatakit.sensors.R; import org.opendatakit.sensors.SensorsSingleton; import org.opendatakit.sensors.ServiceConstants; +import org.opendatakit.utilities.RuntimePermissionUtils; /** * @author wbrunette@gmail.com @@ -33,6 +40,15 @@ public class AddSensorActivity extends Activity { private static final String LOGTAG = AddSensorActivity.class.getSimpleName(); + protected static final String[] REQUIRED_PERMISSIONS = new String[] { + Manifest.permission.BLUETOOTH_ADMIN, + Manifest.permission.BLUETOOTH, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.ACCESS_COARSE_LOCATION + }; + + protected static final int PERMISSION_REQ_CODE = 5; + private static final int RESULT_OK_BT = 1; private static final int RESULT_OK_USB = 2; @@ -41,6 +57,14 @@ public class AddSensorActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + if (!RuntimePermissionUtils.checkSelfAllPermission(this, REQUIRED_PERMISSIONS)) { + ActivityCompat.requestPermissions( + this, + REQUIRED_PERMISSIONS, + PERMISSION_REQ_CODE + ); + } + Intent intent = getIntent(); String tmpAppName = intent.getStringExtra(ServiceConstants.APP_NAME_KEY); if (tmpAppName == null) { @@ -92,4 +116,39 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } } + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode != PERMISSION_REQ_CODE) { + return; + } + + boolean granted = true; + if (grantResults.length > 0) { + for (int i = 0; i < grantResults.length; i++) { + if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { + granted = false; + } + } + + if (granted) + return; + + if (RuntimePermissionUtils.shouldShowAnyPermissionRationale(this, permissions)) { + RuntimePermissionUtils.createPermissionRationaleDialog(this, requestCode, permissions).setMessage(R.string.write_external_storage_rationale) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + setResult(Activity.RESULT_CANCELED); + finish(); + } + }).show(); + } else { + Toast.makeText(this, R.string.write_external_perm_denied, Toast.LENGTH_LONG).show(); + setResult(Activity.RESULT_CANCELED); + finish(); + } + } + } } diff --git a/sensorsframework_app/src/main/res/values/strings.xml b/sensorsframework_app/src/main/res/values/strings.xml index 89c4626..a4c37d7 100644 --- a/sensorsframework_app/src/main/res/values/strings.xml +++ b/sensorsframework_app/src/main/res/values/strings.xml @@ -20,4 +20,7 @@ Single Click: No Action NOT A SENSOR Select Sensor Type + ODK Sensors Framework needs to access external + storage to store resources and interact with other ODK apps + ODK Sensors Framework cannot function without write access to external storage