1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.content.pm.PackageManager;
5import android.net.Uri;
6import android.os.Bundle;
7import android.support.v7.widget.LinearLayoutManager;
8import android.support.v7.widget.RecyclerView;
9import android.util.Log;
10import android.view.Menu;
11import android.view.MenuItem;
12import android.widget.Toast;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import eu.siacs.conversations.Config;
18import eu.siacs.conversations.R;
19import eu.siacs.conversations.entities.Account;
20import eu.siacs.conversations.entities.Conversation;
21import eu.siacs.conversations.services.XmppConnectionService;
22import eu.siacs.conversations.ui.adapter.ConversationAdapter;
23import eu.siacs.conversations.ui.service.EmojiService;
24import eu.siacs.conversations.utils.GeoHelper;
25import rocks.xmpp.addr.Jid;
26
27public class ShareWithActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {
28
29 private static final int REQUEST_STORAGE_PERMISSION = 0x733f32;
30 private Conversation mPendingConversation = null;
31
32 @Override
33 public void onConversationUpdate() {
34 refreshUi();
35 }
36
37 private class Share {
38 ArrayList<Uri> uris = new ArrayList<>();
39 public String account;
40 public String contact;
41 public String text;
42 }
43
44 private Share share;
45
46 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
47 private ConversationAdapter mAdapter;
48 private List<Conversation> mConversations = new ArrayList<>();
49
50
51 protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
52 super.onActivityResult(requestCode, resultCode, data);
53 if (requestCode == REQUEST_START_NEW_CONVERSATION
54 && resultCode == RESULT_OK) {
55 share.contact = data.getStringExtra("contact");
56 share.account = data.getStringExtra(EXTRA_ACCOUNT);
57 }
58 if (xmppConnectionServiceBound
59 && share != null
60 && share.contact != null
61 && share.account != null) {
62 share();
63 }
64 }
65
66 @Override
67 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
68 if (grantResults.length > 0)
69 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
70 if (requestCode == REQUEST_STORAGE_PERMISSION) {
71 if (this.mPendingConversation != null) {
72 share(this.mPendingConversation);
73 } else {
74 Log.d(Config.LOGTAG, "unable to find stored conversation");
75 }
76 }
77 } else {
78 Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
79 }
80 }
81
82 @Override
83 protected void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
85 setContentView(R.layout.activity_share_with);
86
87 setSupportActionBar(findViewById(R.id.toolbar));
88 if (getSupportActionBar() != null) {
89 getSupportActionBar().setDisplayHomeAsUpEnabled(false);
90 getSupportActionBar().setHomeButtonEnabled(false);
91 }
92
93 setTitle(getString(R.string.title_activity_sharewith));
94
95 RecyclerView mListView = findViewById(R.id.choose_conversation_list);
96 mAdapter = new ConversationAdapter(this, this.mConversations);
97 mListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
98 mListView.setAdapter(mAdapter);
99 mAdapter.setConversationClickListener((view, conversation) -> share(conversation));
100 this.share = new Share();
101 }
102
103 @Override
104 public boolean onCreateOptionsMenu(Menu menu) {
105 getMenuInflater().inflate(R.menu.share_with, menu);
106 return true;
107 }
108
109 @Override
110 public boolean onOptionsItemSelected(final MenuItem item) {
111 switch (item.getItemId()) {
112 case R.id.action_add:
113 final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
114 intent.putExtra("direct_search",true);
115 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
116 return true;
117 }
118 return super.onOptionsItemSelected(item);
119 }
120
121 @Override
122 public void onStart() {
123 super.onStart();
124 Intent intent = getIntent();
125 if (intent == null) {
126 return;
127 }
128 final String type = intent.getType();
129 final String action = intent.getAction();
130 final Uri data = intent.getData();
131 if (Intent.ACTION_SEND.equals(action)) {
132 final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
133 final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
134 if (data != null && "geo".equals(data.getScheme())) {
135 this.share.uris.clear();
136 this.share.uris.add(data);
137 } else if (type != null && uri != null && (text == null || !type.equals("text/plain"))) {
138 this.share.uris.clear();
139 this.share.uris.add(uri);
140 } else {
141 this.share.text = text;
142 }
143 } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
144 this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
145 }
146 if (xmppConnectionServiceBound) {
147 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0, false);
148 }
149
150 }
151
152 @Override
153 void onBackendConnected() {
154 if (xmppConnectionServiceBound && share != null && ((share.contact != null && share.account != null))) {
155 share();
156 return;
157 }
158 refreshUiReal();
159 }
160
161 private void share() {
162 final Conversation conversation;
163 Account account;
164 try {
165 account = xmppConnectionService.findAccountByJid(Jid.of(share.account));
166 } catch (final IllegalArgumentException e) {
167 account = null;
168 }
169 if (account == null) {
170 return;
171 }
172
173 try {
174 conversation = xmppConnectionService.findOrCreateConversation(account, Jid.of(share.contact), false, true);
175 } catch (final IllegalArgumentException e) {
176 return;
177 }
178 share(conversation);
179 }
180
181 private void share(final Conversation conversation) {
182 if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
183 mPendingConversation = conversation;
184 return;
185 }
186 Intent intent = new Intent(this, ConversationsActivity.class);
187 intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
188 if (share.uris.size() > 0) {
189 intent.setAction(Intent.ACTION_SEND_MULTIPLE);
190 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, share.uris);
191 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
192 } else if (share.text != null) {
193 intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
194 intent.putExtra(Intent.EXTRA_TEXT, share.text);
195 }
196 startActivity(intent);
197 finish();
198 }
199
200 public void refreshUiReal() {
201 //TODO inject desired order to not resort on refresh
202 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0, false);
203 mAdapter.notifyDataSetChanged();
204 }
205}