Skip to content

Commit

Permalink
Catch exceptions, code defensively
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Aug 18, 2023
1 parent 1db1728 commit cacc9f0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
16 changes: 14 additions & 2 deletions common/src/main/java/com/platypii/baseline/Permissions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.platypii.baseline;

import com.platypii.baseline.util.Exceptions;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -37,7 +39,12 @@ public static String[] btPermissions() {
}

public static boolean hasLocationPermissions(@NonNull Context context) {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED;
try {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED;
} catch (Exception e) {
Exceptions.report(e);
return false;
}
}

public static boolean hasFineLocationPermissions(@NonNull Context context) {
Expand Down Expand Up @@ -76,7 +83,12 @@ public static void openLocationSettings(@NonNull Context context) {

public static boolean hasBluetoothConnectPermissions(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PERMISSION_GRANTED;
try {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PERMISSION_GRANTED;
} catch (Exception e) {
Exceptions.report(e);
return false;
}
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public void start(@NonNull Activity activity) {
}
Log.i(TAG, "Starting BLE service");
setState(BT_STARTING);
if (activity == null) {
Exceptions.report(new NullPointerException("activity should not be null"));
return;
}
central = new BluetoothCentralManager(activity.getApplicationContext(), bluetoothCentralManagerCallback, handler);

final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public int compare(@NonNull BluetoothItem device1, @NonNull BluetoothItem device
}

private int score(@NonNull BluetoothItem device) {
if (device.name.isEmpty()) return 0;
if (device.name.startsWith("Mohawk")) return 2;
else if (device.name.startsWith("RaceBox")) return 1;
else if (device.name.contains("GPS")) return 1;
else return 0;
if (device.name.startsWith("RaceBox")) return 1;
if (device.name.contains("GPS")) return 1;
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static short bytesToShort(byte b1, byte b2) {
@NonNull
public static String getDeviceName(@NonNull BluetoothDevice device) {
try {
return device.getName();
final String name = device.getName();
return name == null ? "" : name;
} catch (SecurityException ignored) {
return "";
}
Expand Down

0 comments on commit cacc9f0

Please sign in to comment.