Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sensorsframework_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- this is defined in org.opendatakit.services -->
<uses-permission android:name="org.opendatakit.database.RUN_DATABASE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}
}
3 changes: 3 additions & 0 deletions sensorsframework_app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
<string name="single_click_no_action">Single Click: No Action</string>
<string name="not_a_sensor">NOT A SENSOR</string>
<string name="select_sensor_type">Select Sensor Type</string>
<string name="write_external_storage_rationale">ODK Sensors Framework needs to access external
storage to store resources and interact with other ODK apps</string>
<string name="write_external_perm_denied">ODK Sensors Framework cannot function without write access to external storage</string>
</resources>