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