Experimental: ACTION_START is now optional -- ACTION_CONNECT is sufficient

master
cpfeiffer 2015-08-06 23:17:41 +02:00
parent f4cb798977
commit 9004a8b0c1
5 changed files with 34 additions and 40 deletions

View File

@ -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();
}

View File

@ -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
*/

View File

@ -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);

View File

@ -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);

View File

@ -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<Alarm> 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;
}