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.io.UnsupportedEncodingException;
17import java.net.URLConnection;
18import java.net.URLDecoder;
19import java.nio.charset.UnsupportedCharsetException;
20import java.util.ArrayList;
21import java.util.List;
22
23import eu.siacs.conversations.Config;
24import eu.siacs.conversations.R;
25import eu.siacs.conversations.entities.Account;
26import eu.siacs.conversations.entities.Conversation;
27import eu.siacs.conversations.entities.Message;
28import eu.siacs.conversations.ui.adapter.ConversationAdapter;
29import eu.siacs.conversations.xmpp.jid.InvalidJidException;
30import eu.siacs.conversations.xmpp.jid.Jid;
31
32public class ShareWithActivity extends XmppActivity {
33
34 private class Share {
35 public Uri uri;
36 public boolean image;
37 public String account;
38 public String contact;
39 public String text;
40 }
41
42 private Share share;
43
44 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
45 private ListView mListView;
46 private List<Conversation> mConversations = new ArrayList<>();
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(Message message) {
58 xmppConnectionService.sendMessage(message);
59 }
60
61 @Override
62 public void error(int errorCode, Message object) {
63 // TODO Auto-generated method stub
64
65 }
66 };
67
68 protected void onActivityResult(int requestCode, int resultCode,
69 final Intent data) {
70 super.onActivityResult(requestCode, resultCode, data);
71 if (requestCode == REQUEST_START_NEW_CONVERSATION
72 && resultCode == RESULT_OK) {
73 share.contact = data.getStringExtra("contact");
74 share.account = data.getStringExtra("account");
75 Log.d(Config.LOGTAG, "contact: " + share.contact + " account:"
76 + share.account);
77 }
78 if (xmppConnectionServiceBound && share != null
79 && share.contact != null && share.account != null) {
80 share();
81 }
82 }
83
84 @Override
85 protected void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87
88 if (getActionBar() != null) {
89 getActionBar().setDisplayHomeAsUpEnabled(false);
90 getActionBar().setHomeButtonEnabled(false);
91 }
92
93 setContentView(R.layout.share_with);
94 setTitle(getString(R.string.title_activity_sharewith));
95
96 mListView = (ListView) findViewById(R.id.choose_conversation_list);
97 ConversationAdapter mAdapter = new ConversationAdapter(this,
98 this.mConversations);
99 mListView.setAdapter(mAdapter);
100 mListView.setOnItemClickListener(new OnItemClickListener() {
101
102 @Override
103 public void onItemClick(AdapterView<?> arg0, View arg1,
104 int position, long arg3) {
105 Conversation conversation = mConversations.get(position);
106 if (conversation.getMode() == Conversation.MODE_SINGLE
107 || share.uri == null) {
108 share(mConversations.get(position));
109 }
110 }
111 });
112
113 this.share = new Share();
114 }
115
116 @Override
117 public boolean onCreateOptionsMenu(Menu menu) {
118 getMenuInflater().inflate(R.menu.share_with, menu);
119 return true;
120 }
121
122 @Override
123 public boolean onOptionsItemSelected(final MenuItem item) {
124 switch (item.getItemId()) {
125 case R.id.action_add:
126 final Intent intent = new Intent(getApplicationContext(),
127 ChooseContactActivity.class);
128 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
129 return true;
130 }
131 return super.onOptionsItemSelected(item);
132 }
133
134 @Override
135 public void onStart() {
136 final String type = getIntent().getType();
137 final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
138 if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) {
139 this.share.uri = uri;
140 this.share.image = type.startsWith("image/") || isImage(uri);
141 } else {
142 this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
143 }
144 if (xmppConnectionServiceBound) {
145 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uri == null);
146 }
147 super.onStart();
148 }
149
150 protected boolean isImage(Uri uri) {
151 try {
152 String guess = URLConnection.guessContentTypeFromName(uri.toString());
153 return (guess != null && guess.startsWith("image/"));
154 } catch (final StringIndexOutOfBoundsException ignored) {
155 return false;
156 }
157 }
158
159 @Override
160 void onBackendConnected() {
161 if (xmppConnectionServiceBound && share != null
162 && share.contact != null && share.account != null) {
163 share();
164 return;
165 }
166 xmppConnectionService.populateWithOrderedConversations(mConversations,
167 this.share != null && this.share.uri == null);
168 }
169
170 private void share() {
171 Account account;
172 try {
173 account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
174 } catch (final InvalidJidException e) {
175 account = null;
176 }
177 if (account == null) {
178 return;
179 }
180 final Conversation conversation;
181 try {
182 conversation = xmppConnectionService
183 .findOrCreateConversation(account, Jid.fromString(share.contact), false);
184 } catch (final InvalidJidException e) {
185 return;
186 }
187 share(conversation);
188 }
189
190 private void share(final Conversation conversation) {
191 if (share.uri != null) {
192 selectPresence(conversation, new OnPresenceSelected() {
193 @Override
194 public void onPresenceSelected() {
195 if (share.image) {
196 Toast.makeText(getApplicationContext(),
197 getText(R.string.preparing_image),
198 Toast.LENGTH_LONG).show();
199 ShareWithActivity.this.xmppConnectionService
200 .attachImageToConversation(conversation, share.uri,
201 attachFileCallback);
202 } else {
203 Toast.makeText(getApplicationContext(),
204 getText(R.string.preparing_file),
205 Toast.LENGTH_LONG).show();
206 ShareWithActivity.this.xmppConnectionService
207 .attachFileToConversation(conversation, share.uri,
208 attachFileCallback);
209 }
210 switchToConversation(conversation, null, true);
211 finish();
212 }
213 });
214
215 } else {
216 switchToConversation(conversation, this.share.text, true);
217 finish();
218 }
219
220 }
221
222}