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 if (type != null && !type.startsWith("text/")) {
138 this.share.uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
139 try {
140 this.share.image = type.startsWith("image/")
141 || URLConnection.guessContentTypeFromName(this.share.uri.toString()).startsWith("image/");
142 } catch (final StringIndexOutOfBoundsException ignored) {
143 this.share.image = false;
144 }
145 } else {
146 this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
147 }
148 if (xmppConnectionServiceBound) {
149 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uri == null);
150 }
151 super.onStart();
152 }
153
154 @Override
155 void onBackendConnected() {
156 if (xmppConnectionServiceBound && share != null
157 && share.contact != null && share.account != null) {
158 share();
159 return;
160 }
161 xmppConnectionService.populateWithOrderedConversations(mConversations,
162 this.share != null && this.share.uri == null);
163 }
164
165 private void share() {
166 Account account;
167 try {
168 account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
169 } catch (final InvalidJidException e) {
170 account = null;
171 }
172 if (account == null) {
173 return;
174 }
175 final Conversation conversation;
176 try {
177 conversation = xmppConnectionService
178 .findOrCreateConversation(account, Jid.fromString(share.contact), false);
179 } catch (final InvalidJidException e) {
180 return;
181 }
182 share(conversation);
183 }
184
185 private void share(final Conversation conversation) {
186 if (share.uri != null) {
187 selectPresence(conversation, new OnPresenceSelected() {
188 @Override
189 public void onPresenceSelected() {
190 if (share.image) {
191 Toast.makeText(getApplicationContext(),
192 getText(R.string.preparing_image),
193 Toast.LENGTH_LONG).show();
194 ShareWithActivity.this.xmppConnectionService
195 .attachImageToConversation(conversation, share.uri,
196 attachFileCallback);
197 } else {
198 Toast.makeText(getApplicationContext(),
199 getText(R.string.preparing_file),
200 Toast.LENGTH_LONG).show();
201 ShareWithActivity.this.xmppConnectionService
202 .attachFileToConversation(conversation, share.uri,
203 attachFileCallback);
204 }
205 switchToConversation(conversation, null, true);
206 finish();
207 }
208 });
209
210 } else {
211 switchToConversation(conversation, this.share.text, true);
212 finish();
213 }
214
215 }
216
217}