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.Config;
21import eu.siacs.conversations.R;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.entities.Conversation;
24import eu.siacs.conversations.entities.Message;
25import eu.siacs.conversations.ui.adapter.ConversationAdapter;
26import eu.siacs.conversations.xmpp.jid.InvalidJidException;
27import eu.siacs.conversations.xmpp.jid.Jid;
28
29public class ShareWithActivity extends XmppActivity {
30
31 private class Share {
32 public List<Uri> uris = new ArrayList<>();
33 public boolean image;
34 public String account;
35 public String contact;
36 public String text;
37 }
38
39 private Share share;
40
41 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
42 private ListView mListView;
43 private List<Conversation> mConversations = new ArrayList<>();
44
45 private UiCallback<Message> attachFileCallback = new UiCallback<Message>() {
46
47 @Override
48 public void userInputRequried(PendingIntent pi, Message object) {
49 // TODO Auto-generated method stub
50
51 }
52
53 @Override
54 public void success(Message message) {
55 xmppConnectionService.sendMessage(message);
56 }
57
58 @Override
59 public void error(int errorCode, Message object) {
60 // TODO Auto-generated method stub
61
62 }
63 };
64
65 protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
66 super.onActivityResult(requestCode, resultCode, data);
67 if (requestCode == REQUEST_START_NEW_CONVERSATION
68 && resultCode == RESULT_OK) {
69 share.contact = data.getStringExtra("contact");
70 share.account = data.getStringExtra("account");
71 }
72 if (xmppConnectionServiceBound
73 && share != null
74 && share.contact != null
75 && share.account != null) {
76 share();
77 }
78 }
79
80 @Override
81 protected void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83
84 if (getActionBar() != null) {
85 getActionBar().setDisplayHomeAsUpEnabled(false);
86 getActionBar().setHomeButtonEnabled(false);
87 }
88
89 setContentView(R.layout.share_with);
90 setTitle(getString(R.string.title_activity_sharewith));
91
92 mListView = (ListView) findViewById(R.id.choose_conversation_list);
93 ConversationAdapter mAdapter = new ConversationAdapter(this,
94 this.mConversations);
95 mListView.setAdapter(mAdapter);
96 mListView.setOnItemClickListener(new OnItemClickListener() {
97
98 @Override
99 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
100 share(mConversations.get(position));
101 }
102 });
103
104 this.share = new Share();
105 }
106
107 @Override
108 public boolean onCreateOptionsMenu(Menu menu) {
109 getMenuInflater().inflate(R.menu.share_with, menu);
110 return true;
111 }
112
113 @Override
114 public boolean onOptionsItemSelected(final MenuItem item) {
115 switch (item.getItemId()) {
116 case R.id.action_add:
117 final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
118 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
119 return true;
120 }
121 return super.onOptionsItemSelected(item);
122 }
123
124 @Override
125 public void onStart() {
126 super.onStart();
127 Intent intent = getIntent();
128 if (intent == null) {
129 return;
130 }
131 final String type = intent.getType();
132 if (Intent.ACTION_SEND.equals(intent.getAction())) {
133 final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
134 if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) {
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}