22
22
import android .content .IntentFilter ;
23
23
import android .content .SharedPreferences ;
24
24
import android .media .AudioManager ;
25
- import android .os .Binder ;
26
25
import android .os .Handler ;
27
26
import android .os .IBinder ;
28
- import android .os .Message ;
29
27
import android .preference .PreferenceManager ;
28
+ import android .support .annotation .NonNull ;
29
+ import android .support .annotation .Nullable ;
30
30
import android .telephony .TelephonyManager ;
31
+ import android .text .TextUtils ;
31
32
import android .util .Log ;
32
33
33
34
import org .thoughtcrime .redphone .audio .IncomingRinger ;
47
48
import org .thoughtcrime .redphone .ui .NotificationBarManager ;
48
49
import org .thoughtcrime .redphone .util .UncaughtExceptionHandlerManager ;
49
50
import org .thoughtcrime .securesms .database .DatabaseFactory ;
51
+ import org .thoughtcrime .securesms .events .RedPhoneEvent ;
52
+ import org .thoughtcrime .securesms .events .RedPhoneEvent .Type ;
50
53
import org .thoughtcrime .securesms .notifications .MessageNotifier ;
51
54
import org .thoughtcrime .securesms .recipients .Recipient ;
52
55
import org .thoughtcrime .securesms .recipients .RecipientFactory ;
57
60
import java .io .IOException ;
58
61
import java .security .NoSuchAlgorithmException ;
59
62
import java .security .SecureRandom ;
60
- import java . util . LinkedList ;
61
- import java . util . List ;
63
+
64
+ import de . greenrobot . event . EventBus ;
62
65
63
66
/**
64
67
* The major entry point for all of the heavy lifting associated with
@@ -73,6 +76,12 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
73
76
74
77
private static final String TAG = RedPhoneService .class .getSimpleName ();
75
78
79
+ private static final int STATE_IDLE = 0 ;
80
+ private static final int STATE_RINGING = 2 ;
81
+ private static final int STATE_DIALING = 3 ;
82
+ private static final int STATE_ANSWERING = 4 ;
83
+ private static final int STATE_CONNECTED = 5 ;
84
+
76
85
public static final String EXTRA_REMOTE_NUMBER = "remote_number" ;
77
86
public static final String EXTRA_SESSION_DESCRIPTOR = "session_descriptor" ;
78
87
public static final String EXTRA_MUTE = "mute_value" ;
@@ -84,8 +93,6 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
84
93
public static final String ACTION_HANGUP_CALL = "org.thoughtcrime.redphone.RedPhoneService.HANGUP" ;
85
94
public static final String ACTION_SET_MUTE = "org.thoughtcrime.redphone.RedPhoneService.SET_MUTE" ;
86
95
87
- private final List <Message > bufferedEvents = new LinkedList <>();
88
- private final IBinder binder = new RedPhoneServiceBinder ();
89
96
private final Handler serviceHandler = new Handler ();
90
97
91
98
private OutgoingRinger outgoingRinger ;
@@ -98,7 +105,6 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
98
105
private LockManager lockManager ;
99
106
private UncaughtExceptionHandlerManager uncaughtExceptionHandlerManager ;
100
107
101
- private Handler handler ;
102
108
private IncomingPstnCallListener pstnCallListener ;
103
109
104
110
@ Override
@@ -119,7 +125,7 @@ public void onStart(Intent intent, int startId) {
119
125
120
126
@ Override
121
127
public IBinder onBind (Intent intent ) {
122
- return binder ;
128
+ return null ;
123
129
}
124
130
125
131
@ Override
@@ -155,7 +161,7 @@ private void initializePstnCallListener() {
155
161
}
156
162
157
163
private void initializeResources () {
158
- this .state = RedPhone . STATE_IDLE ;
164
+ this .state = STATE_IDLE ;
159
165
this .zid = getZID ();
160
166
this .lockManager = new LockManager (this );
161
167
}
@@ -173,7 +179,7 @@ private void handleIncomingCall(Intent intent) {
173
179
SessionDescriptor session = intent .getParcelableExtra (EXTRA_SESSION_DESCRIPTOR );
174
180
175
181
remoteNumber = intent .getStringExtra (EXTRA_REMOTE_NUMBER );
176
- state = RedPhone . STATE_RINGING ;
182
+ state = STATE_RINGING ;
177
183
178
184
lockManager .updatePhoneState (LockManager .PhoneState .PROCESSING );
179
185
this .currentCallManager = new ResponderCallManager (this , this , remoteNumber , localNumber ,
@@ -190,9 +196,9 @@ private void handleOutgoingCall(Intent intent) {
190
196
if (remoteNumber == null || remoteNumber .length () == 0 )
191
197
return ;
192
198
193
- sendMessage (RedPhone . HANDLE_OUTGOING_CALL , getRecipient ());
199
+ sendMessage (Type . OUTGOING_CALL , getRecipient (), null );
194
200
195
- state = RedPhone . STATE_DIALING ;
201
+ state = STATE_DIALING ;
196
202
lockManager .updatePhoneState (LockManager .PhoneState .INTERACTIVE );
197
203
this .currentCallManager = new InitiatingCallManager (this , this , localNumber , password ,
198
204
remoteNumber , zid );
@@ -234,7 +240,7 @@ private void handleMissedCall(String remoteNumber) {
234
240
}
235
241
236
242
private void handleAnswerCall (Intent intent ) {
237
- state = RedPhone . STATE_ANSWERING ;
243
+ state = STATE_ANSWERING ;
238
244
incomingRinger .stop ();
239
245
DatabaseFactory .getSmsDatabase (this ).insertReceivedCall (remoteNumber );
240
246
if (currentCallManager != null ) {
@@ -243,7 +249,7 @@ private void handleAnswerCall(Intent intent) {
243
249
}
244
250
245
251
private void handleDenyCall (Intent intent ) {
246
- state = RedPhone . STATE_IDLE ;
252
+ state = STATE_IDLE ;
247
253
incomingRinger .stop ();
248
254
DatabaseFactory .getSmsDatabase (this ).insertMissedCall (remoteNumber );
249
255
if (currentCallManager != null ) {
@@ -266,12 +272,12 @@ private void handleSetMute(Intent intent) {
266
272
267
273
private boolean isBusy () {
268
274
TelephonyManager telephonyManager = (TelephonyManager )getSystemService (TELEPHONY_SERVICE );
269
- return ((currentCallManager != null && state != RedPhone . STATE_IDLE ) ||
275
+ return ((currentCallManager != null && state != STATE_IDLE ) ||
270
276
telephonyManager .getCallState () != TelephonyManager .CALL_STATE_IDLE );
271
277
}
272
278
273
279
private boolean isIdle () {
274
- return state == RedPhone . STATE_IDLE ;
280
+ return state == STATE_IDLE ;
275
281
}
276
282
277
283
private void shutdownAudio () {
@@ -283,15 +289,9 @@ public int getState() {
283
289
return state ;
284
290
}
285
291
286
- public SASInfo getCurrentCallSAS () {
287
- if (currentCallManager != null )
288
- return currentCallManager .getSasInfo ();
289
- else
290
- return null ;
291
- }
292
-
293
- public Recipient getRecipient () {
294
- if (remoteNumber != null ) {
292
+ public @ NonNull Recipient getRecipient () {
293
+ if (!TextUtils .isEmpty (remoteNumber )) {
294
+ //noinspection ConstantConditions
295
295
return RecipientFactory .getRecipientsFromString (this , remoteNumber , true )
296
296
.getPrimaryRecipient ();
297
297
} else {
@@ -351,22 +351,10 @@ private synchronized void terminate() {
351
351
352
352
shutdownAudio ();
353
353
354
- state = RedPhone . STATE_IDLE ;
354
+ state = STATE_IDLE ;
355
355
lockManager .updatePhoneState (LockManager .PhoneState .IDLE );
356
356
}
357
357
358
- public void setCallStateHandler (Handler handler ) {
359
- this .handler = handler ;
360
-
361
- if (handler != null ) {
362
- for (Message message : bufferedEvents ) {
363
- handler .sendMessage (message );
364
- }
365
-
366
- bufferedEvents .clear ();
367
- }
368
- }
369
-
370
358
///////// CallStateListener Implementation
371
359
372
360
public void notifyCallStale () {
@@ -377,7 +365,7 @@ public void notifyCallStale() {
377
365
378
366
public void notifyCallFresh () {
379
367
Log .w (TAG , "Good call, time to ring and display call card..." );
380
- sendMessage (RedPhone . HANDLE_INCOMING_CALL , getRecipient ());
368
+ sendMessage (Type . INCOMING_CALL , getRecipient (), null );
381
369
382
370
lockManager .updatePhoneState (LockManager .PhoneState .INTERACTIVE );
383
371
@@ -389,7 +377,8 @@ public void notifyCallFresh() {
389
377
390
378
public void notifyBusy () {
391
379
Log .w ("RedPhoneService" , "Got busy signal from responder!" );
392
- sendMessage (RedPhone .HANDLE_CALL_BUSY , null );
380
+ sendMessage (Type .CALL_BUSY , getRecipient (), null );
381
+
393
382
outgoingRinger .playBusy ();
394
383
serviceHandler .postDelayed (new Runnable () {
395
384
@ Override
@@ -401,124 +390,103 @@ public void run() {
401
390
402
391
public void notifyCallRinging () {
403
392
outgoingRinger .playRing ();
404
- sendMessage (RedPhone . HANDLE_CALL_RINGING , null );
393
+ sendMessage (Type . CALL_RINGING , getRecipient () , null );
405
394
}
406
395
407
396
public void notifyCallConnected (SASInfo sas ) {
408
397
outgoingRinger .playComplete ();
409
398
lockManager .updatePhoneState (LockManager .PhoneState .IN_CALL );
410
- state = RedPhone .STATE_CONNECTED ;
411
- synchronized ( this ) {
412
- sendMessage (RedPhone .HANDLE_CALL_CONNECTED , sas );
413
- try {
414
- wait ();
415
- } catch (InterruptedException e ) {
416
- throw new AssertionError ( "Wait interrupted in RedPhoneService" );
417
- }
418
- }
419
- }
420
- public void notifyCallConnectionUIUpdateComplete () {
421
- synchronized ( this ) {
422
- this .notify ();
423
- }
424
- }
425
- public void notifyDebugInfo (String info ) {
426
- sendMessage (RedPhone .HANDLE_DEBUG_INFO , info );
399
+ state = STATE_CONNECTED ;
400
+ sendMessage (Type .CALL_CONNECTED , getRecipient (), sas .getSasText ());
427
401
}
428
402
429
403
public void notifyConnectingtoInitiator () {
430
- sendMessage (RedPhone . HANDLE_CONNECTING_TO_INITIATOR , null );
404
+ sendMessage (Type . CONNECTING_TO_INITIATOR , getRecipient () , null );
431
405
}
432
406
433
407
public void notifyCallDisconnected () {
434
- if (state == RedPhone . STATE_RINGING )
408
+ if (state == STATE_RINGING )
435
409
handleMissedCall (remoteNumber );
436
410
437
- sendMessage (RedPhone . HANDLE_CALL_DISCONNECTED , null );
411
+ sendMessage (Type . CALL_DISCONNECTED , getRecipient () , null );
438
412
this .terminate ();
439
413
}
440
414
441
415
public void notifyHandshakeFailed () {
442
- state = RedPhone . STATE_IDLE ;
416
+ state = STATE_IDLE ;
443
417
outgoingRinger .playFailure ();
444
- sendMessage (RedPhone . HANDLE_HANDSHAKE_FAILED , null );
418
+ sendMessage (Type . HANDSHAKE_FAILED , getRecipient () , null );
445
419
this .terminate ();
446
420
}
447
421
448
422
public void notifyRecipientUnavailable () {
449
- state = RedPhone . STATE_IDLE ;
423
+ state = STATE_IDLE ;
450
424
outgoingRinger .playFailure ();
451
- sendMessage (RedPhone . HANDLE_RECIPIENT_UNAVAILABLE , null );
425
+ sendMessage (Type . RECIPIENT_UNAVAILABLE , getRecipient () , null );
452
426
this .terminate ();
453
427
}
454
428
455
429
public void notifyPerformingHandshake () {
456
430
outgoingRinger .playHandshake ();
457
- sendMessage (RedPhone . HANDLE_PERFORMING_HANDSHAKE , null );
431
+ sendMessage (Type . PERFORMING_HANDSHAKE , getRecipient () , null );
458
432
}
459
433
460
434
public void notifyServerFailure () {
461
- if (state == RedPhone . STATE_RINGING )
435
+ if (state == STATE_RINGING )
462
436
handleMissedCall (remoteNumber );
463
437
464
- state = RedPhone . STATE_IDLE ;
438
+ state = STATE_IDLE ;
465
439
outgoingRinger .playFailure ();
466
- sendMessage (RedPhone . HANDLE_SERVER_FAILURE , null );
440
+ sendMessage (Type . SERVER_FAILURE , getRecipient () , null );
467
441
this .terminate ();
468
442
}
469
443
470
444
public void notifyClientFailure () {
471
- if (state == RedPhone . STATE_RINGING )
445
+ if (state == STATE_RINGING )
472
446
handleMissedCall (remoteNumber );
473
447
474
- state = RedPhone . STATE_IDLE ;
448
+ state = STATE_IDLE ;
475
449
outgoingRinger .playFailure ();
476
- sendMessage (RedPhone . HANDLE_CLIENT_FAILURE , null );
450
+ sendMessage (Type . CLIENT_FAILURE , getRecipient () , null );
477
451
this .terminate ();
478
452
}
479
453
480
454
public void notifyLoginFailed () {
481
- if (state == RedPhone . STATE_RINGING )
455
+ if (state == STATE_RINGING )
482
456
handleMissedCall (remoteNumber );
483
457
484
- state = RedPhone . STATE_IDLE ;
458
+ state = STATE_IDLE ;
485
459
outgoingRinger .playFailure ();
486
- sendMessage (RedPhone . HANDLE_LOGIN_FAILED , null );
460
+ sendMessage (Type . LOGIN_FAILED , getRecipient () , null );
487
461
this .terminate ();
488
462
}
489
463
490
464
public void notifyNoSuchUser () {
491
- sendMessage (RedPhone . HANDLE_NO_SUCH_USER , getRecipient ());
465
+ sendMessage (Type . NO_SUCH_USER , getRecipient (), null );
492
466
this .terminate ();
493
467
}
494
468
495
469
public void notifyServerMessage (String message ) {
496
- sendMessage (RedPhone . HANDLE_SERVER_MESSAGE , message );
470
+ sendMessage (Type . SERVER_MESSAGE , getRecipient () , message );
497
471
this .terminate ();
498
472
}
499
473
500
474
public void notifyClientError (String msg ) {
501
- sendMessage (RedPhone . HANDLE_CLIENT_FAILURE , msg );
475
+ sendMessage (Type . CLIENT_FAILURE , getRecipient (), msg );
502
476
this .terminate ();
503
477
}
504
478
505
- public void notifyClientError (int messageId ) {
506
- notifyClientError (getString (messageId ));
507
- }
508
-
509
479
public void notifyCallConnecting () {
510
480
outgoingRinger .playSonar ();
511
481
}
512
482
513
483
public void notifyWaitingForResponder () {}
514
484
515
- private void sendMessage (int code , Object extra ) {
516
- Message message = Message .obtain ();
517
- message .what = code ;
518
- message .obj = extra ;
519
-
520
- if (handler != null ) handler .sendMessage (message );
521
- else bufferedEvents .add (message );
485
+ private void sendMessage (@ NonNull Type type ,
486
+ @ NonNull Recipient recipient ,
487
+ @ Nullable String error )
488
+ {
489
+ EventBus .getDefault ().postSticky (new RedPhoneEvent (type , recipient , error ));
522
490
}
523
491
524
492
private class IntentRunnable implements Runnable {
@@ -533,21 +501,15 @@ public void run() {
533
501
}
534
502
}
535
503
536
- public class RedPhoneServiceBinder extends Binder {
537
- public RedPhoneService getService () {
538
- return RedPhoneService .this ;
539
- }
540
- }
541
-
542
504
@ Override
543
505
public boolean isInCall () {
544
506
switch (state ) {
545
- case RedPhone . STATE_IDLE :
507
+ case STATE_IDLE :
546
508
return false ;
547
- case RedPhone . STATE_DIALING :
548
- case RedPhone . STATE_RINGING :
549
- case RedPhone . STATE_ANSWERING :
550
- case RedPhone . STATE_CONNECTED :
509
+ case STATE_DIALING :
510
+ case STATE_RINGING :
511
+ case STATE_ANSWERING :
512
+ case STATE_CONNECTED :
551
513
return true ;
552
514
default :
553
515
Log .e (TAG , "Unhandled call state: " + state );
0 commit comments