Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #26 from opendatakit/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
wbrunette authored Jan 15, 2019
2 parents a16b22c + d70d4c1 commit 0e4b71b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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 [email protected]
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>

0 comments on commit 0e4b71b

Please sign in to comment.