1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.Config;
7import eu.siacs.conversations.R;
8import eu.siacs.conversations.entities.Account;
9import eu.siacs.conversations.entities.Conversation;
10import eu.siacs.conversations.entities.Message;
11import eu.siacs.conversations.ui.adapter.ConversationAdapter;
12import eu.siacs.conversations.xmpp.jid.InvalidJidException;
13import eu.siacs.conversations.xmpp.jid.Jid;
14
15import android.app.PendingIntent;
16import android.content.Intent;
17import android.net.Uri;
18import android.os.Bundle;
19import android.util.Log;
20import android.view.Menu;
21import android.view.MenuItem;
22import android.view.View;
23import android.widget.AdapterView;
24import android.widget.AdapterView.OnItemClickListener;
25import android.widget.ListView;
26import android.widget.Toast;
27
28public class ShareWithActivity extends XmppActivity {
29
30 private class Share {
31 public Uri uri;
32 public String account;
33 public String contact;
34 public String text;
35 }
36
37 private Share share;
38
39 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
40 private ListView mListView;
41 private List<Conversation> mConversations = new ArrayList<Conversation>();
42
43 private UiCallback<Message> attachImageCallback = new UiCallback<Message>() {
44
45 @Override
46 public void userInputRequried(PendingIntent pi, Message object) {
47 // TODO Auto-generated method stub
48
49 }
50
51 @Override
52 public void success(Message message) {
53 xmppConnectionService.sendMessage(message);
54 }
55
56 @Override
57 public void error(int errorCode, Message object) {
58 // TODO Auto-generated method stub
59
60 }
61 };
62
63 protected void onActivityResult(int requestCode, int resultCode,
64 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 Log.d(Config.LOGTAG, "contact: " + share.contact + " account:"
71 + share.account);
72 }
73 if (xmppConnectionServiceBound && share != null
74 && share.contact != null && share.account != null) {
75 share();
76 }
77 }
78
79 @Override
80 protected void onCreate(Bundle savedInstanceState) {
81
82 super.onCreate(savedInstanceState);
83
84 getActionBar().setDisplayHomeAsUpEnabled(false);
85 getActionBar().setHomeButtonEnabled(false);
86
87 setContentView(R.layout.share_with);
88 setTitle(getString(R.string.title_activity_sharewith));
89
90 mListView = (ListView) findViewById(R.id.choose_conversation_list);
91 ConversationAdapter mAdapter = new ConversationAdapter(this,
92 this.mConversations);
93 mListView.setAdapter(mAdapter);
94 mListView.setOnItemClickListener(new OnItemClickListener() {
95
96 @Override
97 public void onItemClick(AdapterView<?> arg0, View arg1,
98 int position, long arg3) {
99 Conversation conversation = mConversations.get(position);
100 if (conversation.getMode() == Conversation.MODE_SINGLE
101 || share.uri == null) {
102 share(mConversations.get(position));
103 }
104 }
105 });
106
107 this.share = new Share();
108 }
109
110 @Override
111 public boolean onCreateOptionsMenu(Menu menu) {
112 getMenuInflater().inflate(R.menu.share_with, menu);
113 return true;
114 }
115
116 @Override
117 public boolean onOptionsItemSelected(MenuItem item) {
118 switch (item.getItemId()) {
119 case R.id.action_add:
120 Intent intent = new Intent(getApplicationContext(),
121 ChooseContactActivity.class);
122 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
123 return true;
124 }
125 return super.onOptionsItemSelected(item);
126 }
127
128 @Override
129 public void onStart() {
130 if (getIntent().getType() != null
131 && getIntent().getType().startsWith("image/")) {
132 this.share.uri = (Uri) getIntent().getParcelableExtra(
133 Intent.EXTRA_STREAM);
134 } else {
135 this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
136 }
137 if (xmppConnectionServiceBound) {
138 xmppConnectionService.populateWithOrderedConversations(
139 mConversations, this.share.uri == null);
140 }
141 super.onStart();
142 }
143
144 @Override
145 void onBackendConnected() {
146 if (xmppConnectionServiceBound && share != null
147 && share.contact != null && share.account != null) {
148 share();
149 return;
150 }
151 xmppConnectionService.populateWithOrderedConversations(mConversations,
152 this.share != null && this.share.uri == null);
153 }
154
155 private void share() {
156 Account account;
157 try {
158 account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
159 } catch (final InvalidJidException e) {
160 account = null;
161 }
162 if (account == null) {
163 return;
164 }
165 final Conversation conversation;
166 try {
167 conversation = xmppConnectionService
168 .findOrCreateConversation(account, Jid.fromString(share.contact), false);
169 } catch (final InvalidJidException e) {
170 return;
171 }
172 share(conversation);
173 }
174
175 private void share(final Conversation conversation) {
176 if (share.uri != null) {
177 selectPresence(conversation, new OnPresenceSelected() {
178 @Override
179 public void onPresenceSelected() {
180 Toast.makeText(getApplicationContext(),
181 getText(R.string.preparing_image),
182 Toast.LENGTH_LONG).show();
183 ShareWithActivity.this.xmppConnectionService
184 .attachImageToConversation(conversation, share.uri,
185 attachImageCallback);
186 switchToConversation(conversation, null, true);
187 finish();
188 }
189 });
190
191 } else {
192 switchToConversation(conversation, this.share.text, true);
193 finish();
194 }
195
196 }
197
198}