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