-
Notifications
You must be signed in to change notification settings - Fork 33
/
service.java
2171 lines (1834 loc) · 81.2 KB
/
service.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
package a2dp.Vol;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.UiModeManager;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.IBluetoothA2dp;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RemoteViews;
import android.widget.Toast;
import static a2dp.Vol.R.drawable.ic_launcher;
public class service extends Service implements OnAudioFocusChangeListener {
private static final String A2DP_BACKGROUND = "a2dp_background";
private static final String A2DP_FOREGROUND = "a2dp_foreground";
NotificationChannel channel_b, channel_f;
private boolean carEnterReceiverRegistered = false;
private boolean carExitReceiverRegistered = false;
private boolean homeExitRegistered = false;
private boolean homeEnterRegistered = false;
private boolean powerExitRegistered = false;
private boolean powerEnterRegistered = false;
private boolean headSetPlugReciverRegistered = false;
/*
* (non-Javadoc)
*
* @see android.app.Service#onStartCommand(android.content.Intent, int, int)
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
createNotificationChannel();
updateNot();
return START_STICKY;
}
private TextToSpeech mTts;
public static boolean mTtsReady = false;
static AudioManager am2 = (AudioManager) null;
private static Integer OldVol = 5;
private static Integer OldVol2 = 5;
private static Integer Oldsilent;
public static Integer connects = 0;
public static boolean run = false;
public static boolean ScoRegistered = false;
private static int mvolsLeft = 0;
private static int pvolsLeft = 0;
private static String notify_pref = "always";
public static btDevice[] btdConn = new btDevice[20]; // n the devices in the
// database that has
// connected
private DeviceDB DB; // database of device data stored in SQlite
static boolean mIsBound = false;
static IBluetoothA2dp ibta2;
static String DeviceToConnect = null;
private boolean carMode = true;
private boolean homeDock = false;
private boolean headsetPlug = false;
private boolean power = false;
private boolean enableGTalk = false;
private boolean enableSMS = false;
private static boolean ramp_vol = false;
HashMap<String, String> myHash;
private boolean toasts = true;
private boolean notify = true;
private static boolean hideVolUi = false;
private NotificationManager mNotificationManager = null;
private NotificationManagerCompat notificationManagerCompat = null;
private boolean speakerPhoneWasOn = true;
private boolean musicWasPlaying = false;
private boolean bluetoothWasOff = false;
private boolean clearedTts = true;
private static final String FIX_STREAM = "fix_stream";
AudioFocusRequest afr;
private String LastMessage;
boolean oldwifistate = true;
boolean tmessageRegistered = false; // tracks state of the message reader receiver
WifiManager wifiManager;
LocationManager locmanager;
String a2dpDir = "";
boolean local;
private static final String A2DP_Vol = "A2DP_Vol";
private static final String LOG_TAG = "A2DP_Volume";
// private static final String MY_UUID_STRING =
// "af87c0d0-faac-11de-a839-0800200c9a66";
private static final String OLD_VOLUME = "old_vol";
private static final String OLD_PH_VOL = "old_phone_vol";
private static final int MUSIC_STREAM = 0;
private static final int IN_CALL_STREAM = 1;
private static final int ALARM_STREAM = 2;
private PackageManager mPackageManager;
public static final String PREFS_NAME = "btVol";
private static final String CON_PREFS_NAME = "a2dp.Vol.ConnectWidget";
private static final String PREF_PREFIX_KEY = "appwidget_";
private int MAX_MESSAGE_LENGTH = 350;
float MAX_ACC = 10; // worst acceptable location in meters
long MAX_TIME = 20000; // gps listener timout time in milliseconds and
private long Message_delay = 3000; // delay before reading SMS
private int MessageStream = 0;
private long vol_delay = 5000; // delay time between the device connection
// and the volume adjustment
SharedPreferences preferences;
private static MyApplication application;
private volatile boolean connecting = false;
private volatile boolean disconnecting = false;
private int connectedIcon;
private TelephonyManager tm; // Context.getSystemService(Context.TELEPHONY_SERVICE);
// permissions status flags
Boolean permReadContacts = true;
Boolean permLocation = true;
Boolean permPhone = true;
Boolean permSMS = true;
Boolean permStorage = true;
/*
* private HandlerThread thread; private LinkedList<String> addresses;
* private TalkObserver observer;
*/
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
/* if ((channel_b == null)||(channel_f == null)) {
createNotificationChannel();
}*/
return null;
}
@Override
public void onCreate() {
application = (MyApplication) this.getApplication();
/*
if ((channel_b == null)||(channel_f == null)) {
createNotificationChannel();
}
*/
// get and load preferences
try {
preferences = PreferenceManager
.getDefaultSharedPreferences(application);
carMode = preferences.getBoolean("car_mode", true);
homeDock = preferences.getBoolean("home_dock", false);
headsetPlug = preferences.getBoolean("headset", false);
power = preferences.getBoolean("power", false);
toasts = preferences.getBoolean("toasts", true);
// notify = preferences.getBoolean("notify1", true);
enableSMS = preferences.getBoolean("enableTTS", false);
enableGTalk = preferences.getBoolean("enableGTalk", true);
notify_pref = preferences.getString("notify_pref", "always");
hideVolUi = preferences.getBoolean("hideVolUi", false);
// Long yyy = new Long(preferences.getString("gpsTime", "15000"));
MAX_TIME = Long.valueOf(preferences.getString("gpsTime", "15000"));
// Float xxx = new Float(preferences.getString("gpsDistance",
// "10"));
MAX_ACC = Float.valueOf(preferences.getString("gpsDistance", "10"));
local = preferences.getBoolean("useLocalStorage", false);
if (local)
a2dpDir = getFilesDir().toString();
else
a2dpDir = Environment.getExternalStorageDirectory()
+ "/A2DPVol";
OldVol2 = preferences.getInt(OLD_VOLUME, 10);
OldVol = preferences.getInt(OLD_PH_VOL, 5);
Oldsilent = preferences.getInt("oldsilent", 10);
} catch (NumberFormatException e) {
MAX_ACC = 10;
MAX_TIME = 15000;
Toast.makeText(this, "prefs failed to load ", Toast.LENGTH_LONG)
.show();
e.printStackTrace();
Log.e(LOG_TAG, "prefs failed to load " + e.getMessage());
}
registerRecievers();
// create audio manager instance
am2 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// open database instance
this.DB = new DeviceDB(application);
wifiManager = (WifiManager) getApplicationContext().getSystemService(
Context.WIFI_SERVICE);
locmanager = (LocationManager) getBaseContext().getSystemService(
Context.LOCATION_SERVICE);
// set run flag to true. This is used for the GUI
run = true;
// if all the above works, let the user know it is started
if (toasts)
Toast.makeText(this, R.string.ServiceStarted, Toast.LENGTH_LONG)
.show();
// Tell the world we are running
final String IRun = "a2dp.vol.service.RUNNING";
Intent i = new Intent();
i.setAction(IRun);
application.sendBroadcast(i);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mPackageManager = getPackageManager();
notify = notify_pref.equalsIgnoreCase("always")
|| notify_pref.equalsIgnoreCase("connected_only");
// we will ignore the no notifications preference in API 25 and up since this can be managed
// by the user now within Android
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
notify = true;
}
if (notify) {
updateNot();
}
getConnects();
if (connects < 1) {
getOldvol();
}
// test location file maker
/*
* FileOutputStream fos; try { fos = openFileOutput("My_Last_Location",
* Context.MODE_WORLD_READABLE); String temp =
* "http://maps.google.com/maps?q=40.7423612,-89.63056078333334+(Lambo 11/26/10, 05:59:46 pm acc=8)"
* ; fos.write(temp.getBytes()); fos.close(); } catch
* (FileNotFoundException e) { // TODO Auto-generated catch block
* e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); }
*/
// end test file maker
/*
* if (enableTTS) { mTts = new TextToSpeech(application,
* listenerStarted); }
*/
}
private void registerRecievers() {
// create intent filter for a bluetooth stream connection
IntentFilter filter = new IntentFilter(
android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
// create intent filter for a bluetooth stream disconnection
IntentFilter filter2 = new IntentFilter(
android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED);
IntentFilter btNotEnabled = new IntentFilter(
android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED);
application.registerReceiver(btOFFReciever, btNotEnabled);
IntentFilter clearMessage = new IntentFilter("a2dp.vol.service.CLEAR");
application.registerReceiver(messageClear, clearMessage);
IntentFilter updateNotification = new IntentFilter("a2dp.vol.service.NOTIFY");
application.registerReceiver(runUpdate, updateNotification);
application.registerReceiver(mReceiver, filter);
application.registerReceiver(mReceiver2, filter2);
if (carMode) {
// Create listener for when car mode disconnects
IntentFilter carExit = new IntentFilter(android.app.UiModeManager.ACTION_EXIT_CAR_MODE);
if (!carExitReceiverRegistered) {
application.registerReceiver(CarExitReceiver, carExit);
carExitReceiverRegistered = true;
Log.i(LOG_TAG, "Car Mode exit receiver registered");
}
// Create listener for when car mode connects
IntentFilter carEnter = new IntentFilter(android.app.UiModeManager.ACTION_ENTER_CAR_MODE);
if (!carEnterReceiverRegistered) {
application.registerReceiver(CarEnterReceiver, carEnter);
carEnterReceiverRegistered = true;
Log.i(LOG_TAG, "Car Mode enter receiver registered");
}
}
if (homeDock) {
// Create listener for when car mode disconnects
IntentFilter homeExit = new IntentFilter(android.app.UiModeManager.ACTION_EXIT_DESK_MODE);
if (!homeExitRegistered) {
application.registerReceiver(HomeExitReceiver, homeExit);
homeExitRegistered = true;
Log.i(LOG_TAG, "Home Dock exit receiver registered");
}
// Create listener for when car mode connects
IntentFilter homeEnter = new IntentFilter(android.app.UiModeManager.ACTION_ENTER_DESK_MODE);
if (!homeEnterRegistered) {
application.registerReceiver(HomeEnterReceiver, homeEnter);
homeEnterRegistered = true;
Log.i(LOG_TAG, "Home Dock enter receiver registered");
}
}
if (power) {
// Create listener for when power disconnects
IntentFilter powerExit = new IntentFilter(Intent.ACTION_POWER_DISCONNECTED);
if (!powerExitRegistered) {
application.registerReceiver(PowerExitReceiver, powerExit);
powerExitRegistered = true;
Log.i(LOG_TAG, "Power exit receiver registered");
}
// Create listener for when power connects
IntentFilter powerEnter = new IntentFilter(Intent.ACTION_POWER_CONNECTED);
if (!powerEnterRegistered) {
application.registerReceiver(PowerEnterReceiver, powerEnter);
powerEnterRegistered = true;
Log.i(LOG_TAG, "Power enter receiver registered");
}
}
if (headsetPlug) {
// create listener for headset plug
IntentFilter filter7 = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
if (!headSetPlugReciverRegistered) {
application.registerReceiver(headSetReceiver, filter7);
headSetPlugReciverRegistered = true;
Log.i(LOG_TAG, "Audio Jack receiver registered");
}
}
}
@Override
public void onDestroy() {
// let the GUI know we closed
mNotificationManager.cancelAll();
run = false;
Log.i(LOG_TAG, "Service stopped");
// in case the location listener is running, stop it
stopService(new Intent(application, StoreLoc.class));
// close the database
try {
application.unregisterReceiver(mReceiver);
application.unregisterReceiver(mReceiver2);
application.unregisterReceiver(btOFFReciever);
application.unregisterReceiver(runUpdate);
if (headSetPlugReciverRegistered) {
application.unregisterReceiver(headSetReceiver);
headSetPlugReciverRegistered = false;
Log.i(LOG_TAG, "Audio Jack receiver unregistered");
}
if (carEnterReceiverRegistered) {
application.unregisterReceiver(CarEnterReceiver);
carEnterReceiverRegistered = false;
Log.i(LOG_TAG, "Car Enter receiver unregistered");
}
if (carExitReceiverRegistered) {
application.unregisterReceiver(CarExitReceiver);
carExitReceiverRegistered = false;
Log.i(LOG_TAG, "Car Exit receiver unregistered");
}
if (homeEnterRegistered) {
application.unregisterReceiver(HomeEnterReceiver);
homeEnterRegistered = false;
Log.i(LOG_TAG, "Home enter receiver unregistered");
}
if (homeExitRegistered) {
application.unregisterReceiver(HomeExitReceiver);
homeExitRegistered = false;
Log.i(LOG_TAG, "Home exit receiver unregistered");
}
if (powerExitRegistered) {
application.unregisterReceiver(PowerExitReceiver);
powerExitRegistered = false;
Log.i(LOG_TAG, "Power exit receiver unregistered");
}
if (powerEnterRegistered) {
application.unregisterReceiver(PowerEnterReceiver);
powerEnterRegistered = false;
Log.i(LOG_TAG, "Power enter receiver unregistered");
}
if (mTtsReady) {
try {
if (!clearedTts) {
clearTts();
}
if (mTts != null)
mTts.shutdown();
mTtsReady = false;
if (ScoRegistered) {
application.unregisterReceiver(sco_change);
ScoRegistered = false;
}
if (tmessageRegistered) {
application.unregisterReceiver(tmessage);
tmessageRegistered = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
DB.getDb().close();
application.unregisterReceiver(messageClear);
} catch (Exception e) {
e.printStackTrace();
}
// Tell the world we are not running
final String IStop = "a2dp.vol.service.STOPPED_RUNNING";
Intent i = new Intent();
i.setAction(IStop);
application.sendBroadcast(i);
// let the user know the service stopped
if (toasts)
Toast.makeText(this, R.string.ServiceStopped, Toast.LENGTH_LONG)
.show();
if (mIsBound) {
doUnbind();
}
this.stopForeground(true);
}
public void onStart() {
run = true;
connecting = false;
disconnecting = false;
if (notify)
updateNot();
getConnects();
if (connects < 1) {
getOldvol();
}
Log.i(LOG_TAG, "Service started");
}
private final BroadcastReceiver tmessage = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextReader(intent.getStringExtra("message"));
}
};
private final BroadcastReceiver messageClear = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
clearTts();
}
};
private final BroadcastReceiver runUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateNot();
}
};
// used to clear all the Bluetooth connections if the Bluetooth adapter has
// been turned OFF.
private final BroadcastReceiver btOFFReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state1 = android.bluetooth.BluetoothAdapter.STATE_OFF;
int state2 = android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF;
BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter();
String mac = "";
if (mBTA.getState() == state1) {
if (btdConn != null)
for (int j = 0; j < btdConn.length; j++) {
if (btdConn[j] != null)
if (btdConn[j].getMac().length() > 2) {
mac = btdConn[j].getMac();
DoDisconnected(btdConn[j]);
btdConn[j] = null;
}
}
getConnects();
if (mTtsReady) {
try {
if (!clearedTts) {
clearTts();
}
//mTts.shutdown();
//mTtsReady = false;
//unregisterReceiver(SMScatcher);
} catch (Exception e) {
e.printStackTrace();
}
}
String Ireload = "a2dp.Vol.main.RELOAD_LIST";
Intent itent = new Intent();
itent.setAction(Ireload);
itent.putExtra("disconnect", mac);
application.sendBroadcast(itent);
Log.i(LOG_TAG, "Bluetooth turned OFF broadcast receiver " + mac);
//updateA2DPconnect2();
}
}
};
private final BroadcastReceiver headSetReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
int state = intent.getIntExtra("state", -1);
btDevice bt2 = null;
try {
bt2 = DB.getBTD("3"); // get headset plug data
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "Error receiving audio jack data from databse " + e.getLocalizedMessage());
return;
}
if (bt2 != null && "3".equalsIgnoreCase(bt2.getMac())) {
if (state == 0 && connects > 0) {
disconnecting = true;
DoDisconnected(bt2);
} else if (state == 1) {
connecting = true;
DoConnected(bt2);
}
}
Log.i(LOG_TAG, "Audio jack received " + state);
}
};
// a device has just connected. Do the on connect stuff
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
/*
* (non-Javadoc)
*
* @see
* android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
if (!connecting) {
connecting = true;
BluetoothDevice bt;
try {
bt = (BluetoothDevice) Objects.requireNonNull(intent.getExtras()).get(
BluetoothDevice.EXTRA_DEVICE);
} catch (Exception e1) {
bt = null;
e1.printStackTrace();
connecting = false;
//return;
}
btDevice bt2 = null;
// first see if a bluetooth device connected
if (bt != null) {
try {
String addres = bt.getAddress();
bt2 = DB.getBTD(addres);
} catch (Exception e) {
bt2 = null;
connecting = false;
//return;
}
} else
// if not a bluetooth device, must be a special device
{
try {
// Log.d(LOG_TAG, intent.toString());
if (Objects.requireNonNull(intent.getAction()).equalsIgnoreCase(
"android.app.action.ENTER_CAR_MODE")) {
bt2 = DB.getBTD("1"); // get car mode data
} else if (intent.getAction().equalsIgnoreCase(
"android.app.action.ENTER_DESK_MODE")) {
bt2 = DB.getBTD("2"); // get home dock data
} else if (intent.getAction().equalsIgnoreCase(
Intent.ACTION_POWER_CONNECTED)) {
bt2 = DB.getBTD("4"); // get power data
} else
bt2 = null;
} catch (Exception e) {
bt2 = null;
Log.e(LOG_TAG, "Error" + e.toString());
}
}
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
connecting = false;
Log.i(LOG_TAG, "Unknown device received, ignoring");
} else {
Log.i(LOG_TAG, "Broadcast received: " + bt2.getDesc1() + ", " + bt2.getDesc2());
DoConnected(bt2);
}
}
//updateA2DPconnect2();
}
};
private final BroadcastReceiver CarEnterReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!connecting) {
connecting = true;
btDevice bt2 = DB.getBTD("1");
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
connecting = false;
Log.i(LOG_TAG, "Unknown device received, ignoring");
} else {
Log.i(LOG_TAG, "Broadcast received: " + bt2.getDesc1() + ", " + bt2.getDesc2());
DoConnected(bt2);
}
}
}
};
private final BroadcastReceiver HomeEnterReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!connecting) {
connecting = true;
btDevice bt2 = DB.getBTD("2");
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
connecting = false;
Log.i(LOG_TAG, "Unknown device received, ignoring");
} else {
Log.i(LOG_TAG, "Broadcast received: " + bt2.getDesc1() + ", " + bt2.getDesc2());
DoConnected(bt2);
}
}
}
};
private final BroadcastReceiver PowerEnterReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!connecting) {
connecting = true;
btDevice bt2 = DB.getBTD("4");
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
connecting = false;
Log.i(LOG_TAG, "Unknown device received, ignoring");
} else {
Log.i(LOG_TAG, "Broadcast received: " + bt2.getDesc1() + ", " + bt2.getDesc2());
DoConnected(bt2);
}
}
}
};
protected void DoConnected(btDevice bt2) {
boolean done = false;
int l = 0;
for (int k = 0; k < btdConn.length; k++) {
if (btdConn[k] != null)
if (bt2.getMac().equalsIgnoreCase(btdConn[k].getMac())) {
l = k;
done = true;
}
}
if (!done) {
do {
if (btdConn[l] == null) {
btdConn[l] = bt2;
done = true;
}
l++;
if (l >= btdConn.length)
done = true;
} while (!done);
}
getConnects();
if (connects <= 1) {
//getOldvol();
getOldPvol();
oldwifistate = wifiManager.isWifiEnabled();
}
connectedIcon = checkIcon(bt2.getIcon());
MessageStream = bt2.getSmsstream();
vol_delay = bt2.getVoldelay() * 1000;
Message_delay = bt2.getSmsdelay() * 1000;
ramp_vol = bt2.isVolramp();
if (bt2.wifi) {
try {
oldwifistate = wifiManager.isWifiEnabled();
dowifi(false);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "Error " + e.getMessage());
}
}
// connect the selected BT device
if (bt2.getBdevice() != null && bt2.getBdevice().length() == 17) {
DeviceToConnect = bt2.getBdevice();
getIBluetoothA2dp(application);
}
if (toasts)
Toast.makeText(application, bt2.toString(), Toast.LENGTH_LONG)
.show();
// If we defined an app to auto-start then run it on connect if not in
// call
// tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (bt2.hasIntent()) {
if (tm != null) {
if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE)
runApp(bt2);
} else
runApp(bt2);
}
if (enableGTalk && bt2.isEnableTTS()) {
mTts = new TextToSpeech(application, listenerStarted);
IntentFilter messageFilter = new IntentFilter(
"a2dp.vol.service.MESSAGE");
if (!tmessageRegistered) {
application.registerReceiver(tmessage, messageFilter);
tmessageRegistered = true;
}
IntentFilter sco_filter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
if (!ScoRegistered && am2.isBluetoothScoAvailableOffCall()) {
application.registerReceiver(sco_change, sco_filter);
ScoRegistered = true;
}
}
String Ireload = "a2dp.Vol.main.RELOAD_LIST";
Intent itent = new Intent();
itent.setAction(Ireload);
itent.putExtra("connect", bt2.getMac());
application.sendBroadcast(itent);
connecting = false;
application.sendBroadcast(new Intent("a2dp.Vol.Clear"));
if (bt2.isSetpv()) {
final int vol1 = bt2.getPhonev();
new CountDownTimer(vol_delay + 500, vol_delay + 500) {
@Override
public void onFinish() {
setPVolume(vol1);
}
@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
if (bt2.isSilent())
try {
if (ActivityCompat.checkSelfPermission(application, Manifest.permission.ACCESS_NOTIFICATION_POLICY) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(application, Manifest.permission.ACCESS_NOTIFICATION_POLICY) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(LOG_TAG, "Lacking notification policy access permissions to set silent");
return;
} else {
am2.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
Log.i(LOG_TAG, "Set silent");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(LOG_TAG, "cannot set silent " + e.getLocalizedMessage());
}
if (bt2.isSetV()) {
final int vol = bt2.getDefVol();
new CountDownTimer(vol_delay, vol_delay) {
@Override
public void onFinish() {
setVolume(vol);
}
@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
if (bt2.isCarmode()) {
set_car_mode(true);
}
updateNot();
}
// Device disconnected
private final BroadcastReceiver mReceiver2 = new BroadcastReceiver() {
/*
* (non-Javadoc)
*
* @see
* android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent) Triggered on bluetooth disconnect
*/
@Override
public void onReceive(Context context2, Intent intent2) {
btDevice bt2 = null;
if (!disconnecting && !connecting) {
disconnecting = true;
BluetoothDevice bt;
try {
bt = (BluetoothDevice) Objects.requireNonNull(intent2.getExtras()).get(
BluetoothDevice.EXTRA_DEVICE);
} catch (Exception e1) {
bt = null;
e1.printStackTrace();
disconnecting = false;
return;
}
if (bt != null) {
try {
String addres = bt.getAddress();
bt2 = DB.getBTD(addres);
} catch (Exception e) {
bt2 = null;
Log.e(LOG_TAG, "Error" + e.toString());
disconnecting = false;
return;
}
} else
try {
if (Objects.requireNonNull(intent2.getAction()).equalsIgnoreCase(
"android.app.action.EXIT_CAR_MODE"))
bt2 = DB.getBTD("1");
else if (intent2.getAction().equalsIgnoreCase(
"android.app.action.EXIT_DESK_MODE"))
bt2 = DB.getBTD("2");
else if (intent2.getAction().equalsIgnoreCase(
Intent.ACTION_POWER_DISCONNECTED))
bt2 = DB.getBTD("4");
else
bt2 = null;
} catch (Exception e) {
bt2 = null;
Log.e(LOG_TAG, e.toString());
}
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
disconnecting = false;
Log.i(LOG_TAG, "Unknown device disconnect received, ignoring");
} else {
Log.i(LOG_TAG, "Disconnected: " + bt2.getDesc1() + "," + bt2.getDesc2());
DoDisconnected(bt2);
}
}
}
};
// Device disconnected
private final BroadcastReceiver CarExitReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context2, Intent intent2) {
btDevice bt2 = null;
if (!disconnecting) {
disconnecting = true;
bt2 = DB.getBTD("1");
// if it is none of the devices in the database, exit here
if (bt2 == null || bt2.getMac() == null) {
disconnecting = false;
Log.i(LOG_TAG, "Unknown device disconnect received, ignoring");
} else {
Log.i(LOG_TAG, "Disconnected: " + bt2.getDesc1() + "," + bt2.getDesc2());
DoDisconnected(bt2);
}
}
}
};