ConversationActivity.java

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