XmppActivity.java

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