Fix up security initialization stuff in ConversationActivity

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-09-23 15:47:48 -07:00
parent a0fc948519
commit d9a3f5a3b8
5 changed files with 31 additions and 57 deletions

View File

@ -215,7 +215,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
initializeActionBar();
initializeViews();
initializeResources();
initializeSecurity();
initializeSecurity(false);
initializeDraft();
}
@ -231,7 +231,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
setIntent(intent);
initializeResources();
initializeSecurity();
initializeSecurity(false);
initializeDraft();
if (fragment != null) {
@ -267,7 +267,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
quickAttachmentDrawer.onPause();
}
@Override public void onConfigurationChanged(Configuration newConfig) {
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.w(TAG, "onConfigurationChanged(" + newConfig.orientation + ")");
super.onConfigurationChanged(newConfig);
composeText.setTransport(sendButton.getSelectedTransport());
@ -293,10 +294,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
switch (reqCode) {
case PICK_IMAGE:
setMedia(data.getData(),
MediaUtil.isGif(MediaUtil.getMimeType(this, data.getData())) ? MediaType.GIF
: MediaType.IMAGE,
false);
boolean isGif = MediaUtil.isGif(MediaUtil.getMimeType(this, data.getData()));
setMedia(data.getData(), isGif ? MediaType.GIF : MediaType.IMAGE, false);
break;
case PICK_VIDEO:
setMedia(data.getData(), MediaType.VIDEO, false);
@ -704,6 +703,22 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
startActivity(intent);
}
private void handleSecurityChange(boolean encryptedConversation) {
boolean isMediaMessage = !recipients.isSingleRecipient() || attachmentManager.isAttachmentPresent();
this.isEncryptedConversation = encryptedConversation;
sendButton.resetAvailableTransports(isMediaMessage);
if (!isEncryptedConversation) sendButton.disableTransport(Type.TEXTSECURE);
if (recipients.isGroupRecipient()) sendButton.disableTransport(Type.SMS);
if (isEncryptedConversation) sendButton.setDefaultTransport(Type.TEXTSECURE);
else sendButton.setDefaultTransport(Type.SMS);
calculateCharactersRemaining();
supportInvalidateOptionsMenu();
}
///// Initializers
private void initializeDraft() {
@ -763,8 +778,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
}.execute();
}
private void initializeSecurity() {
initializeSecurity(false);
private void initializeSecurity(final boolean current) {
handleSecurityChange(current);
new AsyncTask<Recipients, Void, Boolean>() {
@Override
@ -785,28 +800,13 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
@Override
protected void onPostExecute(Boolean result) {
if (result != isEncryptedConversation) {
initializeSecurity(result);
if (current != result) {
handleSecurityChange(result);
}
}
}.execute(recipients);
}
private void initializeSecurity(boolean encryptedConversation) {
boolean isMediaMessage = !recipients.isSingleRecipient() || attachmentManager.isAttachmentPresent();
this.isEncryptedConversation = encryptedConversation;
sendButton.resetAvailableTransports(isMediaMessage);
if (!isEncryptedConversation) sendButton.disableTransport(Type.TEXTSECURE);
if (recipients.isGroupRecipient()) sendButton.disableTransport(Type.SMS);
if (isEncryptedConversation) sendButton.setDefaultTransport(Type.TEXTSECURE);
else sendButton.setDefaultTransport(Type.SMS);
calculateCharactersRemaining();
supportInvalidateOptionsMenu();
}
private void initializeMmsEnabledCheck() {
new AsyncTask<Void, Void, Boolean>() {
@ -944,12 +944,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
securityUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long eventThreadId = intent.getLongExtra("thread_id", -1);
if (eventThreadId == threadId || eventThreadId == -2) {
initializeSecurity();
calculateCharactersRemaining();
}
initializeSecurity(isEncryptedConversation);
calculateCharactersRemaining();
}
};
@ -1188,8 +1184,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
if (refreshFragment) {
fragment.reload(recipients, threadId);
initializeSecurity();
}
fragment.scrollToBottom();
@ -1439,7 +1433,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
@Override
public void onAttachmentChanged() {
initializeSecurity();
updateToggleButtonState();
}

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.crypto;
import android.content.Context;
import android.content.Intent;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.service.KeyCachingService;
/**
@ -16,12 +17,7 @@ public class SecurityEvent {
public static final String SECURITY_UPDATE_EVENT = "org.thoughtcrime.securesms.KEY_EXCHANGE_UPDATE";
public static void broadcastSecurityUpdateEvent(Context context) {
broadcastSecurityUpdateEvent(context, -2);
}
public static void broadcastSecurityUpdateEvent(Context context, long threadId) {
Intent intent = new Intent(SECURITY_UPDATE_EVENT);
intent.putExtra("thread_id", threadId);
intent.setPackage(context.getPackageName());
context.sendBroadcast(intent, KeyCachingService.KEY_PERMISSION);
}

View File

@ -195,7 +195,7 @@ public class PushDecryptJob extends ContextJob {
SessionStore sessionStore = new TextSecureSessionStore(context);
sessionStore.deleteAllSessions(envelope.getSource());
SecurityEvent.broadcastSecurityUpdateEvent(context, threadId);
SecurityEvent.broadcastSecurityUpdateEvent(context);
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), threadId);
}

View File

@ -79,14 +79,6 @@ public class SmsSentJob extends MasterSecretJob {
switch (result) {
case Activity.RESULT_OK:
database.markAsSent(messageId);
if (record != null && record.isEndSession()) {
Log.w(TAG, "Ending session...");
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
sessionStore.deleteAllSessions(record.getIndividualRecipient().getNumber());
SecurityEvent.broadcastSecurityUpdateEvent(context, record.getThreadId());
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_RADIO_OFF:
@ -94,7 +86,6 @@ public class SmsSentJob extends MasterSecretJob {
ApplicationContext.getInstance(context)
.getJobManager()
.add(new SmsSendJob(context, messageId, record.getIndividualRecipient().getNumber()));
break;
default:
database.markAsSentFailed(messageId);

View File

@ -3,9 +3,6 @@ package org.thoughtcrime.securesms.push;
import android.content.Context;
import org.thoughtcrime.securesms.crypto.SecurityEvent;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
import org.whispersystems.textsecure.api.push.TextSecureAddress;
@ -21,9 +18,6 @@ public class SecurityEventListener implements TextSecureMessageSender.EventListe
@Override
public void onSecurityEvent(TextSecureAddress textSecureAddress) {
Recipients recipients = RecipientFactory.getRecipientsFromString(context, textSecureAddress.getNumber(), false);
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
SecurityEvent.broadcastSecurityUpdateEvent(context, threadId);
SecurityEvent.broadcastSecurityUpdateEvent(context);
}
}