Skip to content

Commit

Permalink
Fix a bug in TripDiaryStateMachineForegroundService, and tweaked the …
Browse files Browse the repository at this point in the history
…permissions so we ask for bluetooth permissions when the app opens for the time being.
  • Loading branch information
louisg1337 committed Apr 12, 2024
1 parent 41d06f5 commit c3b1564
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/android/DataCollectionPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void pluginInitialize() {
new StatsEvent(myActivity, R.string.app_launched));

TripDiaryStateMachineReceiver.initOnUpgrade(myActivity);

// Ask for bluetooth permissions
// We will change this with future releases, we just ran out of time implementing this into the front end
mControlDelegate.checkAndPromptBluetoothScanPermissions();
}

@Override
Expand Down
20 changes: 11 additions & 9 deletions src/android/location/TripDiaryStateMachineForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ public static String humanizeState(Context ctxt, String state) {
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(this, TAG, "onStartCommand called with intent = "+intent+
" flags = " + flags + " and startId = " + startId);

if (intent.getAction() != null && intent.getAction().equals("foreground_start_bluetooth")) {
Intent bluetoothService = new Intent(this, BluetoothService.class);
this.startService(bluetoothService);
return START_STICKY;
} else if (intent.getAction() != null && intent.getAction().equals("foreground_start_bluetooth_monitoring")) {
Intent bluetoothService = new Intent(this, BluetoothMonitoringService.class);
this.startService(bluetoothService);
return START_STICKY;

if (intent != null && intent.getAction() != null) {
if (intent.getAction().equals("foreground_start_bluetooth")) {
Intent bluetoothService = new Intent(this, BluetoothService.class);
this.startService(bluetoothService);
return START_STICKY;
} else if (intent.getAction().equals("foreground_start_bluetooth_monitoring")) {
Intent bluetoothService = new Intent(this, BluetoothMonitoringService.class);
this.startService(bluetoothService);
return START_STICKY;
}
}

String message = humanizeState(this, TripDiaryStateMachineService.getState(this));
Expand Down
28 changes: 28 additions & 0 deletions src/android/verification/SensorControlForegroundDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,25 @@ public void checkAndPromptBluetoothScanPermissions(CallbackContext cordovaCallba
}
}

/**
* Overloaded version of function aboe so we can use on native side.
*/
public void checkAndPromptBluetoothScanPermissions() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S){
Log.d(cordova.getActivity(), TAG, "Older build version than API 31, return success!");
} else if (cordova.hasPermission(SensorControlConstants.BLUETOOTH_SCAN)){
Log.d(cordova.getActivity(), TAG, "User has already enabled bluetooth scan!");
} else {
Log.d(cordova.getActivity(), TAG, "User has not enabled bluetooth scan, requesting now...");
this.permissionChecker = getPermissionChecker(
SensorControlConstants.ENABLE_BLUETOOTH_SCAN,
SensorControlConstants.BLUETOOTH_SCAN,
"Please enable \'Nearby devices\' permission to use the scanner.",
"Please enable \'Nearby devices\' permission to use the scanner.");
this.permissionChecker.requestPermission();
}
}

public void checkMotionActivityPermissions(CallbackContext cordovaCallback) {
boolean validPerms = SensorControlChecks.checkMotionActivityPermissions(cordova.getActivity());
if(validPerms) {
Expand Down Expand Up @@ -672,6 +691,11 @@ public void onRequestPermissionResult(int requestCode, String[] permissions,
}
break;
case SensorControlConstants.ENABLE_BLUETOOTH_SCAN:
if (cordovaCallback == null) {
this.permissionChecker = null;
break;
}

Log.d(cordova.getActivity(), TAG, "Got return for bluetooth scanning permission...");
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(cordova.getActivity(), TAG, "Bluetooth scanning is allowed!");
Expand Down Expand Up @@ -791,6 +815,10 @@ public void run() {
}
});
case SensorControlConstants.ENABLE_BLUETOOTH_SCAN:
if (cordovaCallback == null) {
break;
}

Log.d(mAct, TAG, requestCode + " is our code, handling callback");
Log.d(mAct, TAG, "Got bluetooth callback from launching app settings");
if (cordova.hasPermission(SensorControlConstants.BLUETOOTH_SCAN)) {
Expand Down

0 comments on commit c3b1564

Please sign in to comment.