Handle NFC action in UriHandlerActivity

This commit is contained in:
Marc Schink 2017-12-10 12:36:21 +01:00
parent d5f8a4dacb
commit 7dc68522a2
3 changed files with 43 additions and 45 deletions

View File

@ -69,14 +69,6 @@
<data android:scheme="xmpp" /> <data android:scheme="xmpp" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="imto" />
<data android:host="jabber" />
</intent-filter>
<intent-filter android:autoVerify="true"> <intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -88,20 +80,20 @@
<data android:pathPrefix="/i/" /> <data android:pathPrefix="/i/" />
<data android:pathPrefix="/j/" /> <data android:pathPrefix="/j/" />
</intent-filter> </intent-filter>
</activity>
<activity
android:name=".ui.StartConversationActivity"
android:label="@string/title_activity_start_conversation"
android:launchMode="singleTop">
<intent-filter> <intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" /> <action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:scheme="imto" /> <data android:scheme="imto" />
<data android:host="jabber" /> <data android:host="jabber" />
</intent-filter> </intent-filter>
</activity>
<activity
android:name=".ui.StartConversationActivity"
android:label="@string/title_activity_start_conversation"
android:launchMode="singleTop">
</activity> </activity>
<activity <activity
android:name=".ui.WelcomeActivity" android:name=".ui.WelcomeActivity"

View File

@ -2,7 +2,6 @@ package eu.siacs.conversations.ui;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.ActionBar.Tab; import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener; import android.app.ActionBar.TabListener;
@ -20,12 +19,8 @@ import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter; import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.text.Editable; import android.text.Editable;
@ -806,11 +801,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
setIntent(null); setIntent(null);
} }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Invite getInviteJellyBean(NdefRecord record) {
return new Invite(record.toUri());
}
protected boolean handleIntent(Intent intent) { protected boolean handleIntent(Intent intent) {
if (intent == null || intent.getAction() == null) { if (intent == null || intent.getAction() == null) {
return false; return false;
@ -826,27 +816,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} else { } else {
return false; return false;
} }
case NfcAdapter.ACTION_NDEF_DISCOVERED:
for (Parcelable message : getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
if (message instanceof NdefMessage) {
for (NdefRecord record : ((NdefMessage) message).getRecords()) {
switch (record.getTnf()) {
case NdefRecord.TNF_WELL_KNOWN:
if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return getInviteJellyBean(record).invite();
} else {
byte[] payload = record.getPayload();
if (payload[0] == 0) {
return new Invite(Uri.parse(new String(Arrays.copyOfRange(
payload, 1, payload.length)))).invite();
}
}
}
}
}
}
}
} }
return false; return false;
} }

View File

@ -1,8 +1,14 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Parcelable;
import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult; import com.google.zxing.integration.android.IntentResult;
@ -27,6 +33,11 @@ public class UriHandlerActivity extends Activity {
handleIntent(intent); handleIntent(intent);
} }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Uri getInviteJellyBean(NdefRecord record) {
return record.toUri();
}
private void handleUri(Uri uri) { private void handleUri(Uri uri) {
final Intent intent; final Intent intent;
final XmppUri xmppUri = new XmppUri(uri); final XmppUri xmppUri = new XmppUri(uri);
@ -62,6 +73,30 @@ public class UriHandlerActivity extends Activity {
startActivity(intent); startActivity(intent);
} }
private void handleNfcIntent(Intent data) {
for (Parcelable message : data.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
if (message instanceof NdefMessage) {
for (NdefRecord record : ((NdefMessage) message).getRecords()) {
switch (record.getTnf()) {
case NdefRecord.TNF_WELL_KNOWN:
if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
handleUri(getInviteJellyBean(record));
} else {
byte[] payload = record.getPayload();
if (payload[0] == 0) {
Uri uri = Uri.parse(new String(Arrays.copyOfRange(
payload, 1, payload.length)));
handleUri(uri);
}
}
}
}
}
}
}
}
private void handleIntent(Intent data) { private void handleIntent(Intent data) {
if (data == null) { if (data == null) {
finish(); finish();
@ -76,6 +111,8 @@ public class UriHandlerActivity extends Activity {
case ACTION_SCAN_QR_CODE: case ACTION_SCAN_QR_CODE:
new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC", "QR_CODE")); new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC", "QR_CODE"));
return; return;
case NfcAdapter.ACTION_NDEF_DISCOVERED:
handleNfcIntent(data);
} }
finish(); finish();