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