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			Intent intent = new Intent(this, XmppConnectionService.class);
 46			intent.setAction("ui");
 47			startService(intent);
 48			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 49		}
 50	}
 51	
 52	@Override
 53	protected void onStop() {
 54		super.onStop();
 55		if (xmppConnectionServiceBound) {
 56			unbindService(mConnection);
 57			xmppConnectionServiceBound = false;
 58		}
 59	}
 60	
 61	protected void hideKeyboard() {
 62		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 63
 64		View focus = getCurrentFocus();
 65
 66		if (focus != null) {
 67
 68			inputManager.hideSoftInputFromWindow(
 69					focus.getWindowToken(),
 70					InputMethodManager.HIDE_NOT_ALWAYS);
 71		}
 72	}
 73	
 74	public boolean hasPgp() {
 75		if (xmppConnectionService.getPgpEngine()!=null) {
 76			return true;
 77		} else {
 78			Builder builder = new AlertDialog.Builder(this);
 79			builder.setTitle("OpenKeychain not found");
 80			builder.setIconAttribute(android.R.attr.alertDialogIcon);
 81			builder.setMessage("Please make sure you have installed OpenKeychain");
 82			builder.create().show();
 83			return false;
 84		}
 85	}
 86	
 87	abstract void onBackendConnected();
 88	
 89	public boolean onOptionsItemSelected(MenuItem item) {
 90		switch (item.getItemId()) {
 91		case R.id.action_settings:
 92			startActivity(new Intent(this, SettingsActivity.class));
 93			break;
 94		case R.id.action_accounts:
 95			startActivity(new Intent(this, ManageAccountActivity.class));
 96			break;
 97		}
 98		return super.onOptionsItemSelected(item);
 99	}
100	
101	@Override
102	protected void onCreate(Bundle savedInstanceState) {
103		super.onCreate(savedInstanceState);
104		ExceptionHelper.init(getApplicationContext());
105	}
106}