Merge pull request #620 from emdete/p0

P0
This commit is contained in:
Daniel Gultsch 2014-11-04 10:50:19 +01:00
commit 17070d972e

View file

@ -16,6 +16,7 @@ import android.net.Uri;
import android.nfc.NdefMessage; import android.nfc.NdefMessage;
import android.nfc.NdefRecord; import android.nfc.NdefRecord;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
@ -44,7 +45,9 @@ import com.google.zxing.integration.android.IntentResult;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -530,13 +533,9 @@ public class StartConversationActivity extends XmppActivity {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null && scanResult.getFormatName() != null) { if (scanResult != null && scanResult.getFormatName() != null) {
String data = scanResult.getContents(); String data = scanResult.getContents();
Invite invite = parseInviteUri(data); Invite invite = new Invite(data);
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
if (invite.muc) { invite.invite();
showJoinConferenceDialog(invite.jid);
} else {
handleJid(invite.jid);
}
} else if (invite.jid != null) { } else if (invite.jid != null) {
this.mPendingInvite = invite; this.mPendingInvite = invite;
} else { } else {
@ -560,11 +559,7 @@ public class StartConversationActivity extends XmppActivity {
this.mKnownConferenceHosts = xmppConnectionService this.mKnownConferenceHosts = xmppConnectionService
.getKnownConferenceHosts(); .getKnownConferenceHosts();
if (this.mPendingInvite != null) { if (this.mPendingInvite != null) {
if (this.mPendingInvite.muc) { mPendingInvite.invite();
showJoinConferenceDialog(this.mPendingInvite.jid);
} else {
handleJid(this.mPendingInvite.jid);
}
this.mPendingInvite = null; this.mPendingInvite = null;
} else if (!handleIntent(getIntent())) { } else if (!handleIntent(getIntent())) {
if (mSearchEditText != null) { if (mSearchEditText != null) {
@ -580,13 +575,12 @@ public class StartConversationActivity extends XmppActivity {
if (intent == null || intent.getAction() == null) { if (intent == null || intent.getAction() == null) {
return false; return false;
} }
String jid; Invite invite = null;
Uri uri;
Invite invite;
switch (intent.getAction()) { switch (intent.getAction()) {
case Intent.ACTION_SENDTO: case Intent.ACTION_SENDTO:
try { try {
jid = URLDecoder.decode( // TODO use Intent.parse ?!?
String jid = URLDecoder.decode(
intent.getData().getEncodedPath(), "UTF-8").split( intent.getData().getEncodedPath(), "UTF-8").split(
"/")[1]; "/")[1];
return handleJid(jid); return handleJid(jid);
@ -594,55 +588,30 @@ public class StartConversationActivity extends XmppActivity {
return false; return false;
} }
case Intent.ACTION_VIEW: case Intent.ACTION_VIEW:
uri = intent.getData(); invite = new Invite(intent.getData());
invite = parseInviteUri(uri); invite.invite();
if (invite.muc) {
showJoinConferenceDialog(invite.jid);
return false;
} else {
return handleJid(invite.jid);
}
case NfcAdapter.ACTION_NDEF_DISCOVERED: case NfcAdapter.ACTION_NDEF_DISCOVERED:
if (android.os.Build.VERSION.SDK_INT >= 16) {
Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage message = (NdefMessage) messages[0]; NdefMessage message = (NdefMessage) messages[0];
NdefRecord record = message.getRecords()[0]; NdefRecord record = message.getRecords()[0];
invite = parseInviteUri(record.toUri()); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
invite = new Invite(record.toUri());
} else {
byte[] mPayload = record.getPayload();
if (mPayload[0] == 0) {
invite = new Invite(Uri.parse(new String(Arrays.copyOfRange(
mPayload, 1, mPayload.length), StandardCharsets.UTF_8)));
}
}
if (invite != null) { if (invite != null) {
if (invite.muc) { return invite.invite();
showJoinConferenceDialog(invite.jid); }
return false; return false;
} else {
return handleJid(invite.jid);
}
}
} else {
return false;
}
default: default:
return false; return false;
} }
} }
private Invite parseInviteUri(String uri) {
try {
return parseInviteUri(Uri.parse(uri));
} catch (IllegalArgumentException e) {
return null;
}
}
private Invite parseInviteUri(Uri uri) {
Invite invite = new Invite();
invite.muc = uri.getQuery() != null && uri.getQuery().equalsIgnoreCase("join");
if (uri.getAuthority() != null) {
invite.jid = uri.getAuthority();
} else {
invite.jid = uri.getSchemeSpecificPart().split("\\?")[0];
}
return invite;
}
private boolean handleJid(String jid) { private boolean handleJid(String jid) {
List<Contact> contacts = xmppConnectionService.findContacts(jid); List<Contact> contacts = xmppConnectionService.findContacts(jid);
if (contacts.size() == 0) { if (contacts.size() == 0) {
@ -769,7 +738,39 @@ public class StartConversationActivity extends XmppActivity {
} }
private class Invite { private class Invite {
public String jid; private String jid;
public boolean muc; private boolean muc;
Invite(Uri uri) {
parse(uri);
}
Invite(String uri) {
try {
parse(Uri.parse(uri));
} catch (IllegalArgumentException e) {
jid = null;
}
}
boolean invite() {
if (jid != null) {
if (muc) {
showJoinConferenceDialog(jid);
} else {
return handleJid(jid);
}
}
return false;
}
void parse(Uri uri) {
muc = uri.getQuery() != null && uri.getQuery().equalsIgnoreCase("join");
if (uri.getAuthority() != null) {
jid = uri.getAuthority();
} else {
jid = uri.getSchemeSpecificPart().split("\\?")[0];
}
}
} }
} }