2014-12-15 20:25:55 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.database.Cursor;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.os.Bundle;
|
2015-07-14 21:31:03 +00:00
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.support.annotation.NonNull;
|
2015-04-21 20:40:21 +00:00
|
|
|
import android.util.Log;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2015-04-21 20:40:21 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Rfc5724Uri;
|
|
|
|
|
|
|
|
import java.net.URISyntaxException;
|
2014-12-15 20:25:55 +00:00
|
|
|
|
|
|
|
public class SmsSendtoActivity extends Activity {
|
2015-04-21 20:40:21 +00:00
|
|
|
|
|
|
|
private static final String TAG = SmsSendtoActivity.class.getSimpleName();
|
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
startActivity(getNextIntent(getIntent()));
|
|
|
|
finish();
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent getNextIntent(Intent original) {
|
2015-07-14 21:31:03 +00:00
|
|
|
DestinationAndBody destination;
|
2015-04-21 20:40:21 +00:00
|
|
|
|
|
|
|
if (original.getAction().equals(Intent.ACTION_SENDTO)) {
|
2015-07-14 21:31:03 +00:00
|
|
|
destination = getDestinationForSendTo(original);
|
|
|
|
} else if (original.getData() != null && "content".equals(original.getData().getScheme())) {
|
|
|
|
destination = getDestinationForSyncAdapter(original);
|
2015-04-21 20:40:21 +00:00
|
|
|
} else {
|
2015-07-14 21:31:03 +00:00
|
|
|
destination = getDestinationForView(original);
|
2015-04-21 20:40:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 01:08:31 +00:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(this, destination.getDestination(), true);
|
2014-12-15 20:25:55 +00:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipients);
|
|
|
|
|
|
|
|
final Intent nextIntent;
|
2015-11-18 20:54:40 +00:00
|
|
|
if (recipients.isEmpty()) {
|
2014-12-15 20:25:55 +00:00
|
|
|
nextIntent = new Intent(this, NewConversationActivity.class);
|
2015-11-18 20:54:40 +00:00
|
|
|
nextIntent.putExtra(ConversationActivity.TEXT_EXTRA, destination.getBody());
|
2014-12-15 20:25:55 +00:00
|
|
|
Toast.makeText(this, R.string.ConversationActivity_specify_recipient, Toast.LENGTH_LONG).show();
|
|
|
|
} else {
|
|
|
|
nextIntent = new Intent(this, ConversationActivity.class);
|
2015-11-18 20:54:40 +00:00
|
|
|
nextIntent.putExtra(ConversationActivity.TEXT_EXTRA, destination.getBody());
|
2014-12-15 20:25:55 +00:00
|
|
|
nextIntent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId);
|
|
|
|
nextIntent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
|
|
|
|
}
|
|
|
|
return nextIntent;
|
|
|
|
}
|
2015-07-14 21:31:03 +00:00
|
|
|
|
|
|
|
private @NonNull DestinationAndBody getDestinationForSendTo(Intent intent) {
|
|
|
|
return new DestinationAndBody(intent.getData().getSchemeSpecificPart(),
|
|
|
|
intent.getStringExtra("sms_body"));
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NonNull DestinationAndBody getDestinationForView(Intent intent) {
|
|
|
|
try {
|
|
|
|
Rfc5724Uri smsUri = new Rfc5724Uri(intent.getData().toString());
|
|
|
|
return new DestinationAndBody(smsUri.getPath(), smsUri.getQueryParams().get("body"));
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
Log.w(TAG, "unable to parse RFC5724 URI from intent", e);
|
|
|
|
return new DestinationAndBody("", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private @NonNull DestinationAndBody getDestinationForSyncAdapter(Intent intent) {
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = getContentResolver().query(intent.getData(), null, null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
|
|
|
return new DestinationAndBody(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.RawContacts.Data.DATA1)), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DestinationAndBody("", "");
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class DestinationAndBody {
|
|
|
|
private final String destination;
|
|
|
|
private final String body;
|
|
|
|
|
|
|
|
private DestinationAndBody(String destination, String body) {
|
|
|
|
this.destination = destination;
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDestination() {
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBody() {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|