Skip to content

Commit 5b6f49c

Browse files
committed
Switch RedPhone view<->service interaction to use event bus
Fixes signalapp#4234 // FREEBIE
1 parent 3e798a9 commit 5b6f49c

12 files changed

+219
-275
lines changed

proguard.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
-keepattributes SourceFile,LineNumberTable
33
-keep class org.whispersystems.** { *; }
44
-keep class org.thoughtcrime.securesms.** { *; }
5+
-keepclassmembers class ** {
6+
public void onEvent*(**);
7+
}
58

src/org/thoughtcrime/redphone/RedPhone.java

+92-167
Large diffs are not rendered by default.

src/org/thoughtcrime/redphone/RedPhoneService.java

+62-100
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import android.content.IntentFilter;
2323
import android.content.SharedPreferences;
2424
import android.media.AudioManager;
25-
import android.os.Binder;
2625
import android.os.Handler;
2726
import android.os.IBinder;
28-
import android.os.Message;
2927
import android.preference.PreferenceManager;
28+
import android.support.annotation.NonNull;
29+
import android.support.annotation.Nullable;
3030
import android.telephony.TelephonyManager;
31+
import android.text.TextUtils;
3132
import android.util.Log;
3233

3334
import org.thoughtcrime.redphone.audio.IncomingRinger;
@@ -47,6 +48,8 @@
4748
import org.thoughtcrime.redphone.ui.NotificationBarManager;
4849
import org.thoughtcrime.redphone.util.UncaughtExceptionHandlerManager;
4950
import org.thoughtcrime.securesms.database.DatabaseFactory;
51+
import org.thoughtcrime.securesms.events.RedPhoneEvent;
52+
import org.thoughtcrime.securesms.events.RedPhoneEvent.Type;
5053
import org.thoughtcrime.securesms.notifications.MessageNotifier;
5154
import org.thoughtcrime.securesms.recipients.Recipient;
5255
import org.thoughtcrime.securesms.recipients.RecipientFactory;
@@ -57,8 +60,8 @@
5760
import java.io.IOException;
5861
import java.security.NoSuchAlgorithmException;
5962
import java.security.SecureRandom;
60-
import java.util.LinkedList;
61-
import java.util.List;
63+
64+
import de.greenrobot.event.EventBus;
6265

6366
/**
6467
* The major entry point for all of the heavy lifting associated with
@@ -73,6 +76,12 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
7376

7477
private static final String TAG = RedPhoneService.class.getSimpleName();
7578

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+
7685
public static final String EXTRA_REMOTE_NUMBER = "remote_number";
7786
public static final String EXTRA_SESSION_DESCRIPTOR = "session_descriptor";
7887
public static final String EXTRA_MUTE = "mute_value";
@@ -84,8 +93,6 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
8493
public static final String ACTION_HANGUP_CALL = "org.thoughtcrime.redphone.RedPhoneService.HANGUP";
8594
public static final String ACTION_SET_MUTE = "org.thoughtcrime.redphone.RedPhoneService.SET_MUTE";
8695

87-
private final List<Message> bufferedEvents = new LinkedList<>();
88-
private final IBinder binder = new RedPhoneServiceBinder();
8996
private final Handler serviceHandler = new Handler();
9097

9198
private OutgoingRinger outgoingRinger;
@@ -98,7 +105,6 @@ public class RedPhoneService extends Service implements CallStateListener, CallS
98105
private LockManager lockManager;
99106
private UncaughtExceptionHandlerManager uncaughtExceptionHandlerManager;
100107

101-
private Handler handler;
102108
private IncomingPstnCallListener pstnCallListener;
103109

104110
@Override
@@ -119,7 +125,7 @@ public void onStart(Intent intent, int startId) {
119125

120126
@Override
121127
public IBinder onBind(Intent intent) {
122-
return binder;
128+
return null;
123129
}
124130

125131
@Override
@@ -155,7 +161,7 @@ private void initializePstnCallListener() {
155161
}
156162

157163
private void initializeResources() {
158-
this.state = RedPhone.STATE_IDLE;
164+
this.state = STATE_IDLE;
159165
this.zid = getZID();
160166
this.lockManager = new LockManager(this);
161167
}
@@ -173,7 +179,7 @@ private void handleIncomingCall(Intent intent) {
173179
SessionDescriptor session = intent.getParcelableExtra(EXTRA_SESSION_DESCRIPTOR);
174180

175181
remoteNumber = intent.getStringExtra(EXTRA_REMOTE_NUMBER);
176-
state = RedPhone.STATE_RINGING;
182+
state = STATE_RINGING;
177183

178184
lockManager.updatePhoneState(LockManager.PhoneState.PROCESSING);
179185
this.currentCallManager = new ResponderCallManager(this, this, remoteNumber, localNumber,
@@ -190,9 +196,9 @@ private void handleOutgoingCall(Intent intent) {
190196
if (remoteNumber == null || remoteNumber.length() == 0)
191197
return;
192198

193-
sendMessage(RedPhone.HANDLE_OUTGOING_CALL, getRecipient());
199+
sendMessage(Type.OUTGOING_CALL, getRecipient(), null);
194200

195-
state = RedPhone.STATE_DIALING;
201+
state = STATE_DIALING;
196202
lockManager.updatePhoneState(LockManager.PhoneState.INTERACTIVE);
197203
this.currentCallManager = new InitiatingCallManager(this, this, localNumber, password,
198204
remoteNumber, zid);
@@ -234,7 +240,7 @@ private void handleMissedCall(String remoteNumber) {
234240
}
235241

236242
private void handleAnswerCall(Intent intent) {
237-
state = RedPhone.STATE_ANSWERING;
243+
state = STATE_ANSWERING;
238244
incomingRinger.stop();
239245
DatabaseFactory.getSmsDatabase(this).insertReceivedCall(remoteNumber);
240246
if (currentCallManager != null) {
@@ -243,7 +249,7 @@ private void handleAnswerCall(Intent intent) {
243249
}
244250

245251
private void handleDenyCall(Intent intent) {
246-
state = RedPhone.STATE_IDLE;
252+
state = STATE_IDLE;
247253
incomingRinger.stop();
248254
DatabaseFactory.getSmsDatabase(this).insertMissedCall(remoteNumber);
249255
if(currentCallManager != null) {
@@ -266,12 +272,12 @@ private void handleSetMute(Intent intent) {
266272

267273
private boolean isBusy() {
268274
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
269-
return ((currentCallManager != null && state != RedPhone.STATE_IDLE) ||
275+
return ((currentCallManager != null && state != STATE_IDLE) ||
270276
telephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE);
271277
}
272278

273279
private boolean isIdle() {
274-
return state == RedPhone.STATE_IDLE;
280+
return state == STATE_IDLE;
275281
}
276282

277283
private void shutdownAudio() {
@@ -283,15 +289,9 @@ public int getState() {
283289
return state;
284290
}
285291

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
295295
return RecipientFactory.getRecipientsFromString(this, remoteNumber, true)
296296
.getPrimaryRecipient();
297297
} else {
@@ -351,22 +351,10 @@ private synchronized void terminate() {
351351

352352
shutdownAudio();
353353

354-
state = RedPhone.STATE_IDLE;
354+
state = STATE_IDLE;
355355
lockManager.updatePhoneState(LockManager.PhoneState.IDLE);
356356
}
357357

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-
370358
///////// CallStateListener Implementation
371359

372360
public void notifyCallStale() {
@@ -377,7 +365,7 @@ public void notifyCallStale() {
377365

378366
public void notifyCallFresh() {
379367
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);
381369

382370
lockManager.updatePhoneState(LockManager.PhoneState.INTERACTIVE);
383371

@@ -389,7 +377,8 @@ public void notifyCallFresh() {
389377

390378
public void notifyBusy() {
391379
Log.w("RedPhoneService", "Got busy signal from responder!");
392-
sendMessage(RedPhone.HANDLE_CALL_BUSY, null);
380+
sendMessage(Type.CALL_BUSY, getRecipient(), null);
381+
393382
outgoingRinger.playBusy();
394383
serviceHandler.postDelayed(new Runnable() {
395384
@Override
@@ -401,124 +390,103 @@ public void run() {
401390

402391
public void notifyCallRinging() {
403392
outgoingRinger.playRing();
404-
sendMessage(RedPhone.HANDLE_CALL_RINGING, null);
393+
sendMessage(Type.CALL_RINGING, getRecipient(), null);
405394
}
406395

407396
public void notifyCallConnected(SASInfo sas) {
408397
outgoingRinger.playComplete();
409398
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());
427401
}
428402

429403
public void notifyConnectingtoInitiator() {
430-
sendMessage(RedPhone.HANDLE_CONNECTING_TO_INITIATOR, null);
404+
sendMessage(Type.CONNECTING_TO_INITIATOR, getRecipient(), null);
431405
}
432406

433407
public void notifyCallDisconnected() {
434-
if (state == RedPhone.STATE_RINGING)
408+
if (state == STATE_RINGING)
435409
handleMissedCall(remoteNumber);
436410

437-
sendMessage(RedPhone.HANDLE_CALL_DISCONNECTED, null);
411+
sendMessage(Type.CALL_DISCONNECTED, getRecipient(), null);
438412
this.terminate();
439413
}
440414

441415
public void notifyHandshakeFailed() {
442-
state = RedPhone.STATE_IDLE;
416+
state = STATE_IDLE;
443417
outgoingRinger.playFailure();
444-
sendMessage(RedPhone.HANDLE_HANDSHAKE_FAILED, null);
418+
sendMessage(Type.HANDSHAKE_FAILED, getRecipient(), null);
445419
this.terminate();
446420
}
447421

448422
public void notifyRecipientUnavailable() {
449-
state = RedPhone.STATE_IDLE;
423+
state = STATE_IDLE;
450424
outgoingRinger.playFailure();
451-
sendMessage(RedPhone.HANDLE_RECIPIENT_UNAVAILABLE, null);
425+
sendMessage(Type.RECIPIENT_UNAVAILABLE, getRecipient(), null);
452426
this.terminate();
453427
}
454428

455429
public void notifyPerformingHandshake() {
456430
outgoingRinger.playHandshake();
457-
sendMessage(RedPhone.HANDLE_PERFORMING_HANDSHAKE, null);
431+
sendMessage(Type.PERFORMING_HANDSHAKE, getRecipient(), null);
458432
}
459433

460434
public void notifyServerFailure() {
461-
if (state == RedPhone.STATE_RINGING)
435+
if (state == STATE_RINGING)
462436
handleMissedCall(remoteNumber);
463437

464-
state = RedPhone.STATE_IDLE;
438+
state = STATE_IDLE;
465439
outgoingRinger.playFailure();
466-
sendMessage(RedPhone.HANDLE_SERVER_FAILURE, null);
440+
sendMessage(Type.SERVER_FAILURE, getRecipient(), null);
467441
this.terminate();
468442
}
469443

470444
public void notifyClientFailure() {
471-
if (state == RedPhone.STATE_RINGING)
445+
if (state == STATE_RINGING)
472446
handleMissedCall(remoteNumber);
473447

474-
state = RedPhone.STATE_IDLE;
448+
state = STATE_IDLE;
475449
outgoingRinger.playFailure();
476-
sendMessage(RedPhone.HANDLE_CLIENT_FAILURE, null);
450+
sendMessage(Type.CLIENT_FAILURE, getRecipient(), null);
477451
this.terminate();
478452
}
479453

480454
public void notifyLoginFailed() {
481-
if (state == RedPhone.STATE_RINGING)
455+
if (state == STATE_RINGING)
482456
handleMissedCall(remoteNumber);
483457

484-
state = RedPhone.STATE_IDLE;
458+
state = STATE_IDLE;
485459
outgoingRinger.playFailure();
486-
sendMessage(RedPhone.HANDLE_LOGIN_FAILED, null);
460+
sendMessage(Type.LOGIN_FAILED, getRecipient(), null);
487461
this.terminate();
488462
}
489463

490464
public void notifyNoSuchUser() {
491-
sendMessage(RedPhone.HANDLE_NO_SUCH_USER, getRecipient());
465+
sendMessage(Type.NO_SUCH_USER, getRecipient(), null);
492466
this.terminate();
493467
}
494468

495469
public void notifyServerMessage(String message) {
496-
sendMessage(RedPhone.HANDLE_SERVER_MESSAGE, message);
470+
sendMessage(Type.SERVER_MESSAGE, getRecipient(), message);
497471
this.terminate();
498472
}
499473

500474
public void notifyClientError(String msg) {
501-
sendMessage(RedPhone.HANDLE_CLIENT_FAILURE,msg);
475+
sendMessage(Type.CLIENT_FAILURE, getRecipient(), msg);
502476
this.terminate();
503477
}
504478

505-
public void notifyClientError(int messageId) {
506-
notifyClientError(getString(messageId));
507-
}
508-
509479
public void notifyCallConnecting() {
510480
outgoingRinger.playSonar();
511481
}
512482

513483
public void notifyWaitingForResponder() {}
514484

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));
522490
}
523491

524492
private class IntentRunnable implements Runnable {
@@ -533,21 +501,15 @@ public void run() {
533501
}
534502
}
535503

536-
public class RedPhoneServiceBinder extends Binder {
537-
public RedPhoneService getService() {
538-
return RedPhoneService.this;
539-
}
540-
}
541-
542504
@Override
543505
public boolean isInCall() {
544506
switch(state) {
545-
case RedPhone.STATE_IDLE:
507+
case STATE_IDLE:
546508
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:
551513
return true;
552514
default:
553515
Log.e(TAG, "Unhandled call state: " + state);

src/org/thoughtcrime/redphone/call/CallStateListener.java

-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ public interface CallStateListener {
4343
public void notifyRecipientUnavailable();
4444
public void notifyBusy();
4545
public void notifyLoginFailed();
46-
public void notifyDebugInfo(String info);
4746
public void notifyCallStale();
4847
public void notifyCallFresh();
49-
public void notifyClientError(int msgId);
5048
public void notifyClientError(String message);
5149
public void notifyCallConnecting();
5250
}

0 commit comments

Comments
 (0)