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.widget.Toast;
|
2017-12-10 10:44:10 +00:00
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
2020-06-21 13:40:51 +00:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
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;
|
2020-06-21 13:40:51 +00:00
|
|
|
import eu.siacs.conversations.services.QuickConversationsService;
|
|
|
|
import eu.siacs.conversations.utils.ProvisioningUtils;
|
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;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.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";
|
2020-06-21 13:40:51 +00:00
|
|
|
private static final String EXTRA_ALLOW_PROVISIONING = "extra_allow_provisioning";
|
2018-10-09 17:35:33 +00:00
|
|
|
private static final int REQUEST_SCAN_QR_CODE = 0x1234;
|
|
|
|
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN = 0x6789;
|
2020-06-21 13:40:51 +00:00
|
|
|
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION = 0x6790;
|
|
|
|
private static final Pattern V_CARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n");
|
2018-10-09 17:35:33 +00:00
|
|
|
private boolean handled = false;
|
|
|
|
|
2020-06-21 13:40:51 +00:00
|
|
|
public static void scan(final Activity activity) {
|
|
|
|
scan(activity, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void scan(final Activity activity, final boolean provisioning) {
|
2018-10-09 17:35:33 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
2020-06-21 13:40:51 +00:00
|
|
|
final Intent intent = new Intent(activity, UriHandlerActivity.class);
|
2018-10-09 17:35:33 +00:00
|
|
|
intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE);
|
2020-06-21 13:40:51 +00:00
|
|
|
if (provisioning) {
|
|
|
|
intent.putExtra(EXTRA_ALLOW_PROVISIONING, true);
|
|
|
|
}
|
2018-10-09 17:35:33 +00:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
|
|
|
|
activity.startActivity(intent);
|
|
|
|
} else {
|
2020-06-21 13:40:51 +00:00
|
|
|
activity.requestPermissions(
|
|
|
|
new String[]{Manifest.permission.CAMERA},
|
|
|
|
provisioning ? REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION : REQUEST_CAMERA_PERMISSIONS_TO_SCAN
|
|
|
|
);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void onRequestPermissionResult(Activity activity, int requestCode, int[] grantResults) {
|
2020-06-21 13:40:51 +00:00
|
|
|
if (requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN && requestCode != REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) {
|
2018-10-09 17:35:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (grantResults.length > 0) {
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
2020-06-21 13:40:51 +00:00
|
|
|
if (requestCode == REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION) {
|
|
|
|
scan(activity, true);
|
|
|
|
} else {
|
|
|
|
scan(activity);
|
|
|
|
}
|
2018-10-09 17:35:33 +00:00
|
|
|
} 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
|
2021-04-30 09:32:02 +00:00
|
|
|
public void onNewIntent(final Intent intent) {
|
|
|
|
super.onNewIntent(intent);
|
2018-10-09 17:35:33 +00:00
|
|
|
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-06-21 13:40:51 +00:00
|
|
|
final String preAuth = xmppUri.getParameter(XmppUri.PARAMETER_PRE_AUTH);
|
2020-01-09 13:13:05 +00:00
|
|
|
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-06-21 13:40:51 +00:00
|
|
|
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preAuth);
|
2020-01-09 19:10:19 +00:00
|
|
|
startActivity(intent);
|
2020-01-09 14:49:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-04-30 09:32:02 +00:00
|
|
|
if (accounts.size() == 0 && xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter(XmppUri.PARAMETER_IBR))) {
|
2020-06-21 13:40:51 +00:00
|
|
|
intent = SignupUtils.getTokenRegistrationIntent(this, 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();
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:40:51 +00:00
|
|
|
private boolean allowProvisioning() {
|
|
|
|
final Intent launchIntent = getIntent();
|
|
|
|
return launchIntent != null && launchIntent.getBooleanExtra(EXTRA_ALLOW_PROVISIONING, false);
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:35:33 +00:00
|
|
|
@Override
|
2020-06-21 13:40:51 +00:00
|
|
|
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
|
2018-10-09 17:35:33 +00:00
|
|
|
super.onActivityResult(requestCode, requestCode, intent);
|
|
|
|
if (requestCode == REQUEST_SCAN_QR_CODE && resultCode == RESULT_OK) {
|
2020-06-21 13:40:51 +00:00
|
|
|
final String result = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
|
|
|
|
if (Strings.isNullOrEmpty(result)) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result.startsWith("BEGIN:VCARD\n")) {
|
|
|
|
final Matcher matcher = V_CARD_XMPP_PATTERN.matcher(result);
|
|
|
|
if (matcher.find()) {
|
|
|
|
handleUri(Uri.parse(matcher.group(2)), true);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
} else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning()) {
|
|
|
|
ProvisioningUtils.provision(this, result);
|
|
|
|
finish();
|
|
|
|
return;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
handleUri(Uri.parse(result), true);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
|
|
|
|
private static boolean looksLikeJsonObject(final String input) {
|
2020-12-07 08:03:01 +00:00
|
|
|
final String trimmed = Strings.emptyToNull(input).trim();
|
|
|
|
return trimmed.charAt(0) == '{' && trimmed.charAt(trimmed.length() - 1) == '}';
|
2020-06-21 13:40:51 +00:00
|
|
|
}
|
2018-03-11 18:09:35 +00:00
|
|
|
}
|