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