-
Notifications
You must be signed in to change notification settings - Fork 37
/
SynchronizationService.java
411 lines (358 loc) · 15.4 KB
/
SynchronizationService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* AsteroidOSSync
* Copyright (c) 2023 AsteroidOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.asteroidos.sync.services;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import org.asteroidos.sync.MainActivity;
import org.asteroidos.sync.R;
import org.asteroidos.sync.asteroid.AsteroidBleManager;
import org.asteroidos.sync.asteroid.IAsteroidDevice;
import org.asteroidos.sync.connectivity.IConnectivityService;
import org.asteroidos.sync.connectivity.IService;
import org.asteroidos.sync.connectivity.IServiceCallback;
import org.asteroidos.sync.connectivity.MediaService;
import org.asteroidos.sync.connectivity.NotificationService;
import org.asteroidos.sync.connectivity.ScreenshotService;
import org.asteroidos.sync.connectivity.SilentModeService;
import org.asteroidos.sync.connectivity.TimeService;
import org.asteroidos.sync.connectivity.WeatherService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import no.nordicsemi.android.ble.observer.ConnectionObserver;
public class SynchronizationService extends Service implements IAsteroidDevice, ConnectionObserver {
public static final String TAG = SynchronizationService.class.toString();
public static final int MSG_CONNECT = 1;
public static final int MSG_DISCONNECT = 2;
public static final int MSG_SET_LOCAL_NAME = 3;
public static final int MSG_SET_STATUS = 4;
public static final int MSG_SET_BATTERY_PERCENTAGE = 5;
public static final int MSG_REQUEST_BATTERY_LIFE = 6;
public static final int MSG_SET_DEVICE = 7;
public static final int MSG_UPDATE = 8;
public static final int MSG_UNSET_DEVICE = 9;
private static final String NOTIFICATION_CHANNEL_ID = "synchronizationservice_channel_id_01";
final Messenger mMessenger = new Messenger(new SynchronizationHandler(this));
private final int NOTIFICATION = 2725;
public BluetoothDevice mDevice;
public int batteryPercentage = 0;
HashMap<UUID, IConnectivityService> bleServices;
List<IService> nonBleServices;
private NotificationManager mNM;
private ConnectionState mState = ConnectionState.STATUS_DISCONNECTED;
private Messenger replyTo;
private SharedPreferences mPrefs;
private AsteroidBleManager mBleMngr;
final void handleConnect() {
if (mBleMngr == null) {
mBleMngr = new AsteroidBleManager(getApplicationContext(), SynchronizationService.this);
mBleMngr.setConnectionObserver(this);
}
if (mState == ConnectionState.STATUS_CONNECTED || mState == ConnectionState.STATUS_CONNECTING) return;
mPrefs = getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
String defaultDevMacAddr = mPrefs.getString(MainActivity.PREFS_DEFAULT_MAC_ADDR, "");
if (defaultDevMacAddr.equals("")) return;
String defaultLocalName = mPrefs.getString(MainActivity.PREFS_DEFAULT_LOC_NAME, "");
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(defaultDevMacAddr);
try {
device.createBond();
mBleMngr.connect(device)
.useAutoConnect(true)
.timeout(100 * 1000)
.retry(3, 200)
.done(device1 -> {
Log.d(TAG, "Connected to " + device1.getName());
// Now we read the current values of the GATT characteristics,
// _after_ the connection has been fully established, to avoid
// connection failures on Android 12 and later.
mBleMngr.readCharacteristics();
})
.fail((device2, error) -> Log.e(TAG, "Failed to connect to " + device.getName() +
" with error code: " + error))
.enqueue();
} catch (SecurityException e) {
e.printStackTrace();
}
}
final void handleDisconnect() {
if (mBleMngr == null) return;
if (mState == ConnectionState.STATUS_DISCONNECTED) return;
bleServices.values().forEach(IService::unsync);
mBleMngr.abort();
mBleMngr.disconnect().enqueue();
}
final void handleSetDevice(BluetoothDevice device) {
SharedPreferences.Editor editor = mPrefs.edit();
Log.d(TAG, "handleSetDevice: " + device.toString());
editor.putString(MainActivity.PREFS_DEFAULT_MAC_ADDR, device.getAddress());
mDevice = device;
try {
String name = mDevice.getName();
Message answer = Message.obtain(null, MSG_SET_LOCAL_NAME);
answer.obj = name;
replyTo.send(answer);
replyTo.send(Message.obtain(null, MSG_SET_STATUS, mState));
} catch (RemoteException | SecurityException | NullPointerException ignored) {
}
editor.putString(MainActivity.PREFS_DEFAULT_LOC_NAME, name);
editor.apply();
}
final void handleUpdateConnectionStatus() {
if (mDevice != null) {
try {
replyTo.send(Message.obtain(null, MSG_SET_STATUS, mState));
} catch (RemoteException | NullPointerException ignored) {
}
}
}
final public void unsyncServices() {
bleServices.values().forEach(IService::unsync);
nonBleServices.forEach(IService::unsync);
}
final public void syncServices() {
bleServices.values().forEach(IService::sync);
nonBleServices.forEach(IService::sync);
}
@Override
public final ConnectionState getConnectionState() {
return mState;
}
@Override
public final void send(UUID characteristic, byte[] data, IConnectivityService service) {
mBleMngr.send(characteristic, data);
Log.d(TAG, characteristic.toString() + " " + Arrays.toString(data));
}
@Override
public final void registerBleService(IConnectivityService service) {
bleServices.put(service.getServiceUUID(), service);
Log.d(TAG, "BLE Service registered: " + service.getServiceUUID());
}
@Override
public final void unregisterBleService(UUID serviceUUID) {
bleServices.remove(getServiceByUUID(serviceUUID));
Log.d(TAG, "BLE Service unregistered: " + serviceUUID);
}
@Override
public final void registerCallback(UUID characteristicUUID, IServiceCallback callback) {
mBleMngr.recvCallbacks.putIfAbsent(characteristicUUID, callback);
}
@Override
public final void unregisterCallback(UUID characteristicUUID) {
mBleMngr.recvCallbacks.remove(characteristicUUID);
}
@Override
public final IConnectivityService getServiceByUUID(UUID uuid) {
return bleServices.get(uuid);
}
@Override
public final HashMap<UUID, IConnectivityService> getServices() {
return bleServices;
}
@Override
public final void onDeviceConnected(@NonNull BluetoothDevice device) {
mState = ConnectionState.STATUS_CONNECTED;
updateNotification();
}
@Override
public void onDeviceFailedToConnect(@NonNull BluetoothDevice device, int reason) {
try {
Log.d(TAG, "Failed to connect to " + device.getName() + ": " + reason);
} catch (SecurityException e) {
e.printStackTrace();
}
}
@Override
public final void onDeviceReady(@NonNull BluetoothDevice device) {
mState = ConnectionState.STATUS_CONNECTED;
updateNotification();
syncServices();
AsteroidBleManager.BatteryLevelEvent bevent = new AsteroidBleManager.BatteryLevelEvent();
bevent.battery = batteryPercentage;
handleUpdateBatteryPercentage(bevent);
}
@Override
public final void onDeviceDisconnecting(@NonNull BluetoothDevice device) {
mState = ConnectionState.STATUS_CONNECTED;
updateNotification();
}
@Override
public final void onDeviceDisconnected(@NonNull BluetoothDevice device, int reason) {
mState = ConnectionState.STATUS_DISCONNECTED;
updateNotification();
unsyncServices();
}
@Override
public void onCreate() {
bleServices = new HashMap<>();
nonBleServices = new ArrayList<>();
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Synchronization Service", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("Connection status");
notificationChannel.setVibrationPattern(new long[]{0L});
notificationChannel.setShowBadge(false);
mNM.createNotificationChannel(notificationChannel);
}
mPrefs = getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
String defaultDevMacAddr = mPrefs.getString(MainActivity.PREFS_DEFAULT_MAC_ADDR, "");
String defaultLocalName = mPrefs.getString(MainActivity.PREFS_DEFAULT_LOC_NAME, "");
if (mBleMngr == null) {
mBleMngr = new AsteroidBleManager(getApplicationContext(), SynchronizationService.this);
mBleMngr.setConnectionObserver(this);
}
if (!(defaultDevMacAddr.equals(""))) {
mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(defaultDevMacAddr);
}
if (nonBleServices.isEmpty())
nonBleServices.add(new SilentModeService(getApplicationContext()));
if (bleServices.isEmpty()) {
// Register Services
registerBleService(new MediaService(getApplicationContext(), this));
registerBleService(new NotificationService(getApplicationContext(), this));
registerBleService(new WeatherService(getApplicationContext(), this));
registerBleService(new ScreenshotService(getApplicationContext(), this));
registerBleService(new TimeService(getApplicationContext(), this));
}
handleConnect();
updateNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private void updateNotification() {
handleUpdateConnectionStatus();
String status = getString(R.string.disconnected);
if (mDevice != null) {
try {
if (mState == ConnectionState.STATUS_CONNECTING)
status = getString(R.string.connecting_formatted, mDevice.getName());
else if (mState == ConnectionState.STATUS_CONNECTED)
status = getString(R.string.connected_formatted, mDevice.getName());
} catch (SecurityException e) {
e.printStackTrace();
}
}
if (mDevice != null) {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(getText(R.string.app_name))
.setContentText(status)
.setContentIntent(contentIntent)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MIN)
.setShowWhen(false)
.build();
mNM.notify(NOTIFICATION, notification);
startForeground(NOTIFICATION, notification);
}
}
@Override
public void onDestroy() {
mBleMngr.disconnect();
mNM.cancel(NOTIFICATION);
}
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
@Override
public void onDeviceConnecting(@NonNull BluetoothDevice device) {
mState = ConnectionState.STATUS_CONNECTING;
updateNotification();
}
private void handleUnSetDevice() {
SharedPreferences.Editor editor = mPrefs.edit();
if (mState != ConnectionState.STATUS_DISCONNECTED) {
mBleMngr.disconnect().enqueue();
}
mDevice = null;
editor.putString(MainActivity.PREFS_DEFAULT_LOC_NAME, "");
editor.putString(MainActivity.PREFS_DEFAULT_MAC_ADDR, "");
editor.putString(MainActivity.PREFS_NAME, "");
editor.apply();
}
public void handleUpdateBatteryPercentage(AsteroidBleManager.BatteryLevelEvent battery) {
Log.d(TAG, "handleBattery: " + battery.battery + "%");
batteryPercentage = battery.battery;
try {
if (replyTo != null)
replyTo.send(Message.obtain(null, MSG_SET_BATTERY_PERCENTAGE, batteryPercentage, 0));
} catch (RemoteException e) {
e.printStackTrace();
}
}
static private class SynchronizationHandler extends Handler {
private final SynchronizationService mService;
SynchronizationHandler(SynchronizationService service) {
super(Looper.getMainLooper());
mService = service;
}
@Override
public void handleMessage(Message msg) {
mService.replyTo = msg.replyTo;
switch (msg.what) {
case MSG_CONNECT:
mService.handleConnect();
break;
case MSG_DISCONNECT:
mService.handleDisconnect();
break;
case MSG_REQUEST_BATTERY_LIFE:
AsteroidBleManager.BatteryLevelEvent batteryLevelEvent = new AsteroidBleManager.BatteryLevelEvent();
batteryLevelEvent.battery = mService.batteryPercentage;
mService.handleUpdateBatteryPercentage(batteryLevelEvent);
break;
case MSG_SET_DEVICE:
mService.handleSetDevice((BluetoothDevice) msg.obj);
break;
case MSG_UNSET_DEVICE:
mService.handleUnSetDevice();
break;
case MSG_UPDATE:
mService.handleUpdateConnectionStatus();
break;
default:
super.handleMessage(msg);
}
}
}
}