2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, Kasha, Steffen Liebergeld
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-08-03 23:51:53 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-06-06 23:24:00 +02:00
|
|
|
import android.net.Uri;
|
2016-03-03 14:23:17 +01:00
|
|
|
import android.util.Pair;
|
|
|
|
|
2016-10-28 00:32:45 +02:00
|
|
|
import org.json.JSONArray;
|
2016-03-03 14:23:17 +01:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2015-06-06 23:24:00 +02:00
|
|
|
|
2015-06-25 14:34:21 +02:00
|
|
|
import java.util.ArrayList;
|
2016-03-03 14:23:17 +01:00
|
|
|
import java.util.Iterator;
|
2017-10-10 00:05:43 +02:00
|
|
|
import java.util.List;
|
2016-03-03 14:23:17 +01:00
|
|
|
import java.util.UUID;
|
2015-06-25 14:34:21 +02:00
|
|
|
|
2017-01-09 14:41:02 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2017-02-05 16:37:59 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-09-30 23:27:19 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
|
2017-10-10 00:05:43 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AudioEffect;
|
2017-10-09 23:36:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AudioEffectType;
|
2016-04-03 18:30:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
2016-05-16 17:30:11 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-08 20:27:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2016-04-03 18:30:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
2016-12-31 15:56:05 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
2015-08-18 01:26:15 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.AbstractSerialDeviceSupport;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol;
|
2015-04-14 01:24:03 +02:00
|
|
|
|
2015-08-17 16:30:12 +02:00
|
|
|
public class PebbleSupport extends AbstractSerialDeviceSupport {
|
2015-04-14 01:24:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean connect() {
|
|
|
|
getDeviceIOThread().start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GBDeviceProtocol createDeviceProtocol() {
|
2016-05-16 23:00:04 +02:00
|
|
|
return new PebbleProtocol(getDevice());
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected GBDeviceIoThread createDeviceIOThread() {
|
2015-05-22 20:20:33 +02:00
|
|
|
return new PebbleIoThread(PebbleSupport.this, getDevice(), getDeviceProtocol(), getBluetoothAdapter(), getContext());
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|
2015-04-19 11:28:03 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean useAutoConnect() {
|
2016-04-03 18:30:20 +02:00
|
|
|
return true;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
2015-06-06 23:24:00 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onInstallApp(Uri uri) {
|
2017-09-30 23:27:19 +02:00
|
|
|
PebbleProtocol pebbleProtocol = (PebbleProtocol) getDeviceProtocol();
|
|
|
|
PebbleIoThread pebbleIoThread = getDeviceIOThread();
|
|
|
|
// catch fake urls first
|
|
|
|
if (uri.equals(Uri.parse("fake://health"))) {
|
|
|
|
getDeviceIOThread().write(pebbleProtocol.encodeActivateHealth(true));
|
|
|
|
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, getContext().getString(R.string.p_unit_metric));
|
|
|
|
if (units.equals(getContext().getString(R.string.p_unit_metric))) {
|
|
|
|
pebbleIoThread.write(pebbleProtocol.encodeSetSaneDistanceUnit(true));
|
|
|
|
} else {
|
|
|
|
pebbleIoThread.write(pebbleProtocol.encodeSetSaneDistanceUnit(false));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (uri.equals(Uri.parse("fake://hrm"))) {
|
|
|
|
getDeviceIOThread().write(pebbleProtocol.encodeActivateHRM(true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (uri.equals(Uri.parse("fake://weather"))) {
|
|
|
|
getDeviceIOThread().write(pebbleProtocol.encodeActivateWeather(true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is a real app
|
|
|
|
pebbleIoThread.installApp(uri, 0);
|
2015-06-06 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 14:23:17 +01:00
|
|
|
@Override
|
|
|
|
public void onAppConfiguration(UUID uuid, String config) {
|
|
|
|
try {
|
|
|
|
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
|
|
|
|
|
|
|
|
JSONObject json = new JSONObject(config);
|
|
|
|
Iterator<String> keysIterator = json.keys();
|
|
|
|
while (keysIterator.hasNext()) {
|
|
|
|
String keyStr = keysIterator.next();
|
|
|
|
Object object = json.get(keyStr);
|
2016-10-28 00:32:45 +02:00
|
|
|
if (object instanceof JSONArray) {
|
|
|
|
JSONArray jsonArray = (JSONArray) object;
|
|
|
|
byte[] byteArray = new byte[jsonArray.length()];
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
byteArray[i] = ((Integer) jsonArray.get(i)).byteValue();
|
|
|
|
}
|
|
|
|
object = byteArray;
|
2017-07-28 23:54:29 +02:00
|
|
|
} else if (object instanceof Boolean) {
|
|
|
|
object = (short) (((Boolean) object) ? 1 : 0);
|
2016-10-28 00:32:45 +02:00
|
|
|
}
|
2016-03-03 14:23:17 +01:00
|
|
|
pairs.add(new Pair<>(Integer.parseInt(keyStr), object));
|
|
|
|
}
|
|
|
|
getDeviceIOThread().write(((PebbleProtocol) getDeviceProtocol()).encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 14:38:56 +01:00
|
|
|
@Override
|
2016-02-29 22:05:29 +01:00
|
|
|
public void onHeartRateTest() {
|
2015-12-28 14:38:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-08 00:14:43 +02:00
|
|
|
@Override
|
2017-10-10 00:05:43 +02:00
|
|
|
public void onSetAudioProperty(AudioEffect effect) {
|
2017-10-08 00:14:43 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-20 20:28:52 +02:00
|
|
|
@Override
|
|
|
|
public void onSetConstantVibration(int intensity) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-06 23:24:00 +02:00
|
|
|
@Override
|
|
|
|
public synchronized PebbleIoThread getDeviceIOThread() {
|
|
|
|
return (PebbleIoThread) super.getDeviceIOThread();
|
|
|
|
}
|
2015-06-24 20:14:08 +02:00
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
private boolean reconnect() {
|
|
|
|
if (!isConnected() && useAutoConnect()) {
|
|
|
|
if (getDevice().getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
|
2016-05-22 01:19:28 +02:00
|
|
|
gbDeviceIOThread.quit();
|
2016-04-03 18:30:20 +02:00
|
|
|
gbDeviceIOThread.interrupt();
|
|
|
|
gbDeviceIOThread = null;
|
|
|
|
if (!connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2016-04-03 18:36:30 +02:00
|
|
|
Thread.sleep(4000); // this is about the time the connect takes, so the notification can come though
|
2016-04-03 18:30:20 +02:00
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
2017-02-05 16:37:59 +01:00
|
|
|
String currentPrivacyMode = GBApplication.getPrefs().getString("pebble_pref_privacy_mode", getContext().getString(R.string.p_pebble_privacy_mode_off));
|
|
|
|
if (getContext().getString(R.string.p_pebble_privacy_mode_complete).equals(currentPrivacyMode)) {
|
|
|
|
notificationSpec.body = null;
|
|
|
|
notificationSpec.sender = null;
|
|
|
|
notificationSpec.subject = null;
|
|
|
|
notificationSpec.title = null;
|
|
|
|
notificationSpec.phoneNumber = null;
|
|
|
|
} else if (getContext().getString(R.string.p_pebble_privacy_mode_content).equals(currentPrivacyMode)) {
|
2017-02-09 17:23:41 +01:00
|
|
|
if (notificationSpec.sender != null) {
|
2017-02-09 17:10:39 +01:00
|
|
|
notificationSpec.sender = "\n\n\n\n\n" + notificationSpec.sender;
|
|
|
|
} else if (notificationSpec.title != null) {
|
|
|
|
notificationSpec.title = "\n\n\n\n\n" + notificationSpec.title;
|
|
|
|
} else if (notificationSpec.subject != null) {
|
|
|
|
notificationSpec.subject = "\n\n\n\n\n" + notificationSpec.subject;
|
|
|
|
}
|
2017-02-05 16:37:59 +01:00
|
|
|
}
|
2016-04-03 18:30:20 +02:00
|
|
|
if (reconnect()) {
|
|
|
|
super.onNotification(notificationSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetCallState(CallSpec callSpec) {
|
2016-04-03 18:30:20 +02:00
|
|
|
if (reconnect()) {
|
2017-01-10 18:23:35 +01:00
|
|
|
if ((callSpec.command != CallSpec.CALL_OUTGOING) || GBApplication.getPrefs().getBoolean("pebble_enable_outgoing_call", true)) {
|
|
|
|
super.onSetCallState(callSpec);
|
2017-01-09 14:41:02 +01:00
|
|
|
}
|
2016-04-03 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 20:27:25 +02:00
|
|
|
@Override
|
|
|
|
public void onSetMusicState(MusicStateSpec musicStateSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onSetMusicState(musicStateSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 18:30:20 +02:00
|
|
|
@Override
|
2016-04-04 20:08:34 +02:00
|
|
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
2016-04-03 18:30:20 +02:00
|
|
|
if (reconnect()) {
|
2016-04-04 20:08:34 +02:00
|
|
|
super.onSetMusicInfo(musicSpec);
|
2016-04-03 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-24 20:14:08 +02:00
|
|
|
@Override
|
2015-08-21 00:58:18 +02:00
|
|
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
2015-06-24 20:14:08 +02:00
|
|
|
//nothing to do ATM
|
|
|
|
}
|
2016-05-16 17:30:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onAddCalendarEvent(calendarEventSpec);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 23:37:40 +02:00
|
|
|
|
|
|
|
@Override
|
2016-05-24 13:11:57 +02:00
|
|
|
public void onDeleteCalendarEvent(byte type, long id) {
|
2016-05-16 23:37:40 +02:00
|
|
|
if (reconnect()) {
|
|
|
|
super.onDeleteCalendarEvent(type, id);
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 23:06:59 +02:00
|
|
|
|
2016-11-13 20:47:24 +01:00
|
|
|
@Override
|
|
|
|
public void onSendConfiguration(String config) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onSendConfiguration(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 23:06:59 +02:00
|
|
|
@Override
|
|
|
|
public void onTestNewFunction() {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onTestNewFunction();
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 15:56:05 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSendWeather(WeatherSpec weatherSpec) {
|
|
|
|
if (reconnect()) {
|
|
|
|
super.onSendWeather(weatherSpec);
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|