1package eu.siacs.conversations.services;
2
3import android.annotation.SuppressLint;
4import android.annotation.TargetApi;
5import android.app.AlarmManager;
6import android.app.PendingIntent;
7import android.app.Service;
8import android.content.Context;
9import android.content.Intent;
10import android.content.IntentFilter;
11import android.content.SharedPreferences;
12import android.database.ContentObserver;
13import android.graphics.Bitmap;
14import android.media.AudioManager;
15import android.net.ConnectivityManager;
16import android.net.NetworkInfo;
17import android.net.Uri;
18import android.os.Binder;
19import android.os.Build;
20import android.os.Bundle;
21import android.os.Environment;
22import android.os.IBinder;
23import android.os.ParcelFileDescriptor;
24import android.os.PowerManager;
25import android.os.PowerManager.WakeLock;
26import android.os.SystemClock;
27import android.preference.PreferenceManager;
28import android.provider.ContactsContract;
29import android.security.KeyChain;
30import android.support.v4.app.RemoteInput;
31import android.util.DisplayMetrics;
32import android.util.Log;
33import android.util.LruCache;
34import android.util.Pair;
35
36import net.java.otr4j.OtrException;
37import net.java.otr4j.session.Session;
38import net.java.otr4j.session.SessionID;
39import net.java.otr4j.session.SessionImpl;
40import net.java.otr4j.session.SessionStatus;
41import net.ypresto.androidtranscoder.MediaTranscoder;
42import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets;
43
44import org.openintents.openpgp.IOpenPgpService2;
45import org.openintents.openpgp.util.OpenPgpApi;
46import org.openintents.openpgp.util.OpenPgpServiceConnection;
47
48import java.io.FileDescriptor;
49import java.io.FileNotFoundException;
50import java.math.BigInteger;
51import java.net.URL;
52import java.security.SecureRandom;
53import java.security.cert.CertificateException;
54import java.security.cert.X509Certificate;
55import java.util.ArrayList;
56import java.util.Arrays;
57import java.util.Collection;
58import java.util.Collections;
59import java.util.HashMap;
60import java.util.HashSet;
61import java.util.Hashtable;
62import java.util.Iterator;
63import java.util.List;
64import java.util.ListIterator;
65import java.util.Locale;
66import java.util.Map;
67import java.util.concurrent.CopyOnWriteArrayList;
68import java.util.concurrent.atomic.AtomicBoolean;
69import java.util.concurrent.atomic.AtomicLong;
70
71import de.duenndns.ssl.MemorizingTrustManager;
72import eu.siacs.conversations.Config;
73import eu.siacs.conversations.R;
74import eu.siacs.conversations.crypto.PgpDecryptionService;
75import eu.siacs.conversations.crypto.PgpEngine;
76import eu.siacs.conversations.crypto.axolotl.AxolotlService;
77import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
78import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
79import eu.siacs.conversations.entities.Account;
80import eu.siacs.conversations.entities.Blockable;
81import eu.siacs.conversations.entities.Bookmark;
82import eu.siacs.conversations.entities.Contact;
83import eu.siacs.conversations.entities.Conversation;
84import eu.siacs.conversations.entities.DownloadableFile;
85import eu.siacs.conversations.entities.Message;
86import eu.siacs.conversations.entities.MucOptions;
87import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
88import eu.siacs.conversations.entities.Presence;
89import eu.siacs.conversations.entities.PresenceTemplate;
90import eu.siacs.conversations.entities.Roster;
91import eu.siacs.conversations.entities.ServiceDiscoveryResult;
92import eu.siacs.conversations.entities.Transferable;
93import eu.siacs.conversations.entities.TransferablePlaceholder;
94import eu.siacs.conversations.generator.AbstractGenerator;
95import eu.siacs.conversations.generator.IqGenerator;
96import eu.siacs.conversations.generator.MessageGenerator;
97import eu.siacs.conversations.generator.PresenceGenerator;
98import eu.siacs.conversations.http.HttpConnectionManager;
99import eu.siacs.conversations.http.AesGcmURLStreamHandlerFactory;
100import eu.siacs.conversations.parser.AbstractParser;
101import eu.siacs.conversations.parser.IqParser;
102import eu.siacs.conversations.parser.MessageParser;
103import eu.siacs.conversations.parser.PresenceParser;
104import eu.siacs.conversations.persistance.DatabaseBackend;
105import eu.siacs.conversations.persistance.FileBackend;
106import eu.siacs.conversations.ui.SettingsActivity;
107import eu.siacs.conversations.ui.UiCallback;
108import eu.siacs.conversations.ui.UiInformableCallback;
109import eu.siacs.conversations.utils.ConversationsFileObserver;
110import eu.siacs.conversations.utils.CryptoHelper;
111import eu.siacs.conversations.utils.ExceptionHelper;
112import eu.siacs.conversations.utils.MimeUtils;
113import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener;
114import eu.siacs.conversations.utils.PRNGFixes;
115import eu.siacs.conversations.utils.PhoneHelper;
116import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
117import eu.siacs.conversations.utils.Resolver;
118import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
119import eu.siacs.conversations.xml.Namespace;
120import eu.siacs.conversations.utils.XmppUri;
121import eu.siacs.conversations.xml.Element;
122import eu.siacs.conversations.xmpp.OnBindListener;
123import eu.siacs.conversations.xmpp.OnContactStatusChanged;
124import eu.siacs.conversations.xmpp.OnIqPacketReceived;
125import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
126import eu.siacs.conversations.xmpp.OnMessageAcknowledged;
127import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
128import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
129import eu.siacs.conversations.xmpp.OnStatusChanged;
130import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
131import eu.siacs.conversations.xmpp.Patches;
132import eu.siacs.conversations.xmpp.XmppConnection;
133import eu.siacs.conversations.xmpp.chatstate.ChatState;
134import eu.siacs.conversations.xmpp.forms.Data;
135import eu.siacs.conversations.xmpp.jid.InvalidJidException;
136import eu.siacs.conversations.xmpp.jid.Jid;
137import eu.siacs.conversations.xmpp.jingle.JingleConnectionManager;
138import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
139import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
140import eu.siacs.conversations.xmpp.mam.MamReference;
141import eu.siacs.conversations.xmpp.pep.Avatar;
142import eu.siacs.conversations.xmpp.stanzas.IqPacket;
143import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
144import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
145import me.leolin.shortcutbadger.ShortcutBadger;
146
147public class XmppConnectionService extends Service {
148
149 static {
150 URL.setURLStreamHandlerFactory(new AesGcmURLStreamHandlerFactory());
151 }
152
153 public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations";
154 public static final String ACTION_MARK_AS_READ = "mark_as_read";
155 public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification";
156 public static final String ACTION_DISABLE_FOREGROUND = "disable_foreground";
157 public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error";
158 public static final String ACTION_TRY_AGAIN = "try_again";
159 public static final String ACTION_IDLE_PING = "idle_ping";
160 private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts";
161 public static final String ACTION_GCM_TOKEN_REFRESH = "gcm_token_refresh";
162 public static final String ACTION_GCM_MESSAGE_RECEIVED = "gcm_message_received";
163 private final SerialSingleThreadExecutor mFileAddingExecutor = new SerialSingleThreadExecutor();
164 private final SerialSingleThreadExecutor mDatabaseExecutor = new SerialSingleThreadExecutor();
165 private ReplacingSerialSingleThreadExecutor mContactMergerExecutor = new ReplacingSerialSingleThreadExecutor(true);
166 private final IBinder mBinder = new XmppConnectionBinder();
167 private final List<Conversation> conversations = new CopyOnWriteArrayList<>();
168 private final IqGenerator mIqGenerator = new IqGenerator(this);
169 private final List<String> mInProgressAvatarFetches = new ArrayList<>();
170 private final HashSet<Jid> mLowPingTimeoutMode = new HashSet<>();
171
172 private long mLastActivity = 0;
173
174 public DatabaseBackend databaseBackend;
175 private ContentObserver contactObserver = new ContentObserver(null) {
176 @Override
177 public void onChange(boolean selfChange) {
178 super.onChange(selfChange);
179 Intent intent = new Intent(getApplicationContext(),
180 XmppConnectionService.class);
181 intent.setAction(ACTION_MERGE_PHONE_CONTACTS);
182 startService(intent);
183 }
184 };
185 private FileBackend fileBackend = new FileBackend(this);
186 private MemorizingTrustManager mMemorizingTrustManager;
187 private NotificationService mNotificationService = new NotificationService(this);
188 private ShortcutService mShortcutService = new ShortcutService(this);
189 private AtomicBoolean mInitialAddressbookSyncCompleted = new AtomicBoolean(false);
190 private OnMessagePacketReceived mMessageParser = new MessageParser(this);
191 private OnPresencePacketReceived mPresenceParser = new PresenceParser(this);
192 private IqParser mIqParser = new IqParser(this);
193 private OnIqPacketReceived mDefaultIqHandler = new OnIqPacketReceived() {
194 @Override
195 public void onIqPacketReceived(Account account, IqPacket packet) {
196 if (packet.getType() != IqPacket.TYPE.RESULT) {
197 Element error = packet.findChild("error");
198 String text = error != null ? error.findChildContent("text") : null;
199 if (text != null) {
200 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": received iq error - " + text);
201 }
202 }
203 }
204 };
205 private MessageGenerator mMessageGenerator = new MessageGenerator(this);
206 private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
207 private List<Account> accounts;
208 private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
209 this);
210 public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() {
211
212 @Override
213 public void onContactStatusChanged(Contact contact, boolean online) {
214 Conversation conversation = find(getConversations(), contact);
215 if (conversation != null) {
216 if (online) {
217 conversation.endOtrIfNeeded();
218 if (contact.getPresences().size() == 1) {
219 sendUnsentMessages(conversation);
220 }
221 } else {
222 //check if the resource we are haveing a conversation with is still online
223 if (conversation.hasValidOtrSession()) {
224 String otrResource = conversation.getOtrSession().getSessionID().getUserID();
225 if (!(Arrays.asList(contact.getPresences().toResourceArray()).contains(otrResource))) {
226 conversation.endOtrIfNeeded();
227 }
228 }
229 }
230 }
231 }
232 };
233 private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
234 this);
235 private AvatarService mAvatarService = new AvatarService(this);
236 private MessageArchiveService mMessageArchiveService = new MessageArchiveService(this);
237 private PushManagementService mPushManagementService = new PushManagementService(this);
238 private OnConversationUpdate mOnConversationUpdate = null;
239
240
241 private final ConversationsFileObserver fileObserver = new ConversationsFileObserver(
242 Environment.getExternalStorageDirectory().getAbsolutePath()
243 ) {
244 @Override
245 public void onEvent(int event, String path) {
246 markFileDeleted(path);
247 }
248 };
249 private final OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
250
251 @Override
252 public void onJinglePacketReceived(Account account, JinglePacket packet) {
253 mJingleConnectionManager.deliverPacket(account, packet);
254 }
255 };
256 private final OnMessageAcknowledged mOnMessageAcknowledgedListener = new OnMessageAcknowledged() {
257
258 @Override
259 public void onMessageAcknowledged(Account account, String uuid) {
260 for (final Conversation conversation : getConversations()) {
261 if (conversation.getAccount() == account) {
262 Message message = conversation.findUnsentMessageWithUuid(uuid);
263 if (message != null) {
264 markMessage(message, Message.STATUS_SEND);
265 }
266 }
267 }
268 }
269 };
270 private int convChangedListenerCount = 0;
271 private OnShowErrorToast mOnShowErrorToast = null;
272 private int showErrorToastListenerCount = 0;
273 private int unreadCount = -1;
274 private OnAccountUpdate mOnAccountUpdate = null;
275 private OnCaptchaRequested mOnCaptchaRequested = null;
276 private int accountChangedListenerCount = 0;
277 private int captchaRequestedListenerCount = 0;
278 private OnRosterUpdate mOnRosterUpdate = null;
279 private OnUpdateBlocklist mOnUpdateBlocklist = null;
280 private int updateBlocklistListenerCount = 0;
281 private int rosterChangedListenerCount = 0;
282 private OnMucRosterUpdate mOnMucRosterUpdate = null;
283 private int mucRosterChangedListenerCount = 0;
284 private OnKeyStatusUpdated mOnKeyStatusUpdated = null;
285 private int keyStatusUpdatedListenerCount = 0;
286 private AtomicLong mLastExpiryRun = new AtomicLong(0);
287 private SecureRandom mRandom;
288 private LruCache<Pair<String,String>,ServiceDiscoveryResult> discoCache = new LruCache<>(20);
289 private final OnBindListener mOnBindListener = new OnBindListener() {
290
291 @Override
292 public void onBind(final Account account) {
293 synchronized (mInProgressAvatarFetches) {
294 for (Iterator<String> iterator = mInProgressAvatarFetches.iterator(); iterator.hasNext(); ) {
295 final String KEY = iterator.next();
296 if (KEY.startsWith(account.getJid().toBareJid() + "_")) {
297 iterator.remove();
298 }
299 }
300 }
301 account.getRoster().clearPresences();
302 mJingleConnectionManager.cancelInTransmission();
303 fetchRosterFromServer(account);
304 fetchBookmarks(account);
305 sendPresence(account);
306 if (mPushManagementService.available(account)) {
307 mPushManagementService.registerPushTokenOnServer(account);
308 }
309 connectMultiModeConversations(account);
310 syncDirtyContacts(account);
311 }
312 };
313 private OnStatusChanged statusListener = new OnStatusChanged() {
314
315 @Override
316 public void onStatusChanged(final Account account) {
317 XmppConnection connection = account.getXmppConnection();
318 if (mOnAccountUpdate != null) {
319 mOnAccountUpdate.onAccountUpdate();
320 }
321 if (account.getStatus() == Account.State.ONLINE) {
322 synchronized (mLowPingTimeoutMode) {
323 if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) {
324 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving low ping timeout mode");
325 }
326 }
327 if (account.setShowErrorNotification(true)) {
328 databaseBackend.updateAccount(account);
329 }
330 mMessageArchiveService.executePendingQueries(account);
331 if (connection != null && connection.getFeatures().csi()) {
332 if (checkListeners()) {
333 Log.d(Config.LOGTAG, account.getJid().toBareJid() + " sending csi//inactive");
334 connection.sendInactive();
335 } else {
336 Log.d(Config.LOGTAG, account.getJid().toBareJid() + " sending csi//active");
337 connection.sendActive();
338 }
339 }
340 List<Conversation> conversations = getConversations();
341 for (Conversation conversation : conversations) {
342 if (conversation.getAccount() == account
343 && !account.pendingConferenceJoins.contains(conversation)) {
344 if (!conversation.startOtrIfNeeded()) {
345 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": couldn't start OTR with "+conversation.getContact().getJid()+" when needed");
346 }
347 sendUnsentMessages(conversation);
348 }
349 }
350 for (Conversation conversation : account.pendingConferenceLeaves) {
351 leaveMuc(conversation);
352 }
353 account.pendingConferenceLeaves.clear();
354 for (Conversation conversation : account.pendingConferenceJoins) {
355 joinMuc(conversation);
356 }
357 account.pendingConferenceJoins.clear();
358 scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
359 } else {
360 if (account.getStatus() == Account.State.OFFLINE || account.getStatus() == Account.State.DISABLED) {
361 resetSendingToWaiting(account);
362 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
363 synchronized (mLowPingTimeoutMode) {
364 if (mLowPingTimeoutMode.contains(account.getJid().toBareJid())) {
365 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": went into offline state during low ping mode. reconnecting now");
366 reconnectAccount(account, true, false);
367 } else {
368 int timeToReconnect = mRandom.nextInt(10) + 2;
369 scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
370 }
371 }
372 }
373 } else if (account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL) {
374 databaseBackend.updateAccount(account);
375 reconnectAccount(account, true, false);
376 } else if ((account.getStatus() != Account.State.CONNECTING)
377 && (account.getStatus() != Account.State.NO_INTERNET)) {
378 resetSendingToWaiting(account);
379 if (connection != null) {
380 int next = connection.getTimeToNextAttempt();
381 Log.d(Config.LOGTAG, account.getJid().toBareJid()
382 + ": error connecting account. try again in "
383 + next + "s for the "
384 + (connection.getAttempt() + 1) + " time");
385 scheduleWakeUpCall(next, account.getUuid().hashCode());
386 }
387 }
388 }
389 getNotificationService().updateErrorNotification();
390 }
391 };
392 private OpenPgpServiceConnection pgpServiceConnection;
393 private PgpEngine mPgpEngine = null;
394 private WakeLock wakeLock;
395 private PowerManager pm;
396 private LruCache<String, Bitmap> mBitmapCache;
397 private EventReceiver mEventReceiver = new EventReceiver();
398
399 private boolean mRestoredFromDatabase = false;
400
401 private static String generateFetchKey(Account account, final Avatar avatar) {
402 return account.getJid().toBareJid() + "_" + avatar.owner + "_" + avatar.sha1sum;
403 }
404
405 public boolean areMessagesInitialized() {
406 return this.mRestoredFromDatabase;
407 }
408
409 public PgpEngine getPgpEngine() {
410 if (!Config.supportOpenPgp()) {
411 return null;
412 } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
413 if (this.mPgpEngine == null) {
414 this.mPgpEngine = new PgpEngine(new OpenPgpApi(
415 getApplicationContext(),
416 pgpServiceConnection.getService()), this);
417 }
418 return mPgpEngine;
419 } else {
420 return null;
421 }
422
423 }
424
425 public OpenPgpApi getOpenPgpApi() {
426 if (!Config.supportOpenPgp()) {
427 return null;
428 } else if (pgpServiceConnection != null && pgpServiceConnection.isBound()) {
429 return new OpenPgpApi(this, pgpServiceConnection.getService());
430 } else {
431 return null;
432 }
433 }
434
435 public FileBackend getFileBackend() {
436 return this.fileBackend;
437 }
438
439 public AvatarService getAvatarService() {
440 return this.mAvatarService;
441 }
442
443 public void attachLocationToConversation(final Conversation conversation,
444 final Uri uri,
445 final UiCallback<Message> callback) {
446 int encryption = conversation.getNextEncryption();
447 if (encryption == Message.ENCRYPTION_PGP) {
448 encryption = Message.ENCRYPTION_DECRYPTED;
449 }
450 Message message = new Message(conversation, uri.toString(), encryption);
451 if (conversation.getNextCounterpart() != null) {
452 message.setCounterpart(conversation.getNextCounterpart());
453 }
454 if (encryption == Message.ENCRYPTION_DECRYPTED) {
455 getPgpEngine().encrypt(message, callback);
456 } else {
457 callback.success(message);
458 }
459 }
460
461 public void attachFileToConversation(final Conversation conversation,
462 final Uri uri,
463 final UiCallback<Message> callback) {
464 if (FileBackend.weOwnFile(this, uri)) {
465 Log.d(Config.LOGTAG,"trying to attach file that belonged to us");
466 callback.error(R.string.security_error_invalid_file_access, null);
467 return;
468 }
469 final Message message;
470 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
471 message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
472 } else {
473 message = new Message(conversation, "", conversation.getNextEncryption());
474 }
475 message.setCounterpart(conversation.getNextCounterpart());
476 message.setType(Message.TYPE_FILE);
477 mFileAddingExecutor.execute(new Runnable() {
478
479 private void processAsFile() {
480 final String path = getFileBackend().getOriginalPath(uri);
481 if (path != null) {
482 message.setRelativeFilePath(path);
483 getFileBackend().updateFileParams(message);
484 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
485 getPgpEngine().encrypt(message, callback);
486 } else {
487 callback.success(message);
488 }
489 } else {
490 try {
491 getFileBackend().copyFileToPrivateStorage(message, uri);
492 getFileBackend().updateFileParams(message);
493 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
494 final PgpEngine pgpEngine = getPgpEngine();
495 if (pgpEngine != null) {
496 pgpEngine.encrypt(message, callback);
497 } else if (callback != null) {
498 callback.error(R.string.unable_to_connect_to_keychain, null);
499 }
500 } else {
501 callback.success(message);
502 }
503 } catch (FileBackend.FileCopyException e) {
504 callback.error(e.getResId(), message);
505 }
506 }
507 }
508
509 private void processAsVideo() throws FileNotFoundException {
510 Log.d(Config.LOGTAG,"processing file as video");
511 message.setRelativeFilePath(message.getUuid() + ".mp4");
512 final DownloadableFile file = getFileBackend().getFile(message);
513 file.getParentFile().mkdirs();
514 ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
515 FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
516 final ArrayList<Integer> progressTracker = new ArrayList<>();
517 final UiInformableCallback<Message> informableCallback;
518 if (callback instanceof UiInformableCallback) {
519 informableCallback = (UiInformableCallback<Message>) callback;
520 } else {
521 informableCallback = null;
522 }
523 MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {
524 @Override
525 public void onTranscodeProgress(double progress) {
526 int p = ((int) Math.round(progress * 100) / 20) * 20;
527 if (!progressTracker.contains(p) && p != 100 && p != 0) {
528 progressTracker.add(p);
529 if (informableCallback != null) {
530
531 informableCallback.inform(getString(R.string.transcoding_video_progress, p));
532 }
533 }
534 }
535
536 @Override
537 public void onTranscodeCompleted() {
538 if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
539 getPgpEngine().encrypt(message, callback);
540 } else {
541 callback.success(message);
542 }
543 }
544
545 @Override
546 public void onTranscodeCanceled() {
547 processAsFile();
548 }
549
550 @Override
551 public void onTranscodeFailed(Exception e) {
552 Log.d(Config.LOGTAG,"video transcoding failed "+e.getMessage());
553 processAsFile();
554 }
555 };
556 MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(),
557 MediaFormatStrategyPresets.createAndroid720pStrategy(), listener);
558 }
559
560 @Override
561 public void run() {
562 final String mimeType = MimeUtils.guessMimeTypeFromUri(XmppConnectionService.this, uri);
563 if (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
564 try {
565 processAsVideo();
566 } catch (Throwable e) {
567 processAsFile();
568 }
569 } else {
570 processAsFile();
571 }
572
573 }
574 });
575 }
576
577 public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
578 if (FileBackend.weOwnFile(this, uri)) {
579 Log.d(Config.LOGTAG,"trying to attach file that belonged to us");
580 callback.error(R.string.security_error_invalid_file_access, null);
581 return;
582 }
583
584 final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
585 final String compressPictures = getCompressPicturesPreference();
586
587 if ("never".equals(compressPictures)
588 || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri))
589 || (mimeType != null && mimeType.endsWith("/gif"))) {
590 Log.d(Config.LOGTAG,conversation.getAccount().getJid().toBareJid()+ ": not compressing picture. sending as file");
591 attachFileToConversation(conversation, uri, callback);
592 return;
593 }
594 final Message message;
595 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
596 message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
597 } else {
598 message = new Message(conversation, "", conversation.getNextEncryption());
599 }
600 message.setCounterpart(conversation.getNextCounterpart());
601 message.setType(Message.TYPE_IMAGE);
602 mFileAddingExecutor.execute(new Runnable() {
603
604 @Override
605 public void run() {
606 try {
607 getFileBackend().copyImageToPrivateStorage(message, uri);
608 if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
609 final PgpEngine pgpEngine = getPgpEngine();
610 if (pgpEngine != null) {
611 pgpEngine.encrypt(message, callback);
612 } else if (callback != null){
613 callback.error(R.string.unable_to_connect_to_keychain, null);
614 }
615 } else {
616 callback.success(message);
617 }
618 } catch (final FileBackend.FileCopyException e) {
619 callback.error(e.getResId(), message);
620 }
621 }
622 });
623 }
624
625 public Conversation find(Bookmark bookmark) {
626 return find(bookmark.getAccount(), bookmark.getJid());
627 }
628
629 public Conversation find(final Account account, final Jid jid) {
630 return find(getConversations(), account, jid);
631 }
632
633 @Override
634 public int onStartCommand(Intent intent, int flags, int startId) {
635 final String action = intent == null ? null : intent.getAction();
636 String pushedAccountHash = null;
637 boolean interactive = false;
638 if (action != null) {
639 final Conversation c = findConversationByUuid(intent.getStringExtra("uuid"));
640 switch (action) {
641 case ConnectivityManager.CONNECTIVITY_ACTION:
642 if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
643 resetAllAttemptCounts(true, false);
644 }
645 break;
646 case ACTION_MERGE_PHONE_CONTACTS:
647 if (mRestoredFromDatabase) {
648 loadPhoneContacts();
649 }
650 return START_STICKY;
651 case Intent.ACTION_SHUTDOWN:
652 logoutAndSave(true);
653 return START_NOT_STICKY;
654 case ACTION_CLEAR_NOTIFICATION:
655 if (c != null) {
656 mNotificationService.clear(c);
657 } else {
658 mNotificationService.clear();
659 }
660 break;
661 case ACTION_DISABLE_FOREGROUND:
662 getPreferences().edit().putBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE, false).commit();
663 toggleForegroundService();
664 break;
665 case ACTION_DISMISS_ERROR_NOTIFICATIONS:
666 dismissErrorNotifications();
667 break;
668 case ACTION_TRY_AGAIN:
669 resetAllAttemptCounts(false, true);
670 interactive = true;
671 break;
672 case ACTION_REPLY_TO_CONVERSATION:
673 Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
674 if (remoteInput != null && c != null) {
675 final CharSequence body = remoteInput.getCharSequence("text_reply");
676 if (body != null && body.length() > 0) {
677 directReply(c, body.toString(),intent.getBooleanExtra("dismiss_notification",false));
678 }
679 }
680 break;
681 case ACTION_MARK_AS_READ:
682 sendReadMarker(c);
683 break;
684 case AudioManager.RINGER_MODE_CHANGED_ACTION:
685 if (dndOnSilentMode()) {
686 refreshAllPresences();
687 }
688 break;
689 case Intent.ACTION_SCREEN_ON:
690 deactivateGracePeriod();
691 case Intent.ACTION_SCREEN_OFF:
692 if (awayWhenScreenOff()) {
693 refreshAllPresences();
694 }
695 break;
696 case ACTION_GCM_TOKEN_REFRESH:
697 refreshAllGcmTokens();
698 break;
699 case ACTION_IDLE_PING:
700 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
701 scheduleNextIdlePing();
702 }
703 break;
704 case ACTION_GCM_MESSAGE_RECEIVED:
705 Log.d(Config.LOGTAG,"gcm push message arrived in service. extras="+intent.getExtras());
706 pushedAccountHash = intent.getStringExtra("account");
707 break;
708 }
709 }
710 synchronized (this) {
711 this.wakeLock.acquire();
712 boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action);
713 HashSet<Account> pingCandidates = new HashSet<>();
714 for (Account account : accounts) {
715 pingNow |= processAccountState(account,
716 interactive,
717 "ui".equals(action),
718 CryptoHelper.getAccountFingerprint(account).equals(pushedAccountHash),
719 pingCandidates);
720 }
721 if (pingNow) {
722 for (Account account : pingCandidates) {
723 final boolean lowTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid());
724 account.getXmppConnection().sendPing();
725 Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ",lowTimeout=" + Boolean.toString(lowTimeout) + ")");
726 scheduleWakeUpCall(lowTimeout ? Config.LOW_PING_TIMEOUT : Config.PING_TIMEOUT, account.getUuid().hashCode());
727 }
728 }
729 if (wakeLock.isHeld()) {
730 try {
731 wakeLock.release();
732 } catch (final RuntimeException ignored) {
733 }
734 }
735 }
736 if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
737 expireOldMessages();
738 }
739 return START_STICKY;
740 }
741
742 private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
743 boolean pingNow = false;
744 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
745 if (!hasInternetConnection()) {
746 account.setStatus(Account.State.NO_INTERNET);
747 if (statusListener != null) {
748 statusListener.onStatusChanged(account);
749 }
750 } else {
751 if (account.getStatus() == Account.State.NO_INTERNET) {
752 account.setStatus(Account.State.OFFLINE);
753 if (statusListener != null) {
754 statusListener.onStatusChanged(account);
755 }
756 }
757 if (account.getStatus() == Account.State.ONLINE) {
758 synchronized (mLowPingTimeoutMode) {
759 long lastReceived = account.getXmppConnection().getLastPacketReceived();
760 long lastSent = account.getXmppConnection().getLastPingSent();
761 long pingInterval = isUiAction ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
762 long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
763 int pingTimeout = mLowPingTimeoutMode.contains(account.getJid().toBareJid()) ? Config.LOW_PING_TIMEOUT * 1000 : Config.PING_TIMEOUT * 1000;
764 long pingTimeoutIn = (lastSent + pingTimeout) - SystemClock.elapsedRealtime();
765 if (lastSent > lastReceived) {
766 if (pingTimeoutIn < 0) {
767 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ping timeout");
768 this.reconnectAccount(account, true, interactive);
769 } else {
770 int secs = (int) (pingTimeoutIn / 1000);
771 this.scheduleWakeUpCall(secs, account.getUuid().hashCode());
772 }
773 } else {
774 pingCandidates.add(account);
775 if (isAccountPushed) {
776 pingNow = true;
777 if (mLowPingTimeoutMode.add(account.getJid().toBareJid())) {
778 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": entering low ping timeout mode");
779 }
780 } else if (msToNextPing <= 0) {
781 pingNow = true;
782 } else {
783 this.scheduleWakeUpCall((int) (msToNextPing / 1000), account.getUuid().hashCode());
784 if (mLowPingTimeoutMode.remove(account.getJid().toBareJid())) {
785 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving low ping timeout mode");
786 }
787 }
788 }
789 }
790 } else if (account.getStatus() == Account.State.OFFLINE) {
791 reconnectAccount(account, true, interactive);
792 } else if (account.getStatus() == Account.State.CONNECTING) {
793 long secondsSinceLastConnect = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastConnect()) / 1000;
794 long secondsSinceLastDisco = (SystemClock.elapsedRealtime() - account.getXmppConnection().getLastDiscoStarted()) / 1000;
795 long discoTimeout = Config.CONNECT_DISCO_TIMEOUT - secondsSinceLastDisco;
796 long timeout = Config.CONNECT_TIMEOUT - secondsSinceLastConnect;
797 if (timeout < 0) {
798 Log.d(Config.LOGTAG, account.getJid() + ": time out during connect reconnecting (secondsSinceLast="+secondsSinceLastConnect+")");
799 account.getXmppConnection().resetAttemptCount(false);
800 reconnectAccount(account, true, interactive);
801 } else if (discoTimeout < 0) {
802 account.getXmppConnection().sendDiscoTimeout();
803 scheduleWakeUpCall((int) Math.min(timeout,discoTimeout), account.getUuid().hashCode());
804 } else {
805 scheduleWakeUpCall((int) Math.min(timeout,discoTimeout), account.getUuid().hashCode());
806 }
807 } else {
808 if (account.getXmppConnection().getTimeToNextAttempt() <= 0) {
809 reconnectAccount(account, true, interactive);
810 }
811 }
812 }
813 }
814 return pingNow;
815 }
816
817 public boolean isDataSaverDisabled() {
818 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
819 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
820 return !connectivityManager.isActiveNetworkMetered()
821 || connectivityManager.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
822 } else {
823 return true;
824 }
825 }
826
827 private void directReply(Conversation conversation, String body, final boolean dismissAfterReply) {
828 Message message = new Message(conversation,body,conversation.getNextEncryption());
829 message.markUnread();
830 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
831 getPgpEngine().encrypt(message, new UiCallback<Message>() {
832 @Override
833 public void success(Message message) {
834 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
835 sendMessage(message);
836 if (dismissAfterReply) {
837 markRead(message.getConversation(),true);
838 } else {
839 mNotificationService.pushFromDirectReply(message);
840 }
841 }
842
843 @Override
844 public void error(int errorCode, Message object) {
845
846 }
847
848 @Override
849 public void userInputRequried(PendingIntent pi, Message object) {
850
851 }
852 });
853 } else {
854 sendMessage(message);
855 if (dismissAfterReply) {
856 markRead(conversation,true);
857 } else {
858 mNotificationService.pushFromDirectReply(message);
859 }
860 }
861 }
862
863 private boolean dndOnSilentMode() {
864 return getPreferences().getBoolean(SettingsActivity.DND_ON_SILENT_MODE, getResources().getBoolean(R.bool.dnd_on_silent_mode));
865 }
866
867 private boolean manuallyChangePresence() {
868 return getPreferences().getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, false);
869 }
870
871 private boolean treatVibrateAsSilent() {
872 return getPreferences().getBoolean(SettingsActivity.TREAT_VIBRATE_AS_SILENT, getResources().getBoolean(R.bool.treat_vibrate_as_silent));
873 }
874
875 private boolean awayWhenScreenOff() {
876 return getPreferences().getBoolean(SettingsActivity.AWAY_WHEN_SCREEN_IS_OFF, false);
877 }
878
879 private String getCompressPicturesPreference() {
880 return getPreferences().getString("picture_compression", "auto");
881 }
882
883 private Presence.Status getTargetPresence() {
884 if (dndOnSilentMode() && isPhoneSilenced()) {
885 return Presence.Status.DND;
886 } else if (awayWhenScreenOff() && !isInteractive()) {
887 return Presence.Status.AWAY;
888 } else {
889 return Presence.Status.ONLINE;
890 }
891 }
892
893 @SuppressLint("NewApi")
894 @SuppressWarnings("deprecation")
895 public boolean isInteractive() {
896 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
897
898 final boolean isScreenOn;
899 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
900 isScreenOn = pm.isScreenOn();
901 } else {
902 isScreenOn = pm.isInteractive();
903 }
904 return isScreenOn;
905 }
906
907 private boolean isPhoneSilenced() {
908 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
909 try {
910 if (treatVibrateAsSilent()) {
911 return audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
912 } else {
913 return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
914 }
915 } catch (Throwable throwable) {
916 Log.d(Config.LOGTAG,"platform bug in isPhoneSilenced ("+ throwable.getMessage()+")");
917 return false;
918 }
919 }
920
921 private void resetAllAttemptCounts(boolean reallyAll, boolean retryImmediately) {
922 Log.d(Config.LOGTAG, "resetting all attempt counts");
923 for (Account account : accounts) {
924 if (account.hasErrorStatus() || reallyAll) {
925 final XmppConnection connection = account.getXmppConnection();
926 if (connection != null) {
927 connection.resetAttemptCount(retryImmediately);
928 }
929 }
930 if (account.setShowErrorNotification(true)) {
931 databaseBackend.updateAccount(account);
932 }
933 }
934 mNotificationService.updateErrorNotification();
935 }
936
937 private void dismissErrorNotifications() {
938 for (final Account account : this.accounts) {
939 if (account.hasErrorStatus()) {
940 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": dismissing error notification");
941 if (account.setShowErrorNotification(false)) {
942 databaseBackend.updateAccount(account);
943 }
944 }
945 }
946 }
947
948 private void expireOldMessages() {
949 expireOldMessages(false);
950 }
951
952 public void expireOldMessages(final boolean resetHasMessagesLeftOnServer) {
953 mLastExpiryRun.set(SystemClock.elapsedRealtime());
954 mDatabaseExecutor.execute(new Runnable() {
955 @Override
956 public void run() {
957 long timestamp = getAutomaticMessageDeletionDate();
958 if (timestamp > 0) {
959 databaseBackend.expireOldMessages(timestamp);
960 synchronized (XmppConnectionService.this.conversations) {
961 for (Conversation conversation : XmppConnectionService.this.conversations) {
962 conversation.expireOldMessages(timestamp);
963 if (resetHasMessagesLeftOnServer) {
964 conversation.messagesLoaded.set(true);
965 conversation.setHasMessagesLeftOnServer(true);
966 }
967 }
968 }
969 updateConversationUi();
970 }
971 }
972 });
973 }
974
975 public boolean hasInternetConnection() {
976 ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
977 .getSystemService(Context.CONNECTIVITY_SERVICE);
978 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
979 return activeNetwork != null && activeNetwork.isConnected();
980 }
981
982 @SuppressLint("TrulyRandom")
983 @Override
984 public void onCreate() {
985 ExceptionHelper.init(getApplicationContext());
986 PRNGFixes.apply();
987 Resolver.registerLookupMechanism(this);
988 this.mRandom = new SecureRandom();
989 updateMemorizingTrustmanager();
990 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
991 final int cacheSize = maxMemory / 8;
992 this.mBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
993 @Override
994 protected int sizeOf(final String key, final Bitmap bitmap) {
995 return bitmap.getByteCount() / 1024;
996 }
997 };
998
999 Log.d(Config.LOGTAG,"initializing database...");
1000 this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
1001 Log.d(Config.LOGTAG,"restoring accounts...");
1002 this.accounts = databaseBackend.getAccounts();
1003
1004 if (this.accounts.size() == 0 && Arrays.asList("Sony","Sony Ericsson").contains(Build.MANUFACTURER)) {
1005 getPreferences().edit().putBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE,true).commit();
1006 Log.d(Config.LOGTAG,Build.MANUFACTURER+" is on blacklist. enabling foreground service");
1007 }
1008
1009 restoreFromDatabase();
1010
1011 getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
1012 new Thread(new Runnable() {
1013 @Override
1014 public void run() {
1015 fileObserver.startWatching();
1016 }
1017 }).start();
1018 if (Config.supportOpenPgp()) {
1019 this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain", new OpenPgpServiceConnection.OnBound() {
1020 @Override
1021 public void onBound(IOpenPgpService2 service) {
1022 for (Account account : accounts) {
1023 final PgpDecryptionService pgp = account.getPgpDecryptionService();
1024 if(pgp != null) {
1025 pgp.continueDecryption(true);
1026 }
1027 }
1028 }
1029
1030 @Override
1031 public void onError(Exception e) {
1032 }
1033 });
1034 this.pgpServiceConnection.bindToService();
1035 }
1036
1037 this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
1038 this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "XmppConnectionService");
1039
1040 toggleForegroundService();
1041 updateUnreadCountBadge();
1042 toggleScreenEventReceiver();
1043 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1044 scheduleNextIdlePing();
1045 }
1046 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
1047 registerReceiver(this.mEventReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
1048 }
1049 }
1050
1051 @Override
1052 public void onTrimMemory(int level) {
1053 super.onTrimMemory(level);
1054 if (level >= TRIM_MEMORY_COMPLETE) {
1055 Log.d(Config.LOGTAG, "clear cache due to low memory");
1056 getBitmapCache().evictAll();
1057 }
1058 }
1059
1060 @Override
1061 public void onDestroy() {
1062 try {
1063 unregisterReceiver(this.mEventReceiver);
1064 } catch (IllegalArgumentException e) {
1065 //ignored
1066 }
1067 fileObserver.stopWatching();
1068 super.onDestroy();
1069 }
1070
1071 public void toggleScreenEventReceiver() {
1072 if (awayWhenScreenOff() && !manuallyChangePresence()) {
1073 final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
1074 filter.addAction(Intent.ACTION_SCREEN_OFF);
1075 registerReceiver(this.mEventReceiver, filter);
1076 } else {
1077 try {
1078 unregisterReceiver(this.mEventReceiver);
1079 } catch (IllegalArgumentException e) {
1080 //ignored
1081 }
1082 }
1083 }
1084
1085 public void toggleForegroundService() {
1086 if (keepForegroundService()) {
1087 startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification());
1088 } else {
1089 stopForeground(true);
1090 }
1091 }
1092
1093 private boolean keepForegroundService() {
1094 return getPreferences().getBoolean(SettingsActivity.KEEP_FOREGROUND_SERVICE,false);
1095 }
1096
1097 @Override
1098 public void onTaskRemoved(final Intent rootIntent) {
1099 super.onTaskRemoved(rootIntent);
1100 if (!keepForegroundService()) {
1101 this.logoutAndSave(false);
1102 } else {
1103 Log.d(Config.LOGTAG,"ignoring onTaskRemoved because foreground service is activated");
1104 }
1105 }
1106
1107 private void logoutAndSave(boolean stop) {
1108 int activeAccounts = 0;
1109 for (final Account account : accounts) {
1110 if (account.getStatus() != Account.State.DISABLED) {
1111 activeAccounts++;
1112 }
1113 databaseBackend.writeRoster(account.getRoster());
1114 if (account.getXmppConnection() != null) {
1115 new Thread(new Runnable() {
1116 @Override
1117 public void run() {
1118 disconnect(account, false);
1119 }
1120 }).start();
1121 }
1122 }
1123 if (stop || activeAccounts == 0) {
1124 Log.d(Config.LOGTAG, "good bye");
1125 stopSelf();
1126 }
1127 }
1128
1129 public void scheduleWakeUpCall(int seconds, int requestCode) {
1130 final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
1131 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1132 Intent intent = new Intent(this, EventReceiver.class);
1133 intent.setAction("ping");
1134 PendingIntent alarmIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
1135 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timeToWake, alarmIntent);
1136 }
1137
1138 @TargetApi(Build.VERSION_CODES.M)
1139 private void scheduleNextIdlePing() {
1140 Log.d(Config.LOGTAG,"schedule next idle ping");
1141 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
1142 Intent intent = new Intent(this, EventReceiver.class);
1143 intent.setAction(ACTION_IDLE_PING);
1144 alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
1145 SystemClock.elapsedRealtime()+(Config.IDLE_PING_INTERVAL * 1000),
1146 PendingIntent.getBroadcast(this,0,intent,0)
1147 );
1148 }
1149
1150 public XmppConnection createConnection(final Account account) {
1151 final SharedPreferences sharedPref = getPreferences();
1152 String resource;
1153 try {
1154 resource = sharedPref.getString("resource", getString(R.string.default_resource)).toLowerCase(Locale.ENGLISH);
1155 if (resource.trim().isEmpty()) {
1156 throw new Exception();
1157 }
1158 } catch (Exception e) {
1159 resource = "conversations";
1160 }
1161 account.setResource(resource);
1162 final XmppConnection connection = new XmppConnection(account, this);
1163 connection.setOnMessagePacketReceivedListener(this.mMessageParser);
1164 connection.setOnStatusChangedListener(this.statusListener);
1165 connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
1166 connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser);
1167 connection.setOnJinglePacketReceivedListener(this.jingleListener);
1168 connection.setOnBindListener(this.mOnBindListener);
1169 connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener);
1170 connection.addOnAdvancedStreamFeaturesAvailableListener(this.mMessageArchiveService);
1171 connection.addOnAdvancedStreamFeaturesAvailableListener(this.mAvatarService);
1172 AxolotlService axolotlService = account.getAxolotlService();
1173 if (axolotlService != null) {
1174 connection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
1175 }
1176 return connection;
1177 }
1178
1179 public void sendChatState(Conversation conversation) {
1180 if (sendChatStates()) {
1181 MessagePacket packet = mMessageGenerator.generateChatState(conversation);
1182 sendMessagePacket(conversation.getAccount(), packet);
1183 }
1184 }
1185
1186 private void sendFileMessage(final Message message, final boolean delay) {
1187 Log.d(Config.LOGTAG, "send file message");
1188 final Account account = message.getConversation().getAccount();
1189 if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize())
1190 || message.getConversation().getMode() == Conversation.MODE_MULTI) {
1191 mHttpConnectionManager.createNewUploadConnection(message, delay);
1192 } else {
1193 mJingleConnectionManager.createNewConnection(message);
1194 }
1195 }
1196
1197 public void sendMessage(final Message message) {
1198 sendMessage(message, false, false);
1199 }
1200
1201 private void sendMessage(final Message message, final boolean resend, final boolean delay) {
1202 final Account account = message.getConversation().getAccount();
1203 if (account.setShowErrorNotification(true)) {
1204 databaseBackend.updateAccount(account);
1205 mNotificationService.updateErrorNotification();
1206 }
1207 final Conversation conversation = message.getConversation();
1208 account.deactivateGracePeriod();
1209 MessagePacket packet = null;
1210 final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI
1211 || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity()))
1212 && !message.edited();
1213 boolean saveInDb = addToConversation;
1214 message.setStatus(Message.STATUS_WAITING);
1215
1216 if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
1217 message.getConversation().endOtrIfNeeded();
1218 message.getConversation().findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR,
1219 new Conversation.OnMessageFound() {
1220 @Override
1221 public void onMessageFound(Message message) {
1222 markMessage(message, Message.STATUS_SEND_FAILED);
1223 }
1224 });
1225 }
1226
1227 if (account.isOnlineAndConnected()) {
1228 switch (message.getEncryption()) {
1229 case Message.ENCRYPTION_NONE:
1230 if (message.needsUploading()) {
1231 if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize())
1232 || message.fixCounterpart()) {
1233 this.sendFileMessage(message, delay);
1234 } else {
1235 break;
1236 }
1237 } else {
1238 packet = mMessageGenerator.generateChat(message);
1239 }
1240 break;
1241 case Message.ENCRYPTION_PGP:
1242 case Message.ENCRYPTION_DECRYPTED:
1243 if (message.needsUploading()) {
1244 if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize())
1245 || message.fixCounterpart()) {
1246 this.sendFileMessage(message, delay);
1247 } else {
1248 break;
1249 }
1250 } else {
1251 packet = mMessageGenerator.generatePgpChat(message);
1252 }
1253 break;
1254 case Message.ENCRYPTION_OTR:
1255 SessionImpl otrSession = conversation.getOtrSession();
1256 if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
1257 try {
1258 message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
1259 } catch (InvalidJidException e) {
1260 break;
1261 }
1262 if (message.needsUploading()) {
1263 mJingleConnectionManager.createNewConnection(message);
1264 } else {
1265 packet = mMessageGenerator.generateOtrChat(message);
1266 }
1267 } else if (otrSession == null) {
1268 if (message.fixCounterpart()) {
1269 conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
1270 } else {
1271 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not fix counterpart for OTR message to contact "+message.getContact().getJid());
1272 break;
1273 }
1274 } else {
1275 Log.d(Config.LOGTAG,account.getJid().toBareJid()+" OTR session with "+message.getContact()+" is in wrong state: "+otrSession.getSessionStatus().toString());
1276 }
1277 break;
1278 case Message.ENCRYPTION_AXOLOTL:
1279 message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1280 if (message.needsUploading()) {
1281 if (account.httpUploadAvailable(fileBackend.getFile(message,false).getSize())
1282 || message.fixCounterpart()) {
1283 this.sendFileMessage(message, delay);
1284 } else {
1285 break;
1286 }
1287 } else {
1288 XmppAxolotlMessage axolotlMessage = account.getAxolotlService().fetchAxolotlMessageFromCache(message);
1289 if (axolotlMessage == null) {
1290 account.getAxolotlService().preparePayloadMessage(message, delay);
1291 } else {
1292 packet = mMessageGenerator.generateAxolotlChat(message, axolotlMessage);
1293 }
1294 }
1295 break;
1296
1297 }
1298 if (packet != null) {
1299 if (account.getXmppConnection().getFeatures().sm()
1300 || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
1301 message.setStatus(Message.STATUS_UNSEND);
1302 } else {
1303 message.setStatus(Message.STATUS_SEND);
1304 }
1305 }
1306 } else {
1307 switch (message.getEncryption()) {
1308 case Message.ENCRYPTION_DECRYPTED:
1309 if (!message.needsUploading()) {
1310 String pgpBody = message.getEncryptedBody();
1311 String decryptedBody = message.getBody();
1312 message.setBody(pgpBody);
1313 message.setEncryption(Message.ENCRYPTION_PGP);
1314 if (message.edited()) {
1315 message.setBody(decryptedBody);
1316 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1317 databaseBackend.updateMessage(message, message.getEditedId());
1318 updateConversationUi();
1319 return;
1320 } else {
1321 databaseBackend.createMessage(message);
1322 saveInDb = false;
1323 message.setBody(decryptedBody);
1324 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1325 }
1326 }
1327 break;
1328 case Message.ENCRYPTION_OTR:
1329 if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
1330 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": create otr session without starting for "+message.getContact().getJid());
1331 conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
1332 }
1333 break;
1334 case Message.ENCRYPTION_AXOLOTL:
1335 message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
1336 break;
1337 }
1338 }
1339
1340 if (resend) {
1341 if (packet != null && addToConversation) {
1342 if (account.getXmppConnection().getFeatures().sm()
1343 || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
1344 markMessage(message, Message.STATUS_UNSEND);
1345 } else {
1346 markMessage(message, Message.STATUS_SEND);
1347 }
1348 }
1349 } else {
1350 if (addToConversation) {
1351 conversation.add(message);
1352 }
1353 if (saveInDb) {
1354 databaseBackend.createMessage(message);
1355 } else if (message.edited()) {
1356 databaseBackend.updateMessage(message, message.getEditedId());
1357 }
1358 updateConversationUi();
1359 }
1360 if (packet != null) {
1361 if (delay) {
1362 mMessageGenerator.addDelay(packet, message.getTimeSent());
1363 }
1364 if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
1365 if (this.sendChatStates()) {
1366 packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
1367 }
1368 }
1369 sendMessagePacket(account, packet);
1370 }
1371 }
1372
1373 private void sendUnsentMessages(final Conversation conversation) {
1374 conversation.findWaitingMessages(new Conversation.OnMessageFound() {
1375
1376 @Override
1377 public void onMessageFound(Message message) {
1378 resendMessage(message, true);
1379 }
1380 });
1381 }
1382
1383 public void resendMessage(final Message message, final boolean delay) {
1384 sendMessage(message, true, delay);
1385 }
1386
1387 public void fetchRosterFromServer(final Account account) {
1388 final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1389 if (!"".equals(account.getRosterVersion())) {
1390 Log.d(Config.LOGTAG, account.getJid().toBareJid()
1391 + ": fetching roster version " + account.getRosterVersion());
1392 } else {
1393 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster");
1394 }
1395 iqPacket.query(Namespace.ROSTER).setAttribute("ver", account.getRosterVersion());
1396 sendIqPacket(account, iqPacket, mIqParser);
1397 }
1398
1399 public void fetchBookmarks(final Account account) {
1400 final IqPacket iqPacket = new IqPacket(IqPacket.TYPE.GET);
1401 final Element query = iqPacket.query("jabber:iq:private");
1402 query.addChild("storage", "storage:bookmarks");
1403 final OnIqPacketReceived callback = new OnIqPacketReceived() {
1404
1405 @Override
1406 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1407 if (packet.getType() == IqPacket.TYPE.RESULT) {
1408 final Element query = packet.query();
1409 final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
1410 final Element storage = query.findChild("storage", "storage:bookmarks");
1411 final boolean autojoin = respectAutojoin();
1412 if (storage != null) {
1413 for (final Element item : storage.getChildren()) {
1414 if (item.getName().equals("conference")) {
1415 final Bookmark bookmark = Bookmark.parse(item, account);
1416 Bookmark old = bookmarks.put(bookmark.getJid(), bookmark);
1417 if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
1418 bookmark.setBookmarkName(old.getBookmarkName());
1419 }
1420 Conversation conversation = find(bookmark);
1421 if (conversation != null) {
1422 conversation.setBookmark(bookmark);
1423 } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) {
1424 conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false);
1425 conversation.setBookmark(bookmark);
1426 }
1427 }
1428 }
1429 }
1430 account.setBookmarks(new ArrayList<>(bookmarks.values()));
1431 } else {
1432 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fetch bookmarks");
1433 }
1434 }
1435 };
1436 sendIqPacket(account, iqPacket, callback);
1437 }
1438
1439 public void pushBookmarks(Account account) {
1440 Log.d(Config.LOGTAG, account.getJid().toBareJid()+": pushing bookmarks");
1441 IqPacket iqPacket = new IqPacket(IqPacket.TYPE.SET);
1442 Element query = iqPacket.query("jabber:iq:private");
1443 Element storage = query.addChild("storage", "storage:bookmarks");
1444 for (Bookmark bookmark : account.getBookmarks()) {
1445 storage.addChild(bookmark);
1446 }
1447 sendIqPacket(account, iqPacket, mDefaultIqHandler);
1448 }
1449
1450 private void restoreFromDatabase() {
1451 synchronized (this.conversations) {
1452 final Map<String, Account> accountLookupTable = new Hashtable<>();
1453 for (Account account : this.accounts) {
1454 accountLookupTable.put(account.getUuid(), account);
1455 }
1456 Log.d(Config.LOGTAG,"restoring conversations...");
1457 final long startTimeConversationsRestore = SystemClock.elapsedRealtime();
1458 this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
1459 for(Iterator<Conversation> iterator = conversations.listIterator(); iterator.hasNext();) {
1460 Conversation conversation = iterator.next();
1461 Account account = accountLookupTable.get(conversation.getAccountUuid());
1462 if (account != null) {
1463 conversation.setAccount(account);
1464 } else {
1465 Log.e(Config.LOGTAG,"unable to restore Conversations with "+conversation.getJid());
1466 iterator.remove();
1467 }
1468 }
1469 long diffConversationsRestore = SystemClock.elapsedRealtime() - startTimeConversationsRestore;
1470 Log.d(Config.LOGTAG,"finished restoring conversations in "+diffConversationsRestore+"ms");
1471 Runnable runnable = new Runnable() {
1472 @Override
1473 public void run() {
1474 long deletionDate = getAutomaticMessageDeletionDate();
1475 mLastExpiryRun.set(SystemClock.elapsedRealtime());
1476 if (deletionDate > 0) {
1477 Log.d(Config.LOGTAG, "deleting messages that are older than "+AbstractGenerator.getTimestamp(deletionDate));
1478 databaseBackend.expireOldMessages(deletionDate);
1479 }
1480 Log.d(Config.LOGTAG,"restoring roster...");
1481 for (Account account : accounts) {
1482 databaseBackend.readRoster(account.getRoster());
1483 account.initAccountServices(XmppConnectionService.this); //roster needs to be loaded at this stage
1484 }
1485 getBitmapCache().evictAll();
1486 loadPhoneContacts();
1487 Log.d(Config.LOGTAG, "restoring messages...");
1488 final long startMessageRestore = SystemClock.elapsedRealtime();
1489 for (Conversation conversation : conversations) {
1490 conversation.addAll(0, databaseBackend.getMessages(conversation, Config.PAGE_SIZE));
1491 checkDeletedFiles(conversation);
1492 conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
1493
1494 @Override
1495 public void onMessageFound(Message message) {
1496 markMessage(message, Message.STATUS_WAITING);
1497 }
1498 });
1499 conversation.findUnreadMessages(new Conversation.OnMessageFound() {
1500 @Override
1501 public void onMessageFound(Message message) {
1502 mNotificationService.pushFromBacklog(message);
1503 }
1504 });
1505 }
1506 mNotificationService.finishBacklog(false);
1507 mRestoredFromDatabase = true;
1508 final long diffMessageRestore = SystemClock.elapsedRealtime() - startMessageRestore;
1509 Log.d(Config.LOGTAG, "finished restoring messages in "+diffMessageRestore+"ms");
1510 updateConversationUi();
1511 }
1512 };
1513 mDatabaseExecutor.execute(runnable);
1514 }
1515 }
1516
1517 public void loadPhoneContacts() {
1518 mContactMergerExecutor.execute(new Runnable() {
1519 @Override
1520 public void run() {
1521 PhoneHelper.loadPhoneContacts(XmppConnectionService.this, new OnPhoneContactsLoadedListener() {
1522 @Override
1523 public void onPhoneContactsLoaded(List<Bundle> phoneContacts) {
1524 Log.d(Config.LOGTAG, "start merging phone contacts with roster");
1525 for (Account account : accounts) {
1526 List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts();
1527 for (Bundle phoneContact : phoneContacts) {
1528 Jid jid;
1529 try {
1530 jid = Jid.fromString(phoneContact.getString("jid"));
1531 } catch (final InvalidJidException e) {
1532 continue;
1533 }
1534 final Contact contact = account.getRoster().getContact(jid);
1535 String systemAccount = phoneContact.getInt("phoneid")
1536 + "#"
1537 + phoneContact.getString("lookup");
1538 contact.setSystemAccount(systemAccount);
1539 boolean needsCacheClean = contact.setPhotoUri(phoneContact.getString("photouri"));
1540 needsCacheClean |= contact.setSystemName(phoneContact.getString("displayname"));
1541 if (needsCacheClean) {
1542 getAvatarService().clear(contact);
1543 }
1544 withSystemAccounts.remove(contact);
1545 }
1546 for (Contact contact : withSystemAccounts) {
1547 contact.setSystemAccount(null);
1548 boolean needsCacheClean = contact.setPhotoUri(null);
1549 needsCacheClean |= contact.setSystemName(null);
1550 if (needsCacheClean) {
1551 getAvatarService().clear(contact);
1552 }
1553 }
1554 }
1555 Log.d(Config.LOGTAG, "finished merging phone contacts");
1556 mShortcutService.refresh(mInitialAddressbookSyncCompleted.compareAndSet(false,true));
1557 updateAccountUi();
1558 }
1559 });
1560 }
1561 });
1562 }
1563
1564 public List<Conversation> getConversations() {
1565 return this.conversations;
1566 }
1567
1568 private void checkDeletedFiles(Conversation conversation) {
1569 conversation.findMessagesWithFiles(new Conversation.OnMessageFound() {
1570
1571 @Override
1572 public void onMessageFound(Message message) {
1573 if (!getFileBackend().isFileAvailable(message)) {
1574 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
1575 final int s = message.getStatus();
1576 if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) {
1577 markMessage(message, Message.STATUS_SEND_FAILED);
1578 }
1579 }
1580 }
1581 });
1582 }
1583
1584 private void markFileDeleted(final String path) {
1585 Log.d(Config.LOGTAG,"deleted file "+path);
1586 for (Conversation conversation : getConversations()) {
1587 conversation.findMessagesWithFiles(new Conversation.OnMessageFound() {
1588 @Override
1589 public void onMessageFound(Message message) {
1590 DownloadableFile file = fileBackend.getFile(message);
1591 if (file.getAbsolutePath().equals(path)) {
1592 if (!file.exists()) {
1593 message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED));
1594 final int s = message.getStatus();
1595 if (s == Message.STATUS_WAITING || s == Message.STATUS_OFFERED || s == Message.STATUS_UNSEND) {
1596 markMessage(message, Message.STATUS_SEND_FAILED);
1597 } else {
1598 updateConversationUi();
1599 }
1600 } else {
1601 Log.d(Config.LOGTAG,"found matching message for file "+path+" but file still exists");
1602 }
1603 }
1604 }
1605 });
1606 }
1607 }
1608
1609 public void populateWithOrderedConversations(final List<Conversation> list) {
1610 populateWithOrderedConversations(list, true);
1611 }
1612
1613 public void populateWithOrderedConversations(final List<Conversation> list, boolean includeNoFileUpload) {
1614 list.clear();
1615 if (includeNoFileUpload) {
1616 list.addAll(getConversations());
1617 } else {
1618 for (Conversation conversation : getConversations()) {
1619 if (conversation.getMode() == Conversation.MODE_SINGLE
1620 || conversation.getAccount().httpUploadAvailable()) {
1621 list.add(conversation);
1622 }
1623 }
1624 }
1625 try {
1626 Collections.sort(list);
1627 } catch (IllegalArgumentException e) {
1628 //ignore
1629 }
1630 }
1631
1632 public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
1633 if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) {
1634 return;
1635 } else if (timestamp == 0) {
1636 return;
1637 }
1638 Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
1639 Runnable runnable = new Runnable() {
1640 @Override
1641 public void run() {
1642 final Account account = conversation.getAccount();
1643 List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
1644 if (messages.size() > 0) {
1645 conversation.addAll(0, messages);
1646 checkDeletedFiles(conversation);
1647 callback.onMoreMessagesLoaded(messages.size(), conversation);
1648 } else if (conversation.hasMessagesLeftOnServer()
1649 && account.isOnlineAndConnected()
1650 && conversation.getLastClearHistory().getTimestamp() == 0) {
1651 if ((conversation.getMode() == Conversation.MODE_SINGLE && account.getXmppConnection().getFeatures().mam())
1652 || (conversation.getMode() == Conversation.MODE_MULTI && conversation.getMucOptions().mamSupport())) {
1653 MessageArchiveService.Query query = getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
1654 if (query != null) {
1655 query.setCallback(callback);
1656 callback.informUser(R.string.fetching_history_from_server);
1657 } else {
1658 callback.informUser(R.string.not_fetching_history_retention_period);
1659 }
1660
1661 }
1662 }
1663 }
1664 };
1665 mDatabaseExecutor.execute(runnable);
1666 }
1667
1668 public List<Account> getAccounts() {
1669 return this.accounts;
1670 }
1671
1672 public List<Conversation> findAllConferencesWith(Contact contact) {
1673 ArrayList<Conversation> results = new ArrayList<>();
1674 for(Conversation conversation : conversations) {
1675 if (conversation.getMode() == Conversation.MODE_MULTI
1676 && conversation.getMucOptions().isContactInRoom(contact)) {
1677 results.add(conversation);
1678 }
1679 }
1680 return results;
1681 }
1682
1683 public Conversation find(final Iterable<Conversation> haystack, final Contact contact) {
1684 for (final Conversation conversation : haystack) {
1685 if (conversation.getContact() == contact) {
1686 return conversation;
1687 }
1688 }
1689 return null;
1690 }
1691
1692 public Conversation find(final Iterable<Conversation> haystack, final Account account, final Jid jid) {
1693 if (jid == null) {
1694 return null;
1695 }
1696 for (final Conversation conversation : haystack) {
1697 if ((account == null || conversation.getAccount() == account)
1698 && (conversation.getJid().toBareJid().equals(jid.toBareJid()))) {
1699 return conversation;
1700 }
1701 }
1702 return null;
1703 }
1704
1705 public Conversation findOrCreateConversation(Account account, Jid jid, boolean muc, final boolean async) {
1706 return this.findOrCreateConversation(account,jid,muc,false, async);
1707 }
1708
1709 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final boolean async) {
1710 return this.findOrCreateConversation(account, jid, muc, joinAfterCreate, null, async);
1711 }
1712
1713 public Conversation findOrCreateConversation(final Account account, final Jid jid, final boolean muc, final boolean joinAfterCreate, final MessageArchiveService.Query query, final boolean async) {
1714 synchronized (this.conversations) {
1715 Conversation conversation = find(account, jid);
1716 if (conversation != null) {
1717 return conversation;
1718 }
1719 conversation = databaseBackend.findConversation(account, jid);
1720 final boolean loadMessagesFromDb;
1721 if (conversation != null) {
1722 conversation.setStatus(Conversation.STATUS_AVAILABLE);
1723 conversation.setAccount(account);
1724 if (muc) {
1725 conversation.setMode(Conversation.MODE_MULTI);
1726 conversation.setContactJid(jid);
1727 } else {
1728 conversation.setMode(Conversation.MODE_SINGLE);
1729 conversation.setContactJid(jid.toBareJid());
1730 }
1731 databaseBackend.updateConversation(conversation);
1732 loadMessagesFromDb = conversation.messagesLoaded.compareAndSet(true,false);
1733 } else {
1734 String conversationName;
1735 Contact contact = account.getRoster().getContact(jid);
1736 if (contact != null) {
1737 conversationName = contact.getDisplayName();
1738 } else {
1739 conversationName = jid.getLocalpart();
1740 }
1741 if (muc) {
1742 conversation = new Conversation(conversationName, account, jid,
1743 Conversation.MODE_MULTI);
1744 } else {
1745 conversation = new Conversation(conversationName, account, jid.toBareJid(),
1746 Conversation.MODE_SINGLE);
1747 }
1748 this.databaseBackend.createConversation(conversation);
1749 loadMessagesFromDb = false;
1750 }
1751 final Conversation c = conversation;
1752 final Runnable runnable = new Runnable() {
1753 @Override
1754 public void run() {
1755 if (loadMessagesFromDb) {
1756 c.addAll(0, databaseBackend.getMessages(c, Config.PAGE_SIZE));
1757 updateConversationUi();
1758 c.messagesLoaded.set(true);
1759 }
1760 if (account.getXmppConnection() != null
1761 && account.getXmppConnection().getFeatures().mam()
1762 && !muc) {
1763 if (query == null) {
1764 mMessageArchiveService.query(c);
1765 } else {
1766 if (query.getConversation() == null) {
1767 mMessageArchiveService.query(c, query.getStart(),query.isCatchup());
1768 }
1769 }
1770 }
1771 checkDeletedFiles(c);
1772 if (joinAfterCreate) {
1773 joinMuc(c);
1774 }
1775 }
1776 };
1777 if (async) {
1778 mDatabaseExecutor.execute(runnable);
1779 } else {
1780 runnable.run();
1781 }
1782 this.conversations.add(conversation);
1783 updateConversationUi();
1784 return conversation;
1785 }
1786 }
1787
1788 public void archiveConversation(Conversation conversation) {
1789 getNotificationService().clear(conversation);
1790 conversation.setStatus(Conversation.STATUS_ARCHIVED);
1791 synchronized (this.conversations) {
1792 if (conversation.getMode() == Conversation.MODE_MULTI) {
1793 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
1794 Bookmark bookmark = conversation.getBookmark();
1795 if (bookmark != null && bookmark.autojoin() && respectAutojoin()) {
1796 bookmark.setAutojoin(false);
1797 pushBookmarks(bookmark.getAccount());
1798 }
1799 }
1800 leaveMuc(conversation);
1801 } else {
1802 conversation.endOtrIfNeeded();
1803 if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
1804 Log.d(Config.LOGTAG, "Canceling presence request from " + conversation.getJid().toString());
1805 sendPresencePacket(
1806 conversation.getAccount(),
1807 mPresenceGenerator.stopPresenceUpdatesTo(conversation.getContact())
1808 );
1809 }
1810 }
1811 updateConversation(conversation);
1812 this.conversations.remove(conversation);
1813 updateConversationUi();
1814 }
1815 }
1816
1817 public void createAccount(final Account account) {
1818 account.initAccountServices(this);
1819 databaseBackend.createAccount(account);
1820 this.accounts.add(account);
1821 this.reconnectAccountInBackground(account);
1822 updateAccountUi();
1823 }
1824
1825 public void createAccountFromKey(final String alias, final OnAccountCreated callback) {
1826 new Thread(new Runnable() {
1827 @Override
1828 public void run() {
1829 try {
1830 X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
1831 Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
1832 if (findAccountByJid(info.first) == null) {
1833 Account account = new Account(info.first, "");
1834 account.setPrivateKeyAlias(alias);
1835 account.setOption(Account.OPTION_DISABLED, true);
1836 account.setDisplayName(info.second);
1837 createAccount(account);
1838 callback.onAccountCreated(account);
1839 if (Config.X509_VERIFICATION) {
1840 try {
1841 getMemorizingTrustManager().getNonInteractive(account.getJid().getDomainpart()).checkClientTrusted(chain, "RSA");
1842 } catch (CertificateException e) {
1843 callback.informUser(R.string.certificate_chain_is_not_trusted);
1844 }
1845 }
1846 } else {
1847 callback.informUser(R.string.account_already_exists);
1848 }
1849 } catch (Exception e) {
1850 e.printStackTrace();
1851 callback.informUser(R.string.unable_to_parse_certificate);
1852 }
1853 }
1854 }).start();
1855
1856 }
1857
1858 public void updateKeyInAccount(final Account account, final String alias) {
1859 Log.d(Config.LOGTAG, account.getJid().toBareJid()+": update key in account " + alias);
1860 try {
1861 X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
1862 Log.d(Config.LOGTAG,account.getJid().toBareJid()+" loaded certificate chain");
1863 Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
1864 if (account.getJid().toBareJid().equals(info.first)) {
1865 account.setPrivateKeyAlias(alias);
1866 account.setDisplayName(info.second);
1867 databaseBackend.updateAccount(account);
1868 if (Config.X509_VERIFICATION) {
1869 try {
1870 getMemorizingTrustManager().getNonInteractive().checkClientTrusted(chain, "RSA");
1871 } catch (CertificateException e) {
1872 showErrorToastInUi(R.string.certificate_chain_is_not_trusted);
1873 }
1874 account.getAxolotlService().regenerateKeys(true);
1875 }
1876 } else {
1877 showErrorToastInUi(R.string.jid_does_not_match_certificate);
1878 }
1879 } catch (Exception e) {
1880 e.printStackTrace();
1881 }
1882 }
1883
1884 public boolean updateAccount(final Account account) {
1885 if (databaseBackend.updateAccount(account)) {
1886 account.setShowErrorNotification(true);
1887 this.statusListener.onStatusChanged(account);
1888 databaseBackend.updateAccount(account);
1889 reconnectAccountInBackground(account);
1890 updateAccountUi();
1891 getNotificationService().updateErrorNotification();
1892 return true;
1893 } else {
1894 return false;
1895 }
1896 }
1897
1898 public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
1899 final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword);
1900 sendIqPacket(account, iq, new OnIqPacketReceived() {
1901 @Override
1902 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1903 if (packet.getType() == IqPacket.TYPE.RESULT) {
1904 account.setPassword(newPassword);
1905 account.setOption(Account.OPTION_MAGIC_CREATE, false);
1906 databaseBackend.updateAccount(account);
1907 callback.onPasswordChangeSucceeded();
1908 } else {
1909 callback.onPasswordChangeFailed();
1910 }
1911 }
1912 });
1913 }
1914
1915 public void deleteAccount(final Account account) {
1916 synchronized (this.conversations) {
1917 for (final Conversation conversation : conversations) {
1918 if (conversation.getAccount() == account) {
1919 if (conversation.getMode() == Conversation.MODE_MULTI) {
1920 leaveMuc(conversation);
1921 } else if (conversation.getMode() == Conversation.MODE_SINGLE) {
1922 conversation.endOtrIfNeeded();
1923 }
1924 conversations.remove(conversation);
1925 }
1926 }
1927 if (account.getXmppConnection() != null) {
1928 new Thread(new Runnable() {
1929 @Override
1930 public void run() {
1931 disconnect(account, true);
1932 }
1933 }).start();
1934 }
1935 Runnable runnable = new Runnable() {
1936 @Override
1937 public void run() {
1938 if (!databaseBackend.deleteAccount(account)) {
1939 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": unable to delete account");
1940 }
1941 }
1942 };
1943 mDatabaseExecutor.execute(runnable);
1944 this.accounts.remove(account);
1945 updateAccountUi();
1946 getNotificationService().updateErrorNotification();
1947 }
1948 }
1949
1950 public void setOnConversationListChangedListener(OnConversationUpdate listener) {
1951 synchronized (this) {
1952 this.mLastActivity = System.currentTimeMillis();
1953 if (checkListeners()) {
1954 switchToForeground();
1955 }
1956 this.mOnConversationUpdate = listener;
1957 this.mNotificationService.setIsInForeground(true);
1958 if (this.convChangedListenerCount < 2) {
1959 this.convChangedListenerCount++;
1960 }
1961 }
1962 }
1963
1964 public void removeOnConversationListChangedListener() {
1965 synchronized (this) {
1966 this.convChangedListenerCount--;
1967 if (this.convChangedListenerCount <= 0) {
1968 this.convChangedListenerCount = 0;
1969 this.mOnConversationUpdate = null;
1970 this.mNotificationService.setIsInForeground(false);
1971 if (checkListeners()) {
1972 switchToBackground();
1973 }
1974 }
1975 }
1976 }
1977
1978 public void setOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
1979 synchronized (this) {
1980 if (checkListeners()) {
1981 switchToForeground();
1982 }
1983 this.mOnShowErrorToast = onShowErrorToast;
1984 if (this.showErrorToastListenerCount < 2) {
1985 this.showErrorToastListenerCount++;
1986 }
1987 }
1988 this.mOnShowErrorToast = onShowErrorToast;
1989 }
1990
1991 public void removeOnShowErrorToastListener() {
1992 synchronized (this) {
1993 this.showErrorToastListenerCount--;
1994 if (this.showErrorToastListenerCount <= 0) {
1995 this.showErrorToastListenerCount = 0;
1996 this.mOnShowErrorToast = null;
1997 if (checkListeners()) {
1998 switchToBackground();
1999 }
2000 }
2001 }
2002 }
2003
2004 public void setOnAccountListChangedListener(OnAccountUpdate listener) {
2005 synchronized (this) {
2006 if (checkListeners()) {
2007 switchToForeground();
2008 }
2009 this.mOnAccountUpdate = listener;
2010 if (this.accountChangedListenerCount < 2) {
2011 this.accountChangedListenerCount++;
2012 }
2013 }
2014 }
2015
2016 public void removeOnAccountListChangedListener() {
2017 synchronized (this) {
2018 this.accountChangedListenerCount--;
2019 if (this.accountChangedListenerCount <= 0) {
2020 this.mOnAccountUpdate = null;
2021 this.accountChangedListenerCount = 0;
2022 if (checkListeners()) {
2023 switchToBackground();
2024 }
2025 }
2026 }
2027 }
2028
2029 public void setOnCaptchaRequestedListener(OnCaptchaRequested listener) {
2030 synchronized (this) {
2031 if (checkListeners()) {
2032 switchToForeground();
2033 }
2034 this.mOnCaptchaRequested = listener;
2035 if (this.captchaRequestedListenerCount < 2) {
2036 this.captchaRequestedListenerCount++;
2037 }
2038 }
2039 }
2040
2041 public void removeOnCaptchaRequestedListener() {
2042 synchronized (this) {
2043 this.captchaRequestedListenerCount--;
2044 if (this.captchaRequestedListenerCount <= 0) {
2045 this.mOnCaptchaRequested = null;
2046 this.captchaRequestedListenerCount = 0;
2047 if (checkListeners()) {
2048 switchToBackground();
2049 }
2050 }
2051 }
2052 }
2053
2054 public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
2055 synchronized (this) {
2056 if (checkListeners()) {
2057 switchToForeground();
2058 }
2059 this.mOnRosterUpdate = listener;
2060 if (this.rosterChangedListenerCount < 2) {
2061 this.rosterChangedListenerCount++;
2062 }
2063 }
2064 }
2065
2066 public void removeOnRosterUpdateListener() {
2067 synchronized (this) {
2068 this.rosterChangedListenerCount--;
2069 if (this.rosterChangedListenerCount <= 0) {
2070 this.rosterChangedListenerCount = 0;
2071 this.mOnRosterUpdate = null;
2072 if (checkListeners()) {
2073 switchToBackground();
2074 }
2075 }
2076 }
2077 }
2078
2079 public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
2080 synchronized (this) {
2081 if (checkListeners()) {
2082 switchToForeground();
2083 }
2084 this.mOnUpdateBlocklist = listener;
2085 if (this.updateBlocklistListenerCount < 2) {
2086 this.updateBlocklistListenerCount++;
2087 }
2088 }
2089 }
2090
2091 public void removeOnUpdateBlocklistListener() {
2092 synchronized (this) {
2093 this.updateBlocklistListenerCount--;
2094 if (this.updateBlocklistListenerCount <= 0) {
2095 this.updateBlocklistListenerCount = 0;
2096 this.mOnUpdateBlocklist = null;
2097 if (checkListeners()) {
2098 switchToBackground();
2099 }
2100 }
2101 }
2102 }
2103
2104 public void setOnKeyStatusUpdatedListener(final OnKeyStatusUpdated listener) {
2105 synchronized (this) {
2106 if (checkListeners()) {
2107 switchToForeground();
2108 }
2109 this.mOnKeyStatusUpdated = listener;
2110 if (this.keyStatusUpdatedListenerCount < 2) {
2111 this.keyStatusUpdatedListenerCount++;
2112 }
2113 }
2114 }
2115
2116 public void removeOnNewKeysAvailableListener() {
2117 synchronized (this) {
2118 this.keyStatusUpdatedListenerCount--;
2119 if (this.keyStatusUpdatedListenerCount <= 0) {
2120 this.keyStatusUpdatedListenerCount = 0;
2121 this.mOnKeyStatusUpdated = null;
2122 if (checkListeners()) {
2123 switchToBackground();
2124 }
2125 }
2126 }
2127 }
2128
2129 public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
2130 synchronized (this) {
2131 if (checkListeners()) {
2132 switchToForeground();
2133 }
2134 this.mOnMucRosterUpdate = listener;
2135 if (this.mucRosterChangedListenerCount < 2) {
2136 this.mucRosterChangedListenerCount++;
2137 }
2138 }
2139 }
2140
2141 public void removeOnMucRosterUpdateListener() {
2142 synchronized (this) {
2143 this.mucRosterChangedListenerCount--;
2144 if (this.mucRosterChangedListenerCount <= 0) {
2145 this.mucRosterChangedListenerCount = 0;
2146 this.mOnMucRosterUpdate = null;
2147 if (checkListeners()) {
2148 switchToBackground();
2149 }
2150 }
2151 }
2152 }
2153
2154 public boolean checkListeners() {
2155 return (this.mOnAccountUpdate == null
2156 && this.mOnConversationUpdate == null
2157 && this.mOnRosterUpdate == null
2158 && this.mOnCaptchaRequested == null
2159 && this.mOnUpdateBlocklist == null
2160 && this.mOnShowErrorToast == null
2161 && this.mOnKeyStatusUpdated == null);
2162 }
2163
2164 private void switchToForeground() {
2165 final boolean broadcastLastActivity = broadcastLastActivity();
2166 for (Conversation conversation : getConversations()) {
2167 if (conversation.getMode() == Conversation.MODE_MULTI) {
2168 conversation.getMucOptions().resetChatState();
2169 } else {
2170 conversation.setIncomingChatState(Config.DEFAULT_CHATSTATE);
2171 }
2172 }
2173 for (Account account : getAccounts()) {
2174 if (account.getStatus() == Account.State.ONLINE) {
2175 account.deactivateGracePeriod();
2176 final XmppConnection connection = account.getXmppConnection();
2177 if (connection != null ) {
2178 if (connection.getFeatures().csi()) {
2179 connection.sendActive();
2180 }
2181 if (broadcastLastActivity) {
2182 sendPresence(account, false); //send new presence but don't include idle because we are not
2183 }
2184 }
2185 }
2186 }
2187 Log.d(Config.LOGTAG, "app switched into foreground");
2188 }
2189
2190 private void switchToBackground() {
2191 final boolean broadcastLastActivity = broadcastLastActivity();
2192 for (Account account : getAccounts()) {
2193 if (account.getStatus() == Account.State.ONLINE) {
2194 XmppConnection connection = account.getXmppConnection();
2195 if (connection != null) {
2196 if (broadcastLastActivity) {
2197 sendPresence(account, broadcastLastActivity);
2198 }
2199 if (connection.getFeatures().csi()) {
2200 connection.sendInactive();
2201 }
2202 }
2203 }
2204 }
2205 this.mNotificationService.setIsInForeground(false);
2206 Log.d(Config.LOGTAG, "app switched into background");
2207 }
2208
2209 private void connectMultiModeConversations(Account account) {
2210 List<Conversation> conversations = getConversations();
2211 for (Conversation conversation : conversations) {
2212 if (conversation.getMode() == Conversation.MODE_MULTI && conversation.getAccount() == account) {
2213 joinMuc(conversation);
2214 }
2215 }
2216 }
2217
2218 public void joinMuc(Conversation conversation) {
2219 joinMuc(conversation,null, false);
2220 }
2221
2222 public void joinMuc(Conversation conversation, boolean followedInvite) {
2223 joinMuc(conversation, null, followedInvite);
2224 }
2225
2226 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined) {
2227 joinMuc(conversation,onConferenceJoined,false);
2228 }
2229
2230 private void joinMuc(Conversation conversation, final OnConferenceJoined onConferenceJoined, final boolean followedInvite) {
2231 Account account = conversation.getAccount();
2232 account.pendingConferenceJoins.remove(conversation);
2233 account.pendingConferenceLeaves.remove(conversation);
2234 if (account.getStatus() == Account.State.ONLINE) {
2235 conversation.resetMucOptions();
2236 if (onConferenceJoined != null) {
2237 conversation.getMucOptions().flagNoAutoPushConfiguration();
2238 }
2239 conversation.setHasMessagesLeftOnServer(false);
2240 fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() {
2241
2242 private void join(Conversation conversation) {
2243 Account account = conversation.getAccount();
2244 final MucOptions mucOptions = conversation.getMucOptions();
2245 final Jid joinJid = mucOptions.getSelf().getFullJid();
2246 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString());
2247 PresencePacket packet = mPresenceGenerator.selfPresence(account, Presence.Status.ONLINE, mucOptions.nonanonymous() || onConferenceJoined != null);
2248 packet.setTo(joinJid);
2249 Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
2250 if (conversation.getMucOptions().getPassword() != null) {
2251 x.addChild("password").setContent(mucOptions.getPassword());
2252 }
2253
2254 if (mucOptions.mamSupport()) {
2255 // Use MAM instead of the limited muc history to get history
2256 x.addChild("history").setAttribute("maxchars", "0");
2257 } else {
2258 // Fallback to muc history
2259 x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted().getTimestamp()));
2260 }
2261 sendPresencePacket(account, packet);
2262 if (onConferenceJoined != null) {
2263 onConferenceJoined.onConferenceJoined(conversation);
2264 }
2265 if (!joinJid.equals(conversation.getJid())) {
2266 conversation.setContactJid(joinJid);
2267 databaseBackend.updateConversation(conversation);
2268 }
2269
2270 if (mucOptions.mamSupport()) {
2271 getMessageArchiveService().catchupMUC(conversation);
2272 }
2273 if (mucOptions.membersOnly() && mucOptions.nonanonymous()) {
2274 fetchConferenceMembers(conversation);
2275 if (followedInvite && conversation.getBookmark() == null) {
2276 saveConversationAsBookmark(conversation,null);
2277 }
2278 }
2279 sendUnsentMessages(conversation);
2280 }
2281
2282 @Override
2283 public void onConferenceConfigurationFetched(Conversation conversation) {
2284 join(conversation);
2285 }
2286
2287 @Override
2288 public void onFetchFailed(final Conversation conversation, Element error) {
2289 if (error != null && "remote-server-not-found".equals(error.getName())) {
2290 conversation.getMucOptions().setError(MucOptions.Error.SERVER_NOT_FOUND);
2291 updateConversationUi();
2292 } else {
2293 join(conversation);
2294 fetchConferenceConfiguration(conversation);
2295 }
2296 }
2297 });
2298 updateConversationUi();
2299 } else {
2300 account.pendingConferenceJoins.add(conversation);
2301 conversation.resetMucOptions();
2302 conversation.setHasMessagesLeftOnServer(false);
2303 updateConversationUi();
2304 }
2305 }
2306
2307 private void fetchConferenceMembers(final Conversation conversation) {
2308 final Account account = conversation.getAccount();
2309 final String[] affiliations = {"member","admin","owner"};
2310 OnIqPacketReceived callback = new OnIqPacketReceived() {
2311
2312 private int i = 0;
2313 private boolean success = true;
2314
2315 @Override
2316 public void onIqPacketReceived(Account account, IqPacket packet) {
2317
2318 Element query = packet.query("http://jabber.org/protocol/muc#admin");
2319 if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2320 for(Element child : query.getChildren()) {
2321 if ("item".equals(child.getName())) {
2322 MucOptions.User user = AbstractParser.parseItem(conversation,child);
2323 if (!user.realJidMatchesAccount()) {
2324 conversation.getMucOptions().updateUser(user);
2325 }
2326 }
2327 }
2328 } else {
2329 success = false;
2330 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not request affiliation "+affiliations[i]+" in "+conversation.getJid().toBareJid());
2331 }
2332 ++i;
2333 if (i >= affiliations.length) {
2334 List<Jid> members = conversation.getMucOptions().getMembers();
2335 if (success) {
2336 List<Jid> cryptoTargets = conversation.getAcceptedCryptoTargets();
2337 boolean changed = false;
2338 for(ListIterator<Jid> iterator = cryptoTargets.listIterator(); iterator.hasNext();) {
2339 Jid jid = iterator.next();
2340 if (!members.contains(jid)) {
2341 iterator.remove();
2342 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": removed "+jid+" from crypto targets of "+conversation.getName());
2343 changed = true;
2344 }
2345 }
2346 if (changed) {
2347 conversation.setAcceptedCryptoTargets(cryptoTargets);
2348 updateConversation(conversation);
2349 }
2350 }
2351 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": retrieved members for "+conversation.getJid().toBareJid()+": "+conversation.getMucOptions().getMembers());
2352 getAvatarService().clear(conversation);
2353 updateMucRosterUi();
2354 updateConversationUi();
2355 }
2356 }
2357 };
2358 for(String affiliation : affiliations) {
2359 sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback);
2360 }
2361 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": fetching members for "+conversation.getName());
2362 }
2363
2364 public void providePasswordForMuc(Conversation conversation, String password) {
2365 if (conversation.getMode() == Conversation.MODE_MULTI) {
2366 conversation.getMucOptions().setPassword(password);
2367 if (conversation.getBookmark() != null) {
2368 if (respectAutojoin()) {
2369 conversation.getBookmark().setAutojoin(true);
2370 }
2371 pushBookmarks(conversation.getAccount());
2372 }
2373 updateConversation(conversation);
2374 joinMuc(conversation);
2375 }
2376 }
2377
2378 public void renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
2379 final MucOptions options = conversation.getMucOptions();
2380 final Jid joinJid = options.createJoinJid(nick);
2381 if (options.online()) {
2382 Account account = conversation.getAccount();
2383 options.setOnRenameListener(new OnRenameListener() {
2384
2385 @Override
2386 public void onSuccess() {
2387 conversation.setContactJid(joinJid);
2388 databaseBackend.updateConversation(conversation);
2389 Bookmark bookmark = conversation.getBookmark();
2390 if (bookmark != null) {
2391 bookmark.setNick(nick);
2392 pushBookmarks(bookmark.getAccount());
2393 }
2394 callback.success(conversation);
2395 }
2396
2397 @Override
2398 public void onFailure() {
2399 callback.error(R.string.nick_in_use, conversation);
2400 }
2401 });
2402
2403 PresencePacket packet = new PresencePacket();
2404 packet.setTo(joinJid);
2405 packet.setFrom(conversation.getAccount().getJid());
2406
2407 String sig = account.getPgpSignature();
2408 if (sig != null) {
2409 packet.addChild("status").setContent("online");
2410 packet.addChild("x", "jabber:x:signed").setContent(sig);
2411 }
2412 sendPresencePacket(account, packet);
2413 } else {
2414 conversation.setContactJid(joinJid);
2415 databaseBackend.updateConversation(conversation);
2416 if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
2417 Bookmark bookmark = conversation.getBookmark();
2418 if (bookmark != null) {
2419 bookmark.setNick(nick);
2420 pushBookmarks(bookmark.getAccount());
2421 }
2422 joinMuc(conversation);
2423 }
2424 }
2425 }
2426
2427 public void leaveMuc(Conversation conversation) {
2428 leaveMuc(conversation, false);
2429 }
2430
2431 private void leaveMuc(Conversation conversation, boolean now) {
2432 Account account = conversation.getAccount();
2433 account.pendingConferenceJoins.remove(conversation);
2434 account.pendingConferenceLeaves.remove(conversation);
2435 if (account.getStatus() == Account.State.ONLINE || now) {
2436 PresencePacket packet = new PresencePacket();
2437 packet.setTo(conversation.getMucOptions().getSelf().getFullJid());
2438 packet.setFrom(conversation.getAccount().getJid());
2439 packet.setAttribute("type", "unavailable");
2440 sendPresencePacket(conversation.getAccount(), packet);
2441 conversation.getMucOptions().setOffline();
2442 conversation.deregisterWithBookmark();
2443 Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid()
2444 + ": leaving muc " + conversation.getJid());
2445 } else {
2446 account.pendingConferenceLeaves.add(conversation);
2447 }
2448 }
2449
2450 public String findConferenceServer(final Account account) {
2451 String server;
2452 if (account.getXmppConnection() != null) {
2453 server = account.getXmppConnection().getMucServer();
2454 if (server != null) {
2455 return server;
2456 }
2457 }
2458 for (Account other : getAccounts()) {
2459 if (other != account && other.getXmppConnection() != null) {
2460 server = other.getXmppConnection().getMucServer();
2461 if (server != null) {
2462 return server;
2463 }
2464 }
2465 }
2466 return null;
2467 }
2468
2469 public boolean createAdhocConference(final Account account,
2470 final String subject,
2471 final Iterable<Jid> jids,
2472 final UiCallback<Conversation> callback) {
2473 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": creating adhoc conference with " + jids.toString());
2474 if (account.getStatus() == Account.State.ONLINE) {
2475 try {
2476 String server = findConferenceServer(account);
2477 if (server == null) {
2478 if (callback != null) {
2479 callback.error(R.string.no_conference_server_found, null);
2480 }
2481 return false;
2482 }
2483 final Jid jid = Jid.fromParts(new BigInteger(64, getRNG()).toString(Character.MAX_RADIX), server, null);
2484 final Conversation conversation = findOrCreateConversation(account, jid, true, false, true);
2485 joinMuc(conversation, new OnConferenceJoined() {
2486 @Override
2487 public void onConferenceJoined(final Conversation conversation) {
2488 pushConferenceConfiguration(conversation, IqGenerator.defaultRoomConfiguration(), new OnConfigurationPushed() {
2489 @Override
2490 public void onPushSucceeded() {
2491 if (subject != null && !subject.trim().isEmpty()) {
2492 pushSubjectToConference(conversation, subject.trim());
2493 }
2494 for (Jid invite : jids) {
2495 invite(conversation, invite);
2496 }
2497 if (account.countPresences() > 1) {
2498 directInvite(conversation, account.getJid().toBareJid());
2499 }
2500 saveConversationAsBookmark(conversation, subject);
2501 if (callback != null) {
2502 callback.success(conversation);
2503 }
2504 }
2505
2506 @Override
2507 public void onPushFailed() {
2508 archiveConversation(conversation);
2509 if (callback != null) {
2510 callback.error(R.string.conference_creation_failed, conversation);
2511 }
2512 }
2513 });
2514 }
2515 });
2516 return true;
2517 } catch (InvalidJidException e) {
2518 if (callback != null) {
2519 callback.error(R.string.conference_creation_failed, null);
2520 }
2521 return false;
2522 }
2523 } else {
2524 if (callback != null) {
2525 callback.error(R.string.not_connected_try_again, null);
2526 }
2527 return false;
2528 }
2529 }
2530
2531 public void fetchConferenceConfiguration(final Conversation conversation) {
2532 fetchConferenceConfiguration(conversation, null);
2533 }
2534
2535 public void fetchConferenceConfiguration(final Conversation conversation, final OnConferenceConfigurationFetched callback) {
2536 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2537 request.setTo(conversation.getJid().toBareJid());
2538 request.query("http://jabber.org/protocol/disco#info");
2539 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
2540 @Override
2541 public void onIqPacketReceived(Account account, IqPacket packet) {
2542 Element query = packet.findChild("query","http://jabber.org/protocol/disco#info");
2543 if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
2544 ArrayList<String> features = new ArrayList<>();
2545 for (Element child : query.getChildren()) {
2546 if (child != null && child.getName().equals("feature")) {
2547 String var = child.getAttribute("var");
2548 if (var != null) {
2549 features.add(var);
2550 }
2551 }
2552 }
2553 Element form = query.findChild("x", "jabber:x:data");
2554 if (form != null) {
2555 conversation.getMucOptions().updateFormData(Data.parse(form));
2556 }
2557 conversation.getMucOptions().updateFeatures(features);
2558 if (callback != null) {
2559 callback.onConferenceConfigurationFetched(conversation);
2560 }
2561 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": fetched muc configuration for "+conversation.getJid().toBareJid()+" - "+features.toString());
2562 updateConversationUi();
2563 } else if (packet.getType() == IqPacket.TYPE.ERROR) {
2564 if (callback != null) {
2565 callback.onFetchFailed(conversation, packet.getError());
2566 }
2567 }
2568 }
2569 });
2570 }
2571
2572 public void pushNodeConfiguration(Account account, final Jid jid, final String node, final Bundle options, final OnConfigurationPushed callback) {
2573 sendIqPacket(account, mIqGenerator.requestPubsubConfiguration(jid,node), new OnIqPacketReceived() {
2574 @Override
2575 public void onIqPacketReceived(Account account, IqPacket packet) {
2576 if (packet.getType() == IqPacket.TYPE.RESULT) {
2577 Element pubsub = packet.findChild("pubsub","http://jabber.org/protocol/pubsub#owner");
2578 Element configuration = pubsub == null ? null : pubsub.findChild("configure");
2579 Element x = configuration == null ? null : configuration.findChild("x","jabber:x:data");
2580 if (x != null) {
2581 Data data = Data.parse(x);
2582 data.submit(options);
2583 sendIqPacket(account, mIqGenerator.publishPubsubConfiguration(jid, node, data), new OnIqPacketReceived() {
2584 @Override
2585 public void onIqPacketReceived(Account account, IqPacket packet) {
2586 if (packet.getType() == IqPacket.TYPE.RESULT) {
2587 callback.onPushSucceeded();
2588 } else {
2589 Log.d(Config.LOGTAG,packet.toString());
2590 }
2591 }
2592 });
2593 } else {
2594 callback.onPushFailed();
2595 }
2596 } else {
2597 callback.onPushFailed();
2598 }
2599 }
2600 });
2601 }
2602
2603 public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConfigurationPushed callback) {
2604 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
2605 request.setTo(conversation.getJid().toBareJid());
2606 request.query("http://jabber.org/protocol/muc#owner");
2607 sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() {
2608 @Override
2609 public void onIqPacketReceived(Account account, IqPacket packet) {
2610 if (packet.getType() == IqPacket.TYPE.RESULT) {
2611 Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));
2612 data.submit(options);
2613 IqPacket set = new IqPacket(IqPacket.TYPE.SET);
2614 set.setTo(conversation.getJid().toBareJid());
2615 set.query("http://jabber.org/protocol/muc#owner").addChild(data);
2616 sendIqPacket(account, set, new OnIqPacketReceived() {
2617 @Override
2618 public void onIqPacketReceived(Account account, IqPacket packet) {
2619 if (callback != null) {
2620 if (packet.getType() == IqPacket.TYPE.RESULT) {
2621 callback.onPushSucceeded();
2622 } else {
2623 callback.onPushFailed();
2624 }
2625 }
2626 }
2627 });
2628 } else {
2629 if (callback != null) {
2630 callback.onPushFailed();
2631 }
2632 }
2633 }
2634 });
2635 }
2636
2637 public void pushSubjectToConference(final Conversation conference, final String subject) {
2638 MessagePacket packet = this.getMessageGenerator().conferenceSubject(conference, subject);
2639 this.sendMessagePacket(conference.getAccount(), packet);
2640 final MucOptions mucOptions = conference.getMucOptions();
2641 final MucOptions.User self = mucOptions.getSelf();
2642 if (!mucOptions.persistent() && self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
2643 Bundle options = new Bundle();
2644 options.putString("muc#roomconfig_persistentroom", "1");
2645 this.pushConferenceConfiguration(conference, options, null);
2646 }
2647 }
2648
2649 public void changeAffiliationInConference(final Conversation conference, Jid user, final MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
2650 final Jid jid = user.toBareJid();
2651 IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
2652 sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
2653 @Override
2654 public void onIqPacketReceived(Account account, IqPacket packet) {
2655 if (packet.getType() == IqPacket.TYPE.RESULT) {
2656 conference.getMucOptions().changeAffiliation(jid, affiliation);
2657 getAvatarService().clear(conference);
2658 callback.onAffiliationChangedSuccessful(jid);
2659 } else {
2660 callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
2661 }
2662 }
2663 });
2664 }
2665
2666 public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
2667 List<Jid> jids = new ArrayList<>();
2668 for (MucOptions.User user : conference.getMucOptions().getUsers()) {
2669 if (user.getAffiliation() == before && user.getRealJid() != null) {
2670 jids.add(user.getRealJid());
2671 }
2672 }
2673 IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
2674 sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
2675 }
2676
2677 public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role, final OnRoleChanged callback) {
2678 IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
2679 Log.d(Config.LOGTAG, request.toString());
2680 sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
2681 @Override
2682 public void onIqPacketReceived(Account account, IqPacket packet) {
2683 Log.d(Config.LOGTAG, packet.toString());
2684 if (packet.getType() == IqPacket.TYPE.RESULT) {
2685 callback.onRoleChangedSuccessful(nick);
2686 } else {
2687 callback.onRoleChangeFailed(nick, R.string.could_not_change_role);
2688 }
2689 }
2690 });
2691 }
2692
2693 private void disconnect(Account account, boolean force) {
2694 if ((account.getStatus() == Account.State.ONLINE)
2695 || (account.getStatus() == Account.State.DISABLED)) {
2696 final XmppConnection connection = account.getXmppConnection();
2697 if (!force) {
2698 List<Conversation> conversations = getConversations();
2699 for (Conversation conversation : conversations) {
2700 if (conversation.getAccount() == account) {
2701 if (conversation.getMode() == Conversation.MODE_MULTI) {
2702 leaveMuc(conversation, true);
2703 } else {
2704 if (conversation.endOtrIfNeeded()) {
2705 Log.d(Config.LOGTAG, account.getJid().toBareJid()
2706 + ": ended otr session with "
2707 + conversation.getJid());
2708 }
2709 }
2710 }
2711 }
2712 sendOfflinePresence(account);
2713 }
2714 connection.disconnect(force);
2715 }
2716 }
2717
2718 @Override
2719 public IBinder onBind(Intent intent) {
2720 return mBinder;
2721 }
2722
2723 public void updateMessage(Message message) {
2724 databaseBackend.updateMessage(message);
2725 updateConversationUi();
2726 }
2727
2728 public void updateMessage(Message message, String uuid) {
2729 databaseBackend.updateMessage(message, uuid);
2730 updateConversationUi();
2731 }
2732
2733 protected void syncDirtyContacts(Account account) {
2734 for (Contact contact : account.getRoster().getContacts()) {
2735 if (contact.getOption(Contact.Options.DIRTY_PUSH)) {
2736 pushContactToServer(contact);
2737 }
2738 if (contact.getOption(Contact.Options.DIRTY_DELETE)) {
2739 deleteContactOnServer(contact);
2740 }
2741 }
2742 }
2743
2744 public void createContact(Contact contact) {
2745 boolean autoGrant = getPreferences().getBoolean("grant_new_contacts", true);
2746 if (autoGrant) {
2747 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
2748 contact.setOption(Contact.Options.ASKING);
2749 }
2750 pushContactToServer(contact);
2751 }
2752
2753 public void onOtrSessionEstablished(Conversation conversation) {
2754 final Account account = conversation.getAccount();
2755 final Session otrSession = conversation.getOtrSession();
2756 Log.d(Config.LOGTAG,
2757 account.getJid().toBareJid() + " otr session established with "
2758 + conversation.getJid() + "/"
2759 + otrSession.getSessionID().getUserID());
2760 conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {
2761
2762 @Override
2763 public void onMessageFound(Message message) {
2764 SessionID id = otrSession.getSessionID();
2765 try {
2766 message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
2767 } catch (InvalidJidException e) {
2768 return;
2769 }
2770 if (message.needsUploading()) {
2771 mJingleConnectionManager.createNewConnection(message);
2772 } else {
2773 MessagePacket outPacket = mMessageGenerator.generateOtrChat(message);
2774 if (outPacket != null) {
2775 mMessageGenerator.addDelay(outPacket, message.getTimeSent());
2776 message.setStatus(Message.STATUS_SEND);
2777 databaseBackend.updateMessage(message);
2778 sendMessagePacket(account, outPacket);
2779 }
2780 }
2781 updateConversationUi();
2782 }
2783 });
2784 }
2785
2786 public boolean renewSymmetricKey(Conversation conversation) {
2787 Account account = conversation.getAccount();
2788 byte[] symmetricKey = new byte[32];
2789 this.mRandom.nextBytes(symmetricKey);
2790 Session otrSession = conversation.getOtrSession();
2791 if (otrSession != null) {
2792 MessagePacket packet = new MessagePacket();
2793 packet.setType(MessagePacket.TYPE_CHAT);
2794 packet.setFrom(account.getJid());
2795 MessageGenerator.addMessageHints(packet);
2796 packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/"
2797 + otrSession.getSessionID().getUserID());
2798 try {
2799 packet.setBody(otrSession
2800 .transformSending(CryptoHelper.FILETRANSFER
2801 + CryptoHelper.bytesToHex(symmetricKey))[0]);
2802 sendMessagePacket(account, packet);
2803 conversation.setSymmetricKey(symmetricKey);
2804 return true;
2805 } catch (OtrException e) {
2806 return false;
2807 }
2808 }
2809 return false;
2810 }
2811
2812 public void pushContactToServer(final Contact contact) {
2813 contact.resetOption(Contact.Options.DIRTY_DELETE);
2814 contact.setOption(Contact.Options.DIRTY_PUSH);
2815 final Account account = contact.getAccount();
2816 if (account.getStatus() == Account.State.ONLINE) {
2817 final boolean ask = contact.getOption(Contact.Options.ASKING);
2818 final boolean sendUpdates = contact
2819 .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
2820 && contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
2821 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
2822 iq.query(Namespace.ROSTER).addChild(contact.asElement());
2823 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
2824 if (sendUpdates) {
2825 sendPresencePacket(account,
2826 mPresenceGenerator.sendPresenceUpdatesTo(contact));
2827 }
2828 if (ask) {
2829 sendPresencePacket(account,
2830 mPresenceGenerator.requestPresenceUpdatesFrom(contact));
2831 }
2832 }
2833 }
2834
2835 public void publishAvatar(Account account, Uri image, UiCallback<Avatar> callback) {
2836 final Bitmap.CompressFormat format = Config.AVATAR_FORMAT;
2837 final int size = Config.AVATAR_SIZE;
2838 final Avatar avatar = getFileBackend().getPepAvatar(image, size, format);
2839 if (avatar != null) {
2840 avatar.height = size;
2841 avatar.width = size;
2842 if (format.equals(Bitmap.CompressFormat.WEBP)) {
2843 avatar.type = "image/webp";
2844 } else if (format.equals(Bitmap.CompressFormat.JPEG)) {
2845 avatar.type = "image/jpeg";
2846 } else if (format.equals(Bitmap.CompressFormat.PNG)) {
2847 avatar.type = "image/png";
2848 }
2849 if (!getFileBackend().save(avatar)) {
2850 callback.error(R.string.error_saving_avatar, avatar);
2851 return;
2852 }
2853 publishAvatar(account, avatar, callback);
2854 } else {
2855 callback.error(R.string.error_publish_avatar_converting, null);
2856 }
2857 }
2858
2859 public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2860 IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
2861 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2862
2863 @Override
2864 public void onIqPacketReceived(Account account, IqPacket result) {
2865 if (result.getType() == IqPacket.TYPE.RESULT) {
2866 final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar);
2867 sendIqPacket(account, packet, new OnIqPacketReceived() {
2868 @Override
2869 public void onIqPacketReceived(Account account, IqPacket result) {
2870 if (result.getType() == IqPacket.TYPE.RESULT) {
2871 if (account.setAvatar(avatar.getFilename())) {
2872 getAvatarService().clear(account);
2873 databaseBackend.updateAccount(account);
2874 }
2875 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": published avatar "+(avatar.size/1024)+"KiB");
2876 if (callback != null) {
2877 callback.success(avatar);
2878 }
2879 } else {
2880 if (callback != null) {
2881 callback.error(R.string.error_publish_avatar_server_reject,avatar);
2882 }
2883 }
2884 }
2885 });
2886 } else {
2887 Element error = result.findChild("error");
2888 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server rejected avatar "+(avatar.size/1024)+"KiB "+(error!=null?error.toString():""));
2889 if (callback != null) {
2890 callback.error(R.string.error_publish_avatar_server_reject, avatar);
2891 }
2892 }
2893 }
2894 });
2895 }
2896
2897 public void republishAvatarIfNeeded(Account account) {
2898 if (account.getAxolotlService().isPepBroken()) {
2899 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": skipping republication of avatar because pep is broken");
2900 return;
2901 }
2902 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
2903 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
2904
2905 private Avatar parseAvatar(IqPacket packet) {
2906 Element pubsub = packet.findChild("pubsub", "http://jabber.org/protocol/pubsub");
2907 if (pubsub != null) {
2908 Element items = pubsub.findChild("items");
2909 if (items != null) {
2910 return Avatar.parseMetadata(items);
2911 }
2912 }
2913 return null;
2914 }
2915
2916 private boolean errorIsItemNotFound(IqPacket packet) {
2917 Element error = packet.findChild("error");
2918 return packet.getType() == IqPacket.TYPE.ERROR
2919 && error != null
2920 && error.hasChild("item-not-found");
2921 }
2922
2923 @Override
2924 public void onIqPacketReceived(Account account, IqPacket packet) {
2925 if (packet.getType() == IqPacket.TYPE.RESULT || errorIsItemNotFound(packet)) {
2926 Avatar serverAvatar = parseAvatar(packet);
2927 if (serverAvatar == null && account.getAvatar() != null) {
2928 Avatar avatar = fileBackend.getStoredPepAvatar(account.getAvatar());
2929 if (avatar != null) {
2930 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": avatar on server was null. republishing");
2931 publishAvatar(account, fileBackend.getStoredPepAvatar(account.getAvatar()), null);
2932 } else {
2933 Log.e(Config.LOGTAG, account.getJid().toBareJid()+": error rereading avatar");
2934 }
2935 }
2936 }
2937 }
2938 });
2939 }
2940
2941 public void fetchAvatar(Account account, Avatar avatar) {
2942 fetchAvatar(account, avatar, null);
2943 }
2944
2945 public void fetchAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2946 final String KEY = generateFetchKey(account, avatar);
2947 synchronized (this.mInProgressAvatarFetches) {
2948 if (!this.mInProgressAvatarFetches.contains(KEY)) {
2949 switch (avatar.origin) {
2950 case PEP:
2951 this.mInProgressAvatarFetches.add(KEY);
2952 fetchAvatarPep(account, avatar, callback);
2953 break;
2954 case VCARD:
2955 this.mInProgressAvatarFetches.add(KEY);
2956 fetchAvatarVcard(account, avatar, callback);
2957 break;
2958 }
2959 }
2960 }
2961 }
2962
2963 private void fetchAvatarPep(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
2964 IqPacket packet = this.mIqGenerator.retrievePepAvatar(avatar);
2965 sendIqPacket(account, packet, new OnIqPacketReceived() {
2966
2967 @Override
2968 public void onIqPacketReceived(Account account, IqPacket result) {
2969 synchronized (mInProgressAvatarFetches) {
2970 mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
2971 }
2972 final String ERROR = account.getJid().toBareJid()
2973 + ": fetching avatar for " + avatar.owner + " failed ";
2974 if (result.getType() == IqPacket.TYPE.RESULT) {
2975 avatar.image = mIqParser.avatarData(result);
2976 if (avatar.image != null) {
2977 if (getFileBackend().save(avatar)) {
2978 if (account.getJid().toBareJid().equals(avatar.owner)) {
2979 if (account.setAvatar(avatar.getFilename())) {
2980 databaseBackend.updateAccount(account);
2981 }
2982 getAvatarService().clear(account);
2983 updateConversationUi();
2984 updateAccountUi();
2985 } else {
2986 Contact contact = account.getRoster()
2987 .getContact(avatar.owner);
2988 contact.setAvatar(avatar);
2989 getAvatarService().clear(contact);
2990 updateConversationUi();
2991 updateRosterUi();
2992 }
2993 if (callback != null) {
2994 callback.success(avatar);
2995 }
2996 Log.d(Config.LOGTAG, account.getJid().toBareJid()
2997 + ": successfully fetched pep avatar for " + avatar.owner);
2998 return;
2999 }
3000 } else {
3001
3002 Log.d(Config.LOGTAG, ERROR + "(parsing error)");
3003 }
3004 } else {
3005 Element error = result.findChild("error");
3006 if (error == null) {
3007 Log.d(Config.LOGTAG, ERROR + "(server error)");
3008 } else {
3009 Log.d(Config.LOGTAG, ERROR + error.toString());
3010 }
3011 }
3012 if (callback != null) {
3013 callback.error(0, null);
3014 }
3015
3016 }
3017 });
3018 }
3019
3020 private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
3021 IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar);
3022 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3023 @Override
3024 public void onIqPacketReceived(Account account, IqPacket packet) {
3025 synchronized (mInProgressAvatarFetches) {
3026 mInProgressAvatarFetches.remove(generateFetchKey(account, avatar));
3027 }
3028 if (packet.getType() == IqPacket.TYPE.RESULT) {
3029 Element vCard = packet.findChild("vCard", "vcard-temp");
3030 Element photo = vCard != null ? vCard.findChild("PHOTO") : null;
3031 String image = photo != null ? photo.findChildContent("BINVAL") : null;
3032 if (image != null) {
3033 avatar.image = image;
3034 if (getFileBackend().save(avatar)) {
3035 Log.d(Config.LOGTAG, account.getJid().toBareJid()
3036 + ": successfully fetched vCard avatar for " + avatar.owner);
3037 if (avatar.owner.isBareJid()) {
3038 if (account.getJid().toBareJid().equals(avatar.owner) && account.getAvatar() == null) {
3039 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": had no avatar. replacing with vcard");
3040 account.setAvatar(avatar.getFilename());
3041 databaseBackend.updateAccount(account);
3042 getAvatarService().clear(account);
3043 updateAccountUi();
3044 } else {
3045 Contact contact = account.getRoster().getContact(avatar.owner);
3046 contact.setAvatar(avatar);
3047 getAvatarService().clear(contact);
3048 updateRosterUi();
3049 }
3050 updateConversationUi();
3051 } else {
3052 Conversation conversation = find(account, avatar.owner.toBareJid());
3053 if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
3054 MucOptions.User user = conversation.getMucOptions().findUserByFullJid(avatar.owner);
3055 if (user != null) {
3056 if (user.setAvatar(avatar)) {
3057 getAvatarService().clear(user);
3058 updateConversationUi();
3059 updateMucRosterUi();
3060 }
3061 }
3062 }
3063 }
3064 }
3065 }
3066 }
3067 }
3068 });
3069 }
3070
3071 public void checkForAvatar(Account account, final UiCallback<Avatar> callback) {
3072 IqPacket packet = this.mIqGenerator.retrieveAvatarMetaData(null);
3073 this.sendIqPacket(account, packet, new OnIqPacketReceived() {
3074
3075 @Override
3076 public void onIqPacketReceived(Account account, IqPacket packet) {
3077 if (packet.getType() == IqPacket.TYPE.RESULT) {
3078 Element pubsub = packet.findChild("pubsub","http://jabber.org/protocol/pubsub");
3079 if (pubsub != null) {
3080 Element items = pubsub.findChild("items");
3081 if (items != null) {
3082 Avatar avatar = Avatar.parseMetadata(items);
3083 if (avatar != null) {
3084 avatar.owner = account.getJid().toBareJid();
3085 if (fileBackend.isAvatarCached(avatar)) {
3086 if (account.setAvatar(avatar.getFilename())) {
3087 databaseBackend.updateAccount(account);
3088 }
3089 getAvatarService().clear(account);
3090 callback.success(avatar);
3091 } else {
3092 fetchAvatarPep(account, avatar, callback);
3093 }
3094 return;
3095 }
3096 }
3097 }
3098 }
3099 callback.error(0, null);
3100 }
3101 });
3102 }
3103
3104 public void deleteContactOnServer(Contact contact) {
3105 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
3106 contact.resetOption(Contact.Options.DIRTY_PUSH);
3107 contact.setOption(Contact.Options.DIRTY_DELETE);
3108 Account account = contact.getAccount();
3109 if (account.getStatus() == Account.State.ONLINE) {
3110 IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
3111 Element item = iq.query(Namespace.ROSTER).addChild("item");
3112 item.setAttribute("jid", contact.getJid().toString());
3113 item.setAttribute("subscription", "remove");
3114 account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler);
3115 }
3116 }
3117
3118 public void updateConversation(final Conversation conversation) {
3119 mDatabaseExecutor.execute(new Runnable() {
3120 @Override
3121 public void run() {
3122 databaseBackend.updateConversation(conversation);
3123 }
3124 });
3125 }
3126
3127 private void reconnectAccount(final Account account, final boolean force, final boolean interactive) {
3128 synchronized (account) {
3129 XmppConnection connection = account.getXmppConnection();
3130 if (connection == null) {
3131 connection = createConnection(account);
3132 account.setXmppConnection(connection);
3133 }
3134 boolean hasInternet = hasInternetConnection();
3135 if (!account.isOptionSet(Account.OPTION_DISABLED) && hasInternet) {
3136 if (!force) {
3137 disconnect(account, false);
3138 }
3139 Thread thread = new Thread(connection);
3140 connection.setInteractive(interactive);
3141 connection.prepareNewConnection();
3142 connection.interrupt();
3143 thread.start();
3144 scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
3145 } else {
3146 disconnect(account, force || account.getTrueStatus().isError() || !hasInternet);
3147 account.getRoster().clearPresences();
3148 connection.resetEverything();
3149 final AxolotlService axolotlService = account.getAxolotlService();
3150 if (axolotlService != null) {
3151 axolotlService.resetBrokenness();
3152 }
3153 if (!hasInternet) {
3154 account.setStatus(Account.State.NO_INTERNET);
3155 }
3156 }
3157 }
3158 }
3159
3160 public void reconnectAccountInBackground(final Account account) {
3161 new Thread(new Runnable() {
3162 @Override
3163 public void run() {
3164 reconnectAccount(account, false, true);
3165 }
3166 }).start();
3167 }
3168
3169 public void invite(Conversation conversation, Jid contact) {
3170 Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": inviting " + contact + " to " + conversation.getJid().toBareJid());
3171 MessagePacket packet = mMessageGenerator.invite(conversation, contact);
3172 sendMessagePacket(conversation.getAccount(), packet);
3173 }
3174
3175 public void directInvite(Conversation conversation, Jid jid) {
3176 MessagePacket packet = mMessageGenerator.directInvite(conversation, jid);
3177 sendMessagePacket(conversation.getAccount(), packet);
3178 }
3179
3180 public void resetSendingToWaiting(Account account) {
3181 for (Conversation conversation : getConversations()) {
3182 if (conversation.getAccount() == account) {
3183 conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
3184
3185 @Override
3186 public void onMessageFound(Message message) {
3187 markMessage(message, Message.STATUS_WAITING);
3188 }
3189 });
3190 }
3191 }
3192 }
3193
3194 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status) {
3195 return markMessage(account, recipient, uuid, status, null);
3196 }
3197
3198 public Message markMessage(final Account account, final Jid recipient, final String uuid, final int status, String errorMessage) {
3199 if (uuid == null) {
3200 return null;
3201 }
3202 for (Conversation conversation : getConversations()) {
3203 if (conversation.getJid().toBareJid().equals(recipient) && conversation.getAccount() == account) {
3204 final Message message = conversation.findSentMessageWithUuidOrRemoteId(uuid);
3205 if (message != null) {
3206 markMessage(message, status, errorMessage);
3207 }
3208 return message;
3209 }
3210 }
3211 return null;
3212 }
3213
3214 public boolean markMessage(Conversation conversation, String uuid, int status, String serverMessageId) {
3215 if (uuid == null) {
3216 return false;
3217 } else {
3218 Message message = conversation.findSentMessageWithUuid(uuid);
3219 if (message != null) {
3220 if (message.getServerMsgId() == null) {
3221 message.setServerMsgId(serverMessageId);
3222 }
3223 markMessage(message, status);
3224 return true;
3225 } else {
3226 return false;
3227 }
3228 }
3229 }
3230
3231 public void markMessage(Message message, int status) {
3232 markMessage(message, status, null);
3233 }
3234
3235
3236 public void markMessage(Message message, int status, String errorMessage) {
3237 if (status == Message.STATUS_SEND_FAILED
3238 && (message.getStatus() == Message.STATUS_SEND_RECEIVED || message
3239 .getStatus() == Message.STATUS_SEND_DISPLAYED)) {
3240 return;
3241 }
3242 message.setErrorMessage(errorMessage);
3243 message.setStatus(status);
3244 databaseBackend.updateMessage(message);
3245 updateConversationUi();
3246 }
3247
3248 public SharedPreferences getPreferences() {
3249 return PreferenceManager
3250 .getDefaultSharedPreferences(getApplicationContext());
3251 }
3252
3253 public long getAutomaticMessageDeletionDate() {
3254 try {
3255 final long timeout = Long.parseLong(getPreferences().getString(SettingsActivity.AUTOMATIC_MESSAGE_DELETION, "0")) * 1000;
3256 return timeout == 0 ? timeout : System.currentTimeMillis() - timeout;
3257 } catch (NumberFormatException e) {
3258 return 0;
3259 }
3260 }
3261
3262 public boolean confirmMessages() {
3263 return getPreferences().getBoolean("confirm_messages", true);
3264 }
3265
3266 public boolean allowMessageCorrection() {
3267 return getPreferences().getBoolean("allow_message_correction", true);
3268 }
3269
3270 public boolean sendChatStates() {
3271 return getPreferences().getBoolean("chat_states", false);
3272 }
3273
3274 private boolean respectAutojoin() {
3275 return getPreferences().getBoolean("autojoin", true);
3276 }
3277
3278 public boolean indicateReceived() {
3279 return getPreferences().getBoolean("indicate_received", false);
3280 }
3281
3282 public boolean useTorToConnect() {
3283 return Config.FORCE_ORBOT || getPreferences().getBoolean("use_tor", false);
3284 }
3285
3286 public boolean showExtendedConnectionOptions() {
3287 return getPreferences().getBoolean("show_connection_options", false);
3288 }
3289
3290 public boolean broadcastLastActivity() {
3291 return getPreferences().getBoolean(SettingsActivity.BROADCAST_LAST_ACTIVITY, false);
3292 }
3293
3294 public int unreadCount() {
3295 int count = 0;
3296 for (Conversation conversation : getConversations()) {
3297 count += conversation.unreadCount();
3298 }
3299 return count;
3300 }
3301
3302
3303 public void showErrorToastInUi(int resId) {
3304 if (mOnShowErrorToast != null) {
3305 mOnShowErrorToast.onShowErrorToast(resId);
3306 }
3307 }
3308
3309 public void updateConversationUi() {
3310 if (mOnConversationUpdate != null) {
3311 mOnConversationUpdate.onConversationUpdate();
3312 }
3313 }
3314
3315 public void updateAccountUi() {
3316 if (mOnAccountUpdate != null) {
3317 mOnAccountUpdate.onAccountUpdate();
3318 }
3319 }
3320
3321 public void updateRosterUi() {
3322 if (mOnRosterUpdate != null) {
3323 mOnRosterUpdate.onRosterUpdate();
3324 }
3325 }
3326
3327 public boolean displayCaptchaRequest(Account account, String id, Data data, Bitmap captcha) {
3328 if (mOnCaptchaRequested != null) {
3329 DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
3330 Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
3331 (int) (captcha.getHeight() * metrics.scaledDensity), false);
3332
3333 mOnCaptchaRequested.onCaptchaRequested(account, id, data, scaled);
3334 return true;
3335 }
3336 return false;
3337 }
3338
3339 public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
3340 if (mOnUpdateBlocklist != null) {
3341 mOnUpdateBlocklist.OnUpdateBlocklist(status);
3342 }
3343 }
3344
3345 public void updateMucRosterUi() {
3346 if (mOnMucRosterUpdate != null) {
3347 mOnMucRosterUpdate.onMucRosterUpdate();
3348 }
3349 }
3350
3351 public void keyStatusUpdated(AxolotlService.FetchStatus report) {
3352 if (mOnKeyStatusUpdated != null) {
3353 mOnKeyStatusUpdated.onKeyStatusUpdated(report);
3354 }
3355 }
3356
3357 public Account findAccountByJid(final Jid accountJid) {
3358 for (Account account : this.accounts) {
3359 if (account.getJid().toBareJid().equals(accountJid.toBareJid())) {
3360 return account;
3361 }
3362 }
3363 return null;
3364 }
3365
3366 public Conversation findConversationByUuid(String uuid) {
3367 for (Conversation conversation : getConversations()) {
3368 if (conversation.getUuid().equals(uuid)) {
3369 return conversation;
3370 }
3371 }
3372 return null;
3373 }
3374
3375 public boolean markRead(final Conversation conversation) {
3376 return markRead(conversation,true);
3377 }
3378
3379 public boolean markRead(final Conversation conversation, boolean clear) {
3380 if (clear) {
3381 mNotificationService.clear(conversation);
3382 }
3383 final List<Message> readMessages = conversation.markRead();
3384 if (readMessages.size() > 0) {
3385 Runnable runnable = new Runnable() {
3386 @Override
3387 public void run() {
3388 for (Message message : readMessages) {
3389 databaseBackend.updateMessage(message);
3390 }
3391 }
3392 };
3393 mDatabaseExecutor.execute(runnable);
3394 updateUnreadCountBadge();
3395 return true;
3396 } else {
3397 return false;
3398 }
3399 }
3400
3401 public synchronized void updateUnreadCountBadge() {
3402 int count = unreadCount();
3403 if (unreadCount != count) {
3404 Log.d(Config.LOGTAG, "update unread count to " + count);
3405 if (count > 0) {
3406 ShortcutBadger.applyCount(getApplicationContext(), count);
3407 } else {
3408 ShortcutBadger.removeCount(getApplicationContext());
3409 }
3410 unreadCount = count;
3411 }
3412 }
3413
3414 public void sendReadMarker(final Conversation conversation) {
3415 final Message markable = conversation.getLatestMarkableMessage();
3416 if (this.markRead(conversation)) {
3417 updateConversationUi();
3418 }
3419 if (confirmMessages()
3420 && markable != null
3421 && markable.trusted()
3422 && markable.getRemoteMsgId() != null) {
3423 Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
3424 Account account = conversation.getAccount();
3425 final Jid to = markable.getCounterpart();
3426 MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId());
3427 this.sendMessagePacket(conversation.getAccount(), packet);
3428 }
3429 }
3430
3431 public SecureRandom getRNG() {
3432 return this.mRandom;
3433 }
3434
3435 public MemorizingTrustManager getMemorizingTrustManager() {
3436 return this.mMemorizingTrustManager;
3437 }
3438
3439 public void setMemorizingTrustManager(MemorizingTrustManager trustManager) {
3440 this.mMemorizingTrustManager = trustManager;
3441 }
3442
3443 public void updateMemorizingTrustmanager() {
3444 final MemorizingTrustManager tm;
3445 final boolean dontTrustSystemCAs = getPreferences().getBoolean("dont_trust_system_cas", false);
3446 if (dontTrustSystemCAs) {
3447 tm = new MemorizingTrustManager(getApplicationContext(), null);
3448 } else {
3449 tm = new MemorizingTrustManager(getApplicationContext());
3450 }
3451 setMemorizingTrustManager(tm);
3452 }
3453
3454 public PowerManager getPowerManager() {
3455 return this.pm;
3456 }
3457
3458 public LruCache<String, Bitmap> getBitmapCache() {
3459 return this.mBitmapCache;
3460 }
3461
3462 public void syncRosterToDisk(final Account account) {
3463 Runnable runnable = new Runnable() {
3464
3465 @Override
3466 public void run() {
3467 databaseBackend.writeRoster(account.getRoster());
3468 }
3469 };
3470 mDatabaseExecutor.execute(runnable);
3471
3472 }
3473
3474 public List<String> getKnownHosts() {
3475 final List<String> hosts = new ArrayList<>();
3476 for (final Account account : getAccounts()) {
3477 if (!hosts.contains(account.getServer().toString())) {
3478 hosts.add(account.getServer().toString());
3479 }
3480 for (final Contact contact : account.getRoster().getContacts()) {
3481 if (contact.showInRoster()) {
3482 final String server = contact.getServer().toString();
3483 if (server != null && !hosts.contains(server)) {
3484 hosts.add(server);
3485 }
3486 }
3487 }
3488 }
3489 if(Config.DOMAIN_LOCK != null && !hosts.contains(Config.DOMAIN_LOCK)) {
3490 hosts.add(Config.DOMAIN_LOCK);
3491 }
3492 if(Config.MAGIC_CREATE_DOMAIN != null && !hosts.contains(Config.MAGIC_CREATE_DOMAIN)) {
3493 hosts.add(Config.MAGIC_CREATE_DOMAIN);
3494 }
3495 return hosts;
3496 }
3497
3498 public List<String> getKnownConferenceHosts() {
3499 final ArrayList<String> mucServers = new ArrayList<>();
3500 for (final Account account : accounts) {
3501 if (account.getXmppConnection() != null) {
3502 final String server = account.getXmppConnection().getMucServer();
3503 if (server != null && !mucServers.contains(server)) {
3504 mucServers.add(server);
3505 }
3506 for(Bookmark bookmark : account.getBookmarks()) {
3507 final Jid jid = bookmark.getJid();
3508 final String s = jid == null ? null : jid.getDomainpart();
3509 if (s != null && !mucServers.contains(s)) {
3510 mucServers.add(s);
3511 }
3512 }
3513 }
3514 }
3515 return mucServers;
3516 }
3517
3518 public void sendMessagePacket(Account account, MessagePacket packet) {
3519 XmppConnection connection = account.getXmppConnection();
3520 if (connection != null) {
3521 connection.sendMessagePacket(packet);
3522 }
3523 }
3524
3525 public void sendPresencePacket(Account account, PresencePacket packet) {
3526 XmppConnection connection = account.getXmppConnection();
3527 if (connection != null) {
3528 connection.sendPresencePacket(packet);
3529 }
3530 }
3531
3532 public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) {
3533 final XmppConnection connection = account.getXmppConnection();
3534 if (connection != null) {
3535 IqPacket request = mIqGenerator.generateCreateAccountWithCaptcha(account, id, data);
3536 connection.sendUnmodifiedIqPacket(request, connection.registrationResponseListener);
3537 }
3538 }
3539
3540 public void sendIqPacket(final Account account, final IqPacket packet, final OnIqPacketReceived callback) {
3541 final XmppConnection connection = account.getXmppConnection();
3542 if (connection != null) {
3543 connection.sendIqPacket(packet, callback);
3544 }
3545 }
3546
3547 public void sendPresence(final Account account) {
3548 sendPresence(account, checkListeners() && broadcastLastActivity());
3549 }
3550
3551 private void sendPresence(final Account account, final boolean includeIdleTimestamp) {
3552 PresencePacket packet;
3553 if (manuallyChangePresence()) {
3554 packet = mPresenceGenerator.selfPresence(account, account.getPresenceStatus());
3555 String message = account.getPresenceStatusMessage();
3556 if (message != null && !message.isEmpty()) {
3557 packet.addChild(new Element("status").setContent(message));
3558 }
3559 } else {
3560 packet = mPresenceGenerator.selfPresence(account, getTargetPresence());
3561 }
3562 if (mLastActivity > 0 && includeIdleTimestamp) {
3563 long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates
3564 packet.addChild("idle",Namespace.IDLE).setAttribute("since", AbstractGenerator.getTimestamp(since));
3565 }
3566 sendPresencePacket(account, packet);
3567 }
3568
3569 private void deactivateGracePeriod() {
3570 for(Account account : getAccounts()) {
3571 account.deactivateGracePeriod();
3572 }
3573 }
3574
3575 public void refreshAllPresences() {
3576 boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity();
3577 for (Account account : getAccounts()) {
3578 if (!account.isOptionSet(Account.OPTION_DISABLED)) {
3579 sendPresence(account, includeIdleTimestamp);
3580 }
3581 }
3582 }
3583
3584 private void refreshAllGcmTokens() {
3585 for(Account account : getAccounts()) {
3586 if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
3587 mPushManagementService.registerPushTokenOnServer(account);
3588 }
3589 }
3590 }
3591
3592 private void sendOfflinePresence(final Account account) {
3593 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sending offline presence");
3594 sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
3595 }
3596
3597 public MessageGenerator getMessageGenerator() {
3598 return this.mMessageGenerator;
3599 }
3600
3601 public PresenceGenerator getPresenceGenerator() {
3602 return this.mPresenceGenerator;
3603 }
3604
3605 public IqGenerator getIqGenerator() {
3606 return this.mIqGenerator;
3607 }
3608
3609 public IqParser getIqParser() {
3610 return this.mIqParser;
3611 }
3612
3613 public JingleConnectionManager getJingleConnectionManager() {
3614 return this.mJingleConnectionManager;
3615 }
3616
3617 public MessageArchiveService getMessageArchiveService() {
3618 return this.mMessageArchiveService;
3619 }
3620
3621 public List<Contact> findContacts(Jid jid, String accountJid) {
3622 ArrayList<Contact> contacts = new ArrayList<>();
3623 for (Account account : getAccounts()) {
3624 if ((!account.isOptionSet(Account.OPTION_DISABLED) || accountJid != null)
3625 && (accountJid == null || accountJid.equals(account.getJid().toBareJid().toString()))) {
3626 Contact contact = account.getRoster().getContactFromRoster(jid);
3627 if (contact != null) {
3628 contacts.add(contact);
3629 }
3630 }
3631 }
3632 return contacts;
3633 }
3634
3635 public Conversation findFirstMuc(Jid jid) {
3636 for(Conversation conversation : getConversations()) {
3637 if (conversation.getJid().toBareJid().equals(jid.toBareJid())
3638 && conversation.getMode() == Conversation.MODE_MULTI) {
3639 return conversation;
3640 }
3641 }
3642 return null;
3643 }
3644
3645 public NotificationService getNotificationService() {
3646 return this.mNotificationService;
3647 }
3648
3649 public HttpConnectionManager getHttpConnectionManager() {
3650 return this.mHttpConnectionManager;
3651 }
3652
3653 public void resendFailedMessages(final Message message) {
3654 final Collection<Message> messages = new ArrayList<>();
3655 Message current = message;
3656 while (current.getStatus() == Message.STATUS_SEND_FAILED) {
3657 messages.add(current);
3658 if (current.mergeable(current.next())) {
3659 current = current.next();
3660 } else {
3661 break;
3662 }
3663 }
3664 for (final Message msg : messages) {
3665 msg.setTime(System.currentTimeMillis());
3666 markMessage(msg, Message.STATUS_WAITING);
3667 this.resendMessage(msg, false);
3668 }
3669 }
3670
3671 public void clearConversationHistory(final Conversation conversation) {
3672 final long clearDate;
3673 final String reference;
3674 if (conversation.countMessages() > 0) {
3675 Message latestMessage = conversation.getLatestMessage();
3676 clearDate = latestMessage.getTimeSent() + 1000;
3677 reference = latestMessage.getServerMsgId();
3678 } else {
3679 clearDate = System.currentTimeMillis();
3680 reference = null;
3681 }
3682 conversation.clearMessages();
3683 conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
3684 conversation.setLastClearHistory(clearDate,reference);
3685 Runnable runnable = new Runnable() {
3686 @Override
3687 public void run() {
3688 databaseBackend.deleteMessagesInConversation(conversation);
3689 databaseBackend.updateConversation(conversation);
3690 }
3691 };
3692 mDatabaseExecutor.execute(runnable);
3693 }
3694
3695 public boolean sendBlockRequest(final Blockable blockable, boolean reportSpam) {
3696 if (blockable != null && blockable.getBlockedJid() != null) {
3697 final Jid jid = blockable.getBlockedJid();
3698 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetBlockRequest(jid, reportSpam), new OnIqPacketReceived() {
3699
3700 @Override
3701 public void onIqPacketReceived(final Account account, final IqPacket packet) {
3702 if (packet.getType() == IqPacket.TYPE.RESULT) {
3703 account.getBlocklist().add(jid);
3704 updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
3705 }
3706 }
3707 });
3708 if (removeBlockedConversations(blockable.getAccount(),jid)) {
3709 updateConversationUi();
3710 return true;
3711 } else {
3712 return false;
3713 }
3714 } else {
3715 return false;
3716 }
3717 }
3718
3719 public boolean removeBlockedConversations(final Account account, final Jid blockedJid) {
3720 boolean removed = false;
3721 synchronized (this.conversations) {
3722 boolean domainJid = blockedJid.isDomainJid();
3723 for(Conversation conversation : this.conversations) {
3724 boolean jidMatches = (domainJid && blockedJid.getDomainpart().equals(conversation.getJid().getDomainpart()))
3725 || blockedJid.equals(conversation.getJid().toBareJid());
3726 if (conversation.getAccount() == account
3727 && conversation.getMode() == Conversation.MODE_SINGLE
3728 && jidMatches) {
3729 this.conversations.remove(conversation);
3730 markRead(conversation);
3731 conversation.setStatus(Conversation.STATUS_ARCHIVED);
3732 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": archiving conversation "+conversation.getJid().toBareJid()+" because jid was blocked");
3733 updateConversation(conversation);
3734 removed = true;
3735 }
3736 }
3737 }
3738 return removed;
3739 }
3740
3741 public void sendUnblockRequest(final Blockable blockable) {
3742 if (blockable != null && blockable.getJid() != null) {
3743 final Jid jid = blockable.getBlockedJid();
3744 this.sendIqPacket(blockable.getAccount(), getIqGenerator().generateSetUnblockRequest(jid), new OnIqPacketReceived() {
3745 @Override
3746 public void onIqPacketReceived(final Account account, final IqPacket packet) {
3747 if (packet.getType() == IqPacket.TYPE.RESULT) {
3748 account.getBlocklist().remove(jid);
3749 updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
3750 }
3751 }
3752 });
3753 }
3754 }
3755
3756 public void publishDisplayName(Account account) {
3757 String displayName = account.getDisplayName();
3758 if (displayName != null && !displayName.isEmpty()) {
3759 IqPacket publish = mIqGenerator.publishNick(displayName);
3760 sendIqPacket(account, publish, new OnIqPacketReceived() {
3761 @Override
3762 public void onIqPacketReceived(Account account, IqPacket packet) {
3763 if (packet.getType() == IqPacket.TYPE.ERROR) {
3764 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not publish nick");
3765 }
3766 }
3767 });
3768 }
3769 }
3770
3771 public ServiceDiscoveryResult getCachedServiceDiscoveryResult(Pair<String, String> key) {
3772 ServiceDiscoveryResult result = discoCache.get(key);
3773 if (result != null) {
3774 return result;
3775 } else {
3776 result = databaseBackend.findDiscoveryResult(key.first, key.second);
3777 if (result != null) {
3778 discoCache.put(key, result);
3779 }
3780 return result;
3781 }
3782 }
3783
3784 public void fetchCaps(Account account, final Jid jid, final Presence presence) {
3785 final Pair<String,String> key = new Pair<>(presence.getHash(), presence.getVer());
3786 ServiceDiscoveryResult disco = getCachedServiceDiscoveryResult(key);
3787 if (disco != null) {
3788 presence.setServiceDiscoveryResult(disco);
3789 } else {
3790 if (!account.inProgressDiscoFetches.contains(key)) {
3791 account.inProgressDiscoFetches.add(key);
3792 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3793 request.setTo(jid);
3794 request.query("http://jabber.org/protocol/disco#info");
3795 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": making disco request for "+key.second+" to "+jid);
3796 sendIqPacket(account, request, new OnIqPacketReceived() {
3797 @Override
3798 public void onIqPacketReceived(Account account, IqPacket discoPacket) {
3799 if (discoPacket.getType() == IqPacket.TYPE.RESULT) {
3800 ServiceDiscoveryResult disco = new ServiceDiscoveryResult(discoPacket);
3801 if (presence.getVer().equals(disco.getVer())) {
3802 databaseBackend.insertDiscoveryResult(disco);
3803 injectServiceDiscorveryResult(account.getRoster(), presence.getHash(), presence.getVer(), disco);
3804 } else {
3805 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": mismatch in caps for contact " + jid + " " + presence.getVer() + " vs " + disco.getVer());
3806 }
3807 }
3808 account.inProgressDiscoFetches.remove(key);
3809 }
3810 });
3811 }
3812 }
3813 }
3814
3815 private void injectServiceDiscorveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) {
3816 for(Contact contact : roster.getContacts()) {
3817 for(Presence presence : contact.getPresences().getPresences().values()) {
3818 if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) {
3819 presence.setServiceDiscoveryResult(disco);
3820 }
3821 }
3822 }
3823 }
3824
3825 public void fetchMamPreferences(Account account, final OnMamPreferencesFetched callback) {
3826 final boolean legacy = account.getXmppConnection().getFeatures().mamLegacy();
3827 IqPacket request = new IqPacket(IqPacket.TYPE.GET);
3828 request.addChild("prefs",legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
3829 sendIqPacket(account, request, new OnIqPacketReceived() {
3830 @Override
3831 public void onIqPacketReceived(Account account, IqPacket packet) {
3832 Element prefs = packet.findChild("prefs",legacy ? Namespace.MAM_LEGACY : Namespace.MAM);
3833 if (packet.getType() == IqPacket.TYPE.RESULT && prefs != null) {
3834 callback.onPreferencesFetched(prefs);
3835 } else {
3836 callback.onPreferencesFetchFailed();
3837 }
3838 }
3839 });
3840 }
3841
3842 public PushManagementService getPushManagementService() {
3843 return mPushManagementService;
3844 }
3845
3846 public Account getPendingAccount() {
3847 Account pending = null;
3848 for(Account account : getAccounts()) {
3849 if (account.isOptionSet(Account.OPTION_REGISTER)) {
3850 pending = account;
3851 } else {
3852 return null;
3853 }
3854 }
3855 return pending;
3856 }
3857
3858 public void changeStatus(Account account, Presence.Status status, String statusMessage, boolean send) {
3859 if (!statusMessage.isEmpty()) {
3860 databaseBackend.insertPresenceTemplate(new PresenceTemplate(status, statusMessage));
3861 }
3862 changeStatusReal(account, status, statusMessage, send);
3863 }
3864
3865 private void changeStatusReal(Account account, Presence.Status status, String statusMessage, boolean send) {
3866 account.setPresenceStatus(status);
3867 account.setPresenceStatusMessage(statusMessage);
3868 databaseBackend.updateAccount(account);
3869 if (!account.isOptionSet(Account.OPTION_DISABLED) && send) {
3870 sendPresence(account);
3871 }
3872 }
3873
3874 public void changeStatus(Presence.Status status, String statusMessage) {
3875 if (!statusMessage.isEmpty()) {
3876 databaseBackend.insertPresenceTemplate(new PresenceTemplate(status, statusMessage));
3877 }
3878 for(Account account : getAccounts()) {
3879 changeStatusReal(account, status, statusMessage, true);
3880 }
3881 }
3882
3883 public List<PresenceTemplate> getPresenceTemplates(Account account) {
3884 List<PresenceTemplate> templates = databaseBackend.getPresenceTemplates();
3885 for(PresenceTemplate template : account.getSelfContact().getPresences().asTemplates()) {
3886 if (!templates.contains(template)) {
3887 templates.add(0, template);
3888 }
3889 }
3890 return templates;
3891 }
3892
3893 public void saveConversationAsBookmark(Conversation conversation, String name) {
3894 Account account = conversation.getAccount();
3895 Bookmark bookmark = new Bookmark(account, conversation.getJid().toBareJid());
3896 if (!conversation.getJid().isBareJid()) {
3897 bookmark.setNick(conversation.getJid().getResourcepart());
3898 }
3899 if (name != null && !name.trim().isEmpty()) {
3900 bookmark.setBookmarkName(name.trim());
3901 }
3902 bookmark.setAutojoin(getPreferences().getBoolean("autojoin",true));
3903 account.getBookmarks().add(bookmark);
3904 pushBookmarks(account);
3905 conversation.setBookmark(bookmark);
3906 }
3907
3908 public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) {
3909 boolean needsRosterWrite = false;
3910 boolean performedVerification = false;
3911 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
3912 for(XmppUri.Fingerprint fp : fingerprints) {
3913 if (fp.type == XmppUri.FingerprintType.OTR) {
3914 performedVerification |= contact.addOtrFingerprint(fp.fingerprint);
3915 needsRosterWrite |= performedVerification;
3916 } else if (fp.type == XmppUri.FingerprintType.OMEMO) {
3917 String fingerprint = "05"+fp.fingerprint.replaceAll("\\s","");
3918 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
3919 if (fingerprintStatus != null) {
3920 if (!fingerprintStatus.isVerified()) {
3921 performedVerification = true;
3922 axolotlService.setFingerprintTrust(fingerprint,fingerprintStatus.toVerified());
3923 }
3924 } else {
3925 axolotlService.preVerifyFingerprint(contact,fingerprint);
3926 }
3927 }
3928 }
3929 if (needsRosterWrite) {
3930 syncRosterToDisk(contact.getAccount());
3931 }
3932 return performedVerification;
3933 }
3934
3935 public boolean verifyFingerprints(Account account, List<XmppUri.Fingerprint> fingerprints) {
3936 final AxolotlService axolotlService = account.getAxolotlService();
3937 boolean verifiedSomething = false;
3938 for(XmppUri.Fingerprint fp : fingerprints) {
3939 if (fp.type == XmppUri.FingerprintType.OMEMO) {
3940 String fingerprint = "05"+fp.fingerprint.replaceAll("\\s","");
3941 Log.d(Config.LOGTAG,"trying to verify own fp="+fingerprint);
3942 FingerprintStatus fingerprintStatus = axolotlService.getFingerprintTrust(fingerprint);
3943 if (fingerprintStatus != null) {
3944 if (!fingerprintStatus.isVerified()) {
3945 axolotlService.setFingerprintTrust(fingerprint,fingerprintStatus.toVerified());
3946 verifiedSomething = true;
3947 }
3948 } else {
3949 axolotlService.preVerifyFingerprint(account,fingerprint);
3950 verifiedSomething = true;
3951 }
3952 }
3953 }
3954 return verifiedSomething;
3955 }
3956
3957 public boolean blindTrustBeforeVerification() {
3958 return getPreferences().getBoolean(SettingsActivity.BLIND_TRUST_BEFORE_VERIFICATION, true);
3959 }
3960
3961 public ShortcutService getShortcutService() {
3962 return mShortcutService;
3963 }
3964
3965 public interface OnMamPreferencesFetched {
3966 void onPreferencesFetched(Element prefs);
3967 void onPreferencesFetchFailed();
3968 }
3969
3970 public void pushMamPreferences(Account account, Element prefs) {
3971 IqPacket set = new IqPacket(IqPacket.TYPE.SET);
3972 set.addChild(prefs);
3973 sendIqPacket(account, set, null);
3974 }
3975
3976 public interface OnAccountCreated {
3977 void onAccountCreated(Account account);
3978
3979 void informUser(int r);
3980 }
3981
3982 public interface OnMoreMessagesLoaded {
3983 void onMoreMessagesLoaded(int count, Conversation conversation);
3984
3985 void informUser(int r);
3986 }
3987
3988 public interface OnAccountPasswordChanged {
3989 void onPasswordChangeSucceeded();
3990
3991 void onPasswordChangeFailed();
3992 }
3993
3994 public interface OnAffiliationChanged {
3995 void onAffiliationChangedSuccessful(Jid jid);
3996
3997 void onAffiliationChangeFailed(Jid jid, int resId);
3998 }
3999
4000 public interface OnRoleChanged {
4001 void onRoleChangedSuccessful(String nick);
4002
4003 void onRoleChangeFailed(String nick, int resid);
4004 }
4005
4006 public interface OnConversationUpdate {
4007 void onConversationUpdate();
4008 }
4009
4010 public interface OnAccountUpdate {
4011 void onAccountUpdate();
4012 }
4013
4014 public interface OnCaptchaRequested {
4015 void onCaptchaRequested(Account account,
4016 String id,
4017 Data data,
4018 Bitmap captcha);
4019 }
4020
4021 public interface OnRosterUpdate {
4022 void onRosterUpdate();
4023 }
4024
4025 public interface OnMucRosterUpdate {
4026 void onMucRosterUpdate();
4027 }
4028
4029 public interface OnConferenceConfigurationFetched {
4030 void onConferenceConfigurationFetched(Conversation conversation);
4031
4032 void onFetchFailed(Conversation conversation, Element error);
4033 }
4034
4035 public interface OnConferenceJoined {
4036 void onConferenceJoined(Conversation conversation);
4037 }
4038
4039 public interface OnConfigurationPushed {
4040 void onPushSucceeded();
4041
4042 void onPushFailed();
4043 }
4044
4045 public interface OnShowErrorToast {
4046 void onShowErrorToast(int resId);
4047 }
4048
4049 public class XmppConnectionBinder extends Binder {
4050 public XmppConnectionService getService() {
4051 return XmppConnectionService.this;
4052 }
4053 }
4054}