This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
/
MQTTService.java
1199 lines (998 loc) · 43.7 KB
/
MQTTService.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Teclib. All rights reserved.
*
* Flyve MDM is a mobile device management software.
*
* Flyve MDM 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.
*
* Flyve MDM 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.
* ------------------------------------------------------------------------------
* @author Rafael Hernandez
* @copyright Copyright Teclib. All rights reserved.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/flyve-mdm/android-mdm-agent
* @link https://flyve-mdm.com
* ------------------------------------------------------------------------------
*/
package org.flyve.mdm.agent.services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.flyve.mdm.agent.R;
import org.flyve.mdm.agent.data.AppData;
import org.flyve.mdm.agent.data.MqttData;
import org.flyve.mdm.agent.utils.FlyveLog;
import org.flyve.mdm.agent.utils.Helpers;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
import javax.net.ssl.SSLContext;
/**
* This is the service get and send message from MQTT
*/
public class MQTTService extends Service implements MqttCallback {
public static final String ACTION_START = "org.flyve.mdm.agent.ACTION_START";
public static final String ACTION_INVENTORY = "org.flyve.mdm.agent.ACTION_INVENTORY";
private static final String MQTT_LOGIN = "MQTT Login";
private static final String ERROR = "ERROR";
private static final String QUERY = "query";
private static final String TAG = "MQTT - %s";
private Timer reconnectionTimer = null;
private int reconnectionCounter = 0;
private int reconnectionPeriod = 60000; //time in milliseconds between successive task executions.
private int reconnectionDelay = 30000; //delay in milliseconds before task is to be executed.
private MqttAndroidClient client;
private Boolean connected = false;
private PoliciesController policiesController;
IBinder mBinder = new LocalBinder();
public static Intent start(Context context) {
MQTTService mMQTTService = new MQTTService();
Intent mServiceIntent = new Intent(context.getApplicationContext(), mMQTTService.getClass());
// Start the service
context.startService(mServiceIntent);
return mServiceIntent;
}
/**
* Constructor
*/
public MQTTService() {
FlyveLog.d("MQTT Service Constructor");
}
public class LocalBinder extends Binder {
public MQTTService getServerInstance() {
return MQTTService.this;
}
}
public void sendInventory() {
if(connected) {
policiesController.createInventory();
} else {
FlyveLog.i("Cannot sent the inventory the device is offline");
}
}
/**
* Return the communication channel to the service
* @param intent that was used to bind to this service
* @return IBinder null if clients cannot bind to the service
*/
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/**
* Called by the system every time a client explicitly starts the service by calling the method startService(Intent)
* https://developer.android.com/reference/android/app/Service.html#START_STICKY Documentation of the Constant
*
* @param intent supplied to start the service
* @param flags the additional data about this start request
* @param startId a unique integer representing this specific request to start
* @return constant START_STICKY
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
String action = "";
if (intent != null && intent.getAction() != null) {
action = intent.getAction();
}
FlyveLog.i(TAG, "Start MQTT Service: with parameter: " + action);
if(!connected) {
connect();
}
return START_STICKY;
}
/**
* Called by the system to notify a Service that it is no longer used and is being removed
* It calls the method from the parent
*/
@Override
public void onDestroy() {
super.onDestroy();
Helpers.deleteMQTTCache(getApplicationContext());
getApplicationContext().startService(new Intent(getApplicationContext(), MQTTService.class));
}
/**
* This function connect the agent with MQTT server
*/
public void connect() {
Context mContext = this.getApplicationContext();
MqttData cache = new MqttData(mContext);
final String mBroker = cache.getBroker();
final String mPort = cache.getPort();
final String mUser = cache.getMqttuser();
final String mPassword = cache.getMqttpasswd();
final String mTopic = cache.getTopic();
final String mTLS = cache.getTls();
if(mPassword==null) {
FlyveLog.d(TAG, "Password can't be null");
return;
}
storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Broker", mBroker));
storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Port", mPort));
storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "User", mUser));
storeLog(Helpers.broadCastMessage(MQTT_LOGIN, "Topic", mTopic));
FlyveLog.i("is TLS (0=false;1=true): %s", mTLS);
String protocol = "tcp";
// TLS is active change protocol
if(mTLS.equals("1")) {
protocol = "ssl";
}
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(mContext, protocol + "://" + mBroker + ":" + mPort, clientId);
client.setCallback( this );
try {
MqttConnectOptions options = new MqttConnectOptions();
options.setPassword(mPassword.toCharArray());
options.setUserName(mUser);
options.setCleanSession(true);
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
options.setConnectionTimeout(0);
options.setAutomaticReconnect(true);
// Create a testament to send when MQTT connection is down
String will = "{ online: false }";
options.setWill("/Status/Online", will.getBytes(), 0, false);
// If TLS is active needs ssl connection option
if(mTLS.equals("1")) {
// SSL
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
options.setSocketFactory(sslContext.getSocketFactory());
}
IMqttToken token = client.connect(options);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// We are connected
// Everything ready waiting for message
FlyveLog.d(TAG, "Success we are online!");
broadcastServiceStatus(true);
policiesController = new PoliciesController(getApplicationContext(), client);
// send status online true to MQTT
policiesController.sendOnlineStatus(true);
// main channel
String channel = mTopic + "/#";
FlyveLog.d(TAG, "MQTT Channel: " + channel);
policiesController.subscribe("#");
// subscribe to manifest
policiesController.subscribe("/FlyvemdmManifest/Status/Version");
// send inventory on connect
policiesController.createInventory();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable ex) {
// Something went wrong e.g. connection timeout or firewall problems
String errorMessage = "";
if(ex.getMessage().equalsIgnoreCase("MqttException")) {
errorMessage = ((MqttException)ex).toString();
} else {
errorMessage = ex.getMessage();
}
FlyveLog.e(TAG, "Connection fail: " + errorMessage);
String errorCode;
try {
errorCode = String.valueOf(((MqttException) ex).getReasonCode());
} catch (Exception exception) {
errorCode = "0";
}
storeLog(Helpers.broadCastMessage(ERROR, "Error on connect - client.connect", errorMessage));
broadcastMessage(Helpers.broadCastMessage(ERROR, errorCode, errorMessage));
broadcastServiceStatus(false);
}
});
}
catch (MqttException ex) {
FlyveLog.e(TAG, ex.getMessage());
broadcastMessage(Helpers.broadCastMessage(ERROR, String.valueOf(ex.getReasonCode()), ex.getMessage()));
storeLog(Helpers.broadCastMessage(ERROR, "Error on connect", ex.getMessage()));
} catch (Exception ex) {
FlyveLog.e(TAG, ex.getMessage());
broadcastMessage(Helpers.broadCastMessage(ERROR, "0", mContext.getResources().getString(R.string.MQTT_ERROR_CONNECTION)));
storeLog(Helpers.broadCastMessage(ERROR, "Error on connect", ex.getMessage()));
}
}
/**
* If connection fail trigger this function
* @param cause Throwable error
*/
@Override
public void connectionLost(Throwable cause) {
// send to backend that agent lost connection
broadcastServiceStatus(false);
storeLog(Helpers.broadCastMessage(ERROR, ERROR, cause.getMessage()));
FlyveLog.d(TAG, "Connection fail " + cause.getMessage());
}
public void reconnect() {
reconnectionTimer = new Timer();
// every 10 times the reconnection Period increase twice
if((reconnectionCounter % 10)==0) {
// stop increase in 5 minutes
if(reconnectionPeriod <= 300000) {
reconnectionPeriod *= 2;
}
}
reconnectionTimer.schedule(new TimerTask() {
@Override
public void run() {
if(!MQTTService.this.connected) {
reconnectionCounter++;
FlyveLog.d( "Reconnecting " + reconnectionCounter + " times");
connect();
} else {
FlyveLog.d("Reconnection finish");
reconnectionTimer.cancel();
reconnectionTimer = null;
}
}
}, reconnectionDelay, reconnectionPeriod);
}
/**
* If delivery of the message was complete
* @param token get message token
*/
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
FlyveLog.d(TAG, "deliveryComplete: " + token.toString());
storeLog(Helpers.broadCastMessage("MQTT Delivery", "Response id", String.valueOf(token.getMessageId())));
}
/**
* When a message from server arrive
* @param topic String topic where the message from
* @param message MqttMessage message content
* @throws Exception error
*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
FlyveLog.d(TAG, "Topic " + topic);
FlyveLog.d(TAG, "Message " + new String(message.getPayload()));
String messageBody = new String(message.getPayload());
storeLog(Helpers.broadCastMessage("MQTT Message", "Body", messageBody));
if(topic.isEmpty()) {
// exit if the topic if empty
return;
}
if(messageBody.isEmpty()) {
// exit if the message if empty
return;
}
// Command/Ping
if(topic.toLowerCase().contains("ping")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if (jsonObj.has(QUERY)
&& "Ping".equalsIgnoreCase(jsonObj.getString(QUERY))) {
policiesController.sendKeepAlive();
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Geolocate
if(topic.toLowerCase().contains("geolocate")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if (jsonObj.has(QUERY)
&& "Geolocate".equalsIgnoreCase(jsonObj.getString(QUERY))) {
policiesController.sendGPS();
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Inventory
if(topic.toLowerCase().contains("inventory")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if (jsonObj.has(QUERY)
&& "Inventory".equalsIgnoreCase(jsonObj.getString(QUERY))) {
policiesController.createInventory();
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Lock
if(topic.toLowerCase().contains("lock")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if (jsonObj.has("lock")) {
String lock = jsonObj.getString("lock");
policiesController.lockDevice(lock.equalsIgnoreCase("now"));
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Wipe
if(topic.toLowerCase().contains("wipe")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has("wipe") && "NOW".equalsIgnoreCase(jsonObj.getString("wipe")) ) {
policiesController.wipe();
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Unenroll
if(topic.toLowerCase().contains("unenroll")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has("unenroll") && "NOW".equalsIgnoreCase(jsonObj.getString("unenroll")) ) {
FlyveLog.d("unroll");
policiesController.unenroll();
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Command/Subscribe
if(topic.toLowerCase().contains("subscribe")) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has("subscribe")) {
JSONArray jsonTopics = jsonObj.getJSONArray("subscribe");
for(int i=0; i<jsonTopics.length();i++) {
JSONObject jsonTopic = jsonTopics.getJSONObject(i);
String channel = jsonTopic.getString("topic")+"/#";
FlyveLog.d(channel);
// Add new channel
policiesController.subscribe(channel);
}
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordEnabled
String PASSWORD_ENABLE = "passwordEnabled";
if(topic.toLowerCase().contains(PASSWORD_ENABLE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_ENABLE)) {
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordEnabled(taskId);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordEnabled
String PASSWORD_QUALITY = "passwordQuality";
if(topic.toLowerCase().contains(PASSWORD_QUALITY.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_QUALITY)) {
String quality = jsonObj.getString(PASSWORD_QUALITY);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordQuality(taskId, quality);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinLength
String PASSWORD_MIN_LENGTH = "passwordMinLength";
if(topic.toLowerCase().contains(PASSWORD_MIN_LENGTH.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_LENGTH)) {
int length = jsonObj.getInt(PASSWORD_MIN_LENGTH);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinLength(taskId, length);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinLowerCase
String PASSWORD_MIN_LOWERCASE = "passwordMinLowerCase";
if(topic.toLowerCase().contains(PASSWORD_MIN_LOWERCASE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_LOWERCASE)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_LOWERCASE);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinLowerCase(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinUpperCase
String PASSWORD_MIN_UPPERCASE = "passwordMinUpperCase";
if(topic.toLowerCase().contains(PASSWORD_MIN_UPPERCASE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_UPPERCASE)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_UPPERCASE);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinUpperCase(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinNonLetter
String PASSWORD_MIN_NON_LETTER = "passwordMinNonLetter";
if(topic.toLowerCase().contains(PASSWORD_MIN_NON_LETTER.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_NON_LETTER)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_NON_LETTER);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinNonLetter(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinLetters
String PASSWORD_MIN_LETTERS = "passwordMinLetters";
if(topic.toLowerCase().contains(PASSWORD_MIN_LETTERS.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_LETTERS)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_LETTERS);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinLetter(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinNumeric
String PASSWORD_MIN_NUMERIC = "passwordMinNumeric";
if(topic.toLowerCase().contains(PASSWORD_MIN_NUMERIC.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_NUMERIC)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_NUMERIC);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinNumeric(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/passwordMinSymbols
String PASSWORD_MIN_SYMBOLS = "passwordMinSymbols";
if(topic.toLowerCase().contains(PASSWORD_MIN_SYMBOLS.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(PASSWORD_MIN_SYMBOLS)) {
int minimum = jsonObj.getInt(PASSWORD_MIN_SYMBOLS);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.passwordMinSymbols(taskId, minimum);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/MaximumFailedPasswordsForWipe
String MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = "maximumFailedPasswordsForWipe";
if(topic.toLowerCase().contains(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE)) {
int max = jsonObj.getInt(MAXIMUM_FAILED_PASSWORDS_FOR_WIPE);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.maximumFailedPasswordsForWipe(taskId, max);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/MaximumTimeToLock
String MAXIMUM_TIME_TO_LOCK = "maximumTimeToLock";
if(topic.toLowerCase().contains(MAXIMUM_TIME_TO_LOCK.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(MAXIMUM_TIME_TO_LOCK)) {
int max = jsonObj.getInt(MAXIMUM_TIME_TO_LOCK);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.maximumTimeToLock(taskId, max);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/storageEncryption
String STORAGE_ENCRYPTION = "storageEncryption";
if(topic.toLowerCase().contains(STORAGE_ENCRYPTION.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(STORAGE_ENCRYPTION)) {
boolean enable = jsonObj.getBoolean(STORAGE_ENCRYPTION);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.storageEncryption(taskId, enable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableCamera
String DISABLE_CAMERA = "disableCamera";
if(topic.toLowerCase().contains(DISABLE_CAMERA.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_CAMERA)) {
Boolean disable = jsonObj.getBoolean(DISABLE_CAMERA);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.disableCamera(taskId, disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableBluetooth
String DISABLE_BLUETOOTH = "disableBluetooth";
if(topic.toLowerCase().contains(DISABLE_BLUETOOTH.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_BLUETOOTH)) {
Boolean disable = jsonObj.getBoolean(DISABLE_BLUETOOTH);
String taskId = jsonObj.getString("taskId");
// execute the policy
policiesController.disableBluetooth(taskId, disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/deployApp
String DEPLOY_APP = "deployApp";
if(topic.toLowerCase().contains(DEPLOY_APP.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DEPLOY_APP)) {
String deployApp = jsonObj.getString(DEPLOY_APP);
String id = jsonObj.getString("id");
String versionCode = jsonObj.getString("versionCode");
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.installPackage(deployApp, id, versionCode, taskId);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/deployApp
String REMOVE_APP = "removeApp";
if(topic.toLowerCase().contains(REMOVE_APP.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(REMOVE_APP)) {
String removeApp = jsonObj.getString(REMOVE_APP);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.removePackage(removeApp);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/deployFile
String DEPLOY_FILE = "deployFile";
if(topic.toLowerCase().contains(DEPLOY_FILE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DEPLOY_FILE)) {
String deployFile = jsonObj.getString(DEPLOY_FILE);
String id = jsonObj.getString("id");
String versionCode = jsonObj.getString("version");
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.downloadFile(deployFile, id, versionCode, taskId);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/deployFile
String REMOVE_FILE = "removeFile";
if(topic.toLowerCase().contains(REMOVE_FILE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(REMOVE_FILE)) {
String removeFile = jsonObj.getString(REMOVE_FILE);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.removeFile(removeFile);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableScreenCapture
// ROOT REQUIRED
String DISABLE_SCREEN_CAPTURE = "disableScreenCapture";
if(topic.toLowerCase().contains(DISABLE_SCREEN_CAPTURE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_SCREEN_CAPTURE)) {
Boolean disable = jsonObj.getBoolean(DISABLE_SCREEN_CAPTURE);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableScreenCapture(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableAirplaneMode
// ROOT REQUIRED
String DISABLE_AIRPLANE_MODE = "disableAirplaneMode";
if(topic.toLowerCase().contains(DISABLE_AIRPLANE_MODE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_AIRPLANE_MODE)) {
Boolean disable = jsonObj.getBoolean(DISABLE_AIRPLANE_MODE);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableAirplaneMode(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableGPS
// ROOT REQUIRED
String DISABLE_GPS = "disableGPS";
if(topic.toLowerCase().contains(DISABLE_GPS.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_GPS)) {
Boolean disable = jsonObj.getBoolean(DISABLE_GPS);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableGPS(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableHostpotTethering
String DISABLE_HOSTPOT_TETHERING = "disableHostpotTethering";
if(topic.toLowerCase().contains(DISABLE_HOSTPOT_TETHERING.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_HOSTPOT_TETHERING)) {
Boolean disable = jsonObj.getBoolean(DISABLE_HOSTPOT_TETHERING);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableHostpotTethering(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableRoaming
String DISABLE_ROAMING = "disableRoaming";
if(topic.toLowerCase().contains(DISABLE_ROAMING.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_ROAMING)) {
Boolean disable = jsonObj.getBoolean(DISABLE_ROAMING);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableRoaming(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableWifi
String DISABLE_WIFI = "disableWifi";
if(topic.toLowerCase().contains(DISABLE_WIFI.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_WIFI)) {
Boolean disable = jsonObj.getBoolean(DISABLE_WIFI);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableWifi(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/useTLS
String USE_TLS = "useTLS";
if(topic.toLowerCase().contains(USE_TLS.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(USE_TLS)) {
Boolean enable = jsonObj.getBoolean(USE_TLS);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.useTLS(enable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableMobileLine
// ROOT
String DISABLE_MOBILE_LINE = "disableMobileLine";
if(topic.toLowerCase().contains(DISABLE_MOBILE_LINE.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_MOBILE_LINE)) {
Boolean disable = jsonObj.getBoolean(DISABLE_MOBILE_LINE);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableMobileLine(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableNfc
// ROOT
String DISABLE_NFC = "disableNfc";
if(topic.toLowerCase().contains(DISABLE_NFC.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_NFC)) {
Boolean disable = jsonObj.getBoolean(DISABLE_NFC);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);
// execute the policy
policiesController.disableNFC(disable);
}
} catch (Exception ex) {
FlyveLog.e(ex.getMessage());
}
}
// Policy/disableStatusBar
// ROOT
String DISABLE_STATUS_BAR = "disableStatusBar";
if(topic.toLowerCase().contains(DISABLE_STATUS_BAR.toLowerCase())) {
try {
JSONObject jsonObj = new JSONObject(messageBody);
if(jsonObj.has(DISABLE_STATUS_BAR)) {
Boolean disable = jsonObj.getBoolean(DISABLE_STATUS_BAR);
String taskId = jsonObj.getString("taskId");
// return the status of the task
policiesController.sendTaskStatus(taskId, FEEDBACK_RECEIVED);