XmppActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import eu.siacs.conversations.R;
  4import eu.siacs.conversations.entities.Conversation;
  5import eu.siacs.conversations.services.XmppConnectionService;
  6import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
  7import eu.siacs.conversations.utils.ExceptionHelper;
  8import android.app.Activity;
  9import android.app.AlertDialog;
 10import android.app.AlertDialog.Builder;
 11import android.content.ComponentName;
 12import android.content.Context;
 13import android.content.Intent;
 14import android.content.ServiceConnection;
 15import android.os.Bundle;
 16import android.os.IBinder;
 17import android.view.MenuItem;
 18import android.view.View;
 19import android.view.inputmethod.InputMethodManager;
 20
 21public abstract class XmppActivity extends Activity {
 22	
 23	protected final static String LOGTAG = "xmppService";
 24	
 25	public XmppConnectionService xmppConnectionService;
 26	public boolean xmppConnectionServiceBound = false;
 27	protected boolean handledViewIntent = false;
 28	
 29	protected ServiceConnection mConnection = new ServiceConnection() {
 30
 31		@Override
 32		public void onServiceConnected(ComponentName className, IBinder service) {
 33			XmppConnectionBinder binder = (XmppConnectionBinder) service;
 34			xmppConnectionService = binder.getService();
 35			xmppConnectionServiceBound = true;
 36			onBackendConnected();
 37		}
 38
 39		@Override
 40		public void onServiceDisconnected(ComponentName arg0) {
 41			xmppConnectionServiceBound = false;
 42		}
 43	};
 44	
 45	@Override
 46	protected void onStart() {
 47		super.onStart();
 48		if (!xmppConnectionServiceBound) {
 49			connectToBackend();
 50		}
 51	}
 52	
 53	public void connectToBackend() {
 54		Intent intent = new Intent(this, XmppConnectionService.class);
 55		intent.setAction("ui");
 56		startService(intent);
 57		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 58	}
 59	
 60	@Override
 61	protected void onStop() {
 62		super.onStop();
 63		if (xmppConnectionServiceBound) {
 64			unbindService(mConnection);
 65			xmppConnectionServiceBound = false;
 66		}
 67	}
 68	
 69	protected void hideKeyboard() {
 70		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 71
 72		View focus = getCurrentFocus();
 73
 74		if (focus != null) {
 75
 76			inputManager.hideSoftInputFromWindow(
 77					focus.getWindowToken(),
 78					InputMethodManager.HIDE_NOT_ALWAYS);
 79		}
 80	}
 81	
 82	public boolean hasPgp() {
 83		if (xmppConnectionService.getPgpEngine()!=null) {
 84			return true;
 85		} else {
 86			Builder builder = new AlertDialog.Builder(this);
 87			builder.setTitle("OpenKeychain not found");
 88			builder.setIconAttribute(android.R.attr.alertDialogIcon);
 89			builder.setMessage("Please make sure you have installed OpenKeychain");
 90			builder.create().show();
 91			return false;
 92		}
 93	}
 94	
 95	abstract void onBackendConnected();
 96	
 97	public boolean onOptionsItemSelected(MenuItem item) {
 98		switch (item.getItemId()) {
 99		case R.id.action_settings:
100			startActivity(new Intent(this, SettingsActivity.class));
101			break;
102		case R.id.action_accounts:
103			startActivity(new Intent(this, ManageAccountActivity.class));
104			break;
105		}
106		return super.onOptionsItemSelected(item);
107	}
108	
109	@Override
110	protected void onCreate(Bundle savedInstanceState) {
111		super.onCreate(savedInstanceState);
112		ExceptionHelper.init(getApplicationContext());
113	}
114	
115	public void switchToConversation(Conversation conversation, String text) {
116		Intent viewConversationIntent = new Intent(this,
117				ConversationActivity.class);
118		viewConversationIntent.setAction(Intent.ACTION_VIEW);
119		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
120				conversation.getUuid());
121		if (text!=null) {
122			viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
123		}
124		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
125		viewConversationIntent.setFlags(viewConversationIntent.getFlags()
126				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
127		startActivity(viewConversationIntent);
128	}
129}