Merge pull request #2720 from af8a524db1/uri_message

Handle 'message action' XMPP URIs
This commit is contained in:
Daniel Gultsch 2017-12-15 16:17:05 +01:00 committed by GitHub
commit 6a9abd0e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 306 additions and 75 deletions

View File

@ -57,17 +57,9 @@
</intent-filter>
</activity>
<activity
android:name=".ui.StartConversationActivity"
android:name=".ui.UriHandlerActivity"
android:label="@string/title_activity_start_conversation"
android:launchMode="singleTop">
<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>
<action android:name="android.intent.action.VIEW" />
@ -76,13 +68,6 @@
<data android:scheme="xmpp" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="xmpp" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
@ -94,7 +79,20 @@
<data android:pathPrefix="/i/" />
<data android:pathPrefix="/j/" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="imto" />
<data android:host="jabber" />
</intent-filter>
</activity>
<activity
android:name=".ui.StartConversationActivity"
android:label="@string/title_activity_start_conversation"
android:launchMode="singleTop">
</activity>
<activity
android:name=".ui.WelcomeActivity"
@ -126,6 +124,10 @@
android:name=".ui.ManageAccountActivity"
android:label="@string/title_activity_manage_accounts"
android:launchMode="singleTask" />
<activity
android:name=".ui.ShareViaAccountActivity"
android:label="@string/title_activity_share_via_account"
android:launchMode="singleTop" />
<activity
android:name=".ui.EditAccountActivity"
android:launchMode="singleTop"

View File

@ -0,0 +1,106 @@
package eu.siacs.conversations.ui;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.ui.adapter.AccountAdapter;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
public class ShareViaAccountActivity extends XmppActivity {
public static final String EXTRA_CONTACT = "contact";
public static final String EXTRA_BODY = "body";
protected final List<Account> accountList = new ArrayList<>();
protected ListView accountListView;
protected AccountAdapter mAccountAdapter;
@Override
protected void refreshUiReal() {
synchronized (this.accountList) {
accountList.clear();
accountList.addAll(xmppConnectionService.getAccounts());
}
ActionBar actionBar = getActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(this.accountList.size() > 0);
actionBar.setDisplayHomeAsUpEnabled(this.accountList.size() > 0);
}
mAccountAdapter.notifyDataSetChanged();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_accounts);
accountListView = (ListView) findViewById(R.id.account_list);
this.mAccountAdapter = new AccountAdapter(this, accountList, false);
accountListView.setAdapter(this.mAccountAdapter);
accountListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
final Account account = accountList.get(position);
final String body = getIntent().getStringExtra(EXTRA_BODY);
try {
final Jid contact = Jid.fromString(getIntent().getStringExtra(EXTRA_CONTACT));
final Conversation conversation = xmppConnectionService.findOrCreateConversation(
account, contact, false, false);
switchToConversation(conversation, body, false);
} catch (InvalidJidException e) {
// ignore error
}
finish();
}
});
}
@Override
protected void onStart() {
super.onStart();
final int theme = findTheme();
if (this.mTheme != theme) {
recreate();
}
}
@Override
void onBackendConnected() {
final int numAccounts = xmppConnectionService.getAccounts().size();
if (numAccounts == 1) {
final String body = getIntent().getStringExtra(EXTRA_BODY);
final Account account = xmppConnectionService.getAccounts().get(0);
try {
final Jid contact = Jid.fromString(getIntent().getStringExtra(EXTRA_CONTACT));
final Conversation conversation = xmppConnectionService.findOrCreateConversation(
account, contact, false, false);
switchToConversation(conversation, body, false);
} catch (InvalidJidException e) {
// ignore error
}
finish();
} else {
refreshUiReal();
}
}
}

View File

@ -2,7 +2,6 @@ package eu.siacs.conversations.ui;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
@ -20,12 +19,8 @@ import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
@ -55,9 +50,6 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -644,7 +636,9 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
showCreateConferenceDialog();
return true;
case R.id.action_scan_qr_code:
new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC","QR_CODE"));
Intent intent = new Intent(this, UriHandlerActivity.class);
intent.setAction(UriHandlerActivity.ACTION_SCAN_QR_CODE);
startActivity(intent);
return true;
case R.id.action_hide_offline:
mHideOfflineContacts = !item.isChecked();
@ -682,20 +676,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if ((requestCode & 0xFFFF) == IntentIntegrator.REQUEST_CODE) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null && scanResult.getFormatName() != null) {
String data = scanResult.getContents();
Invite invite = new Invite(data);
if (xmppConnectionServiceBound) {
invite.invite();
} else if (invite.getJid() != null) {
this.mPendingInvite = invite;
} else {
this.mPendingInvite = null;
}
}
} else if (resultCode == RESULT_OK) {
if (resultCode == RESULT_OK) {
if (xmppConnectionServiceBound) {
this.mPostponedActivityResult = null;
if (requestCode == REQUEST_CREATE_CONFERENCE) {
@ -820,11 +801,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
setIntent(null);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Invite getInviteJellyBean(NdefRecord record) {
return new Invite(record.toUri());
}
protected boolean handleIntent(Intent intent) {
if (intent == null || intent.getAction() == null) {
return false;
@ -840,27 +816,6 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} else {
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;
}
@ -876,7 +831,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return true;
}
List<Contact> contacts = xmppConnectionService.findContacts(invite.getJid(),invite.account);
if (invite.isMuc()) {
if (invite.isAction(XmppUri.ACTION_JOIN)) {
Conversation muc = xmppConnectionService.findFirstMuc(invite.getJid());
if (muc != null) {
switchToConversation(muc,invite.getBody(),false);
@ -1202,9 +1157,5 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
}
return false;
}
public boolean isMuc() {
return muc;
}
}
}

View File

@ -0,0 +1,136 @@
package eu.siacs.conversations.ui;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
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.IntentResult;
import java.util.Arrays;
import eu.siacs.conversations.persistance.DatabaseBackend;
import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xmpp.jid.Jid;
public class UriHandlerActivity extends Activity {
public static final String ACTION_SCAN_QR_CODE = "scan_qr_code";
@Override
public void onStart() {
super.onStart();
handleIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
handleIntent(intent);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Uri getInviteJellyBean(NdefRecord record) {
return record.toUri();
}
private void handleUri(Uri uri) {
final Intent intent;
final XmppUri xmppUri = new XmppUri(uri);
final int numAccounts = DatabaseBackend.getInstance(this).getAccountJids().size();
if (numAccounts == 0) {
intent = new Intent(getApplicationContext(), WelcomeActivity.class);
startActivity(intent);
return;
}
if (xmppUri.isAction(XmppUri.ACTION_MESSAGE)) {
final Jid jid = xmppUri.getJid();
final String body = xmppUri.getBody();
if (jid != null) {
intent = new Intent(getApplicationContext(), ShareViaAccountActivity.class);
intent.putExtra(ShareViaAccountActivity.EXTRA_CONTACT, jid.toString());
intent.putExtra(ShareViaAccountActivity.EXTRA_BODY, body);
} else {
intent = new Intent(getApplicationContext(), ShareWithActivity.class);
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, body);
}
} else {
intent = new Intent(getApplicationContext(), StartConversationActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setData(uri);
}
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) {
if (data == null) {
finish();
return;
}
switch (data.getAction()) {
case Intent.ACTION_VIEW:
case Intent.ACTION_SENDTO:
handleUri(data.getData());
break;
case ACTION_SCAN_QR_CODE:
new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC", "QR_CODE"));
return;
case NfcAdapter.ACTION_NDEF_DISCOVERED:
handleNfcIntent(data);
}
finish();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if ((requestCode & 0xFFFF) == IntentIntegrator.REQUEST_CODE) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode,
intent);
if (scanResult != null && scanResult.getFormatName() != null) {
String data = scanResult.getContents();
handleUri(Uri.parse(data));
}
}
finish();
super.onActivityResult(requestCode, requestCode, intent);
}
}

View File

@ -29,10 +29,18 @@ import eu.siacs.conversations.utils.UIHelper;
public class AccountAdapter extends ArrayAdapter<Account> {
private XmppActivity activity;
private boolean showStateButton;
public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
super(activity, 0, objects);
this.activity = activity;
this.showStateButton = showStateButton;
}
public AccountAdapter(XmppActivity activity, List<Account> objects) {
super(activity, 0, objects);
this.activity = activity;
this.showStateButton = true;
}
@Override
@ -68,6 +76,11 @@ public class AccountAdapter extends ArrayAdapter<Account> {
final Switch tglAccountState = (Switch) view.findViewById(R.id.tgl_account_status);
final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
tglAccountState.setChecked(!isDisabled,false);
if (this.showStateButton) {
tglAccountState.setVisibility(View.VISIBLE);
} else {
tglAccountState.setVisibility(View.GONE);
}
tglAccountState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

View File

@ -14,14 +14,17 @@ import eu.siacs.conversations.xmpp.jid.Jid;
public class XmppUri {
protected String jid;
protected boolean muc;
protected List<Fingerprint> fingerprints = new ArrayList<>();
private String body;
private String action;
protected boolean safeSource = true;
public static final String OMEMO_URI_PARAM = "omemo-sid-";
public static final String OTR_URI_PARAM = "otr-fingerprint";
public static final String ACTION_JOIN = "join";
public static final String ACTION_MESSAGE = "message";
public XmppUri(String uri) {
try {
parse(Uri.parse(uri));
@ -63,11 +66,21 @@ public class XmppUri {
// sample : https://conversations.im/i/foo/bar.com
jid = segments.get(1) + "@" + segments.get(2);
}
muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
if (segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0))) {
action = ACTION_JOIN;
}
fingerprints = parseFingerprints(uri.getQuery(),'&');
} else if ("xmpp".equalsIgnoreCase(scheme)) {
// sample: xmpp:foo@bar.com
muc = isMuc(uri.getQuery());
final String query = uri.getQuery();
if (hasAction(query, ACTION_JOIN)) {
this.action = ACTION_JOIN;
} else if (hasAction(query, ACTION_MESSAGE)) {
this.action = ACTION_MESSAGE;
}
if (uri.getAuthority() != null) {
jid = uri.getAuthority();
} else {
@ -138,16 +151,24 @@ public class XmppUri {
return null;
}
protected boolean isMuc(String query) {
private boolean hasAction(String query, String action) {
for(String pair : query == null ? new String[0] : query.split(";")) {
final String[] parts = pair.split("=",2);
if (parts.length == 1 && "join".equals(parts[0])) {
if (parts.length == 1 && parts[0].equals(action)) {
return true;
}
}
return false;
}
public boolean isAction(final String action) {
if (this.action == null) {
return false;
}
return this.action.equals(action);
}
public Jid getJid() {
try {
return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());

View File

@ -20,6 +20,7 @@
<string name="title_activity_sharewith">Mit Unterhaltung teilen</string>
<string name="title_activity_start_conversation">Unterhaltung beginnen</string>
<string name="title_activity_choose_contact">Kontakt auswählen</string>
<string name="title_activity_share_via_account">Über Account teilen</string>
<string name="title_activity_block_list">Sperrliste</string>
<string name="just_now">gerade</string>
<string name="minute_ago">vor einer Minute</string>

View File

@ -21,6 +21,7 @@
<string name="title_activity_sharewith">Share with Conversation</string>
<string name="title_activity_start_conversation">Start Conversation</string>
<string name="title_activity_choose_contact">Choose Contact</string>
<string name="title_activity_share_via_account">Share via account</string>
<string name="title_activity_block_list">Block list</string>
<string name="just_now">just now</string>
<string name="minute_ago">1 min ago</string>