1package de.gultsch.chat.ui;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.List;
7
8import de.gultsch.chat.R;
9import de.gultsch.chat.R.id;
10import de.gultsch.chat.entities.Conversation;
11import de.gultsch.chat.utils.UIHelper;
12import android.net.Uri;
13import android.os.Bundle;
14import android.app.FragmentTransaction;
15import android.app.NotificationManager;
16import android.content.Context;
17import android.content.Intent;
18import android.support.v4.widget.SlidingPaneLayout;
19import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
20import android.util.Log;
21import android.view.KeyEvent;
22import android.view.LayoutInflater;
23import android.view.Menu;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.ViewGroup;
27import android.view.inputmethod.InputMethodManager;
28import android.widget.AdapterView;
29import android.widget.AdapterView.OnItemClickListener;
30import android.widget.ArrayAdapter;
31import android.widget.ListView;
32import android.widget.TextView;
33import android.widget.ImageView;
34
35public class ConversationActivity extends XmppActivity {
36
37 public static final String VIEW_CONVERSATION = "viewConversation";
38 public static final String CONVERSATION = "conversationUuid";
39
40 protected SlidingPaneLayout spl;
41
42 private List<Conversation> conversationList = new ArrayList<Conversation>();
43 private Conversation selectedConversation = null;
44 private ListView listView;
45
46 private boolean paneShouldBeOpen = true;
47 private ArrayAdapter<Conversation> listAdapter;
48
49 private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() {
50
51 @Override
52 public void onConversationListChanged() {
53 final Conversation currentConv = getSelectedConversation();
54 conversationList.clear();
55 conversationList.addAll(xmppConnectionService
56 .getConversations());
57 runOnUiThread(new Runnable() {
58
59 @Override
60 public void run() {
61 updateConversationList();
62 /*for(int i = 0; i < conversationList.size(); ++i) {
63 if (currentConv == conversationList.get(i)) {
64 selectedConversation = conversationList.get(i);
65 break;
66 }
67 }*/
68 if(paneShouldBeOpen) {
69 selectedConversation = conversationList.get(0);
70 if (conversationList.size() >= 1) {
71 swapConversationFragment();
72 } else {
73 startActivity(new Intent(getApplicationContext(), NewConversationActivity.class));
74 finish();
75 }
76 }
77 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
78 if (selectedFragment!=null) {
79 selectedFragment.updateMessages();
80 }
81 }
82 });
83 }
84 };
85
86
87 public List<Conversation> getConversationList() {
88 return this.conversationList;
89 }
90
91 public Conversation getSelectedConversation() {
92 return this.selectedConversation;
93 }
94
95 public ListView getConversationListView() {
96 return this.listView;
97 }
98
99 public SlidingPaneLayout getSlidingPaneLayout() {
100 return this.spl;
101 }
102
103 public boolean shouldPaneBeOpen() {
104 return paneShouldBeOpen;
105 }
106
107 public void updateConversationList() {
108 if (conversationList.size() >= 1) {
109 Collections.sort(this.conversationList, new Comparator<Conversation>() {
110 @Override
111 public int compare(Conversation lhs, Conversation rhs) {
112 return (int) (rhs.getLatestMessageDate() - lhs.getLatestMessageDate());
113 }
114 });
115 }
116 this.listView.invalidateViews();
117 }
118
119 @Override
120 protected void onCreate(Bundle savedInstanceState) {
121
122 super.onCreate(savedInstanceState);
123
124 setContentView(R.layout.fragment_conversations_overview);
125
126 listView = (ListView) findViewById(R.id.list);
127
128 this.listAdapter = new ArrayAdapter<Conversation>(this,
129 R.layout.conversation_list_row, conversationList) {
130 @Override
131 public View getView(int position, View view, ViewGroup parent) {
132 if (view == null) {
133 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
134 view = (View) inflater.inflate(
135 R.layout.conversation_list_row, null);
136 }
137 ((TextView) view.findViewById(R.id.conversation_name))
138 .setText(getItem(position).getName());
139 ((TextView) view.findViewById(R.id.conversation_lastmsg)).setText(getItem(position).getLatestMessage());
140 ((TextView) view.findViewById(R.id.conversation_lastupdate))
141 .setText(UIHelper.readableTimeDifference(getItem(position).getLatestMessageDate()));
142
143 Uri profilePhoto = getItem(position).getProfilePhotoUri();
144 ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
145 if (profilePhoto!=null) {
146 imageView.setImageURI(profilePhoto);
147 } else {
148 imageView.setImageBitmap(UIHelper.getUnknownContactPicture(getItem(position).getName(),200));
149 }
150
151
152 ((ImageView) view.findViewById(R.id.conversation_image))
153 .setImageURI(getItem(position).getProfilePhotoUri());
154 return view;
155 }
156
157 };
158
159 listView.setAdapter(this.listAdapter);
160
161 listView.setOnItemClickListener(new OnItemClickListener() {
162
163 @Override
164 public void onItemClick(AdapterView<?> arg0, View clickedView,
165 int position, long arg3) {
166 paneShouldBeOpen = false;
167 if (selectedConversation != conversationList.get(position)) {
168 selectedConversation = conversationList.get(position);
169 swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
170 } else {
171 spl.closePane();
172 }
173 }
174 });
175 spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout);
176 spl.setParallaxDistance(150);
177 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
178 spl.setSliderFadeColor(0);
179 spl.setPanelSlideListener(new PanelSlideListener() {
180
181 @Override
182 public void onPanelOpened(View arg0) {
183 paneShouldBeOpen = true;
184 getActionBar().setDisplayHomeAsUpEnabled(false);
185 getActionBar().setTitle(R.string.app_name);
186 invalidateOptionsMenu();
187
188 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
189
190 View focus = getCurrentFocus();
191
192 if (focus != null) {
193
194 inputManager.hideSoftInputFromWindow(
195 focus.getWindowToken(),
196 InputMethodManager.HIDE_NOT_ALWAYS);
197 }
198 }
199
200 @Override
201 public void onPanelClosed(View arg0) {
202 paneShouldBeOpen = false;
203 if (conversationList.size() > 0) {
204 getActionBar().setDisplayHomeAsUpEnabled(true);
205 getActionBar().setTitle(getSelectedConversation().getName());
206 invalidateOptionsMenu();
207 }
208 }
209
210 @Override
211 public void onPanelSlide(View arg0, float arg1) {
212 // TODO Auto-generated method stub
213
214 }
215 });
216 }
217
218 @Override
219 public boolean onCreateOptionsMenu(Menu menu) {
220 getMenuInflater().inflate(R.menu.conversations, menu);
221
222 if (spl.isOpen()) {
223 ((MenuItem) menu.findItem(R.id.action_archive)).setVisible(false);
224 ((MenuItem) menu.findItem(R.id.action_details)).setVisible(false);
225 ((MenuItem) menu.findItem(R.id.action_security)).setVisible(false);
226 } else {
227 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(false);
228 if (this.getSelectedConversation()!=null) {
229 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
230 ((MenuItem) menu.findItem(R.id.action_security)).setVisible(false);
231 }
232 }
233 }
234 return true;
235 }
236
237 @Override
238 public boolean onOptionsItemSelected(MenuItem item) {
239 switch (item.getItemId()) {
240 case android.R.id.home:
241 spl.openPane();
242 break;
243 case R.id.action_settings:
244 startActivity(new Intent(this, SettingsActivity.class));
245 break;
246 case R.id.action_accounts:
247 startActivity(new Intent(this, ManageAccountActivity.class));
248 break;
249 case R.id.action_add:
250 startActivity(new Intent(this, NewConversationActivity.class));
251 break;
252 case R.id.action_archive:
253 Conversation conv = getSelectedConversation();
254 conv.setStatus(Conversation.STATUS_ARCHIVED);
255 paneShouldBeOpen = true;
256 spl.openPane();
257 xmppConnectionService.archiveConversation(conv);
258 selectedConversation = conversationList.get(0);
259 break;
260 default:
261 break;
262 }
263 return super.onOptionsItemSelected(item);
264 }
265
266 protected ConversationFragment swapConversationFragment() {
267 ConversationFragment selectedFragment = new ConversationFragment();
268
269 FragmentTransaction transaction = getFragmentManager()
270 .beginTransaction();
271 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
272 transaction.commit();
273 return selectedFragment;
274 }
275
276 @Override
277 public boolean onKeyDown(int keyCode, KeyEvent event) {
278 if (keyCode == KeyEvent.KEYCODE_BACK) {
279 if (!spl.isOpen()) {
280 spl.openPane();
281 return false;
282 }
283 }
284 return super.onKeyDown(keyCode, event);
285 }
286
287 public void onStart() {
288 super.onStart();
289 NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
290 nm.cancelAll();
291 if (conversationList.size()>=1) {
292 onConvChanged.onConversationListChanged();
293 }
294 }
295
296 /*@Override
297 protected void onPause() {
298 super.onPause();
299 if (xmppConnectionServiceBound) {
300 xmppConnectionService.removeOnConversationListChangedListener();
301 unbindService(mConnection);
302 xmppConnectionServiceBound = false;
303 }
304 }*/
305
306 @Override
307 protected void onStop() {
308 Log.d("gultsch","called on stop in conversation activity");
309 if (xmppConnectionServiceBound) {
310 xmppConnectionService.removeOnConversationListChangedListener();
311 }
312 super.onStop();
313 }
314
315 @Override
316 void onBackendConnected() {
317
318 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
319
320 if (conversationList.size()==0) {
321 conversationList.clear();
322 conversationList.addAll(xmppConnectionService
323 .getConversations());
324
325 for(Conversation conversation : conversationList) {
326 conversation.setMessages(xmppConnectionService.getMessages(conversation));
327 }
328
329 this.updateConversationList();
330 }
331
332 if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
333 if (getIntent().getType().equals(
334 ConversationActivity.VIEW_CONVERSATION)) {
335 handledViewIntent = true;
336
337 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
338
339 for(int i = 0; i < conversationList.size(); ++i) {
340 if (conversationList.get(i).getUuid().equals(convToView)) {
341 selectedConversation = conversationList.get(i);
342 }
343 }
344 paneShouldBeOpen = false;
345 swapConversationFragment();
346 }
347 } else {
348 if (xmppConnectionService.getAccounts().size() == 0) {
349 startActivity(new Intent(this, ManageAccountActivity.class));
350 finish();
351 } else if (conversationList.size() <= 0) {
352 //add no history
353 startActivity(new Intent(this, NewConversationActivity.class));
354 finish();
355 } else {
356 spl.openPane();
357 //find currently loaded fragment
358 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
359 if (selectedFragment!=null) {
360 Log.d("gultsch","ConversationActivity. found old fragment.");
361 selectedFragment.onBackendConnected();
362 } else {
363 Log.d("gultsch","conversationactivity. no old fragment found. creating new one");
364 selectedConversation = conversationList.get(0);
365 Log.d("gultsch","selected conversation is #"+selectedConversation);
366 swapConversationFragment();
367 }
368 }
369 }
370 }
371}