Skip to content

Commit

Permalink
improve error handling and add required "find me" permission for mani…
Browse files Browse the repository at this point in the history
…fest (#135)

Signed-off-by: Jussi Vatjus-Anttila <[email protected]>
  • Loading branch information
jupe authored Apr 4, 2024
1 parent 7bf9ea5 commit 5b8be05
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Expand All @@ -16,10 +15,12 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.DUMP"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ public GeneratedMessageLite respond(Wire.Envelope envelope) throws InvalidProtoc

// Return early if the API level is not sufficient
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
Log.e(TAG, "API level is not sufficient");
return buildResponse(envelope, false);
}

// Return early if Bluetooth is not available
BluetoothManager bm = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if (bm == null) {
Log.e(TAG, "BluetoothManager is null");
return buildResponse(envelope, false);
}

// Return early if cannot get Bluetooth adapter
BluetoothAdapter ba = bm.getAdapter();
if (ba == null) {
Log.e(TAG, "BluetoothAdapter is null");
return buildResponse(envelope, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import android.content.Context;
import android.bluetooth.BluetoothManager;
import android.os.Build;
import android.util.Log;
import android.content.pm.PackageManager;
import android.Manifest;
import androidx.core.content.ContextCompat;


import com.google.protobuf.GeneratedMessageLite;
import com.google.protobuf.InvalidProtocolBufferException;

import jp.co.cyberagent.stf.proto.Wire;

public class SetBluetoothEnabledResponder extends AbstractResponder {
private static final String TAG = SetBluetoothEnabledResponder.class.getSimpleName();

public SetBluetoothEnabledResponder(Context context) {
super(context);
}
Expand All @@ -21,37 +28,43 @@ public GeneratedMessageLite respond(Wire.Envelope envelope) throws InvalidProtoc
Wire.SetBluetoothEnabledRequest.parseFrom(envelope.getMessage());

boolean successful;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
BluetoothManager bm = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if (bm != null) {
BluetoothAdapter ba = bm.getAdapter();
if (ba != null) {
try {
if (request.getEnabled()) {
ba.enable();
} else {
ba.disable();
}
successful = true;
} catch (SecurityException exception) {
successful = false;
}
}
// No Bluetooth available
else {
successful = false;
}
}
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
// getAdapter() is only available since Android API level 18
Log.e(TAG, "API level is not sufficient");
return buildResponse(envelope, false);
}
BluetoothManager bm = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if (bm == null) {
// No Bluetooth available
else {
successful = false;
}
Log.e(TAG, "BluetoothManager is null");
return buildResponse(envelope, false);
}
// getAdapter() is only available since Android API level 18
else {
successful = false;
BluetoothAdapter ba = bm.getAdapter();
if (ba == null) {
// No Bluetooth available
Log.e(TAG, "BluetoothAdapter is null");
return buildResponse(envelope, false);
}

int dumpPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT);
if (dumpPermission == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Permission granted to change Bluetooth state");
} else {
Log.w(TAG, "Permission denied to change Bluetooth state");
}

try {
if (request.getEnabled()) {
ba.enable();
} else {
ba.disable();
}
return buildResponse(envelope, true);
} catch (SecurityException exception) {
Log.e(TAG, "Failed to set Bluetooth enabled: " + exception.getMessage());
return buildResponse(envelope, false);
}
}
private GeneratedMessageLite buildResponse(Wire.Envelope envelope, boolean successful) {
return Wire.Envelope.newBuilder()
.setId(envelope.getId())
.setType(Wire.MessageType.SET_BLUETOOTH_ENABLED)
Expand All @@ -62,6 +75,7 @@ public GeneratedMessageLite respond(Wire.Envelope envelope) throws InvalidProtoc
.build();
}


@Override
public void cleanup() {
// No-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import android.content.Context;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.google.protobuf.GeneratedMessageLite;
import com.google.protobuf.InvalidProtocolBufferException;

import jp.co.cyberagent.stf.proto.Wire;

public class SetWifiEnabledResponder extends AbstractResponder {
private static final String TAG = SetWifiEnabledResponder.class.getSimpleName();

public SetWifiEnabledResponder(Context context) {
super(context);
}
Expand All @@ -19,14 +22,24 @@ public GeneratedMessageLite respond(Wire.Envelope envelope) throws InvalidProtoc
Wire.SetWifiEnabledRequest.parseFrom(envelope.getMessage());

WifiManager wm = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

wm.setWifiEnabled(request.getEnabled());

if (wm == null) {
Log.e(TAG, "WifiManager is null");
return buildResponse(envelope, false);
}
boolean success = false;
try {
success = wm.setWifiEnabled(request.getEnabled());
} catch (Exception e) {
Log.e(TAG, "Failed to set wifi enabled", e);
}
return buildResponse(envelope, success);
}
private GeneratedMessageLite buildResponse(Wire.Envelope envelope, boolean successful) {
return Wire.Envelope.newBuilder()
.setId(envelope.getId())
.setType(Wire.MessageType.SET_WIFI_ENABLED)
.setMessage(Wire.SetWifiEnabledResponse.newBuilder()
.setSuccess(true)
.setSuccess(successful)
.build()
.toByteString())
.build();
Expand Down

0 comments on commit 5b8be05

Please sign in to comment.