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.openPane();
133 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
134 spl.setSliderFadeColor(0);
135 spl.setPanelSlideListener(new PanelSlideListener() {
136
137 @Override
138 public void onPanelOpened(View arg0) {
139 paneShouldBeOpen = true;
140 getActionBar().setDisplayHomeAsUpEnabled(false);
141 getActionBar().setTitle(R.string.app_name);
142 invalidateOptionsMenu();
143
144 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
145
146 View focus = getCurrentFocus();
147
148 if (focus != null) {
149
150 inputManager.hideSoftInputFromWindow(
151 focus.getWindowToken(),
152 InputMethodManager.HIDE_NOT_ALWAYS);
153 }
154 }
155
156 @Override
157 public void onPanelClosed(View arg0) {
158 if (conversationList.size() > 0) {
159 getActionBar().setDisplayHomeAsUpEnabled(true);
160 getActionBar().setTitle(conversationList.get(selectedConversation).getName());
161 invalidateOptionsMenu();
162 }
163 }
164
165 @Override
166 public void onPanelSlide(View arg0, float arg1) {
167 // TODO Auto-generated method stub
168
169 }
170 });
171 }
172
173 @Override
174 public boolean onCreateOptionsMenu(Menu menu) {
175 getMenuInflater().inflate(R.menu.conversations, menu);
176
177 if (spl.isOpen()) {
178 ((MenuItem) menu.findItem(R.id.action_archive)).setVisible(false);
179 ((MenuItem) menu.findItem(R.id.action_details)).setVisible(false);
180 ((MenuItem) menu.findItem(R.id.action_security)).setVisible(false);
181 } else {
182 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(false);
183 }
184 return true;
185 }
186
187 @Override
188 public boolean onOptionsItemSelected(MenuItem item) {
189 switch (item.getItemId()) {
190 case android.R.id.home:
191 spl.openPane();
192 break;
193 case R.id.action_settings:
194 startActivity(new Intent(this, SettingsActivity.class));
195 break;
196 case R.id.action_accounts:
197 startActivity(new Intent(this, ManageAccountActivity.class));
198 break;
199 case R.id.action_add:
200 startActivity(new Intent(this, NewConversationActivity.class));
201 break;
202 case R.id.action_archive:
203 Conversation conv = getConversationList().get(selectedConversation);
204 conv.setStatus(Conversation.STATUS_ARCHIVED);
205 xmppConnectionService.updateConversation(conv);
206 conversationList.remove(selectedConversation);
207 selectedConversation = 0;
208 if (conversationList.size() >= 1) {
209 paneShouldBeOpen = true;
210 swapConversationFragment();
211 ((ArrayAdapter) listView.getAdapter()).notifyDataSetChanged();
212 spl.openPane();
213 } else {
214 startActivity(new Intent(this, NewConversationActivity.class));
215 finish();
216 }
217 //goto new
218 break;
219 default:
220 break;
221 }
222 return super.onOptionsItemSelected(item);
223 }
224
225 protected ConversationFragment swapConversationFragment() {
226 ConversationFragment selectedFragment = new ConversationFragment();
227
228 FragmentTransaction transaction = getFragmentManager()
229 .beginTransaction();
230 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
231 transaction.commit();
232 return selectedFragment;
233 }
234
235 @Override
236 public boolean onKeyDown(int keyCode, KeyEvent event) {
237 if (keyCode == KeyEvent.KEYCODE_BACK) {
238 if (!spl.isOpen()) {
239 spl.openPane();
240 return false;
241 }
242 }
243 return super.onKeyDown(keyCode, event);
244 }
245
246 @Override
247 public void onStart() {
248 super.onStart();
249 if (xmppConnectionServiceBound) {
250 conversationList.clear();
251 conversationList.addAll(xmppConnectionService
252 .getConversations(Conversation.STATUS_AVAILABLE));
253 }
254 }
255
256 @Override
257 void onBackendConnected() {
258 conversationList.clear();
259 conversationList.addAll(xmppConnectionService
260 .getConversations(Conversation.STATUS_AVAILABLE));
261
262 for(Conversation conversation : conversationList) {
263 conversation.setMessages(xmppConnectionService.getMessages(conversation));
264 }
265
266 this.updateConversationList();
267
268 if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
269 if (getIntent().getType().equals(
270 ConversationActivity.VIEW_CONVERSATION)) {
271 handledViewIntent = true;
272
273 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
274
275 for(int i = 0; i < conversationList.size(); ++i) {
276 if (conversationList.get(i).getUuid().equals(convToView)) {
277 selectedConversation = i;
278 }
279 }
280 paneShouldBeOpen = false;
281 swapConversationFragment();
282 }
283 } else {
284 if (conversationList.size() <= 0) {
285 //add no history
286 startActivity(new Intent(this, NewConversationActivity.class));
287 finish();
288 } else {
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}