Experimental: ACTION_START is now optional -- ACTION_CONNECT is sufficient
This commit is contained in:
parent
f4cb798977
commit
9004a8b0c1
|
@ -116,9 +116,6 @@ public class FwAppInstallerActivity extends Activity implements InstallActivity
|
||||||
|
|
||||||
// needed to get the device
|
// needed to get the device
|
||||||
if (device == null || !device.isConnected()) {
|
if (device == null || !device.isConnected()) {
|
||||||
Intent startIntent = new Intent(this, DeviceCommunicationService.class);
|
|
||||||
startIntent.setAction(DeviceCommunicationService.ACTION_START);
|
|
||||||
startService(startIntent);
|
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ public interface InstallHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this handler is able to install the element.
|
* Returns true if this handler is able to install the element.
|
||||||
|
* #validateInstallation may only be called if this method returned true.
|
||||||
*/
|
*/
|
||||||
public boolean isValid();
|
public boolean isValid();
|
||||||
|
|
||||||
|
@ -18,6 +19,9 @@ public interface InstallHandler {
|
||||||
* Checks whether the installation of the 'element' on the device is possible
|
* Checks whether the installation of the 'element' on the device is possible
|
||||||
* and configures the InstallActivity accordingly (sets helpful texts,
|
* and configures the InstallActivity accordingly (sets helpful texts,
|
||||||
* enables/disables the "Install" button, etc.
|
* enables/disables the "Install" button, etc.
|
||||||
|
*
|
||||||
|
* Note: may only be called if #isValid previously returned true.
|
||||||
|
*
|
||||||
* @param installActivity the activity to interact with
|
* @param installActivity the activity to interact with
|
||||||
* @param device the device to which the element shall be installed
|
* @param device the device to which the element shall be installed
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -106,10 +106,6 @@ public class MiBandPairingActivity extends Activity {
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
|
LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
|
||||||
|
|
||||||
Intent serviceIntent = new Intent(this, DeviceCommunicationService.class);
|
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.setAction(DeviceCommunicationService.ACTION_CONNECT);
|
||||||
serviceIntent.putExtra(DeviceCommunicationService.EXTRA_PERFORM_PAIR, true);
|
serviceIntent.putExtra(DeviceCommunicationService.EXTRA_PERFORM_PAIR, true);
|
||||||
serviceIntent.putExtra(DeviceCommunicationService.EXTRA_DEVICE_ADDRESS, macAddress);
|
serviceIntent.putExtra(DeviceCommunicationService.EXTRA_DEVICE_ADDRESS, macAddress);
|
||||||
|
|
|
@ -8,8 +8,8 @@ import android.content.SharedPreferences;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
|
import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenter;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
||||||
|
|
||||||
public class BluetoothStateChangeReceiver extends BroadcastReceiver {
|
public class BluetoothStateChangeReceiver extends BroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
|
@ -27,16 +27,9 @@ public class BluetoothStateChangeReceiver extends BroadcastReceiver {
|
||||||
return;
|
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);
|
Intent connectIntent = new Intent(context, DeviceCommunicationService.class);
|
||||||
connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT);
|
connectIntent.setAction(DeviceCommunicationService.ACTION_CONNECT);
|
||||||
connectIntent.putExtra(DeviceCommunicationService.EXTRA_DEVICE_ADDRESS, deviceAddress);
|
|
||||||
context.startService(connectIntent);
|
context.startService(connectIntent);
|
||||||
}
|
|
||||||
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
|
} else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
|
||||||
Intent stopIntent = new Intent(context, DeviceCommunicationService.class);
|
Intent stopIntent = new Intent(context, DeviceCommunicationService.class);
|
||||||
context.stopService(stopIntent);
|
context.stopService(stopIntent);
|
||||||
|
|
|
@ -117,18 +117,13 @@ public class DeviceCommunicationService extends Service {
|
||||||
|
|
||||||
LOG.debug("Service startcommand: " + action);
|
LOG.debug("Service startcommand: " + action);
|
||||||
|
|
||||||
if (!mStarted && !action.equals(ACTION_START)) {
|
if (!action.equals(ACTION_START) && !action.equals(ACTION_CONNECT)) {
|
||||||
|
if (!mStarted) {
|
||||||
// using the service before issuing ACTION_START
|
// using the service before issuing ACTION_START
|
||||||
LOG.info("Must start service with " + ACTION_START + " before using it: " + action);
|
LOG.info("Must start service with " + ACTION_START + " or " + ACTION_CONNECT + " before using it: " + action);
|
||||||
return START_NOT_STICKY;
|
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 (mDeviceSupport == null || (!isConnected() && !mDeviceSupport.useAutoConnect())) {
|
if (mDeviceSupport == null || (!isConnected() && !mDeviceSupport.useAutoConnect())) {
|
||||||
// trying to send notification without valid Bluetooth connection
|
// trying to send notification without valid Bluetooth connection
|
||||||
if (mGBDevice != null) {
|
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) {
|
switch (action) {
|
||||||
|
case ACTION_START:
|
||||||
|
start();
|
||||||
|
break;
|
||||||
case ACTION_CONNECT:
|
case ACTION_CONNECT:
|
||||||
|
start(); // ensure started
|
||||||
String btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
|
String btDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
|
||||||
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
if (btDeviceAddress == null) {
|
if (btDeviceAddress == null) {
|
||||||
|
@ -172,6 +173,13 @@ public class DeviceCommunicationService extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case ACTION_REQUEST_VERSIONINFO:
|
||||||
|
if (mGBDevice.getFirmwareVersion() == null) {
|
||||||
|
mDeviceSupport.onFirmwareVersionReq();
|
||||||
|
} else {
|
||||||
|
mGBDevice.sendDeviceUpdateIntent(this);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case ACTION_NOTIFICATION_GENERIC: {
|
case ACTION_NOTIFICATION_GENERIC: {
|
||||||
String title = intent.getStringExtra("notification_title");
|
String title = intent.getStringExtra("notification_title");
|
||||||
String body = intent.getStringExtra("notification_body");
|
String body = intent.getStringExtra("notification_body");
|
||||||
|
@ -229,13 +237,6 @@ public class DeviceCommunicationService extends Service {
|
||||||
String track = intent.getStringExtra("music_track");
|
String track = intent.getStringExtra("music_track");
|
||||||
mDeviceSupport.onSetMusicInfo(artist, album, track);
|
mDeviceSupport.onSetMusicInfo(artist, album, track);
|
||||||
break;
|
break;
|
||||||
case ACTION_REQUEST_VERSIONINFO:
|
|
||||||
if (mGBDevice != null && mGBDevice.getFirmwareVersion() == null) {
|
|
||||||
mDeviceSupport.onFirmwareVersionReq();
|
|
||||||
} else {
|
|
||||||
mGBDevice.sendDeviceUpdateIntent(this);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ACTION_REQUEST_APPINFO:
|
case ACTION_REQUEST_APPINFO:
|
||||||
mDeviceSupport.onAppInfoReq();
|
mDeviceSupport.onAppInfoReq();
|
||||||
break;
|
break;
|
||||||
|
@ -257,10 +258,6 @@ public class DeviceCommunicationService extends Service {
|
||||||
mDeviceSupport.onInstallApp(uri);
|
mDeviceSupport.onInstallApp(uri);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ACTION_START:
|
|
||||||
startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this));
|
|
||||||
mStarted = true;
|
|
||||||
break;
|
|
||||||
case ACTION_SET_ALARMS:
|
case ACTION_SET_ALARMS:
|
||||||
ArrayList<Alarm> alarms = intent.getParcelableArrayListExtra("alarms");
|
ArrayList<Alarm> alarms = intent.getParcelableArrayListExtra("alarms");
|
||||||
mDeviceSupport.onSetAlarms(alarms);
|
mDeviceSupport.onSetAlarms(alarms);
|
||||||
|
@ -270,6 +267,13 @@ public class DeviceCommunicationService extends Service {
|
||||||
return START_STICKY;
|
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() {
|
private boolean isConnected() {
|
||||||
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED;
|
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue