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;
|
2021-10-07 07:48:49 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2018-02-25 22:58:56 +00:00
|
|
|
import android.widget.Toast;
|
2017-12-10 10:44:10 +00:00
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
import androidx.annotation.StringRes;
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.core.content.ContextCompat;
|
2021-10-07 07:48:49 +00:00
|
|
|
import androidx.databinding.DataBindingUtil;
|
2021-01-23 08:25:34 +00:00
|
|
|
|
2020-06-21 13:40:51 +00:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
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
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-02-24 20:57:42 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2021-10-07 07:48:49 +00:00
|
|
|
import eu.siacs.conversations.databinding.ActivityUriHandlerBinding;
|
|
|
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
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;
|
2021-10-07 07:48:49 +00:00
|
|
|
import okhttp3.Call;
|
|
|
|
import okhttp3.Callback;
|
|
|
|
import okhttp3.HttpUrl;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
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");
|
2021-10-07 07:48:49 +00:00
|
|
|
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("<(.*?)>");
|
|
|
|
private ActivityUriHandlerBinding binding;
|
|
|
|
private Call call;
|
2018-10-09 17:35:33 +00:00
|
|
|
|
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);
|
2021-10-07 07:48:49 +00:00
|
|
|
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_uri_handler);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
handleIntent(getIntent());
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
private boolean handleUri(final Uri uri) {
|
|
|
|
return handleUri(uri, false);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
private boolean handleUri(final Uri uri, final boolean scanned) {
|
2018-10-09 17:35:33 +00:00
|
|
|
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())) {
|
2021-10-07 07:48:49 +00:00
|
|
|
showError(R.string.account_already_exists);
|
|
|
|
return false;
|
2020-01-09 14:49:49 +00:00
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preAuth);
|
2020-01-09 19:10:19 +00:00
|
|
|
startActivity(intent);
|
2021-10-07 07:48:49 +00:00
|
|
|
return true;
|
2020-01-09 14:49:49 +00:00
|
|
|
}
|
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);
|
2021-10-07 07:48:49 +00:00
|
|
|
return true;
|
2020-01-09 13:13:05 +00:00
|
|
|
}
|
2021-10-07 07:48:49 +00:00
|
|
|
} else if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
|
|
|
|
showError(R.string.account_registrations_are_not_supported);
|
|
|
|
return false;
|
2020-01-09 13:13:05 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-10-07 07:48:49 +00:00
|
|
|
return true;
|
2018-10-09 17:35:33 +00:00
|
|
|
} else {
|
2021-10-07 07:48:49 +00:00
|
|
|
showError(R.string.invalid_jid);
|
|
|
|
return false;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xmppUri.isAction(XmppUri.ACTION_MESSAGE)) {
|
|
|
|
final Jid jid = xmppUri.getJid();
|
|
|
|
final String body = xmppUri.getBody();
|
|
|
|
|
|
|
|
if (jid != null) {
|
2021-10-06 10:18:58 +00:00
|
|
|
final Class<?> clazz = findShareViaAccountClass();
|
2018-10-09 17:35:33 +00:00
|
|
|
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 {
|
2021-10-07 07:48:49 +00:00
|
|
|
showError(R.string.invalid_jid);
|
|
|
|
return false;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
startActivity(intent);
|
2021-10-07 07:48:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkForLinkHeader(final HttpUrl url) {
|
|
|
|
Log.d(Config.LOGTAG, "checking for link header on " + url);
|
|
|
|
this.call = HttpConnectionManager.OK_HTTP_CLIENT.newCall(new Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
.head()
|
|
|
|
.build());
|
|
|
|
this.call.enqueue(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to check HTTP url", e);
|
|
|
|
showError(R.string.no_xmpp_adddress_found);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NotNull Call call, @NotNull Response response) {
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
final String linkHeader = response.header("Link");
|
|
|
|
if (linkHeader != null && processLinkHeader(linkHeader)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showError(R.string.no_xmpp_adddress_found);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean processLinkHeader(final String header) {
|
|
|
|
final Matcher matcher = LINK_HEADER_PATTERN.matcher(header);
|
|
|
|
if (matcher.find()) {
|
|
|
|
final String group = matcher.group();
|
|
|
|
final String link = group.substring(1, group.length() - 1);
|
|
|
|
if (handleUri(Uri.parse(link))) {
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showError(@StringRes int error) {
|
|
|
|
this.binding.progress.setVisibility(View.INVISIBLE);
|
|
|
|
this.binding.error.setText(error);
|
|
|
|
this.binding.error.setVisibility(View.VISIBLE);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 10:18:58 +00:00
|
|
|
private static Class<?> findShareViaAccountClass() {
|
|
|
|
try {
|
|
|
|
return Class.forName("eu.siacs.conversations.ui.ShareViaAccountActivity");
|
|
|
|
} catch (final ClassNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
private void handleIntent(final Intent data) {
|
|
|
|
final String action = data == null ? null : data.getAction();
|
|
|
|
if (action == null) {
|
2018-10-09 17:35:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-07 07:48:49 +00:00
|
|
|
switch (action) {
|
|
|
|
case Intent.ACTION_MAIN:
|
|
|
|
binding.progress.setVisibility(call != null && !call.isCanceled() ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
break;
|
2018-10-09 17:35:33 +00:00
|
|
|
case Intent.ACTION_VIEW:
|
|
|
|
case Intent.ACTION_SENDTO:
|
2021-10-07 07:48:49 +00:00
|
|
|
if (handleUri(data.getData())) {
|
|
|
|
finish();
|
|
|
|
}
|
2018-10-09 17:35:33 +00:00
|
|
|
break;
|
|
|
|
case ACTION_SCAN_QR_CODE:
|
2021-10-07 07:48:49 +00:00
|
|
|
Log.d(Config.LOGTAG, "scan. allow=" + allowProvisioning());
|
|
|
|
setIntent(createMainIntent());
|
|
|
|
startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_SCAN_QR_CODE);
|
|
|
|
break;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
2021-10-07 07:48:49 +00:00
|
|
|
}
|
2018-10-09 17:35:33 +00:00
|
|
|
|
2021-10-07 07:48:49 +00:00
|
|
|
private Intent createMainIntent() {
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
|
|
intent.putExtra(EXTRA_ALLOW_PROVISIONING, allowProvisioning());
|
|
|
|
return intent;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-10-07 07:48:49 +00:00
|
|
|
final boolean allowProvisioning = allowProvisioning();
|
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()) {
|
2021-10-07 07:48:49 +00:00
|
|
|
if (handleUri(Uri.parse(matcher.group(2)), true)) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showError(R.string.no_xmpp_adddress_found);
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
return;
|
2021-10-07 07:48:49 +00:00
|
|
|
} else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning) {
|
2020-06-21 13:40:51 +00:00
|
|
|
ProvisioningUtils.provision(this, result);
|
|
|
|
finish();
|
|
|
|
return;
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
2021-10-07 07:48:49 +00:00
|
|
|
final Uri uri = Uri.parse(result.trim());
|
|
|
|
if (allowProvisioning && "https".equalsIgnoreCase(uri.getScheme()) && !XmppUri.INVITE_DOMAIN.equalsIgnoreCase(uri.getHost())) {
|
|
|
|
final HttpUrl httpUrl = HttpUrl.parse(uri.toString());
|
|
|
|
if (httpUrl != null) {
|
|
|
|
checkForLinkHeader(httpUrl);
|
|
|
|
} else {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
} else if (handleUri(uri, true)) {
|
|
|
|
finish();
|
|
|
|
} else {
|
|
|
|
setIntent(new Intent(Intent.ACTION_VIEW, uri));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
finish();
|
2018-10-09 17:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
|
|
|
|
private static boolean looksLikeJsonObject(final String input) {
|
2021-10-06 10:18:58 +00:00
|
|
|
final String trimmed = Strings.nullToEmpty(input).trim();
|
2020-12-07 08:03:01 +00:00
|
|
|
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
|
|
|
}
|