1package eu.siacs.conversations.ui;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5import android.net.Uri;
6import android.os.Bundle;
7import android.util.Log;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.widget.AdapterView;
12import android.widget.AdapterView.OnItemClickListener;
13import android.widget.ListView;
14import android.widget.Toast;
15
16import java.net.URLConnection;
17import java.util.ArrayList;
18import java.util.Iterator;
19import java.util.List;
20
21import eu.siacs.conversations.Config;
22import eu.siacs.conversations.R;
23import eu.siacs.conversations.entities.Account;
24import eu.siacs.conversations.entities.Conversation;
25import eu.siacs.conversations.entities.Message;
26import eu.siacs.conversations.ui.adapter.ConversationAdapter;
27import eu.siacs.conversations.xmpp.jid.InvalidJidException;
28import eu.siacs.conversations.xmpp.jid.Jid;
29
30public class ShareWithActivity extends XmppActivity {
31
32 private class Share {
33 public List<Uri> uris = new ArrayList<>();
34 public boolean image;
35 public String account;
36 public String contact;
37 public String text;
38 public String uuid;
39 }
40
41 private Share share;
42
43 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
44 private ListView mListView;
45 private List<Conversation> mConversations = new ArrayList<>();
46 private Toast mToast;
47
48 private UiCallback<Message> attachFileCallback = new UiCallback<Message>() {
49
50 @Override
51 public void userInputRequried(PendingIntent pi, Message object) {
52 // TODO Auto-generated method stub
53
54 }
55
56 @Override
57 public void success(final Message message) {
58 xmppConnectionService.sendMessage(message);
59 runOnUiThread(new Runnable() {
60 @Override
61 public void run() {
62 if (mToast != null) {
63 mToast.cancel();
64 }
65 if (share.uuid != null) {
66 mToast = Toast.makeText(getApplicationContext(),
67 getString(share.image ? R.string.shared_image_with_x : R.string.shared_file_with_x,message.getConversation().getName()),
68 Toast.LENGTH_SHORT);
69 mToast.show();
70 }
71 }
72 });
73 }
74
75 @Override
76 public void error(int errorCode, Message object) {
77 // TODO Auto-generated method stub
78
79 }
80 };
81
82 protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
83 super.onActivityResult(requestCode, resultCode, data);
84 if (requestCode == REQUEST_START_NEW_CONVERSATION
85 && resultCode == RESULT_OK) {
86 share.contact = data.getStringExtra("contact");
87 share.account = data.getStringExtra(EXTRA_ACCOUNT);
88 }
89 if (xmppConnectionServiceBound
90 && share != null
91 && share.contact != null
92 && share.account != null) {
93 share();
94 }
95 }
96
97 @Override
98 protected void onCreate(Bundle savedInstanceState) {
99 super.onCreate(savedInstanceState);
100
101 if (getActionBar() != null) {
102 getActionBar().setDisplayHomeAsUpEnabled(false);
103 getActionBar().setHomeButtonEnabled(false);
104 }
105
106 setContentView(R.layout.share_with);
107 setTitle(getString(R.string.title_activity_sharewith));
108
109 mListView = (ListView) findViewById(R.id.choose_conversation_list);
110 ConversationAdapter mAdapter = new ConversationAdapter(this,
111 this.mConversations);
112 mListView.setAdapter(mAdapter);
113 mListView.setOnItemClickListener(new OnItemClickListener() {
114
115 @Override
116 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
117 share(mConversations.get(position));
118 }
119 });
120
121 this.share = new Share();
122 }
123
124 @Override
125 public boolean onCreateOptionsMenu(Menu menu) {
126 getMenuInflater().inflate(R.menu.share_with, menu);
127 return true;
128 }
129
130 @Override
131 public boolean onOptionsItemSelected(final MenuItem item) {
132 switch (item.getItemId()) {
133 case R.id.action_add:
134 final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
135 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
136 return true;
137 }
138 return super.onOptionsItemSelected(item);
139 }
140
141 @Override
142 public void onStart() {
143 super.onStart();
144 Intent intent = getIntent();
145 if (intent == null) {
146 return;
147 }
148 final String type = intent.getType();
149 Log.d(Config.LOGTAG, "action: "+intent.getAction()+ ", type:"+type);
150 share.uuid = intent.getStringExtra("uuid");
151 if (Intent.ACTION_SEND.equals(intent.getAction())) {
152 final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
153 if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) {
154 this.share.uris.clear();
155 this.share.uris.add(uri);
156 this.share.image = type.startsWith("image/") || isImage(uri);
157 } else {
158 this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
159 }
160 } else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
161 this.share.image = type != null && type.startsWith("image/");
162 if (!this.share.image) {
163 return;
164 }
165
166 this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
167 }
168 if (xmppConnectionServiceBound) {
169 if (share.uuid != null) {
170 share();
171 } else {
172 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
173 }
174 }
175
176 }
177
178 protected boolean isImage(Uri uri) {
179 try {
180 String guess = URLConnection.guessContentTypeFromName(uri.toString());
181 return (guess != null && guess.startsWith("image/"));
182 } catch (final StringIndexOutOfBoundsException ignored) {
183 return false;
184 }
185 }
186
187 @Override
188 void onBackendConnected() {
189 if (xmppConnectionServiceBound && share != null
190 && ((share.contact != null && share.account != null) || share.uuid != null)) {
191 share();
192 return;
193 }
194 xmppConnectionService.populateWithOrderedConversations(mConversations,
195 this.share != null && this.share.uris.size() == 0);
196 }
197
198 private void share() {
199 final Conversation conversation;
200 if (share.uuid != null) {
201 conversation = xmppConnectionService.findConversationByUuid(share.uuid);
202 if (conversation == null) {
203 return;
204 }
205 }else{
206 Account account;
207 try {
208 account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
209 } catch (final InvalidJidException e) {
210 account = null;
211 }
212 if (account == null) {
213 return;
214 }
215
216 try {
217 conversation = xmppConnectionService
218 .findOrCreateConversation(account, Jid.fromString(share.contact), false);
219 } catch (final InvalidJidException e) {
220 return;
221 }
222 }
223 share(conversation);
224 }
225
226 private void share(final Conversation conversation) {
227 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) {
228 if (share.uuid == null) {
229 showInstallPgpDialog();
230 } else {
231 Toast.makeText(this,R.string.openkeychain_not_installed,Toast.LENGTH_SHORT).show();
232 finish();
233 }
234 return;
235 }
236 if (share.uris.size() != 0) {
237 OnPresenceSelected callback = new OnPresenceSelected() {
238 @Override
239 public void onPresenceSelected() {
240 if (share.image) {
241 mToast = Toast.makeText(getApplicationContext(),
242 getText(R.string.preparing_image),
243 Toast.LENGTH_LONG);
244 mToast.show();
245 for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
246 ShareWithActivity.this.xmppConnectionService
247 .attachImageToConversation(conversation, i.next(),
248 attachFileCallback);
249 }
250 } else {
251 mToast = Toast.makeText(getApplicationContext(),
252 getText(R.string.preparing_file),
253 Toast.LENGTH_LONG);
254 mToast.show();
255 ShareWithActivity.this.xmppConnectionService
256 .attachFileToConversation(conversation, share.uris.get(0),
257 attachFileCallback);
258 }
259 if (share.uuid == null) {
260 switchToConversation(conversation, null, true);
261 }
262 finish();
263 }
264 };
265 if (conversation.getAccount().httpUploadAvailable()) {
266 callback.onPresenceSelected();
267 } else {
268 selectPresence(conversation, callback);
269 }
270 } else {
271 switchToConversation(conversation, this.share.text, true);
272 finish();
273 }
274
275 }
276
277 public void refreshUiReal() {
278 //nothing to do. This Activity doesn't implement any listeners
279 }
280
281}