2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2015-2017 Andreas Shimokawa, Avamander, Carsten Pfeiffer,
|
2017-04-26 00:14:25 +02:00
|
|
|
Daniele Gobbetti, Daniel Hauck, ivanovlev, João Paulo Barraca, Julien
|
|
|
|
Pivotto, Kasha, Sergey Trofimov, Steffen Liebergeld, Uwe Hermann
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
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:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service;
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2017-05-09 12:32:00 +02:00
|
|
|
import android.Manifest;
|
2017-04-21 22:21:24 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2015-02-06 13:55:44 +01:00
|
|
|
import android.app.NotificationManager;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.app.Service;
|
2016-05-22 01:19:28 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-04-19 14:34:18 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
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-04-19 14:34:18 +02:00
|
|
|
import android.content.IntentFilter;
|
2016-04-28 23:17:13 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-05-09 12:32:00 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2015-01-23 11:32:58 +01:00
|
|
|
import android.net.Uri;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.os.IBinder;
|
2015-08-23 00:54:28 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2017-05-09 12:32:00 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-04-19 15:11:03 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2015-01-12 00:35:15 +01:00
|
|
|
import android.widget.Toast;
|
2015-04-19 15:27:16 +02:00
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-06-25 14:34:21 +02:00
|
|
|
import java.util.ArrayList;
|
2015-12-14 23:31:31 +01:00
|
|
|
import java.util.Random;
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-12-14 23:31:31 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-04-19 21:51:23 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2017-02-07 23:49:10 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmClockReceiver;
|
2016-05-24 11:19:57 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.AlarmReceiver;
|
2016-05-22 01:19:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
|
2017-04-18 11:47:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothPairingRequestReceiver;
|
2017-04-24 12:39:40 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
|
2015-10-15 17:23:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
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;
|
2016-06-24 10:25:08 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-08 20:27:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
2016-12-31 15:56:05 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
2015-12-13 00:43:07 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-08-04 01:01:14 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2016-04-28 23:17:13 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-08-23 00:54:28 +02:00
|
|
|
|
2016-05-16 17:30:11 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ADD_CALENDAREVENT;
|
2016-03-03 14:23:17 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_APP_CONFIGURE;
|
2016-06-12 01:20:12 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_APP_REORDER;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CALLSTATE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_DELETEAPP;
|
2016-05-16 23:37:40 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_DELETE_CALENDAREVENT;
|
2017-01-09 16:33:00 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_DELETE_NOTIFICATION;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_DISCONNECT;
|
2016-04-03 22:38:06 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ENABLE_HEARTRATE_SLEEP_SUPPORT;
|
2016-04-12 23:12:15 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ENABLE_REALTIME_HEARTRATE_MEASUREMENT;
|
2015-09-02 08:00:26 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_ENABLE_REALTIME_STEPS;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_FETCH_ACTIVITY_DATA;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_FIND_DEVICE;
|
2015-12-28 14:38:56 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_HEARTRATE_TEST;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_INSTALL;
|
2015-09-24 14:45:21 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_NOTIFICATION;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REBOOT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_APPINFO;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_DEVICEINFO;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_REQUEST_SCREENSHOT;
|
2016-11-13 20:47:24 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SEND_CONFIGURATION;
|
2016-12-31 15:56:05 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SEND_WEATHER;
|
2016-06-24 10:25:08 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETCANNEDMESSAGES;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICINFO;
|
2016-06-08 20:27:25 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETMUSICSTATE;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SETTIME;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_ALARMS;
|
2016-09-20 20:28:52 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_CONSTANT_VIBRATION;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_START;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_STARTAPP;
|
2016-10-11 23:06:59 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_TEST_NEW_FUNCTION;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_ALARMS;
|
2016-03-03 14:23:17 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_CONFIG;
|
2015-09-13 21:44:26 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_START;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_APP_UUID;
|
2016-04-04 20:08:34 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_BOOLEAN_ENABLE;
|
2016-05-16 17:30:11 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_DESCRIPTION;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_DURATION;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_ID;
|
2017-04-21 10:50:10 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_LOCATION;
|
2016-05-16 17:30:11 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TIMESTAMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TITLE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TYPE;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_COMMAND;
|
2017-01-27 21:16:19 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_DISPLAYNAME;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_PHONENUMBER;
|
2016-06-24 10:25:08 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE;
|
2016-11-13 20:47:24 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CONFIG;
|
2017-04-16 19:37:43 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CONNECT_FIRST_TIME;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_FIND_START;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ALBUM;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_ARTIST;
|
2016-03-27 17:44:20 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_DURATION;
|
2016-06-08 20:27:25 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_POSITION;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_RATE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_REPEAT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_SHUFFLE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_STATE;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACK;
|
2016-03-27 17:44:20 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACKCOUNT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_MUSIC_TRACKNR;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_BODY;
|
2016-01-09 17:54:17 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_FLAGS;
|
2015-09-24 14:45:21 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_ID;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_PHONENUMBER;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SENDER;
|
2015-09-24 14:45:21 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SOURCENAME;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SUBJECT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TITLE;
|
2015-09-24 14:45:21 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE;
|
2015-08-23 00:54:28 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_URI;
|
2016-09-20 20:28:52 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_VIBRATION_INTENSITY;
|
2016-12-31 15:56:05 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITION;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTCONDITIONCODE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_CURRENTTEMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_LOCATION;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TIMESTAMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMAXTEMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TODAYMINTEMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWCONDITIONCODE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMAXTEMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_WEATHER_TOMORROWMINTEMP;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2016-04-28 23:17:13 +02:00
|
|
|
public class DeviceCommunicationService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2015-08-04 01:01:14 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class);
|
2017-04-21 22:21:24 +02:00
|
|
|
@SuppressLint("StaticFieldLeak") // only used for test cases
|
2016-09-10 10:52:02 +02:00
|
|
|
private static DeviceSupportFactory DEVICE_SUPPORT_FACTORY = null;
|
2015-02-06 13:55:44 +01:00
|
|
|
|
|
|
|
private boolean mStarted = false;
|
2015-03-22 23:38:51 +01:00
|
|
|
|
2015-08-23 00:54:28 +02:00
|
|
|
private DeviceSupportFactory mFactory;
|
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
|
|
|
|
2015-10-15 17:23:16 +02:00
|
|
|
private PhoneCallReceiver mPhoneCallReceiver = null;
|
|
|
|
private SMSReceiver mSMSReceiver = null;
|
|
|
|
private PebbleReceiver mPebbleReceiver = null;
|
|
|
|
private MusicPlaybackReceiver mMusicPlaybackReceiver = null;
|
|
|
|
private TimeChangeReceiver mTimeChangeReceiver = null;
|
2016-05-22 01:19:28 +02:00
|
|
|
private BluetoothConnectReceiver mBlueToothConnectReceiver = null;
|
2017-04-18 11:47:28 +02:00
|
|
|
private BluetoothPairingRequestReceiver mBlueToothPairingRequestReceiver = null;
|
2017-02-07 23:49:10 +01:00
|
|
|
private AlarmClockReceiver mAlarmClockReceiver = null;
|
2015-10-15 17:23:16 +02:00
|
|
|
|
2017-02-07 23:49:10 +01:00
|
|
|
private AlarmReceiver mAlarmReceiver = null;
|
2017-04-16 12:34:37 +02:00
|
|
|
private CalendarReceiver mCalendarReceiver = null;
|
2015-12-14 23:31:31 +01:00
|
|
|
private Random mRandom = new Random();
|
|
|
|
|
2017-02-11 09:49:01 +01:00
|
|
|
private final String[] mMusicActions = {
|
|
|
|
"com.android.music.metachanged",
|
|
|
|
"com.android.music.playstatechanged",
|
|
|
|
"com.android.music.queuechanged",
|
|
|
|
"com.android.music.playbackcomplete",
|
|
|
|
"net.sourceforge.subsonic.androidapp.EVENT_META_CHANGED",
|
|
|
|
"com.maxmpz.audioplayer.TPOS_SYNC",
|
2017-02-17 09:01:37 +01:00
|
|
|
"com.maxmpz.audioplayer.STATUS_CHANGED",
|
2017-09-01 23:47:03 +02:00
|
|
|
"com.maxmpz.audioplayer.PLAYING_MODE_CHANGED",
|
|
|
|
"com.spotify.music.metadatachanged",
|
|
|
|
"com.spotify.music.playbackstatechanged"
|
|
|
|
};
|
2017-02-11 09:49:01 +01:00
|
|
|
|
2016-09-10 10:52:02 +02:00
|
|
|
/**
|
|
|
|
* For testing!
|
|
|
|
*
|
|
|
|
* @param factory
|
|
|
|
*/
|
|
|
|
public static void setDeviceSupportFactory(DeviceSupportFactory factory) {
|
|
|
|
DEVICE_SUPPORT_FACTORY = factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeviceCommunicationService() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
2015-04-19 14:34:18 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
|
2015-07-10 21:35:28 +02:00
|
|
|
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
2017-04-22 23:56:04 +02:00
|
|
|
if (mGBDevice != null && mGBDevice.equals(device)) {
|
2015-04-20 11:58:59 +02:00
|
|
|
mGBDevice = device;
|
2015-08-07 13:24:54 +02:00
|
|
|
boolean enableReceivers = mDeviceSupport != null && (mDeviceSupport.useAutoConnect() || mGBDevice.isInitialized());
|
2017-04-19 21:51:23 +02:00
|
|
|
setReceiversEnableState(enableReceivers, mGBDevice.isInitialized(), DeviceHelper.getInstance().getCoordinator(device));
|
2016-04-15 22:56:37 +02:00
|
|
|
GB.updateNotification(mGBDevice.getName() + " " + mGBDevice.getStateString(), mGBDevice.isInitialized(), context);
|
2015-07-10 21:35:28 +02:00
|
|
|
} else {
|
2017-04-22 23:56:04 +02:00
|
|
|
LOG.error("Got ACTION_DEVICE_CHANGED from unexpected device: " + device);
|
2015-04-19 14:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-04-19 15:27:16 +02:00
|
|
|
|
2015-01-12 00:35:15 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2015-08-04 01:01:14 +02:00
|
|
|
LOG.debug("DeviceCommunicationService is being created");
|
2015-01-12 00:35:15 +01:00
|
|
|
super.onCreate();
|
2015-04-19 15:11:03 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
|
2016-09-10 10:52:02 +02:00
|
|
|
mFactory = getDeviceSupportFactory();
|
2016-04-28 23:17:13 +02:00
|
|
|
|
2016-04-29 21:49:17 +02:00
|
|
|
if (hasPrefs()) {
|
|
|
|
getPrefs().getPreferences().registerOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
|
|
|
|
2016-09-10 10:52:02 +02:00
|
|
|
private DeviceSupportFactory getDeviceSupportFactory() {
|
|
|
|
if (DEVICE_SUPPORT_FACTORY != null) {
|
|
|
|
return DEVICE_SUPPORT_FACTORY;
|
|
|
|
}
|
|
|
|
return new DeviceSupportFactory(this);
|
|
|
|
}
|
|
|
|
|
2015-01-12 00:35:15 +01:00
|
|
|
@Override
|
2015-10-22 00:52:45 +02:00
|
|
|
public synchronized 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) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("no intent");
|
2015-04-03 22:39:25 +02:00
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:55:44 +01:00
|
|
|
String action = intent.getAction();
|
2017-04-12 21:33:19 +02:00
|
|
|
boolean firstTime = intent.getBooleanExtra(EXTRA_CONNECT_FIRST_TIME, false);
|
2015-04-03 22:39:25 +02:00
|
|
|
|
2015-03-17 21:41:58 +01:00
|
|
|
if (action == null) {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.info("no action");
|
2015-03-17 21:41:58 +01:00
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.debug("Service startcommand: " + action);
|
2015-04-22 20:37:07 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
if (!action.equals(ACTION_START) && !action.equals(ACTION_CONNECT)) {
|
2015-08-06 23:17:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-07 22:07:48 +02:00
|
|
|
if (mDeviceSupport == null || (!isInitialized() && !mDeviceSupport.useAutoConnect())) {
|
2015-04-19 02:37:29 +02:00
|
|
|
// trying to send notification without valid Bluetooth connection
|
2015-04-24 23:08:47 +02:00
|
|
|
if (mGBDevice != null) {
|
|
|
|
// at least send back the current device state
|
|
|
|
mGBDevice.sendDeviceUpdateIntent(this);
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
2015-02-06 13:55:44 +01:00
|
|
|
}
|
2015-01-12 00:35:15 +01:00
|
|
|
|
2015-08-14 00:23:01 +02:00
|
|
|
// when we get past this, we should have valid mDeviceSupport and mGBDevice instances
|
2015-08-06 23:17:41 +02:00
|
|
|
|
2016-04-29 21:49:17 +02:00
|
|
|
Prefs prefs = getPrefs();
|
2015-05-08 11:18:06 +02:00
|
|
|
switch (action) {
|
2015-08-06 23:17:41 +02:00
|
|
|
case ACTION_START:
|
|
|
|
start();
|
|
|
|
break;
|
2015-05-08 11:18:06 +02:00
|
|
|
case ACTION_CONNECT:
|
2015-08-06 23:17:41 +02:00
|
|
|
start(); // ensure started
|
2015-12-13 00:43:07 +01:00
|
|
|
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
2016-03-20 19:48:54 +01:00
|
|
|
String btDeviceAddress = null;
|
2015-12-13 00:43:07 +01:00
|
|
|
if (gbDevice == null) {
|
2017-01-26 00:11:52 +01:00
|
|
|
if (prefs != null) { // may be null in test cases
|
2016-04-25 23:18:55 +02:00
|
|
|
btDeviceAddress = prefs.getString("last_device_address", null);
|
2017-01-26 00:11:52 +01:00
|
|
|
if (btDeviceAddress != null) {
|
|
|
|
gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
|
|
|
}
|
2015-08-22 01:08:46 +02:00
|
|
|
}
|
2016-03-20 19:48:54 +01:00
|
|
|
} else {
|
|
|
|
btDeviceAddress = gbDevice.getAddress();
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:17:13 +02:00
|
|
|
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
|
2016-04-29 21:49:17 +02:00
|
|
|
if (prefs != null && prefs.getPreferences() != null) {
|
2016-04-25 23:18:55 +02:00
|
|
|
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
|
2016-04-29 21:49:17 +02:00
|
|
|
autoReconnect = getGBPrefs().getAutoReconnect();
|
2015-08-06 02:17:38 +02:00
|
|
|
}
|
2015-05-08 11:18:06 +02:00
|
|
|
|
2015-12-13 00:43:07 +01:00
|
|
|
if (gbDevice != null && !isConnecting() && !isConnected()) {
|
2015-08-23 00:54:28 +02:00
|
|
|
setDeviceSupport(null);
|
2015-08-04 01:01:14 +02:00
|
|
|
try {
|
2015-12-13 00:43:07 +01:00
|
|
|
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
|
2015-08-23 00:54:28 +02:00
|
|
|
if (deviceSupport != null) {
|
|
|
|
setDeviceSupport(deviceSupport);
|
2017-04-12 21:33:19 +02:00
|
|
|
if (firstTime) {
|
|
|
|
deviceSupport.connectFirstTime();
|
2015-08-04 01:01:14 +02:00
|
|
|
} else {
|
2016-04-28 23:17:13 +02:00
|
|
|
deviceSupport.setAutoReconnect(autoReconnect);
|
2015-08-23 00:54:28 +02:00
|
|
|
deviceSupport.connect();
|
2015-04-14 01:24:03 +02:00
|
|
|
}
|
2015-10-18 08:34:51 +02:00
|
|
|
} else {
|
|
|
|
GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
|
2015-04-01 18:34:52 +02:00
|
|
|
}
|
2015-08-04 01:01:14 +02:00
|
|
|
} catch (Exception e) {
|
2015-10-18 23:43:27 +02:00
|
|
|
GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
|
2015-08-23 00:54:28 +02:00
|
|
|
setDeviceSupport(null);
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
2015-08-14 23:37:47 +02:00
|
|
|
} else if (mGBDevice != null) {
|
|
|
|
// send an update at least
|
|
|
|
mGBDevice.sendDeviceUpdateIntent(this);
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
2015-08-14 23:37:47 +02:00
|
|
|
case ACTION_REQUEST_DEVICEINFO:
|
2015-08-14 00:29:05 +02:00
|
|
|
mGBDevice.sendDeviceUpdateIntent(this);
|
2015-08-06 23:17:41 +02:00
|
|
|
break;
|
2015-09-24 14:45:21 +02:00
|
|
|
case ACTION_NOTIFICATION: {
|
|
|
|
NotificationSpec notificationSpec = new NotificationSpec();
|
2017-01-24 19:04:06 +01:00
|
|
|
notificationSpec.phoneNumber = intent.getStringExtra(EXTRA_NOTIFICATION_PHONENUMBER);
|
|
|
|
notificationSpec.sender = intent.getStringExtra(EXTRA_NOTIFICATION_SENDER);
|
|
|
|
notificationSpec.subject = intent.getStringExtra(EXTRA_NOTIFICATION_SUBJECT);
|
|
|
|
notificationSpec.title = intent.getStringExtra(EXTRA_NOTIFICATION_TITLE);
|
|
|
|
notificationSpec.body = intent.getStringExtra(EXTRA_NOTIFICATION_BODY);
|
|
|
|
notificationSpec.sourceName = intent.getStringExtra(EXTRA_NOTIFICATION_SOURCENAME);
|
2015-09-24 14:45:21 +02:00
|
|
|
notificationSpec.type = (NotificationType) intent.getSerializableExtra(EXTRA_NOTIFICATION_TYPE);
|
|
|
|
notificationSpec.id = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
|
2016-01-09 17:54:17 +01:00
|
|
|
notificationSpec.flags = intent.getIntExtra(EXTRA_NOTIFICATION_FLAGS, 0);
|
2017-01-17 21:07:12 +01:00
|
|
|
|
2016-10-11 11:54:52 +02:00
|
|
|
if (notificationSpec.type == NotificationType.GENERIC_SMS && notificationSpec.phoneNumber != null) {
|
2015-12-14 23:31:31 +01:00
|
|
|
notificationSpec.id = mRandom.nextInt(); // FIXME: add this in external SMS Receiver?
|
|
|
|
GBApplication.getIDSenderLookup().add(notificationSpec.id, notificationSpec.phoneNumber);
|
2016-01-09 17:54:17 +01:00
|
|
|
}
|
2017-01-17 21:07:12 +01:00
|
|
|
|
2016-01-09 17:54:17 +01:00
|
|
|
if (((notificationSpec.flags & NotificationSpec.FLAG_WEARABLE_REPLY) > 0)
|
2016-10-11 11:54:52 +02:00
|
|
|
|| (notificationSpec.type == NotificationType.GENERIC_SMS && notificationSpec.phoneNumber != null)) {
|
2015-12-13 12:03:57 +01:00
|
|
|
// NOTE: maybe not where it belongs
|
2016-04-25 23:18:55 +02:00
|
|
|
if (prefs.getBoolean("pebble_force_untested", false)) {
|
2015-12-13 12:03:57 +01:00
|
|
|
// I would rather like to save that as an array in ShadredPreferences
|
|
|
|
// this would work but I dont know how to do the same in the Settings Activity's xml
|
|
|
|
ArrayList<String> replies = new ArrayList<>();
|
2015-12-17 00:05:42 +01:00
|
|
|
for (int i = 1; i <= 16; i++) {
|
2016-04-25 23:18:55 +02:00
|
|
|
String reply = prefs.getString("canned_reply_" + i, null);
|
2015-12-13 12:03:57 +01:00
|
|
|
if (reply != null && !reply.equals("")) {
|
|
|
|
replies.add(reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notificationSpec.cannedReplies = replies.toArray(new String[replies.size()]);
|
|
|
|
}
|
2015-09-24 14:45:21 +02:00
|
|
|
}
|
2017-01-17 21:07:12 +01:00
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
mDeviceSupport.onNotification(notificationSpec);
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
2015-04-06 20:58:35 +02:00
|
|
|
}
|
2017-01-09 16:33:00 +01:00
|
|
|
case ACTION_DELETE_NOTIFICATION: {
|
|
|
|
mDeviceSupport.onDeleteNotification(intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1));
|
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:30:11 +02:00
|
|
|
case ACTION_ADD_CALENDAREVENT: {
|
|
|
|
CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
|
|
|
|
calendarEventSpec.id = intent.getLongExtra(EXTRA_CALENDAREVENT_ID, -1);
|
2016-05-24 13:11:57 +02:00
|
|
|
calendarEventSpec.type = intent.getByteExtra(EXTRA_CALENDAREVENT_TYPE, (byte) -1);
|
2016-05-16 17:30:11 +02:00
|
|
|
calendarEventSpec.timestamp = intent.getIntExtra(EXTRA_CALENDAREVENT_TIMESTAMP, -1);
|
|
|
|
calendarEventSpec.durationInSeconds = intent.getIntExtra(EXTRA_CALENDAREVENT_DURATION, -1);
|
2017-01-24 19:04:06 +01:00
|
|
|
calendarEventSpec.title = intent.getStringExtra(EXTRA_CALENDAREVENT_TITLE);
|
|
|
|
calendarEventSpec.description = intent.getStringExtra(EXTRA_CALENDAREVENT_DESCRIPTION);
|
2017-04-21 10:50:10 +02:00
|
|
|
calendarEventSpec.location = intent.getStringExtra(EXTRA_CALENDAREVENT_LOCATION);
|
2016-05-16 17:30:11 +02:00
|
|
|
mDeviceSupport.onAddCalendarEvent(calendarEventSpec);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-16 23:37:40 +02:00
|
|
|
case ACTION_DELETE_CALENDAREVENT: {
|
|
|
|
long id = intent.getLongExtra(EXTRA_CALENDAREVENT_ID, -1);
|
2016-05-24 13:11:57 +02:00
|
|
|
byte type = intent.getByteExtra(EXTRA_CALENDAREVENT_TYPE, (byte) -1);
|
2016-05-16 23:37:40 +02:00
|
|
|
mDeviceSupport.onDeleteCalendarEvent(type, id);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-17 21:58:08 +02:00
|
|
|
case ACTION_REBOOT: {
|
|
|
|
mDeviceSupport.onReboot();
|
|
|
|
break;
|
|
|
|
}
|
2015-12-28 14:38:56 +01:00
|
|
|
case ACTION_HEARTRATE_TEST: {
|
2016-02-29 22:05:29 +01:00
|
|
|
mDeviceSupport.onHeartRateTest();
|
2015-12-28 14:38:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-06-06 19:39:04 +02:00
|
|
|
case ACTION_FETCH_ACTIVITY_DATA: {
|
|
|
|
mDeviceSupport.onFetchActivityData();
|
|
|
|
break;
|
|
|
|
}
|
2015-06-07 15:31:42 +02:00
|
|
|
case ACTION_DISCONNECT: {
|
|
|
|
mDeviceSupport.dispose();
|
2016-05-22 01:19:28 +02:00
|
|
|
if (mGBDevice != null && mGBDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
|
2017-04-19 21:51:23 +02:00
|
|
|
setReceiversEnableState(false, false, null);
|
2016-05-22 01:19:28 +02:00
|
|
|
mGBDevice.setState(GBDevice.State.NOT_CONNECTED);
|
|
|
|
mGBDevice.sendDeviceUpdateIntent(this);
|
|
|
|
}
|
2015-06-07 15:31:42 +02:00
|
|
|
mDeviceSupport = null;
|
|
|
|
break;
|
|
|
|
}
|
2015-06-21 00:34:05 +02:00
|
|
|
case ACTION_FIND_DEVICE: {
|
2015-08-21 00:58:18 +02:00
|
|
|
boolean start = intent.getBooleanExtra(EXTRA_FIND_START, false);
|
2015-06-21 00:34:05 +02:00
|
|
|
mDeviceSupport.onFindDevice(start);
|
|
|
|
break;
|
2016-09-20 20:28:52 +02:00
|
|
|
}
|
|
|
|
case ACTION_SET_CONSTANT_VIBRATION: {
|
|
|
|
int intensity = intent.getIntExtra(EXTRA_VIBRATION_INTENSITY, 0);
|
|
|
|
mDeviceSupport.onSetConstantVibration(intensity);
|
|
|
|
break;
|
2015-06-21 00:34:05 +02:00
|
|
|
}
|
2015-05-08 11:18:06 +02:00
|
|
|
case ACTION_CALLSTATE:
|
2016-04-04 20:08:34 +02:00
|
|
|
CallSpec callSpec = new CallSpec();
|
2017-01-27 21:16:19 +01:00
|
|
|
callSpec.command = intent.getIntExtra(EXTRA_CALL_COMMAND, CallSpec.CALL_UNDEFINED);
|
|
|
|
callSpec.number = intent.getStringExtra(EXTRA_CALL_PHONENUMBER);
|
|
|
|
callSpec.name = intent.getStringExtra(EXTRA_CALL_DISPLAYNAME);
|
2016-04-04 20:08:34 +02:00
|
|
|
mDeviceSupport.onSetCallState(callSpec);
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
2016-06-24 10:25:08 +02:00
|
|
|
case ACTION_SETCANNEDMESSAGES:
|
|
|
|
int type = intent.getIntExtra(EXTRA_CANNEDMESSAGES_TYPE, -1);
|
|
|
|
String[] cannedMessages = intent.getStringArrayExtra(EXTRA_CANNEDMESSAGES);
|
|
|
|
|
|
|
|
CannedMessagesSpec cannedMessagesSpec = new CannedMessagesSpec();
|
|
|
|
cannedMessagesSpec.type = type;
|
|
|
|
cannedMessagesSpec.cannedMessages = cannedMessages;
|
|
|
|
mDeviceSupport.onSetCannedMessages(cannedMessagesSpec);
|
|
|
|
break;
|
2015-05-08 11:18:06 +02:00
|
|
|
case ACTION_SETTIME:
|
2015-08-21 00:58:18 +02:00
|
|
|
mDeviceSupport.onSetTime();
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
|
|
|
case ACTION_SETMUSICINFO:
|
2016-04-04 20:08:34 +02:00
|
|
|
MusicSpec musicSpec = new MusicSpec();
|
2017-01-24 19:04:06 +01:00
|
|
|
musicSpec.artist = intent.getStringExtra(EXTRA_MUSIC_ARTIST);
|
|
|
|
musicSpec.album = intent.getStringExtra(EXTRA_MUSIC_ALBUM);
|
|
|
|
musicSpec.track = intent.getStringExtra(EXTRA_MUSIC_TRACK);
|
2016-04-04 20:08:34 +02:00
|
|
|
musicSpec.duration = intent.getIntExtra(EXTRA_MUSIC_DURATION, 0);
|
|
|
|
musicSpec.trackCount = intent.getIntExtra(EXTRA_MUSIC_TRACKCOUNT, 0);
|
|
|
|
musicSpec.trackNr = intent.getIntExtra(EXTRA_MUSIC_TRACKNR, 0);
|
|
|
|
mDeviceSupport.onSetMusicInfo(musicSpec);
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
2016-06-08 20:27:25 +02:00
|
|
|
case ACTION_SETMUSICSTATE:
|
|
|
|
MusicStateSpec stateSpec = new MusicStateSpec();
|
2016-11-27 10:32:56 +01:00
|
|
|
stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte) 0);
|
|
|
|
stateSpec.repeat = intent.getByteExtra(EXTRA_MUSIC_REPEAT, (byte) 0);
|
2016-06-08 20:27:25 +02:00
|
|
|
stateSpec.position = intent.getIntExtra(EXTRA_MUSIC_POSITION, 0);
|
|
|
|
stateSpec.playRate = intent.getIntExtra(EXTRA_MUSIC_RATE, 0);
|
2016-11-27 10:32:56 +01:00
|
|
|
stateSpec.state = intent.getByteExtra(EXTRA_MUSIC_STATE, (byte) 0);
|
2016-06-08 20:27:25 +02:00
|
|
|
mDeviceSupport.onSetMusicState(stateSpec);
|
|
|
|
break;
|
2015-05-08 11:18:06 +02:00
|
|
|
case ACTION_REQUEST_APPINFO:
|
|
|
|
mDeviceSupport.onAppInfoReq();
|
|
|
|
break;
|
2015-06-24 00:23:38 +02:00
|
|
|
case ACTION_REQUEST_SCREENSHOT:
|
|
|
|
mDeviceSupport.onScreenshotReq();
|
|
|
|
break;
|
2015-08-21 00:58:18 +02:00
|
|
|
case ACTION_STARTAPP: {
|
|
|
|
UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
|
2015-09-13 21:44:26 +02:00
|
|
|
boolean start = intent.getBooleanExtra(EXTRA_APP_START, true);
|
|
|
|
mDeviceSupport.onAppStart(uuid, start);
|
2015-05-18 22:20:01 +02:00
|
|
|
break;
|
2015-08-21 00:58:18 +02:00
|
|
|
}
|
|
|
|
case ACTION_DELETEAPP: {
|
|
|
|
UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
|
2015-05-18 20:56:19 +02:00
|
|
|
mDeviceSupport.onAppDelete(uuid);
|
2015-05-08 11:18:06 +02:00
|
|
|
break;
|
2015-08-21 00:58:18 +02:00
|
|
|
}
|
2016-03-03 14:23:17 +01:00
|
|
|
case ACTION_APP_CONFIGURE: {
|
|
|
|
UUID uuid = (UUID) intent.getSerializableExtra(EXTRA_APP_UUID);
|
|
|
|
String config = intent.getStringExtra(EXTRA_APP_CONFIG);
|
|
|
|
mDeviceSupport.onAppConfiguration(uuid, config);
|
2016-06-12 01:20:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION_APP_REORDER: {
|
|
|
|
UUID[] uuids = (UUID[]) intent.getSerializableExtra(EXTRA_APP_UUID);
|
|
|
|
mDeviceSupport.onAppReorder(uuids);
|
|
|
|
break;
|
2016-03-03 14:23:17 +01:00
|
|
|
}
|
2015-07-23 12:09:01 +02:00
|
|
|
case ACTION_INSTALL:
|
2015-08-21 00:58:18 +02:00
|
|
|
Uri uri = intent.getParcelableExtra(EXTRA_URI);
|
2015-08-06 02:17:38 +02:00
|
|
|
if (uri != null) {
|
2015-07-23 12:09:01 +02:00
|
|
|
LOG.info("will try to install app/fw");
|
2015-08-06 02:17:38 +02:00
|
|
|
mDeviceSupport.onInstallApp(uri);
|
2015-05-08 11:18:06 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-06-24 20:14:08 +02:00
|
|
|
case ACTION_SET_ALARMS:
|
2015-08-21 00:58:18 +02:00
|
|
|
ArrayList<Alarm> alarms = intent.getParcelableArrayListExtra(EXTRA_ALARMS);
|
2015-06-25 14:34:21 +02:00
|
|
|
mDeviceSupport.onSetAlarms(alarms);
|
2015-06-25 12:42:47 +02:00
|
|
|
break;
|
2016-04-03 22:38:06 +02:00
|
|
|
case ACTION_ENABLE_REALTIME_STEPS: {
|
|
|
|
boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
|
2015-09-02 08:00:26 +02:00
|
|
|
mDeviceSupport.onEnableRealtimeSteps(enable);
|
|
|
|
break;
|
2016-04-03 22:38:06 +02:00
|
|
|
}
|
|
|
|
case ACTION_ENABLE_HEARTRATE_SLEEP_SUPPORT: {
|
|
|
|
boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
|
|
|
|
mDeviceSupport.onEnableHeartRateSleepSupport(enable);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-12 23:12:15 +02:00
|
|
|
case ACTION_ENABLE_REALTIME_HEARTRATE_MEASUREMENT: {
|
|
|
|
boolean enable = intent.getBooleanExtra(EXTRA_BOOLEAN_ENABLE, false);
|
|
|
|
mDeviceSupport.onEnableRealtimeHeartRateMeasurement(enable);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-13 20:47:24 +01:00
|
|
|
case ACTION_SEND_CONFIGURATION: {
|
|
|
|
String config = intent.getStringExtra(EXTRA_CONFIG);
|
|
|
|
mDeviceSupport.onSendConfiguration(config);
|
|
|
|
break;
|
|
|
|
}
|
2016-10-11 23:06:59 +02:00
|
|
|
case ACTION_TEST_NEW_FUNCTION: {
|
|
|
|
mDeviceSupport.onTestNewFunction();
|
|
|
|
break;
|
|
|
|
}
|
2016-12-31 15:56:05 +01:00
|
|
|
case ACTION_SEND_WEATHER: {
|
|
|
|
WeatherSpec weatherSpec = new WeatherSpec();
|
|
|
|
weatherSpec.timestamp = intent.getIntExtra(EXTRA_WEATHER_TIMESTAMP, 0);
|
|
|
|
weatherSpec.location = intent.getStringExtra(EXTRA_WEATHER_LOCATION);
|
|
|
|
weatherSpec.currentTemp = intent.getIntExtra(EXTRA_WEATHER_CURRENTTEMP, 0);
|
|
|
|
weatherSpec.currentConditionCode = intent.getIntExtra(EXTRA_WEATHER_CURRENTCONDITIONCODE, 0);
|
|
|
|
weatherSpec.currentCondition = intent.getStringExtra(EXTRA_WEATHER_CURRENTCONDITION);
|
|
|
|
weatherSpec.todayMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMAXTEMP, 0);
|
|
|
|
weatherSpec.todayMinTemp = intent.getIntExtra(EXTRA_WEATHER_TODAYMINTEMP, 0);
|
|
|
|
weatherSpec.tomorrowMaxTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMAXTEMP, 0);
|
|
|
|
weatherSpec.tomorrowMinTemp = intent.getIntExtra(EXTRA_WEATHER_TOMORROWMINTEMP, 0);
|
|
|
|
weatherSpec.tomorrowConditionCode = intent.getIntExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, 0);
|
|
|
|
mDeviceSupport.onSendWeather(weatherSpec);
|
|
|
|
break;
|
|
|
|
}
|
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-08-23 00:54:28 +02:00
|
|
|
/**
|
|
|
|
* Disposes the current DeviceSupport instance (if any) and sets a new device support instance
|
|
|
|
* (if not null).
|
2015-09-13 21:44:26 +02:00
|
|
|
*
|
2015-08-23 00:54:28 +02:00
|
|
|
* @param deviceSupport
|
|
|
|
*/
|
|
|
|
private void setDeviceSupport(@Nullable DeviceSupport deviceSupport) {
|
|
|
|
if (deviceSupport != mDeviceSupport && mDeviceSupport != null) {
|
|
|
|
mDeviceSupport.dispose();
|
|
|
|
mDeviceSupport = null;
|
|
|
|
mGBDevice = null;
|
|
|
|
}
|
|
|
|
mDeviceSupport = deviceSupport;
|
|
|
|
mGBDevice = mDeviceSupport != null ? mDeviceSupport.getDevice() : null;
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:17:41 +02:00
|
|
|
private void start() {
|
|
|
|
if (!mStarted) {
|
2016-04-15 22:56:37 +02:00
|
|
|
startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), false, this));
|
2015-08-06 23:17:41 +02:00
|
|
|
mStarted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:54:28 +02:00
|
|
|
public boolean isStarted() {
|
|
|
|
return mStarted;
|
|
|
|
}
|
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
private boolean isConnected() {
|
2015-08-07 13:24:54 +02:00
|
|
|
return mGBDevice != null && mGBDevice.isConnected();
|
2015-04-13 11:22:03 +02:00
|
|
|
}
|
2015-04-13 01:01:52 +02:00
|
|
|
|
2015-04-13 13:26:22 +02:00
|
|
|
private boolean isConnecting() {
|
2015-08-07 13:24:54 +02:00
|
|
|
return mGBDevice != null && mGBDevice.isConnecting();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isInitialized() {
|
|
|
|
return mGBDevice != null && mGBDevice.isInitialized();
|
2015-04-13 13:26:22 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:23:16 +02:00
|
|
|
|
2017-04-19 21:51:23 +02:00
|
|
|
private void setReceiversEnableState(boolean enable, boolean initialized, DeviceCoordinator coordinator) {
|
2015-10-15 17:23:16 +02:00
|
|
|
LOG.info("Setting broadcast receivers to: " + enable);
|
|
|
|
|
2017-04-19 21:51:23 +02:00
|
|
|
if (enable && initialized && coordinator != null && coordinator.supportsCalendarEvents()) {
|
2017-04-24 12:39:40 +02:00
|
|
|
if (mCalendarReceiver == null && getPrefs().getBoolean("enable_calendar_sync", true)) {
|
2017-05-09 12:32:00 +02:00
|
|
|
if (!(GBApplication.isRunningMarshmallowOrLater() && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_DENIED)) {
|
|
|
|
IntentFilter calendarIntentFilter = new IntentFilter();
|
|
|
|
calendarIntentFilter.addAction("android.intent.action.PROVIDER_CHANGED");
|
|
|
|
calendarIntentFilter.addDataScheme("content");
|
|
|
|
calendarIntentFilter.addDataAuthority("com.android.calendar", null);
|
|
|
|
mCalendarReceiver = new CalendarReceiver(mGBDevice);
|
|
|
|
registerReceiver(mCalendarReceiver, calendarIntentFilter);
|
|
|
|
}
|
2017-04-17 21:09:29 +02:00
|
|
|
}
|
2017-04-19 21:51:23 +02:00
|
|
|
if (mAlarmReceiver == null) {
|
|
|
|
mAlarmReceiver = new AlarmReceiver();
|
|
|
|
registerReceiver(mAlarmReceiver, new IntentFilter("DAILY_ALARM"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mCalendarReceiver != null) {
|
|
|
|
unregisterReceiver(mCalendarReceiver);
|
|
|
|
mCalendarReceiver = null;
|
|
|
|
}
|
|
|
|
if (mAlarmReceiver != null) {
|
|
|
|
unregisterReceiver(mAlarmReceiver);
|
|
|
|
mAlarmReceiver = null;
|
|
|
|
}
|
2017-04-17 21:09:29 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:23:16 +02:00
|
|
|
if (enable) {
|
|
|
|
if (mPhoneCallReceiver == null) {
|
|
|
|
mPhoneCallReceiver = new PhoneCallReceiver();
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction("android.intent.action.PHONE_STATE");
|
|
|
|
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
|
|
|
|
registerReceiver(mPhoneCallReceiver, filter);
|
|
|
|
}
|
|
|
|
if (mSMSReceiver == null) {
|
|
|
|
mSMSReceiver = new SMSReceiver();
|
|
|
|
registerReceiver(mSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
|
|
|
|
}
|
|
|
|
if (mPebbleReceiver == null) {
|
|
|
|
mPebbleReceiver = new PebbleReceiver();
|
|
|
|
registerReceiver(mPebbleReceiver, new IntentFilter("com.getpebble.action.SEND_NOTIFICATION"));
|
|
|
|
}
|
|
|
|
if (mMusicPlaybackReceiver == null) {
|
|
|
|
mMusicPlaybackReceiver = new MusicPlaybackReceiver();
|
2016-04-04 20:08:34 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
2017-02-11 09:49:01 +01:00
|
|
|
for (String action : mMusicActions){
|
|
|
|
filter.addAction(action);
|
|
|
|
}
|
2016-04-04 20:08:34 +02:00
|
|
|
registerReceiver(mMusicPlaybackReceiver, filter);
|
2015-10-15 17:23:16 +02:00
|
|
|
}
|
|
|
|
if (mTimeChangeReceiver == null) {
|
|
|
|
mTimeChangeReceiver = new TimeChangeReceiver();
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction("android.intent.action.TIME_SET");
|
|
|
|
filter.addAction("android.intent.action.TIMEZONE_CHANGED");
|
|
|
|
registerReceiver(mTimeChangeReceiver, filter);
|
|
|
|
}
|
2016-05-22 01:19:28 +02:00
|
|
|
if (mBlueToothConnectReceiver == null) {
|
|
|
|
mBlueToothConnectReceiver = new BluetoothConnectReceiver(this);
|
|
|
|
registerReceiver(mBlueToothConnectReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
|
|
|
|
}
|
2017-04-18 11:47:28 +02:00
|
|
|
if (mBlueToothPairingRequestReceiver == null) {
|
|
|
|
mBlueToothPairingRequestReceiver = new BluetoothPairingRequestReceiver(this);
|
|
|
|
registerReceiver(mBlueToothPairingRequestReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
|
|
|
|
}
|
2017-02-07 23:49:10 +01:00
|
|
|
if (mAlarmClockReceiver == null) {
|
|
|
|
mAlarmClockReceiver = new AlarmClockReceiver();
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(AlarmClockReceiver.ALARM_ALERT_ACTION);
|
|
|
|
filter.addAction(AlarmClockReceiver.ALARM_DONE_ACTION);
|
|
|
|
registerReceiver(mAlarmClockReceiver, filter);
|
|
|
|
}
|
2015-10-15 17:23:16 +02:00
|
|
|
} else {
|
|
|
|
if (mPhoneCallReceiver != null) {
|
|
|
|
unregisterReceiver(mPhoneCallReceiver);
|
|
|
|
mPhoneCallReceiver = null;
|
|
|
|
}
|
|
|
|
if (mSMSReceiver != null) {
|
|
|
|
unregisterReceiver(mSMSReceiver);
|
|
|
|
mSMSReceiver = null;
|
|
|
|
}
|
|
|
|
if (mPebbleReceiver != null) {
|
|
|
|
unregisterReceiver(mPebbleReceiver);
|
|
|
|
mPebbleReceiver = null;
|
|
|
|
}
|
|
|
|
if (mMusicPlaybackReceiver != null) {
|
|
|
|
unregisterReceiver(mMusicPlaybackReceiver);
|
|
|
|
mMusicPlaybackReceiver = null;
|
|
|
|
}
|
|
|
|
if (mTimeChangeReceiver != null) {
|
|
|
|
unregisterReceiver(mTimeChangeReceiver);
|
|
|
|
mTimeChangeReceiver = null;
|
|
|
|
}
|
2016-05-22 01:19:28 +02:00
|
|
|
if (mBlueToothConnectReceiver != null) {
|
|
|
|
unregisterReceiver(mBlueToothConnectReceiver);
|
|
|
|
mBlueToothConnectReceiver = null;
|
|
|
|
}
|
2017-04-18 11:47:28 +02:00
|
|
|
|
|
|
|
if (mBlueToothPairingRequestReceiver != null) {
|
|
|
|
unregisterReceiver(mBlueToothPairingRequestReceiver);
|
|
|
|
mBlueToothPairingRequestReceiver = null;
|
|
|
|
}
|
2017-02-07 23:49:10 +01:00
|
|
|
if (mAlarmClockReceiver != null) {
|
|
|
|
unregisterReceiver(mAlarmClockReceiver);
|
|
|
|
mAlarmClockReceiver = null;
|
|
|
|
}
|
2015-10-15 17:23:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 11:22:03 +02:00
|
|
|
@Override
|
2015-01-12 00:35:15 +01:00
|
|
|
public void onDestroy() {
|
2016-04-29 21:49:17 +02:00
|
|
|
if (hasPrefs()) {
|
|
|
|
getPrefs().getPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
}
|
2016-04-28 23:17:13 +02:00
|
|
|
|
2015-08-04 01:01:14 +02:00
|
|
|
LOG.debug("DeviceCommunicationService is being destroyed");
|
2015-01-12 00:35:15 +01:00
|
|
|
super.onDestroy();
|
2015-02-02 21:16:42 +01:00
|
|
|
|
2015-04-19 15:11:03 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
2017-04-19 21:51:23 +02:00
|
|
|
setReceiversEnableState(false, false, null); // disable BroadcastReceivers
|
2015-02-02 21:16:42 +01:00
|
|
|
|
2015-08-23 00:54:28 +02:00
|
|
|
setDeviceSupport(null);
|
2015-02-06 13:55:44 +01:00
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
2016-12-01 20:18:36 +01:00
|
|
|
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops
|
2015-01-12 00:35:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:17:13 +02:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
if (GBPrefs.AUTO_RECONNECT.equals(key)) {
|
2016-04-29 21:49:17 +02:00
|
|
|
boolean autoReconnect = getGBPrefs().getAutoReconnect();
|
2016-04-28 23:17:13 +02:00
|
|
|
if (mDeviceSupport != null) {
|
|
|
|
mDeviceSupport.setAutoReconnect(autoReconnect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 21:49:17 +02:00
|
|
|
|
|
|
|
protected boolean hasPrefs() {
|
|
|
|
return getPrefs().getPreferences() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Prefs getPrefs() {
|
|
|
|
return GBApplication.getPrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
public GBPrefs getGBPrefs() {
|
|
|
|
return GBApplication.getGBPrefs();
|
|
|
|
}
|
2016-05-22 01:19:28 +02:00
|
|
|
|
|
|
|
public GBDevice getGBDevice() {
|
|
|
|
return mGBDevice;
|
|
|
|
}
|
2015-01-30 11:59:36 +01:00
|
|
|
}
|