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