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.DialogInterface;
 14import android.content.DialogInterface.OnClickListener;
 15import android.content.Intent;
 16import android.content.ServiceConnection;
 17import android.net.Uri;
 18import android.os.Bundle;
 19import android.os.IBinder;
 20import android.view.MenuItem;
 21import android.view.View;
 22import android.view.inputmethod.InputMethodManager;
 23
 24public abstract class XmppActivity extends Activity {
 25	
 26	protected final static String LOGTAG = "xmppService";
 27	
 28	public XmppConnectionService xmppConnectionService;
 29	public boolean xmppConnectionServiceBound = false;
 30	protected boolean handledViewIntent = false;
 31	
 32	protected ServiceConnection mConnection = new ServiceConnection() {
 33
 34		@Override
 35		public void onServiceConnected(ComponentName className, IBinder service) {
 36			XmppConnectionBinder binder = (XmppConnectionBinder) service;
 37			xmppConnectionService = binder.getService();
 38			xmppConnectionServiceBound = true;
 39			onBackendConnected();
 40		}
 41
 42		@Override
 43		public void onServiceDisconnected(ComponentName arg0) {
 44			xmppConnectionServiceBound = false;
 45		}
 46	};
 47	
 48	@Override
 49	protected void onStart() {
 50		super.onStart();
 51		if (!xmppConnectionServiceBound) {
 52			connectToBackend();
 53		}
 54	}
 55	
 56	public void connectToBackend() {
 57		Intent intent = new Intent(this, XmppConnectionService.class);
 58		intent.setAction("ui");
 59		startService(intent);
 60		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 61	}
 62	
 63	@Override
 64	protected void onStop() {
 65		super.onStop();
 66		if (xmppConnectionServiceBound) {
 67			unbindService(mConnection);
 68			xmppConnectionServiceBound = false;
 69		}
 70	}
 71	
 72	protected void hideKeyboard() {
 73		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 74
 75		View focus = getCurrentFocus();
 76
 77		if (focus != null) {
 78
 79			inputManager.hideSoftInputFromWindow(
 80					focus.getWindowToken(),
 81					InputMethodManager.HIDE_NOT_ALWAYS);
 82		}
 83	}
 84	
 85	public boolean hasPgp() {
 86		if (xmppConnectionService.getPgpEngine()!=null) {
 87			return true;
 88		} else {
 89			Builder builder = new AlertDialog.Builder(this);
 90			builder.setTitle(getString(R.string.openkeychain_required));
 91			builder.setIconAttribute(android.R.attr.alertDialogIcon);
 92			builder.setMessage(getText(R.string.openkeychain_required_long));
 93			builder.setNegativeButton(getString(R.string.cancel), null);
 94			builder.setNeutralButton(getString(R.string.restart), new OnClickListener() {
 95				
 96				@Override
 97				public void onClick(DialogInterface dialog, int which) {
 98					if (xmppConnectionServiceBound) {
 99						unbindService(mConnection);
100						xmppConnectionServiceBound = false;
101					}
102					stopService(new Intent(XmppActivity.this, XmppConnectionService.class));
103					finish();
104				}
105			});
106			builder.setPositiveButton(getString(R.string.install), new OnClickListener() {
107				
108				@Override
109				public void onClick(DialogInterface dialog, int which) {
110					Uri uri = Uri.parse("market://details?id=org.sufficientlysecure.keychain");
111					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
112					startActivity(intent);
113					finish();
114				}
115			});
116			builder.create().show();
117			return false;
118		}
119	}
120	
121	abstract void onBackendConnected();
122	
123	public boolean onOptionsItemSelected(MenuItem item) {
124		switch (item.getItemId()) {
125		case R.id.action_settings:
126			startActivity(new Intent(this, SettingsActivity.class));
127			break;
128		case R.id.action_accounts:
129			startActivity(new Intent(this, ManageAccountActivity.class));
130			break;
131		}
132		return super.onOptionsItemSelected(item);
133	}
134	
135	@Override
136	protected void onCreate(Bundle savedInstanceState) {
137		super.onCreate(savedInstanceState);
138		ExceptionHelper.init(getApplicationContext());
139	}
140	
141	public void switchToConversation(Conversation conversation, String text) {
142		Intent viewConversationIntent = new Intent(this,
143				ConversationActivity.class);
144		viewConversationIntent.setAction(Intent.ACTION_VIEW);
145		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
146				conversation.getUuid());
147		if (text!=null) {
148			viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
149		}
150		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
151		viewConversationIntent.setFlags(viewConversationIntent.getFlags()
152				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
153		startActivity(viewConversationIntent);
154	}
155}