2015-01-12 00:35:15 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
import android.app.NotificationManager;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.app.Service;
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-01-23 11:32:58 +01:00
|
|
|
import android.content.ContentResolver;
|
2015-02-06 13:55:44 +01:00
|
|
|
import android.content.Context;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.content.Intent;
|
2015-03-21 18:18:07 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-01-23 11:32:58 +01:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.os.IBinder;
|
2015-03-21 18:18:07 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-01-23 11:32:58 +01:00
|
|
|
import android.provider.ContactsContract;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-04-14 10:29:09 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleIoThread;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.pebble.PebbleSupport;
|
|
|
|
|
2015-01-12 00:35:15 +01:00
|
|
|
public class BluetoothCommunicationService extends Service {
|
2015-01-18 01:10:44 +01:00
|
|
|
public static final String ACTION_START
|
2015-01-12 00:35:15 +01:00
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.start";
|
2015-02-06 13:55:44 +01:00
|
|
|
public static final String ACTION_CONNECT
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.connect";
|
2015-01-24 12:21:15 +01:00
|
|
|
public static final String ACTION_NOTIFICATION_GENERIC
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_generic";
|
|
|
|
public static final String ACTION_NOTIFICATION_SMS
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_sms";
|
2015-01-26 18:52:19 +01:00
|
|
|
public static final String ACTION_NOTIFICATION_EMAIL
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.notification_email";
|
2015-02-07 12:58:18 +01:00
|
|
|
public static final String ACTION_CALLSTATE
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.callstate";
|
2015-01-18 01:10:44 +01:00
|
|
|
public static final String ACTION_SETTIME
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.settime";
|
2015-02-08 23:53:40 +01:00
|
|
|
public static final String ACTION_SETMUSICINFO
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.setmusicinfo";
|
2015-03-22 00:34:54 +01:00
|
|
|
public static final String ACTION_REQUEST_VERSIONINFO
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.request_versioninfo";
|
2015-03-25 22:23:45 +01:00
|
|
|
public static final String ACTION_REQUEST_APPINFO
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.request_appinfo";
|
2015-03-26 18:11:47 +01:00
|
|
|
public static final String ACTION_DELETEAPP
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.deleteapp";
|
2015-04-06 20:58:35 +02:00
|
|
|
public static final String ACTION_INSTALL_PEBBLEAPP
|
|
|
|
= "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.install_pebbbleapp";
|
2015-03-25 22:23:45 +01:00
|
|
|
|
2015-04-06 20:58:35 +02:00
|
|
|
private static final String TAG = "CommunicationService";
|
2015-01-12 00:35:15 +01:00
|
|
|
private BluetoothAdapter mBtAdapter = null;
|
2015-04-01 19:11:18 +02:00
|
|
|
private GBDeviceIoThread mGBDeviceIoThread = null;
|
2015-02-06 13:55:44 +01:00
|
|
|
|
|
|
|
private boolean mStarted = false;
|
2015-03-22 23:38:51 +01:00
|
|
|
|
2015-04-01 18:34:52 +02:00
|
|
|
private GBDevice mGBDevice = null;
|
2015-04-14 01:24:03 +02:00
|
|
|
private DeviceSupport mDeviceSupport;
|
2015-01-12 00:35:15 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2015-01-26 18:52:19 +01:00
|
|
|
|
2015-04-03 22:39:25 +02:00
|
|
|
if (intent == null) {
|
|
|
|
Log.i(TAG, "no intent");
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
String action = intent.getAction();
|
2015-04-03 22:39:25 +02:00
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
if (action == null) {
|
|
|
|
Log.i(TAG, "no action");
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
if (!mStarted && !action.equals(ACTION_START)) {
|
|
|
|
// using the service before issuing ACTION_START
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
2015-01-30 11:59:36 +01:00
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
if (mStarted && action.equals(ACTION_START)) {
|
|
|
|
// using ACTION_START when the service has already been started
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
if (!action.equals(ACTION_START) && !action.equals(ACTION_CONNECT)) {
|
|
|
|
if (mDeviceSupport == null || (!isConnected() && !mDeviceSupport.useAutoConnect())) {
|
|
|
|
// trying to send notification without valid Bluetooth connection
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-03-22 12:46:28 +01:00
|
|
|
if (action.equals(ACTION_CONNECT)) {
|
2015-01-12 00:35:15 +01:00
|
|
|
//Check the system status
|
|
|
|
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
if (mBtAdapter == null) {
|
|
|
|
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
|
|
|
|
} else if (!mBtAdapter.isEnabled()) {
|
|
|
|
Toast.makeText(this, "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
2015-03-22 23:38:51 +01:00
|
|
|
String btDeviceAddress = intent.getStringExtra("device_address");
|
2015-03-21 18:18:07 +01:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-03-22 23:38:51 +01:00
|
|
|
sharedPrefs.edit().putString("last_device_address", btDeviceAddress).commit();
|
2015-03-21 18:18:07 +01:00
|
|
|
|
2015-04-13 13:26:22 +02:00
|
|
|
if (btDeviceAddress != null && !isConnected() && !isConnecting()) {
|
2015-04-14 01:39:00 +02:00
|
|
|
if (mDeviceSupport != null) {
|
|
|
|
mDeviceSupport.dispose();
|
2015-03-22 23:38:51 +01:00
|
|
|
}
|
|
|
|
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress);
|
|
|
|
if (btDevice != null) {
|
2015-04-01 23:15:08 +02:00
|
|
|
if (btDevice.getName() == null || btDevice.getName().equals("MI")) { //FIXME: workaround for Miband not being paired
|
2015-04-13 01:01:52 +02:00
|
|
|
mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), GBDevice.Type.MIBAND);
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport = new MiBandSupport();
|
2015-04-03 22:39:25 +02:00
|
|
|
} else if (btDevice.getName().indexOf("Pebble") == 0) {
|
2015-04-13 01:01:52 +02:00
|
|
|
mGBDevice = new GBDevice(btDeviceAddress, btDevice.getName(), GBDevice.Type.PEBBLE);
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport = new PebbleSupport();
|
2015-04-01 23:15:08 +02:00
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
if (mDeviceSupport != null) {
|
|
|
|
mDeviceSupport.initialize(mGBDevice, mBtAdapter, this);
|
|
|
|
mDeviceSupport.connect();
|
|
|
|
if (mDeviceSupport instanceof AbstractBTDeviceSupport) {
|
|
|
|
mGBDeviceIoThread = ((AbstractBTDeviceSupport) mDeviceSupport).getDeviceIOThread();
|
|
|
|
}
|
2015-04-01 18:34:52 +02:00
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_NOTIFICATION_GENERIC)) {
|
2015-01-12 00:35:15 +01:00
|
|
|
String title = intent.getStringExtra("notification_title");
|
2015-01-24 12:21:15 +01:00
|
|
|
String body = intent.getStringExtra("notification_body");
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onSMS(title, body);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_NOTIFICATION_SMS)) {
|
2015-01-24 12:21:15 +01:00
|
|
|
String sender = intent.getStringExtra("notification_sender");
|
|
|
|
String body = intent.getStringExtra("notification_body");
|
|
|
|
String senderName = getContactDisplayNameByNumber(sender);
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onSMS(senderName, body);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_NOTIFICATION_EMAIL)) {
|
2015-01-26 18:52:19 +01:00
|
|
|
String sender = intent.getStringExtra("notification_sender");
|
|
|
|
String subject = intent.getStringExtra("notification_subject");
|
|
|
|
String body = intent.getStringExtra("notification_body");
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onEmail(sender, subject, body);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_CALLSTATE)) {
|
2015-02-12 16:00:45 +01:00
|
|
|
GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY
|
2015-02-08 23:53:40 +01:00
|
|
|
String phoneNumber = intent.getStringExtra("call_phonenumber");
|
2015-02-07 12:58:18 +01:00
|
|
|
String callerName = null;
|
|
|
|
if (phoneNumber != null) {
|
|
|
|
callerName = getContactDisplayNameByNumber(phoneNumber);
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onSetCallState(phoneNumber, callerName, command);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_SETTIME)) {
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onSetTime(-1);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_SETMUSICINFO)) {
|
2015-02-08 23:53:40 +01:00
|
|
|
String artist = intent.getStringExtra("music_artist");
|
|
|
|
String album = intent.getStringExtra("music_album");
|
|
|
|
String track = intent.getStringExtra("music_track");
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onSetMusicInfo(artist, album, track);
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_REQUEST_VERSIONINFO)) {
|
2015-04-01 18:34:52 +02:00
|
|
|
if (mGBDevice != null && mGBDevice.getFirmwareVersion() == null) {
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onFirmwareVersionReq();
|
2015-03-22 23:38:51 +01:00
|
|
|
} else {
|
2015-04-13 01:01:52 +02:00
|
|
|
mGBDevice.sendDeviceUpdateIntent(this);
|
2015-03-22 23:38:51 +01:00
|
|
|
}
|
2015-03-25 22:23:45 +01:00
|
|
|
} else if (action.equals(ACTION_REQUEST_APPINFO)) {
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onAppInfoReq();
|
2015-03-26 18:11:47 +01:00
|
|
|
} else if (action.equals(ACTION_DELETEAPP)) {
|
|
|
|
int id = intent.getIntExtra("app_id", -1);
|
|
|
|
int index = intent.getIntExtra("app_index", -1);
|
2015-04-14 01:24:03 +02:00
|
|
|
mDeviceSupport.onAppDelete(id, index);
|
2015-04-06 20:58:35 +02:00
|
|
|
} else if (action.equals(ACTION_INSTALL_PEBBLEAPP)) {
|
|
|
|
String uriString = intent.getStringExtra("app_uri");
|
2015-04-07 23:57:12 +02:00
|
|
|
if (uriString != null) {
|
|
|
|
Log.i(TAG, "will try to install app");
|
|
|
|
((PebbleIoThread) mGBDeviceIoThread).installApp(Uri.parse(uriString));
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|
2015-03-22 12:46:28 +01:00
|
|
|
} else if (action.equals(ACTION_START)) {
|
2015-04-13 01:01:52 +02:00
|
|
|
startForeground(GB.NOTIFICATION_ID, GB.createNotification("Gadgetbridge running", this));
|
2015-02-06 13:55:44 +01:00
|
|
|
mStarted = true;
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
|
2015-01-12 00:35:15 +01:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
private boolean isConnected() {
|
2015-04-13 11:22:03 +02:00
|
|
|
return mGBDevice != null && mGBDevice.getState() == State.CONNECTED;
|
|
|
|
}
|
2015-04-13 01:01:52 +02:00
|
|
|
|
2015-04-13 13:26:22 +02:00
|
|
|
private boolean isConnecting() {
|
|
|
|
return mGBDevice != null && mGBDevice.getState() == State.CONNECTING;
|
|
|
|
}
|
|
|
|
|
2015-04-13 11:22:03 +02:00
|
|
|
@Override
|
2015-01-12 00:35:15 +01:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2015-02-02 21:16:42 +01:00
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
GB.setReceiversEnableState(false, this); // disable BroadcastReceivers
|
2015-02-02 21:16:42 +01:00
|
|
|
|
2015-04-14 01:39:00 +02:00
|
|
|
if (mDeviceSupport != null) {
|
|
|
|
mDeviceSupport.dispose();
|
2015-01-30 11:59:36 +01:00
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2015-04-13 01:01:52 +02:00
|
|
|
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification wont be cancelled when service stops
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-23 11:32:58 +01:00
|
|
|
|
|
|
|
private String getContactDisplayNameByNumber(String number) {
|
|
|
|
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
2015-01-31 11:49:46 +01:00
|
|
|
String name = number;
|
2015-01-23 11:32:58 +01:00
|
|
|
|
|
|
|
if (number == null || number.equals("")) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentResolver contentResolver = getContentResolver();
|
|
|
|
Cursor contactLookup = contentResolver.query(uri, null, null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (contactLookup != null && contactLookup.getCount() > 0) {
|
|
|
|
contactLookup.moveToNext();
|
|
|
|
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (contactLookup != null) {
|
|
|
|
contactLookup.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2015-01-30 11:59:36 +01:00
|
|
|
}
|