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 public void switchToContactDetails(Contact contact) {
175 Intent intent = new Intent(this, ContactDetailsActivity.class);
176 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
177 intent.putExtra("account", contact.getAccount().getJid());
178 intent.putExtra("contact", contact.getJid());
179 startActivity(intent);
180 }
181
182 protected void announcePgp(Account account, final Conversation conversation) {
183 xmppConnectionService.getPgpEngine().generateSignature(account,
184 "online", new UiCallback<Account>() {
185
186 @Override
187 public void userInputRequried(PendingIntent pi,
188 Account account) {
189 try {
190 startIntentSenderForResult(pi.getIntentSender(),
191 REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
192 } catch (SendIntentException e) {
193 Log.d("xmppService",
194 "coulnd start intent for pgp anncouncment");
195 }
196 }
197
198 @Override
199 public void success(Account account) {
200 xmppConnectionService.databaseBackend
201 .updateAccount(account);
202 xmppConnectionService.sendPresence(account);
203 if (conversation != null) {
204 conversation
205 .setNextEncryption(Message.ENCRYPTION_PGP);
206 }
207 }
208
209 @Override
210 public void error(int error, Account account) {
211 displayErrorDialog(error);
212 }
213 });
214 }
215
216 protected void displayErrorDialog(final int errorCode) {
217 runOnUiThread(new Runnable() {
218
219 @Override
220 public void run() {
221 AlertDialog.Builder builder = new AlertDialog.Builder(
222 XmppActivity.this);
223 builder.setIconAttribute(android.R.attr.alertDialogIcon);
224 builder.setTitle(getString(R.string.error));
225 builder.setMessage(errorCode);
226 builder.setNeutralButton(R.string.accept, null);
227 builder.create().show();
228 }
229 });
230
231 }
232
233 protected void showAddToRosterDialog(final Conversation conversation) {
234 String jid = conversation.getContactJid();
235 AlertDialog.Builder builder = new AlertDialog.Builder(this);
236 builder.setTitle(jid);
237 builder.setMessage(getString(R.string.not_in_roster));
238 builder.setNegativeButton(getString(R.string.cancel), null);
239 builder.setPositiveButton(getString(R.string.add_contact),
240 new DialogInterface.OnClickListener() {
241
242 @Override
243 public void onClick(DialogInterface dialog, int which) {
244 String jid = conversation.getContactJid();
245 Account account = conversation.getAccount();
246 Contact contact = account.getRoster().getContact(jid);
247 xmppConnectionService.createContact(contact);
248 }
249 });
250 builder.create().show();
251 }
252
253 public void selectPresence(final Conversation conversation,
254 final OnPresenceSelected listener) {
255 Contact contact = conversation.getContact();
256 if (contact == null) {
257 showAddToRosterDialog(conversation);
258 } else {
259 Presences presences = contact.getPresences();
260 if (presences.size() == 0) {
261 conversation.setNextPresence(null);
262 listener.onPresenceSelected();
263 } else if (presences.size() == 1) {
264 String presence = (String) presences.asStringArray()[0];
265 conversation.setNextPresence(presence);
266 listener.onPresenceSelected();
267 } else {
268 final StringBuilder presence = new StringBuilder();
269 AlertDialog.Builder builder = new AlertDialog.Builder(this);
270 builder.setTitle(getString(R.string.choose_presence));
271 final String[] presencesArray = presences.asStringArray();
272 int preselectedPresence = 0;
273 for (int i = 0; i < presencesArray.length; ++i) {
274 if (presencesArray[i].equals(contact.lastseen.presence)) {
275 preselectedPresence = i;
276 break;
277 }
278 }
279 presence.append(presencesArray[preselectedPresence]);
280 builder.setSingleChoiceItems(presencesArray,
281 preselectedPresence,
282 new DialogInterface.OnClickListener() {
283
284 @Override
285 public void onClick(DialogInterface dialog,
286 int which) {
287 presence.delete(0, presence.length());
288 presence.append(presencesArray[which]);
289 }
290 });
291 builder.setNegativeButton(R.string.cancel, null);
292 builder.setPositiveButton(R.string.ok, new OnClickListener() {
293
294 @Override
295 public void onClick(DialogInterface dialog, int which) {
296 conversation.setNextPresence(presence.toString());
297 listener.onPresenceSelected();
298 }
299 });
300 builder.create().show();
301 }
302 }
303 }
304}