2013-02-17 19:42:30 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2014-04-20 13:54:16 +00:00
|
|
|
import android.webkit.MimeTypeMap;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2013-07-11 21:58:40 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
2014-06-29 03:40:57 +00:00
|
|
|
public class RoutingActivity extends PassphraseRequiredActionBarActivity {
|
2013-02-17 19:42:30 +00:00
|
|
|
|
2013-07-11 21:58:40 +00:00
|
|
|
private static final int STATE_CREATE_PASSPHRASE = 1;
|
|
|
|
private static final int STATE_PROMPT_PASSPHRASE = 2;
|
2014-07-13 01:06:22 +00:00
|
|
|
|
2014-07-23 03:27:05 +00:00
|
|
|
private static final int STATE_CONVERSATION_OR_LIST = 3;
|
|
|
|
private static final int STATE_UPGRADE_DATABASE = 4;
|
|
|
|
private static final int STATE_PROMPT_PUSH_REGISTRATION = 5;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
2014-02-28 23:58:30 +00:00
|
|
|
private MasterSecret masterSecret = null;
|
|
|
|
private boolean isVisible = false;
|
|
|
|
private boolean canceledResult = false;
|
|
|
|
private boolean newIntent = false;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNewIntent(Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
2014-02-28 23:58:30 +00:00
|
|
|
this.newIntent = true;
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2014-02-28 23:58:30 +00:00
|
|
|
if (this.canceledResult && !this.newIntent) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.newIntent = false;
|
|
|
|
this.canceledResult = false;
|
|
|
|
this.isVisible = true;
|
2013-02-17 19:42:30 +00:00
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
this.isVisible = false;
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNewMasterSecret(MasterSecret masterSecret) {
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
|
|
|
|
if (isVisible) {
|
|
|
|
routeApplicationState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onMasterSecretCleared() {
|
|
|
|
this.masterSecret = null;
|
|
|
|
|
|
|
|
if (isVisible) {
|
|
|
|
routeApplicationState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2014-02-28 23:58:30 +00:00
|
|
|
if (resultCode == RESULT_CANCELED) {
|
|
|
|
canceledResult = true;
|
|
|
|
}
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void routeApplicationState() {
|
2013-07-11 21:58:40 +00:00
|
|
|
int state = getApplicationState();
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case STATE_CREATE_PASSPHRASE: handleCreatePassphrase(); break;
|
|
|
|
case STATE_PROMPT_PASSPHRASE: handlePromptPassphrase(); break;
|
|
|
|
case STATE_CONVERSATION_OR_LIST: handleDisplayConversationOrList(); break;
|
|
|
|
case STATE_UPGRADE_DATABASE: handleUpgradeDatabase(); break;
|
|
|
|
case STATE_PROMPT_PUSH_REGISTRATION: handlePushRegistration(); break;
|
|
|
|
}
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleCreatePassphrase() {
|
|
|
|
Intent intent = new Intent(this, PassphraseCreateActivity.class);
|
|
|
|
startActivityForResult(intent, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handlePromptPassphrase() {
|
|
|
|
Intent intent = new Intent(this, PassphrasePromptActivity.class);
|
|
|
|
startActivityForResult(intent, 2);
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private void handleUpgradeDatabase() {
|
|
|
|
Intent intent = new Intent(this, DatabaseUpgradeActivity.class);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
2013-07-11 21:58:40 +00:00
|
|
|
intent.putExtra("next_intent", TextSecurePreferences.hasPromptedPushRegistration(this) ?
|
|
|
|
getConversationListIntent() : getPushRegistrationIntent());
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:58:40 +00:00
|
|
|
private void handlePushRegistration() {
|
2013-08-15 15:25:30 +00:00
|
|
|
Intent intent = getPushRegistrationIntent();
|
2013-07-11 21:58:40 +00:00
|
|
|
intent.putExtra("next_intent", getConversationListIntent());
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
2013-04-01 02:16:06 +00:00
|
|
|
|
2013-07-11 21:58:40 +00:00
|
|
|
private void handleDisplayConversationOrList() {
|
2014-02-28 06:44:02 +00:00
|
|
|
final ConversationParameters parameters = getConversationParameters();
|
|
|
|
final Intent intent;
|
2014-07-23 22:40:45 +00:00
|
|
|
|
2014-07-13 01:06:22 +00:00
|
|
|
if (isShareAction()) intent = getShareIntent(parameters);
|
|
|
|
else if (parameters.recipients != null) intent = getConversationIntent(parameters);
|
|
|
|
else intent = getConversationListIntent();
|
|
|
|
|
2013-04-01 02:16:06 +00:00
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getConversationIntent(ConversationParameters parameters) {
|
|
|
|
Intent intent = new Intent(this, ConversationActivity.class);
|
2014-02-24 08:19:54 +00:00
|
|
|
intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, parameters.recipients != null ? parameters.recipients.toIdString() : "");
|
2013-02-17 19:42:30 +00:00
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, parameters.thread);
|
|
|
|
intent.putExtra(ConversationActivity.MASTER_SECRET_EXTRA, masterSecret);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_TEXT_EXTRA, parameters.draftText);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_IMAGE_EXTRA, parameters.draftImage);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_AUDIO_EXTRA, parameters.draftAudio);
|
2014-03-03 16:55:34 +00:00
|
|
|
intent.putExtra(ConversationActivity.DRAFT_VIDEO_EXTRA, parameters.draftVideo);
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2014-02-28 06:44:02 +00:00
|
|
|
private Intent getShareIntent(ConversationParameters parameters) {
|
|
|
|
Intent intent = new Intent(this, ShareActivity.class);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
|
|
|
|
|
|
|
if (parameters != null) {
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_TEXT_EXTRA, parameters.draftText);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_IMAGE_EXTRA, parameters.draftImage);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_AUDIO_EXTRA, parameters.draftAudio);
|
|
|
|
intent.putExtra(ConversationActivity.DRAFT_VIDEO_EXTRA, parameters.draftVideo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
private Intent getConversationListIntent() {
|
|
|
|
Intent intent = new Intent(this, ConversationListActivity.class);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
|
|
|
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:58:40 +00:00
|
|
|
private Intent getPushRegistrationIntent() {
|
2013-08-15 15:25:30 +00:00
|
|
|
Intent intent = new Intent(this, RegistrationActivity.class);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
|
|
|
|
|
|
|
return intent;
|
2013-07-11 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
private int getApplicationState() {
|
|
|
|
if (!MasterSecretUtil.isPassphraseInitialized(this))
|
|
|
|
return STATE_CREATE_PASSPHRASE;
|
|
|
|
|
|
|
|
if (masterSecret == null)
|
|
|
|
return STATE_PROMPT_PASSPHRASE;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
if (DatabaseUpgradeActivity.isUpdate(this))
|
|
|
|
return STATE_UPGRADE_DATABASE;
|
|
|
|
|
2013-07-11 21:58:40 +00:00
|
|
|
if (!TextSecurePreferences.hasPromptedPushRegistration(this))
|
|
|
|
return STATE_PROMPT_PUSH_REGISTRATION;
|
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
return STATE_CONVERSATION_OR_LIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ConversationParameters getConversationParameters() {
|
|
|
|
if (isSendAction()) {
|
|
|
|
return getConversationParametersForSendAction();
|
|
|
|
} else if (isShareAction()) {
|
|
|
|
return getConversationParametersForShareAction();
|
|
|
|
} else {
|
|
|
|
return getConversationParametersForInternalAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ConversationParameters getConversationParametersForSendAction() {
|
2014-02-28 06:44:02 +00:00
|
|
|
Recipients recipients;
|
2014-11-05 19:04:37 +00:00
|
|
|
String body = getIntent().getStringExtra("sms_body");
|
|
|
|
long threadId = getIntent().getLongExtra("thread_id", -1);
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
String data = getIntent().getData().getSchemeSpecificPart();
|
|
|
|
recipients = RecipientFactory.getRecipientsFromString(this, data, false);
|
|
|
|
threadId = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipients);
|
|
|
|
} catch (RecipientFormattingException rfe) {
|
|
|
|
recipients = null;
|
|
|
|
}
|
|
|
|
|
2014-11-05 19:04:37 +00:00
|
|
|
return new ConversationParameters(threadId, recipients, body, null, null, null);
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ConversationParameters getConversationParametersForShareAction() {
|
|
|
|
String type = getIntent().getType();
|
2014-04-20 13:54:16 +00:00
|
|
|
String draftText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
|
2013-02-17 19:42:30 +00:00
|
|
|
Uri draftImage = null;
|
|
|
|
Uri draftAudio = null;
|
2014-03-03 16:55:34 +00:00
|
|
|
Uri draftVideo = null;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
2014-04-20 13:54:16 +00:00
|
|
|
Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
|
|
|
|
|
|
|
|
if (streamExtra != null) {
|
|
|
|
type = getMimeType(streamExtra);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != null && type.startsWith("image/")) {
|
|
|
|
draftImage = streamExtra;
|
2014-02-28 06:44:02 +00:00
|
|
|
} else if (type != null && type.startsWith("audio/")) {
|
2014-04-20 13:54:16 +00:00
|
|
|
draftAudio = streamExtra;
|
2014-02-28 06:44:02 +00:00
|
|
|
} else if (type != null && type.startsWith("video/")) {
|
2014-04-20 13:54:16 +00:00
|
|
|
draftVideo = streamExtra;
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 16:55:34 +00:00
|
|
|
return new ConversationParameters(-1, null, draftText, draftImage, draftAudio, draftVideo);
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 13:54:16 +00:00
|
|
|
private String getMimeType(Uri uri) {
|
|
|
|
String type = getContentResolver().getType(uri);
|
|
|
|
|
|
|
|
if (type == null) {
|
|
|
|
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:42:30 +00:00
|
|
|
private ConversationParameters getConversationParametersForInternalAction() {
|
|
|
|
long threadId = getIntent().getLongExtra("thread_id", -1);
|
|
|
|
Recipients recipients = getIntent().getParcelableExtra("recipients");
|
|
|
|
|
2014-03-03 16:55:34 +00:00
|
|
|
return new ConversationParameters(threadId, recipients, null, null, null, null);
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isShareAction() {
|
|
|
|
return Intent.ACTION_SEND.equals(getIntent().getAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isSendAction() {
|
|
|
|
return Intent.ACTION_SENDTO.equals(getIntent().getAction());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ConversationParameters {
|
|
|
|
public final long thread;
|
|
|
|
public final Recipients recipients;
|
|
|
|
public final String draftText;
|
|
|
|
public final Uri draftImage;
|
|
|
|
public final Uri draftAudio;
|
2014-03-03 16:55:34 +00:00
|
|
|
public final Uri draftVideo;
|
2013-02-17 19:42:30 +00:00
|
|
|
|
|
|
|
public ConversationParameters(long thread, Recipients recipients,
|
2014-03-03 16:55:34 +00:00
|
|
|
String draftText, Uri draftImage, Uri draftAudio, Uri draftVideo)
|
2013-02-17 19:42:30 +00:00
|
|
|
{
|
|
|
|
this.thread = thread;
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.draftText = draftText;
|
|
|
|
this.draftImage = draftImage;
|
|
|
|
this.draftAudio = draftAudio;
|
2014-03-03 16:55:34 +00:00
|
|
|
this.draftVideo = draftVideo;
|
2013-02-17 19:42:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|