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.clear();
135 this.share.uris.add(uri);
136 this.share.image = type.startsWith("image/") || isImage(uri);
137 } else {
138 this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
139 }
140 } else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
141 this.share.image = type != null && type.startsWith("image/");
142 if (!this.share.image) {
143 return;
144 }
145
146 this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
147 }
148 if (xmppConnectionServiceBound) {
149 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
150 }
151
152 }
153
154 protected boolean isImage(Uri uri) {
155 try {
156 String guess = URLConnection.guessContentTypeFromName(uri.toString());
157 return (guess != null && guess.startsWith("image/"));
158 } catch (final StringIndexOutOfBoundsException ignored) {
159 return false;
160 }
161 }
162
163 @Override
164 void onBackendConnected() {
165 if (xmppConnectionServiceBound && share != null
166 && share.contact != null && share.account != null) {
167 share();
168 return;
169 }
170 xmppConnectionService.populateWithOrderedConversations(mConversations,
171 this.share != null && this.share.uris.size() == 0);
172 }
173
174 private void share() {
175 Account account;
176 try {
177 account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
178 } catch (final InvalidJidException e) {
179 account = null;
180 }
181 if (account == null) {
182 return;
183 }
184 final Conversation conversation;
185 try {
186 conversation = xmppConnectionService
187 .findOrCreateConversation(account, Jid.fromString(share.contact), false);
188 } catch (final InvalidJidException e) {
189 return;
190 }
191 share(conversation);
192 }
193
194 private void share(final Conversation conversation) {
195 if (share.uris.size() != 0) {
196 OnPresenceSelected callback = new OnPresenceSelected() {
197 @Override
198 public void onPresenceSelected() {
199 if (share.image) {
200 Toast.makeText(getApplicationContext(),
201 getText(R.string.preparing_image),
202 Toast.LENGTH_LONG).show();
203 for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
204 ShareWithActivity.this.xmppConnectionService
205 .attachImageToConversation(conversation, i.next(),
206 attachFileCallback);
207 }
208 } else {
209 Toast.makeText(getApplicationContext(),
210 getText(R.string.preparing_file),
211 Toast.LENGTH_LONG).show();
212 ShareWithActivity.this.xmppConnectionService
213 .attachFileToConversation(conversation, share.uris.get(0),
214 attachFileCallback);
215 }
216 switchToConversation(conversation, null, true);
217 finish();
218 }
219 };
220 if (conversation.getAccount().httpUploadAvailable()) {
221 callback.onPresenceSelected();
222 } else {
223 selectPresence(conversation, callback);
224 }
225 } else {
226 switchToConversation(conversation, this.share.text, true);
227 finish();
228 }
229
230 }
231
232 public void refreshUiReal() {
233 //nothing to do. This Activity doesn't implement any listeners
234 }
235
236}