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;
|
2017-07-26 16:59:15 +00:00
|
|
|
import android.text.TextUtils;
|
2015-04-21 20:40:21 +00:00
|
|
|
import android.util.Log;
|
2014-12-15 20:25:55 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-12-15 20:25:55 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
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
|
|
|
}
|
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
final Intent nextIntent;
|
2017-07-26 16:59:15 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(destination.destination)) {
|
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 {
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(this, Address.fromExternal(this, destination.getDestination()), true);
|
2017-08-01 15:56:00 +00:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipient);
|
2017-07-26 16:59:15 +00:00
|
|
|
|
2014-12-15 20:25:55 +00:00
|
|
|
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);
|
2017-08-01 15:56:00 +00:00
|
|
|
nextIntent.putExtra(ConversationActivity.ADDRESS_EXTRA, recipient.getAddress());
|
2014-12-15 20:25:55 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|