From 88e2b7ff99eceb2cd30b3a2579ff532e4a6a065a Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 19 Apr 2015 14:34:18 +0200 Subject: [PATCH] #15 automatically toggle the broadcast receiver using connectoin status Makes "real" notifications where for the MI Band, not just those from the debug activity. --- .../BluetoothCommunicationService.java | 20 ++++++++++++++++++- .../freeyourgadget/gadgetbridge/GBDevice.java | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 03a43807f..f4a35fd27 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -4,9 +4,11 @@ import android.app.NotificationManager; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; @@ -15,7 +17,6 @@ import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.util.Log; import android.widget.Toast; - import nodomain.freeyourgadget.gadgetbridge.GBDevice.State; import nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport; import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread; @@ -56,9 +57,25 @@ public class BluetoothCommunicationService extends Service { private GBDevice mGBDevice = null; private DeviceSupport mDeviceSupport; + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) { + String deviceAddress = intent.getStringExtra("device_address"); + GBDevice.State state = GBDevice.State.values()[intent.getIntExtra("device_state", 0)]; + if (mGBDevice.getAddress().equals(deviceAddress)) { + mGBDevice.setState(state); + GB.setReceiversEnableState(mGBDevice.isConnected(), context); + } + } + } + }; + @Override public void onCreate() { super.onCreate(); + registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED)); } @Override @@ -195,6 +212,7 @@ public class BluetoothCommunicationService extends Service { public void onDestroy() { super.onDestroy(); + unregisterReceiver(mReceiver); GB.setReceiversEnableState(false, this); // disable BroadcastReceivers if (mDeviceSupport != null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java index d86ce6ab1..86266b1b8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBDevice.java @@ -5,6 +5,9 @@ import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; public class GBDevice { + public static final String ACTION_DEVICE_CHANGED + = "nodomain.freeyourgadget.gadgetbride.gbdevice.action.device_changed"; + private final String name; private final String address; private final Type type; @@ -75,7 +78,7 @@ public class GBDevice { // TODO: this doesn't really belong here public void sendDeviceUpdateIntent(Context context) { - Intent deviceUpdateIntent = new Intent(ControlCenter.ACTION_REFRESH_DEVICELIST); + Intent deviceUpdateIntent = new Intent(ACTION_DEVICE_CHANGED); deviceUpdateIntent.putExtra("device_address", getAddress()); deviceUpdateIntent.putExtra("device_state", getState().ordinal()); deviceUpdateIntent.putExtra("firmware_version", getFirmwareVersion());