2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2017-10-27 15:25:01 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2015-12-06 10:55:37 +00:00
|
|
|
import android.util.Log;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2014-12-16 03:31:55 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-12-06 10:55:37 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-12-16 03:31:55 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2016-03-16 09:46:33 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2014-12-16 03:31:55 +00:00
|
|
|
import eu.siacs.conversations.ui.adapter.ConversationAdapter;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2014-12-16 03:31:55 +00:00
|
|
|
|
2016-03-16 09:46:33 +00:00
|
|
|
public class ShareWithActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {
|
|
|
|
|
2018-09-12 15:21:32 +00:00
|
|
|
private static final int REQUEST_STORAGE_PERMISSION = 0x733f32;
|
|
|
|
private Conversation mPendingConversation = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConversationUpdate() {
|
|
|
|
refreshUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Share {
|
|
|
|
ArrayList<Uri> uris = new ArrayList<>();
|
|
|
|
public String account;
|
|
|
|
public String contact;
|
|
|
|
public String text;
|
2019-08-26 11:40:47 +00:00
|
|
|
public boolean asQuote = false;
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Share share;
|
|
|
|
|
|
|
|
private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
|
|
|
|
private ConversationAdapter mAdapter;
|
2021-01-23 08:25:34 +00:00
|
|
|
private final List<Conversation> mConversations = new ArrayList<>();
|
2018-09-12 15:21:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (requestCode == REQUEST_START_NEW_CONVERSATION
|
|
|
|
&& resultCode == RESULT_OK) {
|
|
|
|
share.contact = data.getStringExtra("contact");
|
|
|
|
share.account = data.getStringExtra(EXTRA_ACCOUNT);
|
|
|
|
}
|
|
|
|
if (xmppConnectionServiceBound
|
|
|
|
&& share != null
|
|
|
|
&& share.contact != null
|
|
|
|
&& share.account != null) {
|
|
|
|
share();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-23 08:25:34 +00:00
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
2018-09-12 15:21:32 +00:00
|
|
|
if (grantResults.length > 0)
|
|
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (requestCode == REQUEST_STORAGE_PERMISSION) {
|
|
|
|
if (this.mPendingConversation != null) {
|
|
|
|
share(this.mPendingConversation);
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, "unable to find stored conversation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-08 15:03:30 +00:00
|
|
|
Toast.makeText(this, getString(R.string.no_storage_permission, getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_share_with);
|
|
|
|
|
|
|
|
setSupportActionBar(findViewById(R.id.toolbar));
|
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
|
|
|
getSupportActionBar().setHomeButtonEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTitle(getString(R.string.title_activity_sharewith));
|
|
|
|
|
|
|
|
RecyclerView mListView = findViewById(R.id.choose_conversation_list);
|
|
|
|
mAdapter = new ConversationAdapter(this, this.mConversations);
|
|
|
|
mListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
|
|
|
|
mListView.setAdapter(mAdapter);
|
|
|
|
mAdapter.setConversationClickListener((view, conversation) -> share(conversation));
|
|
|
|
this.share = new Share();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.share_with, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_add:
|
|
|
|
final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
|
2018-12-01 12:27:00 +00:00
|
|
|
intent.putExtra("direct_search",true);
|
2018-09-12 15:21:32 +00:00
|
|
|
startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
Intent intent = getIntent();
|
|
|
|
if (intent == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String type = intent.getType();
|
|
|
|
final String action = intent.getAction();
|
2019-05-28 20:32:42 +00:00
|
|
|
final Uri data = intent.getData();
|
2018-09-12 15:21:32 +00:00
|
|
|
if (Intent.ACTION_SEND.equals(action)) {
|
|
|
|
final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
|
|
|
|
final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
2019-08-26 11:40:47 +00:00
|
|
|
final boolean asQuote = intent.getBooleanExtra(ConversationsActivity.EXTRA_AS_QUOTE, false);
|
2019-07-14 14:39:09 +00:00
|
|
|
|
2019-05-28 20:32:42 +00:00
|
|
|
if (data != null && "geo".equals(data.getScheme())) {
|
|
|
|
this.share.uris.clear();
|
|
|
|
this.share.uris.add(data);
|
2019-07-14 14:39:09 +00:00
|
|
|
} else if (type != null && uri != null) {
|
2018-09-12 15:21:32 +00:00
|
|
|
this.share.uris.clear();
|
|
|
|
this.share.uris.add(uri);
|
|
|
|
} else {
|
|
|
|
this.share.text = text;
|
2019-08-26 11:40:47 +00:00
|
|
|
this.share.asQuote = asQuote;
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
|
|
|
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
|
2019-08-05 19:32:20 +00:00
|
|
|
final ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
|
|
|
this.share.uris = uris == null ? new ArrayList<>() : uris;
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
|
|
|
if (xmppConnectionServiceBound) {
|
2019-01-11 16:19:54 +00:00
|
|
|
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0, false);
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
|
|
|
if (xmppConnectionServiceBound && share != null && ((share.contact != null && share.account != null))) {
|
|
|
|
share();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
refreshUiReal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void share() {
|
|
|
|
final Conversation conversation;
|
|
|
|
Account account;
|
|
|
|
try {
|
2020-05-20 18:14:13 +00:00
|
|
|
account = xmppConnectionService.findAccountByJid(Jid.ofEscaped(share.account));
|
2018-09-12 15:21:32 +00:00
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
account = null;
|
|
|
|
}
|
|
|
|
if (account == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
conversation = xmppConnectionService.findOrCreateConversation(account, Jid.of(share.contact), false, true);
|
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
share(conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void share(final Conversation conversation) {
|
|
|
|
if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
|
|
|
|
mPendingConversation = conversation;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Intent intent = new Intent(this, ConversationsActivity.class);
|
|
|
|
intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
|
|
|
|
if (share.uris.size() > 0) {
|
|
|
|
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
|
|
|
|
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, share.uris);
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
} else if (share.text != null) {
|
|
|
|
intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
|
2018-09-18 15:03:08 +00:00
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, share.text);
|
2019-08-26 11:40:47 +00:00
|
|
|
intent.putExtra(ConversationsActivity.EXTRA_AS_QUOTE, share.asQuote);
|
2018-09-12 15:21:32 +00:00
|
|
|
}
|
2019-11-01 09:40:17 +00:00
|
|
|
try {
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
Toast.makeText(this, R.string.sharing_application_not_grant_permission, Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
2018-09-12 15:21:32 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refreshUiReal() {
|
2019-01-11 16:19:54 +00:00
|
|
|
//TODO inject desired order to not resort on refresh
|
|
|
|
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0, false);
|
2018-09-12 15:21:32 +00:00
|
|
|
mAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2014-12-16 03:31:55 +00:00
|
|
|
}
|