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