Search in sources :

Example 11 with ThreadDatabase

use of org.thoughtcrime.securesms.database.ThreadDatabase in project Signal-Android by WhisperSystems.

the class MessageSender method sendPushWithPreUploadedMedia.

public static long sendPushWithPreUploadedMedia(final Context context, final OutgoingMediaMessage message, final Collection<PreUploadResult> preUploadResults, final long threadId, final SmsDatabase.InsertListener insertListener) {
    Log.i(TAG, "Sending media message with pre-uploads to " + message.getRecipient().getId() + ", thread: " + threadId);
    Preconditions.checkArgument(message.getAttachments().isEmpty(), "If the media is pre-uploaded, there should be no attachments on the message.");
    try {
        ThreadDatabase threadDatabase = SignalDatabase.threads();
        MessageDatabase mmsDatabase = SignalDatabase.mms();
        AttachmentDatabase attachmentDatabase = SignalDatabase.attachments();
        long allocatedThreadId;
        if (threadId == -1) {
            allocatedThreadId = threadDatabase.getOrCreateThreadIdFor(message.getRecipient(), message.getDistributionType());
        } else {
            allocatedThreadId = threadId;
        }
        Recipient recipient = message.getRecipient();
        long messageId = mmsDatabase.insertMessageOutbox(applyUniversalExpireTimerIfNecessary(context, recipient, message, allocatedThreadId), allocatedThreadId, false, insertListener);
        List<AttachmentId> attachmentIds = Stream.of(preUploadResults).map(PreUploadResult::getAttachmentId).toList();
        List<String> jobIds = Stream.of(preUploadResults).map(PreUploadResult::getJobIds).flatMap(Stream::of).toList();
        attachmentDatabase.updateMessageId(attachmentIds, messageId);
        sendMediaMessage(context, recipient, false, messageId, jobIds);
        onMessageSent();
        threadDatabase.update(threadId, true);
        return allocatedThreadId;
    } catch (MmsException e) {
        Log.w(TAG, e);
        return threadId;
    }
}
Also used : MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId)

Example 12 with ThreadDatabase

use of org.thoughtcrime.securesms.database.ThreadDatabase in project Signal-Android by WhisperSystems.

the class ConversationActivity method saveDraft.

protected ListenableFuture<Long> saveDraft() {
    final SettableFuture<Long> future = new SettableFuture<>();
    if (this.recipients == null || this.recipients.isEmpty()) {
        future.set(threadId);
        return future;
    }
    final Drafts drafts = getDraftsForCurrentState();
    final long thisThreadId = this.threadId;
    final MasterSecret thisMasterSecret = this.masterSecret.parcelClone();
    final int thisDistributionType = this.distributionType;
    new AsyncTask<Long, Void, Long>() {

        @Override
        protected Long doInBackground(Long... params) {
            ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(ConversationActivity.this);
            DraftDatabase draftDatabase = DatabaseFactory.getDraftDatabase(ConversationActivity.this);
            long threadId = params[0];
            if (drafts.size() > 0) {
                if (threadId == -1)
                    threadId = threadDatabase.getThreadIdFor(getRecipients(), thisDistributionType);
                draftDatabase.insertDrafts(new MasterCipher(thisMasterSecret), threadId, drafts);
                threadDatabase.updateSnippet(threadId, drafts.getSnippet(ConversationActivity.this), drafts.getUriSnippet(ConversationActivity.this), System.currentTimeMillis(), Types.BASE_DRAFT_TYPE, true);
            } else if (threadId > 0) {
                threadDatabase.update(threadId, false);
            }
            return threadId;
        }

        @Override
        protected void onPostExecute(Long result) {
            future.set(result);
        }
    }.execute(thisThreadId);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts)

Example 13 with ThreadDatabase

use of org.thoughtcrime.securesms.database.ThreadDatabase in project Signal-Android by WhisperSystems.

the class DirectShareService method onGetChooserTargets.

@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
    List<ChooserTarget> results = new LinkedList<>();
    MasterSecret masterSecret = KeyCachingService.getMasterSecret(this);
    if (masterSecret == null) {
        return results;
    }
    ComponentName componentName = new ComponentName(this, ShareActivity.class);
    ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
    Cursor cursor = threadDatabase.getDirectShareList();
    try {
        ThreadDatabase.Reader reader = threadDatabase.readerFor(cursor, new MasterCipher(masterSecret));
        ThreadRecord record;
        while ((record = reader.getNext()) != null && results.size() < 10) {
            Recipients recipients = RecipientFactory.getRecipientsForIds(this, record.getRecipients().getIds(), false);
            String name = recipients.toShortString();
            Drawable drawable = recipients.getContactPhoto().asDrawable(this, recipients.getColor().toConversationColor(this));
            Bitmap avatar = BitmapUtil.createFromDrawable(drawable, 500, 500);
            Bundle bundle = new Bundle();
            bundle.putLong(ShareActivity.EXTRA_THREAD_ID, record.getThreadId());
            bundle.putLongArray(ShareActivity.EXTRA_RECIPIENT_IDS, recipients.getIds());
            bundle.putInt(ShareActivity.EXTRA_DISTRIBUTION_TYPE, record.getDistributionType());
            results.add(new ChooserTarget(name, Icon.createWithBitmap(avatar), 1.0f, componentName, bundle));
        }
        return results;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) Recipients(org.thoughtcrime.securesms.recipients.Recipients) Bundle(android.os.Bundle) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) Drawable(android.graphics.drawable.Drawable) MasterCipher(org.thoughtcrime.securesms.crypto.MasterCipher) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) Bitmap(android.graphics.Bitmap) ChooserTarget(android.service.chooser.ChooserTarget) ComponentName(android.content.ComponentName)

Example 14 with ThreadDatabase

use of org.thoughtcrime.securesms.database.ThreadDatabase in project Signal-Android by WhisperSystems.

the class MessageSender method send.

public static long send(final Context context, final MasterSecret masterSecret, final OutgoingMediaMessage message, final long threadId, final boolean forceSms) {
    try {
        ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
        MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
        long allocatedThreadId;
        if (threadId == -1) {
            allocatedThreadId = threadDatabase.getThreadIdFor(message.getRecipients(), message.getDistributionType());
        } else {
            allocatedThreadId = threadId;
        }
        Recipients recipients = message.getRecipients();
        long messageId = database.insertMessageOutbox(new MasterSecretUnion(masterSecret), message, allocatedThreadId, forceSms);
        sendMediaMessage(context, masterSecret, recipients, forceSms, messageId, message.getExpiresIn());
        return allocatedThreadId;
    } catch (MmsException e) {
        Log.w(TAG, e);
        return threadId;
    }
}
Also used : MasterSecretUnion(org.thoughtcrime.securesms.crypto.MasterSecretUnion) MmsException(ws.com.google.android.mms.MmsException) Recipients(org.thoughtcrime.securesms.recipients.Recipients) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase)

Example 15 with ThreadDatabase

use of org.thoughtcrime.securesms.database.ThreadDatabase in project Signal-Android by signalapp.

the class ContactsCursorLoader method loadInBackground.

@Override
public Cursor loadInBackground() {
    ContactsDatabase contactsDatabase = DatabaseFactory.getContactsDatabase(getContext());
    ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(getContext());
    ArrayList<Cursor> cursorList = new ArrayList<>(4);
    if (recents && TextUtils.isEmpty(filter)) {
        try (Cursor recentConversations = DatabaseFactory.getThreadDatabase(getContext()).getRecentConversationList(5)) {
            MatrixCursor synthesizedContacts = new MatrixCursor(CONTACT_PROJECTION);
            synthesizedContacts.addRow(new Object[] { getContext().getString(R.string.ContactsCursorLoader_recent_chats), "", ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, "", ContactsDatabase.DIVIDER_TYPE });
            ThreadDatabase.Reader reader = threadDatabase.readerFor(recentConversations);
            ThreadRecord threadRecord;
            while ((threadRecord = reader.getNext()) != null) {
                synthesizedContacts.addRow(new Object[] { threadRecord.getRecipient().toShortString(), threadRecord.getRecipient().getAddress().serialize(), ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, "", ContactsDatabase.RECENT_TYPE });
            }
            synthesizedContacts.addRow(new Object[] { getContext().getString(R.string.ContactsCursorLoader_contacts), "", ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, "", ContactsDatabase.DIVIDER_TYPE });
            if (synthesizedContacts.getCount() > 2)
                cursorList.add(synthesizedContacts);
        }
    }
    if (Permissions.hasAny(getContext(), Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)) {
        if (mode != MODE_SMS_ONLY) {
            cursorList.add(contactsDatabase.queryTextSecureContacts(filter));
        }
        if (mode == MODE_ALL) {
            cursorList.add(contactsDatabase.querySystemContacts(filter));
        } else if (mode == MODE_SMS_ONLY) {
            cursorList.add(filterNonPushContacts(contactsDatabase.querySystemContacts(filter)));
        }
    }
    if (!TextUtils.isEmpty(filter) && NumberUtil.isValidSmsOrEmail(filter)) {
        MatrixCursor newNumberCursor = new MatrixCursor(CONTACT_PROJECTION, 1);
        newNumberCursor.addRow(new Object[] { getContext().getString(R.string.contact_selection_list__unknown_contact), filter, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM, "\u21e2", ContactsDatabase.NEW_TYPE });
        cursorList.add(newNumberCursor);
    }
    if (cursorList.size() > 0)
        return new MergeCursor(cursorList.toArray(new Cursor[0]));
    else
        return null;
}
Also used : ArrayList(java.util.ArrayList) ThreadRecord(org.thoughtcrime.securesms.database.model.ThreadRecord) MergeCursor(android.database.MergeCursor) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor)

Aggregations

ThreadDatabase (org.thoughtcrime.securesms.database.ThreadDatabase)29 Recipient (org.thoughtcrime.securesms.recipients.Recipient)14 ThreadRecord (org.thoughtcrime.securesms.database.model.ThreadRecord)7 ArrayList (java.util.ArrayList)6 SuppressLint (android.annotation.SuppressLint)5 Cursor (android.database.Cursor)5 WorkerThread (androidx.annotation.WorkerThread)5 Context (android.content.Context)4 MmsException (org.thoughtcrime.securesms.mms.MmsException)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 Recipients (org.thoughtcrime.securesms.recipients.Recipients)4 Bundle (android.os.Bundle)3 NonNull (androidx.annotation.NonNull)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 DraftDatabase (org.thoughtcrime.securesms.database.DraftDatabase)3 Drafts (org.thoughtcrime.securesms.database.DraftDatabase.Drafts)3 MessageDatabase (org.thoughtcrime.securesms.database.MessageDatabase)3 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)3 ComponentName (android.content.ComponentName)2