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