XmppActivity.java

 1package eu.siacs.conversations.ui;
 2
 3import eu.siacs.conversations.services.XmppConnectionService;
 4import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
 5import android.app.Activity;
 6import android.content.ComponentName;
 7import android.content.Context;
 8import android.content.Intent;
 9import android.content.ServiceConnection;
10import android.os.IBinder;
11import android.view.View;
12import android.view.inputmethod.InputMethodManager;
13
14public abstract class XmppActivity extends Activity {
15	public XmppConnectionService xmppConnectionService;
16	public boolean xmppConnectionServiceBound = false;
17	protected boolean handledViewIntent = false;
18	protected ServiceConnection mConnection = new ServiceConnection() {
19
20		@Override
21		public void onServiceConnected(ComponentName className, IBinder service) {
22			XmppConnectionBinder binder = (XmppConnectionBinder) service;
23			xmppConnectionService = binder.getService();
24			xmppConnectionServiceBound = true;
25			onBackendConnected();
26		}
27
28		@Override
29		public void onServiceDisconnected(ComponentName arg0) {
30			xmppConnectionServiceBound = false;
31		}
32	};
33	
34	@Override
35	protected void onStart() {
36		startService(new Intent(this, XmppConnectionService.class));
37		super.onStart();
38		if (!xmppConnectionServiceBound) {
39			Intent intent = new Intent(this, XmppConnectionService.class);
40			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
41		}
42	}
43	
44	@Override
45	protected void onStop() {
46		super.onStop();
47		if (xmppConnectionServiceBound) {
48			unbindService(mConnection);
49			xmppConnectionServiceBound = false;
50		}
51	}
52	
53	protected void hideKeyboard() {
54		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
55
56		View focus = getCurrentFocus();
57
58		if (focus != null) {
59
60			inputManager.hideSoftInputFromWindow(
61					focus.getWindowToken(),
62					InputMethodManager.HIDE_NOT_ALWAYS);
63		}
64	}
65	
66	abstract void onBackendConnected();
67}