-
Notifications
You must be signed in to change notification settings - Fork 154
/
AutoRecordingService.java
262 lines (217 loc) · 9.11 KB
/
AutoRecordingService.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
/**
* Copyright (C) 2013 - 2021 the enviroCar community
*
* This file is part of the enviroCar app.
*
* The enviroCar app 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.
*
* The enviroCar app 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 the enviroCar app. If not, see http://www.gnu.org/licenses/.
*/
package org.envirocar.app.services.autoconnect;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import com.squareup.otto.Subscribe;
import org.envirocar.app.BaseApplication;
import org.envirocar.app.handler.ApplicationSettings;
import org.envirocar.app.injection.ScopedBaseInjectorService;
import org.envirocar.app.notifications.NotificationHandler;
import org.envirocar.app.notifications.ServiceStateForNotification;
import org.envirocar.app.recording.RecordingService;
import org.envirocar.app.recording.RecordingType;
import org.envirocar.app.recording.events.RecordingStateEvent;
import org.envirocar.app.recording.events.RecordingTypeSelectedEvent;
import org.envirocar.app.rxutils.RxBroadcastReceiver;
import org.envirocar.core.logging.Logger;
import org.envirocar.core.utils.ServiceUtils;
import javax.inject.Inject;
import io.reactivex.disposables.CompositeDisposable;
import static org.envirocar.app.notifications.NotificationHandler.context;
/**
* @author dewall
*/
public class AutoRecordingService extends ScopedBaseInjectorService implements AutoRecordingStrategy.AutoRecordingCallback {
private static final Logger LOG = Logger.getLogger(AutoRecordingService.class);
public static final void startService(Context context) {
ServiceUtils.startService(context, AutoRecordingService.class);
}
public static final void stopService(Context context) {
ServiceUtils.stopService(context, AutoRecordingService.class);
}
private static final int REDISCOVERY_INTERVAL = 30;
// Static identifiers for actions for the broadcast receiver.
public static final String ACTION_START_BT_DISCOVERY = "action_start_bt_discovery";
public static final String ACTION_STOP_BT_DISCOVERY = "action_stop_bt_discvoery";
public static final String ACTION_START_TRACK_RECORDING = "action_start_track_recording";
public static final String ACTION_STOP_TRACK_RECORDING = "action_stop_track_recording";
// Injected variables
@Inject
protected AutoRecordingStrategy.Factory factory;
@Inject
@AutoRecordingScope
protected PowerManager.WakeLock wakeLock;
@Inject
protected AutoRecordingNotification notification;
private boolean isAutoConnectEnabled = ApplicationSettings.DEFAULT_BLUETOOTH_AUTOCONNECT;
private int mDiscoveryInterval = ApplicationSettings.DEFAULT_BLUETOOTH_DISCOVERY_INTERVAL;
private RecordingType recordingType = ApplicationSettings.DEFAULT_RECORDING_TYPE;
private CompositeDisposable disposables = new CompositeDisposable();
private AutoRecordingStrategy autoStrategy = null;
@Override
protected void setupServiceComponent() {
BaseApplication.get(this)
.getBaseApplicationComponent()
.plus(new AutoRecordingModule(this))
.inject(this);
}
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
return null;
}
@Override
public void onCreate() {
LOG.info("onCreate()");
super.onCreate();
// Register on the event bus.
this.bus.register(this);
// Get the required preference settings.
this.mDiscoveryInterval = ApplicationSettings.getDiscoveryInterval(context);
// Register a new BroadcastReceiver that waits for different incoming actions issued from
// the notification.
IntentFilter notificationClickedFilter = new IntentFilter();
notificationClickedFilter.addAction(ACTION_START_BT_DISCOVERY);
notificationClickedFilter.addAction(ACTION_STOP_BT_DISCOVERY);
notificationClickedFilter.addAction(ACTION_START_TRACK_RECORDING);
// Acquire wakelock to keep this service running.
wakeLock.acquire();
this.disposables.add(
RxBroadcastReceiver.create(this, notificationClickedFilter)
.subscribe(this::onReceiveNotificationIntent, LOG::error));
// init preference subscriptions.
this.disposables.add(
ApplicationSettings.getAutoconnectEnabledObservable(this)
.doOnNext(isAutoConnectEnabled -> {
this.isAutoConnectEnabled = isAutoConnectEnabled;
updateAutoRecording();
})
.doOnError(LOG::error)
.subscribe());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
LOG.info("onStartCommand()");
if (intent != null) {
this.updateAutoRecording();
}
return START_STICKY;
}
@Override
public void onDestroy() {
LOG.info("onDestroy()");
super.onDestroy();
this.bus.unregister(this);
if (!disposables.isDisposed()) {
disposables.dispose();
}
// release acquired wakelock
this.wakeLock.release();
this.notification.onDestroy();
}
@Override
public void onPreconditionUpdate(AutoRecordingStrategy.AutoRecordingState state) {
LOG.info("Changed auto recording state to %s", state.toString());
notification.setAutoRecordingState(recordingType, state);
}
@Override
public void onRecordingTypeConditionsMet() {
LOG.info("Conditions to start a track are satisfied. Starting the recording service");
if (this.autoStrategy != null) {
this.autoStrategy.stop();
}
this.startRecordingService();
}
@Subscribe
public void onRecordingTypeSelectedEvent(RecordingTypeSelectedEvent event) {
LOG.info("Received event [%]", event.toString());
if (this.recordingType != event.recordingType && !RecordingService.isRunning()) {
this.recordingType = event.recordingType;
this.updateAutoRecording();
} else {
this.recordingType = event.recordingType;
}
}
@Subscribe
public void onRecordingStateEvent(RecordingStateEvent event) {
LOG.info("Received event [%s]", event.toString());
switch (event.recordingState) {
case RECORDING_INIT:
case RECORDING_RUNNING:
if (this.autoStrategy != null) {
this.autoStrategy.stop();
}
break;
case RECORDING_STOPPED:
updateAutoRecording();
break;
}
}
/**
* Broadcast receiver that handles the different actions that could be issued by the
* corresponding notification of the notification bar.
*
* @param intent
*/
private void onReceiveNotificationIntent(Intent intent) {
String action = intent.getAction();
// Received action matches the command for starting the discovery process for the
// selected OBDII-Adapter.
if (ACTION_START_BT_DISCOVERY.equals(action)) {
LOG.info("Received Broadcast: Start Discovery.");
}
// Received action matches the command for stopping the Bluetooth discovery process.
else if (ACTION_STOP_BT_DISCOVERY.equals(action)) {
LOG.info("Received Broadcast: Stop Discovery.");
// Set the notification state to unconnected.
NotificationHandler.setRecordingState(ServiceStateForNotification.UNCONNECTED);
}
// Received action matches the command for starting the recording of a track.
else if (ACTION_START_TRACK_RECORDING.equals(action)) {
LOG.info("Received Broadcast: Start Track Recording.");
}
}
private void updateAutoRecording() {
if (this.autoStrategy != null) {
this.autoStrategy.stop();
}
if (!this.isAutoConnectEnabled) {
return;
}
if (this.autoStrategy == null) {
this.autoStrategy = this.factory.create();
}
this.autoStrategy.run(this);
}
/**
* Starts the GPSOnlyConnectionService if it is not already running. This also initiates the
* start of a new track.
*/
private void startRecordingService() {
if (!ServiceUtils.isServiceRunning(getApplicationContext(), RecordingService.class)) {
// Start the GPS Only Connection Service
ServiceUtils.startService(this, RecordingService.class);
}
}
}