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