31
31
import java .util .concurrent .Executors ;
32
32
import java .util .concurrent .atomic .AtomicBoolean ;
33
33
34
- import static org .ostrya .presencepublisher .ui .ConnectionFragment .*;
35
- import static org .ostrya .presencepublisher .ui .ScheduleFragment .*;
34
+ import static org .ostrya .presencepublisher .ui .ScheduleFragment .SSID ;
36
35
import static org .ostrya .presencepublisher .ui .notification .NotificationFactory .getServiceNotification ;
37
36
import static org .ostrya .presencepublisher .ui .notification .NotificationFactory .updateServiceNotification ;
37
+ import static org .ostrya .presencepublisher .ui .preference .AutostartPreference .AUTOSTART ;
38
+ import static org .ostrya .presencepublisher .ui .preference .BatteryTopicPreference .BATTERY_TOPIC ;
39
+ import static org .ostrya .presencepublisher .ui .preference .ClientCertificatePreference .CLIENT_CERTIFICATE ;
40
+ import static org .ostrya .presencepublisher .ui .preference .HostPreference .HOST ;
41
+ import static org .ostrya .presencepublisher .ui .preference .LastSuccessTimestampPreference .LAST_SUCCESS ;
42
+ import static org .ostrya .presencepublisher .ui .preference .MessageSchedulePreference .MESSAGE_SCHEDULE ;
43
+ import static org .ostrya .presencepublisher .ui .preference .NextScheduleTimestampPreference .NEXT_SCHEDULE ;
44
+ import static org .ostrya .presencepublisher .ui .preference .OfflineContentPreference .OFFLINE_CONTENT ;
45
+ import static org .ostrya .presencepublisher .ui .preference .PasswordPreference .PASSWORD ;
46
+ import static org .ostrya .presencepublisher .ui .preference .PortPreference .PORT ;
47
+ import static org .ostrya .presencepublisher .ui .preference .PresenceTopicPreference .PRESENCE_TOPIC ;
48
+ import static org .ostrya .presencepublisher .ui .preference .SendBatteryMessagePreference .SEND_BATTERY_MESSAGE ;
49
+ import static org .ostrya .presencepublisher .ui .preference .SendOfflineMessagePreference .SEND_OFFLINE_MESSAGE ;
50
+ import static org .ostrya .presencepublisher .ui .preference .SendViaMobileNetworkPreference .SEND_VIA_MOBILE_NETWORK ;
51
+ import static org .ostrya .presencepublisher .ui .preference .SsidListPreference .SSID_LIST ;
52
+ import static org .ostrya .presencepublisher .ui .preference .UseTlsPreference .USE_TLS ;
53
+ import static org .ostrya .presencepublisher .ui .preference .UsernamePreference .USERNAME ;
54
+ import static org .ostrya .presencepublisher .ui .preference .WifiContentPreference .WIFI_CONTENT_PREFIX ;
38
55
39
56
public class ForegroundService extends Service {
40
57
public static final String ALARM_ACTION = "org.ostrya.presencepublisher.ALARM_ACTION" ;
@@ -51,9 +68,9 @@ public class ForegroundService extends Service {
51
68
private MqttService mqttService ;
52
69
private ConnectivityManager connectivityManager ;
53
70
private AlarmManager alarmManager ;
54
- private long lastPing ;
71
+ private long lastSuccess ;
55
72
private SharedPreferences sharedPreferences ;
56
- private long nextPing ;
73
+ private long nextSchedule ;
57
74
private WifiMessageProvider wifiMessageProvider ;
58
75
private BatteryMessageProvider batteryMessageProvider ;
59
76
private final OnSharedPreferenceChangeListener sharedPreferenceListener = this ::onSharedPreferenceChanged ;
@@ -107,8 +124,8 @@ public void onCreate() {
107
124
intent .setClass (getApplicationContext (), AlarmReceiver .class );
108
125
pendingIntent = PendingIntent .getBroadcast (getApplicationContext (), 0 , intent , 0 );
109
126
sharedPreferences = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
110
- lastPing = sharedPreferences .getLong (LAST_PING , 0L );
111
- nextPing = sharedPreferences .getLong (NEXT_PING , 0L );
127
+ lastSuccess = sharedPreferences .getLong (LAST_SUCCESS , 0L );
128
+ nextSchedule = sharedPreferences .getLong (NEXT_SCHEDULE , 0L );
112
129
wifiMessageProvider = new WifiMessageProvider (this );
113
130
batteryMessageProvider = new BatteryMessageProvider (this );
114
131
registerPreferenceCallback ();
@@ -156,24 +173,32 @@ private void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Stri
156
173
switch (key ) {
157
174
case HOST :
158
175
case PORT :
159
- case TLS :
160
- case CLIENT_CERT :
176
+ case USE_TLS :
177
+ case CLIENT_CERTIFICATE :
161
178
case PRESENCE_TOPIC :
162
- case PING :
163
- case LOGIN :
179
+ case MESSAGE_SCHEDULE :
180
+ case USERNAME :
164
181
case PASSWORD :
165
182
case SSID_LIST :
166
- case OFFLINE_PING :
167
- case MOBILE_NETWORK_PING :
183
+ case SEND_OFFLINE_MESSAGE :
184
+ case SEND_VIA_MOBILE_NETWORK :
185
+ case SEND_BATTERY_MESSAGE :
186
+ case BATTERY_TOPIC :
187
+ case OFFLINE_CONTENT :
168
188
HyperLog .i (TAG , "Changed parameter " + key );
169
189
start ();
170
190
break ;
171
191
case AUTOSTART :
172
- case LAST_PING :
173
- case NEXT_PING :
192
+ case LAST_SUCCESS :
193
+ case NEXT_SCHEDULE :
174
194
break ;
175
195
default :
176
- HyperLog .v (TAG , "Ignoring unexpected value " + key );
196
+ if (key .startsWith (WIFI_CONTENT_PREFIX )) {
197
+ HyperLog .i (TAG , "Changed parameter " + key );
198
+ start ();
199
+ } else {
200
+ HyperLog .v (TAG , "Ignoring unexpected value " + key );
201
+ }
177
202
}
178
203
}
179
204
@@ -192,17 +217,17 @@ private void start() {
192
217
} catch (RuntimeException e ) {
193
218
HyperLog .w (TAG , "Error while getting messages to send" , e );
194
219
}
195
- int ping = sharedPreferences .getInt (PING , 15 );
196
- nextPing = System .currentTimeMillis () + ping * 60_000L ;
197
- HyperLog .i (TAG , "Re-scheduling for " + new Date (nextPing ));
198
- sharedPreferences .edit ().putLong (NEXT_PING , nextPing ).apply ();
220
+ int ping = sharedPreferences .getInt (MESSAGE_SCHEDULE , 15 );
221
+ nextSchedule = System .currentTimeMillis () + ping * 60_000L ;
222
+ HyperLog .i (TAG , "Re-scheduling for " + new Date (nextSchedule ));
223
+ sharedPreferences .edit ().putLong (NEXT_SCHEDULE , nextSchedule ).apply ();
199
224
updateNotification ();
200
225
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
201
- alarmManager .setExactAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , nextPing , pendingIntent );
226
+ alarmManager .setExactAndAllowWhileIdle (AlarmManager .RTC_WAKEUP , nextSchedule , pendingIntent );
202
227
} else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
203
- alarmManager .setExact (AlarmManager .RTC_WAKEUP , nextPing , pendingIntent );
228
+ alarmManager .setExact (AlarmManager .RTC_WAKEUP , nextSchedule , pendingIntent );
204
229
} else {
205
- alarmManager .set (AlarmManager .RTC_WAKEUP , nextPing , pendingIntent );
230
+ alarmManager .set (AlarmManager .RTC_WAKEUP , nextSchedule , pendingIntent );
206
231
}
207
232
} finally {
208
233
currentlyRunning .set (false );
@@ -211,7 +236,7 @@ private void start() {
211
236
212
237
private void updateNotification () {
213
238
NotificationManagerCompat .from (this )
214
- .notify (NOTIFICATION_ID , updateServiceNotification (getApplicationContext (), lastPing , nextPing , CHANNEL_ID ));
239
+ .notify (NOTIFICATION_ID , updateServiceNotification (getApplicationContext (), lastSuccess , nextSchedule , CHANNEL_ID ));
215
240
}
216
241
217
242
@ Override
@@ -222,8 +247,8 @@ public IBinder onBind(final Intent intent) {
222
247
private void doSend (List <Message > messages ) {
223
248
try {
224
249
mqttService .sendMessages (messages );
225
- lastPing = System .currentTimeMillis ();
226
- sharedPreferences .edit ().putLong (LAST_PING , lastPing ).apply ();
250
+ lastSuccess = System .currentTimeMillis ();
251
+ sharedPreferences .edit ().putLong (LAST_SUCCESS , lastSuccess ).apply ();
227
252
updateNotification ();
228
253
} catch (Exception e ) {
229
254
HyperLog .w (TAG , "Error while sending messages" , e );
0 commit comments