2017-12-07 20:59:13 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
2018-02-25 22:58:56 +00:00
|
|
|
import android.Manifest;
|
2018-02-24 20:57:42 +00:00
|
|
|
import android.app.Activity;
|
2018-10-09 17:35:33 +00:00
|
|
|
import android.content.Intent;
|
2018-02-25 22:58:56 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2018-10-09 17:35:33 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2018-02-25 22:58:56 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.widget.Toast;
|
2017-12-10 10:44:10 +00:00
|
|
|
|
2017-12-15 17:25:21 +00:00
|
|
|
import java.util.List;
|
2018-05-16 13:08:38 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2017-12-10 10:44:10 +00:00
|
|
|
|
2018-02-24 20:57:42 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2017-12-07 21:13:24 +00:00
|
|
|
import eu.siacs.conversations.persistance.DatabaseBackend;
|
2018-10-09 17:35:33 +00:00
|
|
|
import eu.siacs.conversations.utils.SignupUtils;
|
2017-12-07 21:13:24 +00:00
|
|
|
import eu.siacs.conversations.utils.XmppUri;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2017-12-07 20:59:13 +00:00
|
|
|
|
2018-02-17 08:21:50 +00:00
|
|
|
public class UriHandlerActivity extends AppCompatActivity {
|
2018-02-25 22:58:56 +00:00
|
|
|
|
2018-10-09 17:35:33 +00:00
|
|
|
public static final String ACTION_SCAN_QR_CODE = "scan_qr_code";
|
|
|
|
private static final int REQUEST_SCAN_QR_CODE = 0x1234;
|
|
|
|
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN = 0x6789;
|
2020-01-09 19:10:19 +00:00
|
|
|
private static final Pattern VCARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n");
|
2018-10-09 17:35:33 +00:00
|
|
|
private boolean handled = false;
|
|
|
|
|
|
|
|
public static void scan(Activity activity) {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
Intent intent = new Intent(activity, UriHandlerActivity.class);
|
|
|
|
intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
|
|
|
|
activity.startActivity(intent);
|
|
|
|
} else {
|
|
|
|
activity.requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSIONS_TO_SCAN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void onRequestPermissionResult(Activity activity, int requestCode, int[] grantResults) {
|
|
|
|
if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (grantResults.length > 0) {
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
scan(activity);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(activity, R.string.qr_code_scanner_needs_access_to_camera, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
this.handled = savedInstanceState != null && savedInstanceState.getBoolean("handled", false);
|
|
|
|
getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
|
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
handleIntent(getIntent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle savedInstanceState) {
|
|
|
|
savedInstanceState.putBoolean("handled", this.handled);
|
|
|
|
super.onSaveInstanceState(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNewIntent(Intent intent) {
|
|
|
|
handleIntent(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleUri(Uri uri) {
|
|
|
|
handleUri(uri, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleUri(Uri uri, final boolean scanned) {
|
|
|
|
final Intent intent;
|
|
|
|
final XmppUri xmppUri = new XmppUri(uri);
|
2019-01-23 11:45:15 +00:00
|
|
|
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true);
|
2018-10-09 17:35:33 +00:00
|
|
|
|
2020-01-10 11:51:04 +00:00
|
|
|
if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) {
|
2020-01-09 13:13:05 +00:00
|
|
|
final String preauth = xmppUri.getParamater("preauth");
|
|
|
|
final Jid jid = xmppUri.getJid();
|
2020-01-09 14:49:49 +00:00
|
|
|
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
|
2020-01-09 19:10:19 +00:00
|
|
|
if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) {
|
|
|
|
Toast.makeText(this, R.string.account_already_exists, Toast.LENGTH_LONG).show();
|
2020-01-09 14:49:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-09 19:10:19 +00:00
|
|
|
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth);
|
|
|
|
startActivity(intent);
|
2020-01-09 14:49:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParamater("ibr"))) {
|
2020-01-09 19:10:19 +00:00
|
|
|
intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth);
|
2020-01-09 14:49:49 +00:00
|
|
|
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
2020-01-09 13:13:05 +00:00
|
|
|
startActivity(intent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:35:33 +00:00
|
|
|
if (accounts.size() == 0) {
|
2020-01-10 11:51:04 +00:00
|
|
|
if (xmppUri.isValidJid()) {
|
2018-10-09 17:35:33 +00:00
|
|
|
intent = SignupUtils.getSignUpIntent(this);
|
2019-07-20 13:58:05 +00:00
|
|
|
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
2018-10-09 17:35:33 +00:00
|
|
|
startActivity(intent);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xmppUri.isAction(XmppUri.ACTION_MESSAGE)) {
|
|
|
|
|
|
|
|
final Jid jid = xmppUri.getJid();
|
|
|
|
final String body = xmppUri.getBody();
|
|
|
|
|
|
|
|
if (jid != null) {
|
|
|
|
Class clazz;
|
|
|
|
try {
|
|
|
|
clazz = Class.forName("eu.siacs.conversations.ui.ShareViaAccountActivity");
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
clazz = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (clazz != null) {
|
|
|
|
intent = new Intent(this, clazz);
|
|
|
|
intent.putExtra("contact", jid.toEscapedString());
|
|
|
|
intent.putExtra("body", body);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(this, StartConversationActivity.class);
|
2018-10-24 11:54:42 +00:00
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
2018-10-09 17:35:33 +00:00
|
|
|
intent.setData(uri);
|
|
|
|
intent.putExtra("account", accounts.get(0).toEscapedString());
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
intent = new Intent(this, ShareWithActivity.class);
|
|
|
|
intent.setAction(Intent.ACTION_SEND);
|
|
|
|
intent.setType("text/plain");
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, body);
|
|
|
|
}
|
|
|
|
} else if (accounts.contains(xmppUri.getJid())) {
|
|
|
|
intent = new Intent(getApplicationContext(), EditAccountActivity.class);
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
intent.putExtra("jid", xmppUri.getJid().asBareJid().toString());
|
|
|
|
intent.setData(uri);
|
|
|
|
intent.putExtra("scanned", scanned);
|
2020-01-10 11:51:04 +00:00
|
|
|
} else if (xmppUri.isValidJid()) {
|
2018-10-09 17:35:33 +00:00
|
|
|
intent = new Intent(getApplicationContext(), StartConversationActivity.class);
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
|
|
|
|
intent.putExtra("scanned", scanned);
|
|
|
|
intent.setData(uri);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIntent(Intent data) {
|
|
|
|
if (handled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data == null || data.getAction() == null) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = true;
|
|
|
|
|
|
|
|
switch (data.getAction()) {
|
|
|
|
case Intent.ACTION_VIEW:
|
|
|
|
case Intent.ACTION_SENDTO:
|
|
|
|
handleUri(data.getData());
|
|
|
|
break;
|
|
|
|
case ACTION_SCAN_QR_CODE:
|
|
|
|
Intent intent = new Intent(this, ScanActivity.class);
|
|
|
|
startActivityForResult(intent, REQUEST_SCAN_QR_CODE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
super.onActivityResult(requestCode, requestCode, intent);
|
|
|
|
if (requestCode == REQUEST_SCAN_QR_CODE && resultCode == RESULT_OK) {
|
|
|
|
String result = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
|
|
|
|
if (result != null) {
|
|
|
|
if (result.startsWith("BEGIN:VCARD\n")) {
|
|
|
|
Matcher matcher = VCARD_XMPP_PATTERN.matcher(result);
|
|
|
|
if (matcher.find()) {
|
|
|
|
result = matcher.group(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Uri uri = Uri.parse(result);
|
|
|
|
handleUri(uri, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
2018-03-11 18:09:35 +00:00
|
|
|
}
|