XmppActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import eu.siacs.conversations.R;
  4import eu.siacs.conversations.entities.Account;
  5import eu.siacs.conversations.entities.Contact;
  6import eu.siacs.conversations.entities.Conversation;
  7import eu.siacs.conversations.entities.Message;
  8import eu.siacs.conversations.entities.Presences;
  9import eu.siacs.conversations.services.XmppConnectionService;
 10import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
 11import eu.siacs.conversations.utils.ExceptionHelper;
 12import android.app.Activity;
 13import android.app.AlertDialog;
 14import android.app.PendingIntent;
 15import android.app.AlertDialog.Builder;
 16import android.content.ComponentName;
 17import android.content.Context;
 18import android.content.DialogInterface;
 19import android.content.DialogInterface.OnClickListener;
 20import android.content.IntentSender.SendIntentException;
 21import android.content.Intent;
 22import android.content.ServiceConnection;
 23import android.net.Uri;
 24import android.os.Bundle;
 25import android.os.IBinder;
 26import android.util.Log;
 27import android.view.MenuItem;
 28import android.view.View;
 29import android.view.inputmethod.InputMethodManager;
 30
 31public abstract class XmppActivity extends Activity {
 32
 33	public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
 34
 35	protected final static String LOGTAG = "xmppService";
 36
 37	public XmppConnectionService xmppConnectionService;
 38	public boolean xmppConnectionServiceBound = false;
 39	protected boolean handledViewIntent = false;
 40
 41	protected ServiceConnection mConnection = new ServiceConnection() {
 42
 43		@Override
 44		public void onServiceConnected(ComponentName className, IBinder service) {
 45			XmppConnectionBinder binder = (XmppConnectionBinder) service;
 46			xmppConnectionService = binder.getService();
 47			xmppConnectionServiceBound = true;
 48			onBackendConnected();
 49		}
 50
 51		@Override
 52		public void onServiceDisconnected(ComponentName arg0) {
 53			xmppConnectionServiceBound = false;
 54		}
 55	};
 56
 57	@Override
 58	protected void onStart() {
 59		super.onStart();
 60		if (!xmppConnectionServiceBound) {
 61			connectToBackend();
 62		}
 63	}
 64
 65	public void connectToBackend() {
 66		Intent intent = new Intent(this, XmppConnectionService.class);
 67		intent.setAction("ui");
 68		startService(intent);
 69		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 70	}
 71
 72	@Override
 73	protected void onStop() {
 74		super.onStop();
 75		if (xmppConnectionServiceBound) {
 76			unbindService(mConnection);
 77			xmppConnectionServiceBound = false;
 78		}
 79	}
 80
 81	protected void hideKeyboard() {
 82		InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 83
 84		View focus = getCurrentFocus();
 85
 86		if (focus != null) {
 87
 88			inputManager.hideSoftInputFromWindow(focus.getWindowToken(),
 89					InputMethodManager.HIDE_NOT_ALWAYS);
 90		}
 91	}
 92
 93	public boolean hasPgp() {
 94		return xmppConnectionService.getPgpEngine() != null;
 95	}
 96
 97	public void showInstallPgpDialog() {
 98		Builder builder = new AlertDialog.Builder(this);
 99		builder.setTitle(getString(R.string.openkeychain_required));
100		builder.setIconAttribute(android.R.attr.alertDialogIcon);
101		builder.setMessage(getText(R.string.openkeychain_required_long));
102		builder.setNegativeButton(getString(R.string.cancel), null);
103		builder.setNeutralButton(getString(R.string.restart),
104				new OnClickListener() {
105
106					@Override
107					public void onClick(DialogInterface dialog, int which) {
108						if (xmppConnectionServiceBound) {
109							unbindService(mConnection);
110							xmppConnectionServiceBound = false;
111						}
112						stopService(new Intent(XmppActivity.this,
113								XmppConnectionService.class));
114						finish();
115					}
116				});
117		builder.setPositiveButton(getString(R.string.install),
118				new OnClickListener() {
119
120					@Override
121					public void onClick(DialogInterface dialog, int which) {
122						Uri uri = Uri
123								.parse("market://details?id=org.sufficientlysecure.keychain");
124						Intent intent = new Intent(Intent.ACTION_VIEW, uri);
125						startActivity(intent);
126						finish();
127					}
128				});
129		builder.create().show();
130	}
131
132	abstract void onBackendConnected();
133
134	public boolean onOptionsItemSelected(MenuItem item) {
135		switch (item.getItemId()) {
136		case R.id.action_settings:
137			startActivity(new Intent(this, SettingsActivity.class));
138			break;
139		case R.id.action_accounts:
140			startActivity(new Intent(this, ManageAccountActivity.class));
141			break;
142		}
143		return super.onOptionsItemSelected(item);
144	}
145
146	@Override
147	protected void onCreate(Bundle savedInstanceState) {
148		super.onCreate(savedInstanceState);
149		ExceptionHelper.init(getApplicationContext());
150	}
151
152	public void switchToConversation(Conversation conversation, String text,
153			boolean newTask) {
154		Intent viewConversationIntent = new Intent(this,
155				ConversationActivity.class);
156		viewConversationIntent.setAction(Intent.ACTION_VIEW);
157		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
158				conversation.getUuid());
159		if (text != null) {
160			viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
161		}
162		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
163		if (newTask) {
164			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
165					| Intent.FLAG_ACTIVITY_NEW_TASK
166					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
167		} else {
168			viewConversationIntent.setFlags(viewConversationIntent.getFlags()
169					| Intent.FLAG_ACTIVITY_CLEAR_TOP);
170		}
171		startActivity(viewConversationIntent);
172	}
173
174	protected void announcePgp(Account account, final Conversation conversation) {
175		xmppConnectionService.getPgpEngine().generateSignature(account,
176				"online", new UiCallback<Account>() {
177
178					@Override
179					public void userInputRequried(PendingIntent pi,
180							Account account) {
181						try {
182							startIntentSenderForResult(pi.getIntentSender(),
183									REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
184						} catch (SendIntentException e) {
185							Log.d("xmppService",
186									"coulnd start intent for pgp anncouncment");
187						}
188					}
189
190					@Override
191					public void success(Account account) {
192						xmppConnectionService.databaseBackend
193								.updateAccount(account);
194						xmppConnectionService.sendPresence(account);
195						if (conversation != null) {
196							conversation
197									.setNextEncryption(Message.ENCRYPTION_PGP);
198						}
199					}
200
201					@Override
202					public void error(int error, Account account) {
203						displayErrorDialog(error);
204					}
205				});
206	}
207
208	protected void displayErrorDialog(final int errorCode) {
209		runOnUiThread(new Runnable() {
210
211			@Override
212			public void run() {
213				AlertDialog.Builder builder = new AlertDialog.Builder(
214						XmppActivity.this);
215				builder.setIconAttribute(android.R.attr.alertDialogIcon);
216				builder.setTitle(getString(R.string.error));
217				builder.setMessage(errorCode);
218				builder.setNeutralButton(R.string.accept, null);
219				builder.create().show();
220			}
221		});
222
223	}
224	
225	protected void showAddToRosterDialog(final Conversation conversation) {
226		String jid = conversation.getContactJid();
227		AlertDialog.Builder builder = new AlertDialog.Builder(this);
228		builder.setTitle(jid);
229		builder.setMessage(getString(R.string.not_in_roster));
230		builder.setNegativeButton(getString(R.string.cancel), null);
231		builder.setPositiveButton(getString(R.string.add_contact),
232				new DialogInterface.OnClickListener() {
233
234					@Override
235					public void onClick(DialogInterface dialog, int which) {
236						String jid = conversation.getContactJid();
237						Account account = conversation.getAccount();
238						Contact contact = account.getRoster().getContact(jid);
239						xmppConnectionService.createContact(contact);
240					}
241				});
242		builder.create().show();
243	}
244	
245	public void selectPresence(final Conversation conversation,
246			final OnPresenceSelected listener) {
247		Contact contact = conversation.getContact();
248		if (contact == null) {
249			showAddToRosterDialog(conversation);
250		} else {
251			Presences presences = contact.getPresences();
252			if (presences.size() == 0) {
253				conversation.setNextPresence(null);
254				listener.onPresenceSelected();
255			} else if (presences.size() == 1) {
256				String presence = (String) presences.asStringArray()[0];
257				conversation.setNextPresence(presence);
258				listener.onPresenceSelected();
259			} else {
260				final StringBuilder presence = new StringBuilder();
261				AlertDialog.Builder builder = new AlertDialog.Builder(this);
262				builder.setTitle(getString(R.string.choose_presence));
263				final String[] presencesArray = presences.asStringArray();
264				int preselectedPresence = 0;
265				for (int i = 0; i < presencesArray.length; ++i) {
266					if (presencesArray[i].equals(contact.lastseen.presence)) {
267						preselectedPresence = i;
268						break;
269					}
270				}
271				presence.append(presencesArray[preselectedPresence]);
272				builder.setSingleChoiceItems(presencesArray,
273						preselectedPresence,
274						new DialogInterface.OnClickListener() {
275
276							@Override
277							public void onClick(DialogInterface dialog,
278									int which) {
279								presence.delete(0, presence.length());
280								presence.append(presencesArray[which]);
281							}
282						});
283				builder.setNegativeButton(R.string.cancel, null);
284				builder.setPositiveButton(R.string.ok, new OnClickListener() {
285
286					@Override
287					public void onClick(DialogInterface dialog, int which) {
288						conversation.setNextPresence(presence.toString());
289						listener.onPresenceSelected();
290					}
291				});
292				builder.create().show();
293			}
294		}
295	}
296}