diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7b500f3..0148863 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,6 @@ - @@ -16,10 +15,12 @@ + - + + diff --git a/app/src/main/java/jp/co/cyberagent/stf/query/DoCleanBluetoothBondedDevicesResponder.java b/app/src/main/java/jp/co/cyberagent/stf/query/DoCleanBluetoothBondedDevicesResponder.java index 98a5762..3d5c94c 100644 --- a/app/src/main/java/jp/co/cyberagent/stf/query/DoCleanBluetoothBondedDevicesResponder.java +++ b/app/src/main/java/jp/co/cyberagent/stf/query/DoCleanBluetoothBondedDevicesResponder.java @@ -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); } diff --git a/app/src/main/java/jp/co/cyberagent/stf/query/SetBluetoothEnabledResponder.java b/app/src/main/java/jp/co/cyberagent/stf/query/SetBluetoothEnabledResponder.java index cbe1ffb..285c837 100644 --- a/app/src/main/java/jp/co/cyberagent/stf/query/SetBluetoothEnabledResponder.java +++ b/app/src/main/java/jp/co/cyberagent/stf/query/SetBluetoothEnabledResponder.java @@ -4,6 +4,11 @@ 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; @@ -11,6 +16,8 @@ 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); } @@ -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) @@ -62,6 +75,7 @@ public GeneratedMessageLite respond(Wire.Envelope envelope) throws InvalidProtoc .build(); } + @Override public void cleanup() { // No-op diff --git a/app/src/main/java/jp/co/cyberagent/stf/query/SetWifiEnabledResponder.java b/app/src/main/java/jp/co/cyberagent/stf/query/SetWifiEnabledResponder.java index f198580..dda0e56 100644 --- a/app/src/main/java/jp/co/cyberagent/stf/query/SetWifiEnabledResponder.java +++ b/app/src/main/java/jp/co/cyberagent/stf/query/SetWifiEnabledResponder.java @@ -2,6 +2,7 @@ import android.content.Context; import android.net.wifi.WifiManager; +import android.util.Log; import com.google.protobuf.GeneratedMessageLite; import com.google.protobuf.InvalidProtocolBufferException; @@ -9,6 +10,8 @@ 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); } @@ -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();