attempt to parse Link header from https url scanned from welcome screen
This commit is contained in:
parent
cc489ef7bf
commit
6d2e406ee5
|
@ -106,7 +106,8 @@ public class WelcomeActivity extends XmppActivity implements XmppConnectionServi
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewIntent(Intent intent) {
|
public void onNewIntent(final Intent intent) {
|
||||||
|
super.onNewIntent(intent);
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
setIntent(intent);
|
setIntent(intent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,24 +7,39 @@ import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.databinding.DataBindingUtil;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
|
import eu.siacs.conversations.databinding.ActivityUriHandlerBinding;
|
||||||
|
import eu.siacs.conversations.http.HttpConnectionManager;
|
||||||
import eu.siacs.conversations.persistance.DatabaseBackend;
|
import eu.siacs.conversations.persistance.DatabaseBackend;
|
||||||
import eu.siacs.conversations.services.QuickConversationsService;
|
import eu.siacs.conversations.services.QuickConversationsService;
|
||||||
import eu.siacs.conversations.utils.ProvisioningUtils;
|
import eu.siacs.conversations.utils.ProvisioningUtils;
|
||||||
import eu.siacs.conversations.utils.SignupUtils;
|
import eu.siacs.conversations.utils.SignupUtils;
|
||||||
import eu.siacs.conversations.utils.XmppUri;
|
import eu.siacs.conversations.utils.XmppUri;
|
||||||
import eu.siacs.conversations.xmpp.Jid;
|
import eu.siacs.conversations.xmpp.Jid;
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class UriHandlerActivity extends AppCompatActivity {
|
public class UriHandlerActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
@ -34,7 +49,9 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN = 0x6789;
|
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN = 0x6789;
|
||||||
private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN_AND_PROVISION = 0x6790;
|
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");
|
private static final Pattern V_CARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n");
|
||||||
private boolean handled = false;
|
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("<(.*?)>");
|
||||||
|
private ActivityUriHandlerBinding binding;
|
||||||
|
private Call call;
|
||||||
|
|
||||||
public static void scan(final Activity activity) {
|
public static void scan(final Activity activity) {
|
||||||
scan(activity, false);
|
scan(activity, false);
|
||||||
|
@ -77,9 +94,7 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
this.handled = savedInstanceState != null && savedInstanceState.getBoolean("handled", false);
|
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_uri_handler);
|
||||||
getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
|
|
||||||
setSupportActionBar(findViewById(R.id.toolbar));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -88,23 +103,17 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
handleIntent(getIntent());
|
handleIntent(getIntent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSaveInstanceState(Bundle savedInstanceState) {
|
|
||||||
savedInstanceState.putBoolean("handled", this.handled);
|
|
||||||
super.onSaveInstanceState(savedInstanceState);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewIntent(final Intent intent) {
|
public void onNewIntent(final Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
handleIntent(intent);
|
handleIntent(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleUri(Uri uri) {
|
private boolean handleUri(final Uri uri) {
|
||||||
handleUri(uri, false);
|
return handleUri(uri, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleUri(Uri uri, final boolean scanned) {
|
private boolean handleUri(final Uri uri, final boolean scanned) {
|
||||||
final Intent intent;
|
final Intent intent;
|
||||||
final XmppUri xmppUri = new XmppUri(uri);
|
final XmppUri xmppUri = new XmppUri(uri);
|
||||||
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true);
|
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true);
|
||||||
|
@ -114,19 +123,22 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
final Jid jid = xmppUri.getJid();
|
final Jid jid = xmppUri.getJid();
|
||||||
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
|
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
|
||||||
if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) {
|
if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) {
|
||||||
Toast.makeText(this, R.string.account_already_exists, Toast.LENGTH_LONG).show();
|
showError(R.string.account_already_exists);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preAuth);
|
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preAuth);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
if (accounts.size() == 0 && xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter(XmppUri.PARAMETER_IBR))) {
|
if (accounts.size() == 0 && xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter(XmppUri.PARAMETER_IBR))) {
|
||||||
intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preAuth);
|
intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preAuth);
|
||||||
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
} else if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
|
||||||
|
showError(R.string.account_registrations_are_not_supported);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accounts.size() == 0) {
|
if (accounts.size() == 0) {
|
||||||
|
@ -134,15 +146,14 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
intent = SignupUtils.getSignUpIntent(this);
|
intent = SignupUtils.getSignUpIntent(this);
|
||||||
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
|
showError(R.string.invalid_jid);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xmppUri.isAction(XmppUri.ACTION_MESSAGE)) {
|
if (xmppUri.isAction(XmppUri.ACTION_MESSAGE)) {
|
||||||
|
|
||||||
final Jid jid = xmppUri.getJid();
|
final Jid jid = xmppUri.getJid();
|
||||||
final String body = xmppUri.getBody();
|
final String body = xmppUri.getBody();
|
||||||
|
|
||||||
|
@ -177,11 +188,57 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
intent.putExtra("scanned", scanned);
|
intent.putExtra("scanned", scanned);
|
||||||
intent.setData(uri);
|
intent.setData(uri);
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
|
showError(R.string.invalid_jid);
|
||||||
return;
|
return false;
|
||||||
|
}
|
||||||
|
startActivity(intent);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
startActivity(intent);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Class<?> findShareViaAccountClass() {
|
private static Class<?> findShareViaAccountClass() {
|
||||||
|
@ -192,29 +249,33 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleIntent(Intent data) {
|
private void handleIntent(final Intent data) {
|
||||||
if (handled) {
|
final String action = data == null ? null : data.getAction();
|
||||||
|
if (action == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data == null || data.getAction() == null) {
|
switch (action) {
|
||||||
finish();
|
case Intent.ACTION_MAIN:
|
||||||
return;
|
binding.progress.setVisibility(call != null && !call.isCanceled() ? View.VISIBLE : View.INVISIBLE);
|
||||||
}
|
break;
|
||||||
|
|
||||||
handled = true;
|
|
||||||
|
|
||||||
switch (data.getAction()) {
|
|
||||||
case Intent.ACTION_VIEW:
|
case Intent.ACTION_VIEW:
|
||||||
case Intent.ACTION_SENDTO:
|
case Intent.ACTION_SENDTO:
|
||||||
handleUri(data.getData());
|
if (handleUri(data.getData())) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ACTION_SCAN_QR_CODE:
|
case ACTION_SCAN_QR_CODE:
|
||||||
Intent intent = new Intent(this, ScanActivity.class);
|
Log.d(Config.LOGTAG, "scan. allow=" + allowProvisioning());
|
||||||
startActivityForResult(intent, REQUEST_SCAN_QR_CODE);
|
setIntent(createMainIntent());
|
||||||
return;
|
startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_SCAN_QR_CODE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
finish();
|
private Intent createMainIntent() {
|
||||||
|
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||||
|
intent.putExtra(EXTRA_ALLOW_PROVISIONING, allowProvisioning());
|
||||||
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean allowProvisioning() {
|
private boolean allowProvisioning() {
|
||||||
|
@ -226,6 +287,7 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
|
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
|
||||||
super.onActivityResult(requestCode, requestCode, intent);
|
super.onActivityResult(requestCode, requestCode, intent);
|
||||||
if (requestCode == REQUEST_SCAN_QR_CODE && resultCode == RESULT_OK) {
|
if (requestCode == REQUEST_SCAN_QR_CODE && resultCode == RESULT_OK) {
|
||||||
|
final boolean allowProvisioning = allowProvisioning();
|
||||||
final String result = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
|
final String result = intent.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
|
||||||
if (Strings.isNullOrEmpty(result)) {
|
if (Strings.isNullOrEmpty(result)) {
|
||||||
finish();
|
finish();
|
||||||
|
@ -234,19 +296,35 @@ public class UriHandlerActivity extends AppCompatActivity {
|
||||||
if (result.startsWith("BEGIN:VCARD\n")) {
|
if (result.startsWith("BEGIN:VCARD\n")) {
|
||||||
final Matcher matcher = V_CARD_XMPP_PATTERN.matcher(result);
|
final Matcher matcher = V_CARD_XMPP_PATTERN.matcher(result);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
handleUri(Uri.parse(matcher.group(2)), true);
|
if (handleUri(Uri.parse(matcher.group(2)), true)) {
|
||||||
}
|
|
||||||
finish();
|
finish();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
showError(R.string.no_xmpp_adddress_found);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
} else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning()) {
|
} else if (QuickConversationsService.isConversations() && looksLikeJsonObject(result) && allowProvisioning) {
|
||||||
ProvisioningUtils.provision(this, result);
|
ProvisioningUtils.provision(this, result);
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
handleUri(Uri.parse(result), true);
|
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();
|
finish();
|
||||||
}
|
}
|
||||||
|
} else if (handleUri(uri, true)) {
|
||||||
|
finish();
|
||||||
|
} else {
|
||||||
|
setIntent(new Intent(Intent.ACTION_VIEW, uri));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean looksLikeJsonObject(final String input) {
|
private static boolean looksLikeJsonObject(final String input) {
|
||||||
final String trimmed = Strings.nullToEmpty(input).trim();
|
final String trimmed = Strings.nullToEmpty(input).trim();
|
||||||
|
|
|
@ -35,6 +35,8 @@ public class XmppUri {
|
||||||
private Map<String, String> parameters = Collections.emptyMap();
|
private Map<String, String> parameters = Collections.emptyMap();
|
||||||
private boolean safeSource = true;
|
private boolean safeSource = true;
|
||||||
|
|
||||||
|
public static final String INVITE_DOMAIN = "conversations.im";
|
||||||
|
|
||||||
public XmppUri(final String uri) {
|
public XmppUri(final String uri) {
|
||||||
try {
|
try {
|
||||||
parse(Uri.parse(uri));
|
parse(Uri.parse(uri));
|
||||||
|
@ -136,10 +138,10 @@ public class XmppUri {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
String scheme = uri.getScheme();
|
final String scheme = uri.getScheme();
|
||||||
String host = uri.getHost();
|
final String host = uri.getHost();
|
||||||
List<String> segments = uri.getPathSegments();
|
List<String> segments = uri.getPathSegments();
|
||||||
if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
|
if ("https".equalsIgnoreCase(scheme) && INVITE_DOMAIN.equalsIgnoreCase(host)) {
|
||||||
if (segments.size() >= 2 && segments.get(1).contains("@")) {
|
if (segments.size() >= 2 && segments.get(1).contains("@")) {
|
||||||
// sample : https://conversations.im/i/foo@bar.com
|
// sample : https://conversations.im/i/foo@bar.com
|
||||||
try {
|
try {
|
||||||
|
|
31
src/main/res/layout/activity_uri_handler.xml
Normal file
31
src/main/res/layout/activity_uri_handler.xml
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="?colorPrimaryDark"
|
||||||
|
android:padding="24dp">
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerInParent="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:indeterminateTint="@color/white" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/error"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/progress"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:textAppearance="@style/TextAppearance.Conversations.Body2"
|
||||||
|
android:textColor="@color/white87"
|
||||||
|
android:visibility="invisible" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
</layout>
|
|
@ -968,5 +968,7 @@
|
||||||
<string name="backup_started_message">The backup has been started. You’ll get a notification once it has been completed.</string>
|
<string name="backup_started_message">The backup has been started. You’ll get a notification once it has been completed.</string>
|
||||||
<string name="unable_to_enable_video">Unable to enable video.</string>
|
<string name="unable_to_enable_video">Unable to enable video.</string>
|
||||||
<string name="plain_text_document">Plain text document</string>
|
<string name="plain_text_document">Plain text document</string>
|
||||||
|
<string name="account_registrations_are_not_supported">Account registrations are not supported</string>
|
||||||
|
<string name="no_xmpp_adddress_found">No XMPP address found</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in a new issue