1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.R;
7import eu.siacs.conversations.entities.Account;
8import eu.siacs.conversations.entities.Contact;
9import eu.siacs.conversations.entities.Conversation;
10import eu.siacs.conversations.entities.Message;
11import eu.siacs.conversations.utils.UIHelper;
12import android.os.Bundle;
13import android.preference.PreferenceManager;
14import android.app.AlertDialog;
15import android.app.FragmentTransaction;
16import android.content.Context;
17import android.content.DialogInterface;
18import android.content.Intent;
19import android.content.SharedPreferences;
20import android.graphics.Color;
21import android.graphics.Typeface;
22import android.support.v4.widget.SlidingPaneLayout;
23import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
24import android.util.Log;
25import android.view.KeyEvent;
26import android.view.LayoutInflater;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.AdapterView;
32import android.widget.AdapterView.OnItemClickListener;
33import android.widget.ArrayAdapter;
34import android.widget.ListView;
35import android.widget.PopupMenu;
36import android.widget.PopupMenu.OnMenuItemClickListener;
37import android.widget.TextView;
38import android.widget.ImageView;
39
40public class ConversationActivity extends XmppActivity {
41
42 public static final String VIEW_CONVERSATION = "viewConversation";
43 public static final String CONVERSATION = "conversationUuid";
44 public static final String TEXT = "text";
45
46 public static final int REQUEST_SEND_MESSAGE = 0x75441;
47 public static final int REQUEST_DECRYPT_PGP = 0x76783;
48
49 protected SlidingPaneLayout spl;
50
51 private List<Conversation> conversationList = new ArrayList<Conversation>();
52 private Conversation selectedConversation = null;
53 private ListView listView;
54
55 private boolean paneShouldBeOpen = true;
56 private boolean useSubject = true;
57 private ArrayAdapter<Conversation> listAdapter;
58
59 private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() {
60
61 @Override
62 public void onConversationListChanged() {
63 runOnUiThread(new Runnable() {
64
65 @Override
66 public void run() {
67 updateConversationList();
68 if(paneShouldBeOpen) {
69 if (conversationList.size() >= 1) {
70 swapConversationFragment();
71 } else {
72 startActivity(new Intent(getApplicationContext(), ContactsActivity.class));
73 finish();
74 }
75 }
76 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
77 if (selectedFragment!=null) {
78 selectedFragment.updateMessages();
79 }
80 }
81 });
82 }
83 };
84
85 private DialogInterface.OnClickListener addToRoster = new DialogInterface.OnClickListener() {
86
87 @Override
88 public void onClick(DialogInterface dialog, int which) {
89 String jid = getSelectedConversation().getContactJid();
90 Account account = getSelectedConversation().getAccount();
91 String name = jid.split("@")[0];
92 Contact contact = new Contact(account, name, jid, null);
93 xmppConnectionService.createContact(contact);
94 }
95 };
96 protected ConversationActivity activity = this;
97
98 public List<Conversation> getConversationList() {
99 return this.conversationList;
100 }
101
102 public Conversation getSelectedConversation() {
103 return this.selectedConversation;
104 }
105
106 public ListView getConversationListView() {
107 return this.listView;
108 }
109
110 public SlidingPaneLayout getSlidingPaneLayout() {
111 return this.spl;
112 }
113
114 public boolean shouldPaneBeOpen() {
115 return paneShouldBeOpen;
116 }
117
118 @Override
119 protected void onCreate(Bundle savedInstanceState) {
120
121 super.onCreate(savedInstanceState);
122
123 setContentView(R.layout.fragment_conversations_overview);
124
125 listView = (ListView) findViewById(R.id.list);
126
127 this.listAdapter = new ArrayAdapter<Conversation>(this,
128 R.layout.conversation_list_row, conversationList) {
129 @Override
130 public View getView(int position, View view, ViewGroup parent) {
131 if (view == null) {
132 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
133 view = (View) inflater.inflate(
134 R.layout.conversation_list_row, null);
135 }
136 Conversation conv;
137 if (conversationList.size() > position) {
138 conv = getItem(position);
139 } else {
140 return view;
141 }
142 if (!spl.isSlideable()) {
143 if (conv==getSelectedConversation()) {
144 view.setBackgroundColor(0xffdddddd);
145 } else {
146 view.setBackgroundColor(Color.TRANSPARENT);
147 }
148 } else {
149 view.setBackgroundColor(Color.TRANSPARENT);
150 }
151 TextView convName = (TextView) view.findViewById(R.id.conversation_name);
152 convName.setText(conv.getName(useSubject));
153 TextView convLastMsg = (TextView) view.findViewById(R.id.conversation_lastmsg);
154 convLastMsg.setText(conv.getLatestMessage().getBody());
155
156 if(!conv.isRead()) {
157 convName.setTypeface(null,Typeface.BOLD);
158 convLastMsg.setTypeface(null,Typeface.BOLD);
159 } else {
160 convName.setTypeface(null,Typeface.NORMAL);
161 convLastMsg.setTypeface(null,Typeface.NORMAL);
162 }
163
164 ((TextView) view.findViewById(R.id.conversation_lastupdate))
165 .setText(UIHelper.readableTimeDifference(conv.getLatestMessage().getTimeSent()));
166
167 ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
168 imageView.setImageBitmap(UIHelper.getContactPicture(conv.getContact(), conv.getName(useSubject),200, activity.getApplicationContext()));
169 return view;
170 }
171
172 };
173
174 listView.setAdapter(this.listAdapter);
175
176 listView.setOnItemClickListener(new OnItemClickListener() {
177
178 @Override
179 public void onItemClick(AdapterView<?> arg0, View clickedView,
180 int position, long arg3) {
181 paneShouldBeOpen = false;
182 if (selectedConversation != conversationList.get(position)) {
183 selectedConversation = conversationList.get(position);
184 swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
185 } else {
186 spl.closePane();
187 }
188 }
189 });
190 spl = (SlidingPaneLayout) findViewById(R.id.slidingpanelayout);
191 spl.setParallaxDistance(150);
192 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
193 spl.setSliderFadeColor(0);
194 spl.setPanelSlideListener(new PanelSlideListener() {
195
196 @Override
197 public void onPanelOpened(View arg0) {
198 paneShouldBeOpen = true;
199 getActionBar().setDisplayHomeAsUpEnabled(false);
200 getActionBar().setTitle(R.string.app_name);
201 invalidateOptionsMenu();
202 hideKeyboard();
203 }
204
205 @Override
206 public void onPanelClosed(View arg0) {
207 paneShouldBeOpen = false;
208 if (conversationList.size() > 0) {
209 getActionBar().setDisplayHomeAsUpEnabled(true);
210 getActionBar().setTitle(getSelectedConversation().getName(useSubject));
211 invalidateOptionsMenu();
212 if (!getSelectedConversation().isRead()) {
213 getSelectedConversation().markRead();
214 UIHelper.updateNotification(getApplicationContext(), getConversationList(), null, false);
215 listView.invalidateViews();
216 }
217 }
218 }
219
220 @Override
221 public void onPanelSlide(View arg0, float arg1) {
222 // TODO Auto-generated method stub
223
224 }
225 });
226 }
227
228 @Override
229 public boolean onCreateOptionsMenu(Menu menu) {
230 getMenuInflater().inflate(R.menu.conversations, menu);
231 MenuItem menuSecure = (MenuItem) menu.findItem(R.id.action_security);
232 MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
233 MenuItem menuMucDetails = (MenuItem) menu.findItem(R.id.action_muc_details);
234 MenuItem menuContactDetails = (MenuItem) menu.findItem(R.id.action_contact_details);
235 MenuItem menuInviteContacts = (MenuItem) menu.findItem(R.id.action_invite);
236
237 if ((spl.isOpen()&&(spl.isSlideable()))) {
238 menuArchive.setVisible(false);
239 menuMucDetails.setVisible(false);
240 menuContactDetails.setVisible(false);
241 menuSecure.setVisible(false);
242 menuInviteContacts.setVisible(false);
243 } else {
244 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(!spl.isSlideable());
245 if (this.getSelectedConversation()!=null) {
246 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
247 menuMucDetails.setVisible(true);
248 menuContactDetails.setVisible(false);
249 menuSecure.setVisible(false);
250 menuArchive.setTitle("Leave conference");
251 menuInviteContacts.setVisible(true);
252 } else {
253 menuContactDetails.setVisible(true);
254 menuMucDetails.setVisible(false);
255 menuInviteContacts.setVisible(false);
256 if (this.getSelectedConversation().getLatestMessage().getEncryption() != Message.ENCRYPTION_NONE) {
257 menuSecure.setIcon(R.drawable.ic_action_secure);
258 }
259 }
260 }
261 }
262 return true;
263 }
264
265 @Override
266 public boolean onOptionsItemSelected(MenuItem item) {
267 switch (item.getItemId()) {
268 case android.R.id.home:
269 spl.openPane();
270 break;
271 case R.id.action_add:
272 startActivity(new Intent(this, ContactsActivity.class));
273 break;
274 case R.id.action_archive:
275 Conversation conv = getSelectedConversation();
276 conv.setStatus(Conversation.STATUS_ARCHIVED);
277 paneShouldBeOpen = true;
278 spl.openPane();
279 xmppConnectionService.archiveConversation(conv);
280 if (conversationList.size() > 0) {
281 selectedConversation = conversationList.get(0);
282 } else {
283 selectedConversation = null;
284 }
285 break;
286 case R.id.action_contact_details:
287 Contact contact = this.getSelectedConversation().getContact();
288 if (contact != null) {
289 Intent intent = new Intent(this,ContactDetailsActivity.class);
290 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
291 intent.putExtra("uuid", contact.getUuid());
292 startActivity(intent);
293 } else {
294 String jid = getSelectedConversation().getContactJid();
295 AlertDialog.Builder builder = new AlertDialog.Builder(this);
296 builder.setTitle(jid);
297 builder.setMessage("The contact is not in your roster. Would you like to add it.");
298 builder.setNegativeButton("Cancel", null);
299 builder.setPositiveButton("Add",addToRoster);
300 builder.create().show();
301 }
302 break;
303 case R.id.action_muc_details:
304 Intent intent = new Intent(this,MucDetailsActivity.class);
305 intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
306 intent.putExtra("uuid", getSelectedConversation().getUuid());
307 startActivity(intent);
308 break;
309 case R.id.action_invite:
310 Intent inviteIntent = new Intent(getApplicationContext(),
311 ContactsActivity.class);
312 inviteIntent.setAction("invite");
313 inviteIntent.putExtra("uuid",selectedConversation.getUuid());
314 startActivity(inviteIntent);
315 break;
316 case R.id.action_security:
317 final Conversation selConv = getSelectedConversation();
318 View menuItemView = findViewById(R.id.action_security);
319 PopupMenu popup = new PopupMenu(this, menuItemView);
320 final ConversationFragment fragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
321 if (fragment!=null) {
322 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
323
324 @Override
325 public boolean onMenuItemClick(MenuItem item) {
326 switch (item.getItemId()) {
327 case R.id.encryption_choice_none:
328 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
329 item.setChecked(true);
330 break;
331 case R.id.encryption_choice_otr:
332 selConv.nextMessageEncryption = Message.ENCRYPTION_OTR;
333 item.setChecked(true);
334 break;
335 case R.id.encryption_choice_pgp:
336 selConv.nextMessageEncryption = Message.ENCRYPTION_PGP;
337 item.setChecked(true);
338 break;
339 default:
340 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
341 break;
342 }
343 fragment.updateChatMsgHint();
344 return true;
345 }
346 });
347 popup.inflate(R.menu.encryption_choices);
348 switch (selConv.nextMessageEncryption) {
349 case Message.ENCRYPTION_NONE:
350 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
351 break;
352 case Message.ENCRYPTION_OTR:
353 popup.getMenu().findItem(R.id.encryption_choice_otr).setChecked(true);
354 break;
355 case Message.ENCRYPTION_PGP:
356 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
357 break;
358 case Message.ENCRYPTION_DECRYPTED:
359 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
360 break;
361 default:
362 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
363 break;
364 }
365 popup.show();
366 }
367
368 break;
369 default:
370 break;
371 }
372 return super.onOptionsItemSelected(item);
373 }
374
375 protected ConversationFragment swapConversationFragment() {
376 ConversationFragment selectedFragment = new ConversationFragment();
377
378 FragmentTransaction transaction = getFragmentManager()
379 .beginTransaction();
380 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
381 transaction.commit();
382 return selectedFragment;
383 }
384
385 @Override
386 public boolean onKeyDown(int keyCode, KeyEvent event) {
387 if (keyCode == KeyEvent.KEYCODE_BACK) {
388 if (!spl.isOpen()) {
389 spl.openPane();
390 return false;
391 }
392 }
393 return super.onKeyDown(keyCode, event);
394 }
395
396 public void onStart() {
397 super.onStart();
398 this.registerListener();
399 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
400 this.useSubject = preferences.getBoolean("use_subject_in_muc", true);
401 if (conversationList.size()>=1) {
402 onConvChanged.onConversationListChanged();
403 }
404 }
405
406 @Override
407 protected void onStop() {
408 Log.d("gultsch","called on stop in conversation activity");
409 if (xmppConnectionServiceBound) {
410 xmppConnectionService.removeOnConversationListChangedListener();
411 }
412 super.onStop();
413 }
414
415 @Override
416 void onBackendConnected() {
417
418 this.registerListener();
419 if (conversationList.size()==0) {
420 updateConversationList();
421 }
422
423 if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
424 if (getIntent().getType().equals(
425 ConversationActivity.VIEW_CONVERSATION)) {
426 handledViewIntent = true;
427
428 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
429
430 for(int i = 0; i < conversationList.size(); ++i) {
431 if (conversationList.get(i).getUuid().equals(convToView)) {
432 selectedConversation = conversationList.get(i);
433 }
434 }
435 paneShouldBeOpen = false;
436 String text = getIntent().getExtras().getString(TEXT, null);
437 swapConversationFragment().setText(text);
438 }
439 } else {
440 if (xmppConnectionService.getAccounts().size() == 0) {
441 startActivity(new Intent(this, ManageAccountActivity.class));
442 finish();
443 } else if (conversationList.size() <= 0) {
444 //add no history
445 startActivity(new Intent(this, ContactsActivity.class));
446 finish();
447 } else {
448 spl.openPane();
449 //find currently loaded fragment
450 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
451 if (selectedFragment!=null) {
452 selectedFragment.onBackendConnected();
453 } else {
454 selectedConversation = conversationList.get(0);
455 swapConversationFragment();
456 }
457 }
458 }
459 }
460 public void registerListener() {
461 if (xmppConnectionServiceBound) {
462 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
463 }
464 }
465
466 @Override
467 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
468 super.onActivityResult(requestCode, resultCode, data);
469 if (resultCode == RESULT_OK) {
470 if (requestCode == REQUEST_DECRYPT_PGP) {
471 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
472 if (selectedFragment!=null) {
473 selectedFragment.hidePgpPassphraseBox();
474 }
475 }
476 }
477 }
478
479 public void updateConversationList() {
480 conversationList.clear();
481 conversationList.addAll(xmppConnectionService
482 .getConversations());
483 listView.invalidateViews();
484 }
485}