From 9004a8b0c1135dd6ba93a23c0bb1df56d0d22210 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Thu, 6 Aug 2015 23:17:41 +0200 Subject: [PATCH] Experimental: ACTION_START is now optional -- ACTION_CONNECT is sufficient --- .../activities/FwAppInstallerActivity.java | 3 -- .../gadgetbridge/devices/InstallHandler.java | 4 ++ .../devices/miband/MiBandPairingActivity.java | 4 -- .../BluetoothStateChangeReceiver.java | 15 ++---- .../service/DeviceCommunicationService.java | 48 ++++++++++--------- 5 files changed, 34 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java index 51c570a3b..1dff90396 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java @@ -116,9 +116,6 @@ public class FwAppInstallerActivity extends Activity implements InstallActivity // needed to get the device if (device == null || !device.isConnected()) { - Intent startIntent = new Intent(this, DeviceCommunicationService.class); - startIntent.setAction(DeviceCommunicationService.ACTION_START); - startService(startIntent); connect(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/InstallHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/InstallHandler.java index b65bdc513..572fbc6bf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/InstallHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/InstallHandler.java @@ -11,6 +11,7 @@ public interface InstallHandler { /** * Returns true if this handler is able to install the element. + * #validateInstallation may only be called if this method returned true. */ public boolean isValid(); @@ -18,6 +19,9 @@ public interface InstallHandler { * Checks whether the installation of the 'element' on the device is possible * and configures the InstallActivity accordingly (sets helpful texts, * enables/disables the "Install" button, etc. + * + * Note: may only be called if #isValid previously returned true. + * * @param installActivity the activity to interact with * @param device the device to which the element shall be installed */ diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java index dbb5814ce..aba2ab631 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java @@ -106,10 +106,6 @@ public class MiBandPairingActivity extends Activity { LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter); Intent serviceIntent = new Intent(this, DeviceCommunicationService.class); - serviceIntent.setAction(DeviceCommunicationService.ACTION_START); - startService(serviceIntent); - - serviceIntent = new Intent(this, DeviceCommunicationService.class); serviceIntent.setAction(DeviceCommunicationService.ACTION_CONNECT); serviceIntent.putExtra(DeviceCommunicationService.EXTRA_PERFORM_PAIR, true); serviceIntent.putExtra(DeviceCommunicationService.EXTRA_DEVICE_ADDRESS, macAddress); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java index ae7f52ad3..9d1681c35 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/BluetoothStateChangeReceiver.java @@ -8,8 +8,8 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; -import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; public class BluetoothStateChangeReceiver extends BroadcastReceiver { @Override @@ -27,16 +27,9 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver { return; } - String deviceAddress = sharedPrefs.getString("last_device_address", null); - Intent startIntent = new Intent(context, DeviceCommunicationService.class); - startIntent.setAction(DeviceCommunicationService.ACTION_START); - context.startService(startIntent); - if (deviceAddress != null) { - Intent connectIntent = new Intent(context, DeviceCommunicationService.class); - connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT); - connectIntent.putExtra(DeviceCommunicationService.EXTRA_DEVICE_ADDRESS, deviceAddress); - context.startService(connectIntent); - } + Intent connectIntent = new Intent(context, DeviceCommunicationService.class); + connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT); + context.startService(connectIntent); } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { Intent stopIntent = new Intent(context, DeviceCommunicationService.class); context.stopService(stopIntent); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 7d88d4606..8eb2a0511 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -117,18 +117,13 @@ public class DeviceCommunicationService extends Service { LOG.debug("Service startcommand: " + action); - if (!mStarted && !action.equals(ACTION_START)) { - // using the service before issuing ACTION_START - LOG.info("Must start service with " + ACTION_START + " before using it: " + action); - return START_NOT_STICKY; - } - - if (mStarted && action.equals(ACTION_START)) { - // using ACTION_START when the service has already been started - return START_STICKY; - } - if (!action.equals(ACTION_START) && !action.equals(ACTION_CONNECT)) { + if (!mStarted) { + // using the service before issuing ACTION_START + LOG.info("Must start service with " + ACTION_START + " or " + ACTION_CONNECT + " before using it: " + action); + return START_NOT_STICKY; + } + if (mDeviceSupport == null || (!isConnected() && !mDeviceSupport.useAutoConnect())) { // trying to send notification without valid Bluetooth connection if (mGBDevice != null) { @@ -139,8 +134,14 @@ public class DeviceCommunicationService extends Service { } } + // when we get past this, we should have a valid mDeviceSupport and mGBDevice instances + switch (action) { + case ACTION_START: + start(); + break; case ACTION_CONNECT: + start(); // ensure started String btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (btDeviceAddress == null) { @@ -172,6 +173,13 @@ public class DeviceCommunicationService extends Service { } } break; + case ACTION_REQUEST_VERSIONINFO: + if (mGBDevice.getFirmwareVersion() == null) { + mDeviceSupport.onFirmwareVersionReq(); + } else { + mGBDevice.sendDeviceUpdateIntent(this); + } + break; case ACTION_NOTIFICATION_GENERIC: { String title = intent.getStringExtra("notification_title"); String body = intent.getStringExtra("notification_body"); @@ -229,13 +237,6 @@ public class DeviceCommunicationService extends Service { String track = intent.getStringExtra("music_track"); mDeviceSupport.onSetMusicInfo(artist, album, track); break; - case ACTION_REQUEST_VERSIONINFO: - if (mGBDevice != null && mGBDevice.getFirmwareVersion() == null) { - mDeviceSupport.onFirmwareVersionReq(); - } else { - mGBDevice.sendDeviceUpdateIntent(this); - } - break; case ACTION_REQUEST_APPINFO: mDeviceSupport.onAppInfoReq(); break; @@ -257,10 +258,6 @@ public class DeviceCommunicationService extends Service { mDeviceSupport.onInstallApp(uri); } break; - case ACTION_START: - startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this)); - mStarted = true; - break; case ACTION_SET_ALARMS: ArrayList alarms = intent.getParcelableArrayListExtra("alarms"); mDeviceSupport.onSetAlarms(alarms); @@ -270,6 +267,13 @@ public class DeviceCommunicationService extends Service { return START_STICKY; } + private void start() { + if (!mStarted) { + startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this)); + mStarted = true; + } + } + private boolean isConnected() { return mGBDevice != null && mGBDevice.getState() == State.CONNECTED; }