1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
package de.pixart.messenger.ui;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.pixart.messenger.R;
import de.pixart.messenger.persistance.DatabaseBackend;
import de.pixart.messenger.utils.SignupUtils;
import de.pixart.messenger.utils.XmppUri;
import me.drakeet.support.toast.ToastCompat;
import rocks.xmpp.addr.Jid;
public class UriHandlerActivity extends AppCompatActivity {
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;
private static final Pattern VCARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n");
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 {
ToastCompat.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);
final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true);
if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) {
final String preauth = xmppUri.getParameter("preauth");
final Jid jid = xmppUri.getJid();
if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) {
if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) {
ToastCompat.makeText(this, R.string.account_already_exists, Toast.LENGTH_LONG).show();
return;
}
intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth);
startActivity(intent);
return;
}
if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParameter("ibr"))) {
intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth);
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
startActivity(intent);
return;
}
}
if (accounts.size() == 0) {
if (xmppUri.isValidJid()) {
intent = SignupUtils.getSignUpIntent(this);
intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString());
startActivity(intent);
} else {
ToastCompat.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("de.pixart.messenger.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);
intent.setAction(Intent.ACTION_VIEW);
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);
} else if (xmppUri.isValidJid()) {
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 {
ToastCompat.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();
}
}
|