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