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