XmppActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import eu.siacs.conversations.R;
  4import eu.siacs.conversations.entities.Account;
  5import eu.siacs.conversations.entities.Conversation;
  6import eu.siacs.conversations.entities.Message;
  7import eu.siacs.conversations.services.XmppConnectionService;
  8import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
  9import eu.siacs.conversations.utils.ExceptionHelper;
 10import android.app.Activity;
 11import android.app.AlertDialog;
 12import android.app.PendingIntent;
 13import android.app.AlertDialog.Builder;
 14import android.content.ComponentName;
 15import android.content.Context;
 16import android.content.DialogInterface;
 17import android.content.DialogInterface.OnClickListener;
 18import android.content.IntentSender.SendIntentException;
 19import android.content.Intent;
 20import android.content.ServiceConnection;
 21import android.net.Uri;
 22import android.os.Bundle;
 23import android.os.IBinder;
 24import android.util.Log;
 25import android.view.MenuItem;
 26import android.view.View;
 27import android.view.inputmethod.InputMethodManager;
 28
 29public abstract class XmppActivity extends Activity {
 30	
 31	public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
 32	
 33	protected final static String LOGTAG = "xmppService";
 34	
 35	public XmppConnectionService xmppConnectionService;
 36	public boolean xmppConnectionServiceBound = false;
 37	protected boolean handledViewIntent = false;
 38	
 39	protected ServiceConnection mConnection = new ServiceConnection() {
 40
 41		@Override
 42		public void onServiceConnected(ComponentName className, IBinder service) {
 43			XmppConnectionBinder binder = (XmppConnectionBinder) service;
 44			xmppConnectionService = binder.getService();
 45			xmppConnectionServiceBound = true;
 46			onBackendConnected();
 47		}
 48
 49		@Override
 50		public void onServiceDisconnected(ComponentName arg0) {
 51			xmppConnectionServiceBound = false;
 52		}
 53	};
 54	
 55	@Override
 56	protected void onStart() {
 57		super.onStart();
 58		if (!xmppConnectionServiceBound) {
 59			connectToBackend();
 60		}
 61	}
 62	
 63	public void connectToBackend() {
 64		Intent intent = new Intent(this, XmppConnectionService.class);
 65		intent.setAction("ui");
 66		startService(intent);
 67		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 68	}
 69	
 70	@Override
 71	protected void onStop() {
 72		super.onStop();
 73		if (xmppConnectionServiceBound) {
 74			unbindService(mConnection);
 75			xmppConnectionServiceBound = false;
 76		}
 77	}
 78	
 79	protected void hideKeyboard() {
 80		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 81
 82		View focus = getCurrentFocus();
 83
 84		if (focus != null) {
 85
 86			inputManager.hideSoftInputFromWindow(
 87					focus.getWindowToken(),
 88					InputMethodManager.HIDE_NOT_ALWAYS);
 89		}
 90	}
 91	
 92	public boolean hasPgp() {
 93		if (xmppConnectionService.getPgpEngine()!=null) {
 94			return true;
 95		} else {
 96			Builder builder = new AlertDialog.Builder(this);
 97			builder.setTitle(getString(R.string.openkeychain_required));
 98			builder.setIconAttribute(android.R.attr.alertDialogIcon);
 99			builder.setMessage(getText(R.string.openkeychain_required_long));
100			builder.setNegativeButton(getString(R.string.cancel), null);
101			builder.setNeutralButton(getString(R.string.restart), new OnClickListener() {
102				
103				@Override
104				public void onClick(DialogInterface dialog, int which) {
105					if (xmppConnectionServiceBound) {
106						unbindService(mConnection);
107						xmppConnectionServiceBound = false;
108					}
109					stopService(new Intent(XmppActivity.this, XmppConnectionService.class));
110					finish();
111				}
112			});
113			builder.setPositiveButton(getString(R.string.install), new OnClickListener() {
114				
115				@Override
116				public void onClick(DialogInterface dialog, int which) {
117					Uri uri = Uri.parse("market://details?id=org.sufficientlysecure.keychain");
118					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
119					startActivity(intent);
120					finish();
121				}
122			});
123			builder.create().show();
124			return false;
125		}
126	}
127	
128	abstract void onBackendConnected();
129	
130	public boolean onOptionsItemSelected(MenuItem item) {
131		switch (item.getItemId()) {
132		case R.id.action_settings:
133			startActivity(new Intent(this, SettingsActivity.class));
134			break;
135		case R.id.action_accounts:
136			startActivity(new Intent(this, ManageAccountActivity.class));
137			break;
138		}
139		return super.onOptionsItemSelected(item);
140	}
141	
142	@Override
143	protected void onCreate(Bundle savedInstanceState) {
144		super.onCreate(savedInstanceState);
145		ExceptionHelper.init(getApplicationContext());
146	}
147	
148	public void switchToConversation(Conversation conversation, String text, boolean newTask) {
149		Intent viewConversationIntent = new Intent(this,
150				ConversationActivity.class);
151		viewConversationIntent.setAction(Intent.ACTION_VIEW);
152		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
153				conversation.getUuid());
154		if (text!=null) {
155			viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
156		}
157		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
158		if (newTask) {
159			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
160				| Intent.FLAG_ACTIVITY_NEW_TASK
161				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
162		} else {
163			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
164				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
165		}
166		startActivity(viewConversationIntent);
167	}
168	
169	protected void announcePgp(final Account account, final Conversation conversation) {
170		xmppConnectionService.getPgpEngine().generateSignature(account, "online", new UiCallback() {
171			
172			@Override
173			public void userInputRequried(PendingIntent pi) {
174				try {
175					startIntentSenderForResult(pi.getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
176				} catch (SendIntentException e) {
177					Log.d("xmppService","coulnd start intent for pgp anncouncment");
178				}
179			}
180			
181			@Override
182			public void success() {
183				xmppConnectionService.databaseBackend.updateAccount(account);
184				xmppConnectionService.sendPresence(account);
185				if (conversation!=null) {
186					conversation.setNextEncryption(Message.ENCRYPTION_PGP);
187				}
188			}
189			
190			@Override
191			public void error(int error) {
192				displayErrorDialog(error);
193			}
194		});
195	}
196	
197	protected void displayErrorDialog(final int errorCode) {
198		runOnUiThread(new Runnable() {
199			
200			@Override
201			public void run() {
202				AlertDialog.Builder builder = new AlertDialog.Builder(XmppActivity.this);
203				builder.setIconAttribute(android.R.attr.alertDialogIcon);
204				builder.setTitle(getString(R.string.error));
205				builder.setMessage(errorCode);
206				builder.setNeutralButton(R.string.accept, null);
207				builder.create().show();
208			}
209		});
210		
211	}
212}