Search in sources :

Example 1 with UpdatedBodyAndMentions

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

the class ConversationParentFragment method initializeDraftFromDatabase.

private ListenableFuture<Boolean> initializeDraftFromDatabase() {
    SettableFuture<Boolean> future = new SettableFuture<>();
    final Context context = requireContext().getApplicationContext();
    new AsyncTask<Void, Void, Pair<Drafts, CharSequence>>() {

        @Override
        protected Pair<Drafts, CharSequence> doInBackground(Void... params) {
            DraftDatabase draftDatabase = SignalDatabase.drafts();
            Drafts results = draftDatabase.getDrafts(threadId);
            Draft mentionsDraft = results.getDraftOfType(Draft.MENTION);
            Spannable updatedText = null;
            if (mentionsDraft != null) {
                String text = results.getDraftOfType(Draft.TEXT).getValue();
                List<Mention> mentions = MentionUtil.bodyRangeListToMentions(context, Base64.decodeOrThrow(mentionsDraft.getValue()));
                UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, text, mentions);
                updatedText = new SpannableString(updated.getBody());
                MentionAnnotation.setMentionAnnotations(updatedText, updated.getMentions());
            }
            draftDatabase.clearDrafts(threadId);
            return new Pair<>(results, updatedText);
        }

        @Override
        protected void onPostExecute(Pair<Drafts, CharSequence> draftsWithUpdatedMentions) {
            Drafts drafts = Objects.requireNonNull(draftsWithUpdatedMentions.first());
            CharSequence updatedText = draftsWithUpdatedMentions.second();
            if (drafts.isEmpty()) {
                future.set(false);
                updateToggleButtonState();
                return;
            }
            AtomicInteger draftsRemaining = new AtomicInteger(drafts.size());
            AtomicBoolean success = new AtomicBoolean(false);
            ListenableFuture.Listener<Boolean> listener = new AssertedSuccessListener<Boolean>() {

                @Override
                public void onSuccess(Boolean result) {
                    success.compareAndSet(false, result);
                    if (draftsRemaining.decrementAndGet() <= 0) {
                        future.set(success.get());
                    }
                }
            };
            for (Draft draft : drafts) {
                try {
                    switch(draft.getType()) {
                        case Draft.TEXT:
                            composeText.setText(updatedText == null ? draft.getValue() : updatedText);
                            listener.onSuccess(true);
                            break;
                        case Draft.LOCATION:
                            attachmentManager.setLocation(SignalPlace.deserialize(draft.getValue()), getCurrentMediaConstraints()).addListener(listener);
                            break;
                        case Draft.IMAGE:
                            setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE).addListener(listener);
                            break;
                        case Draft.AUDIO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO).addListener(listener);
                            break;
                        case Draft.VIDEO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO).addListener(listener);
                            break;
                        case Draft.QUOTE:
                            SettableFuture<Boolean> quoteResult = new SettableFuture<>();
                            new QuoteRestorationTask(draft.getValue(), quoteResult).execute();
                            quoteResult.addListener(listener);
                            break;
                        case Draft.VOICE_NOTE:
                            draftViewModel.setVoiceNoteDraft(recipient.getId(), draft);
                            break;
                    }
                } catch (IOException e) {
                    Log.w(TAG, e);
                }
            }
            updateToggleButtonState();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) EmojiEventListener(org.thoughtcrime.securesms.components.emoji.EmojiEventListener) OnFocusChangeListener(android.view.View.OnFocusChangeListener) OnKeyListener(android.view.View.OnKeyListener) StickerEventListener(org.thoughtcrime.securesms.stickers.StickerEventListener) AssertedSuccessListener(org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener) OnKeyboardShownListener(org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout.OnKeyboardShownListener) OnClickListener(android.view.View.OnClickListener) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) SpannableString(android.text.SpannableString) ArrayList(java.util.ArrayList) IdentityRecordList(org.thoughtcrime.securesms.database.identity.IdentityRecordList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(org.whispersystems.libsignal.util.Pair) Context(android.content.Context) UpdatedBodyAndMentions(org.thoughtcrime.securesms.database.MentionUtil.UpdatedBodyAndMentions) IOException(java.io.IOException) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts) SpannableString(android.text.SpannableString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VoiceNoteDraft(org.thoughtcrime.securesms.components.voice.VoiceNoteDraft) Draft(org.thoughtcrime.securesms.database.DraftDatabase.Draft) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Spannable(android.text.Spannable)

Example 2 with UpdatedBodyAndMentions

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

the class ConversationParentFragment method initializeDraftFromDatabase.

private ListenableFuture<Boolean> initializeDraftFromDatabase() {
    SettableFuture<Boolean> future = new SettableFuture<>();
    final Context context = requireContext().getApplicationContext();
    new AsyncTask<Void, Void, Pair<Drafts, CharSequence>>() {

        @Override
        protected Pair<Drafts, CharSequence> doInBackground(Void... params) {
            DraftDatabase draftDatabase = SignalDatabase.drafts();
            Drafts results = draftDatabase.getDrafts(threadId);
            Draft mentionsDraft = results.getDraftOfType(Draft.MENTION);
            Spannable updatedText = null;
            if (mentionsDraft != null) {
                String text = results.getDraftOfType(Draft.TEXT).getValue();
                List<Mention> mentions = MentionUtil.bodyRangeListToMentions(context, Base64.decodeOrThrow(mentionsDraft.getValue()));
                UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, text, mentions);
                updatedText = new SpannableString(updated.getBody());
                MentionAnnotation.setMentionAnnotations(updatedText, updated.getMentions());
            }
            draftDatabase.clearDrafts(threadId);
            return new Pair<>(results, updatedText);
        }

        @Override
        protected void onPostExecute(Pair<Drafts, CharSequence> draftsWithUpdatedMentions) {
            Drafts drafts = Objects.requireNonNull(draftsWithUpdatedMentions.first());
            CharSequence updatedText = draftsWithUpdatedMentions.second();
            if (drafts.isEmpty()) {
                future.set(false);
                updateToggleButtonState();
                return;
            }
            AtomicInteger draftsRemaining = new AtomicInteger(drafts.size());
            AtomicBoolean success = new AtomicBoolean(false);
            ListenableFuture.Listener<Boolean> listener = new AssertedSuccessListener<Boolean>() {

                @Override
                public void onSuccess(Boolean result) {
                    success.compareAndSet(false, result);
                    if (draftsRemaining.decrementAndGet() <= 0) {
                        future.set(success.get());
                    }
                }
            };
            for (Draft draft : drafts) {
                try {
                    switch(draft.getType()) {
                        case Draft.TEXT:
                            composeText.setText(updatedText == null ? draft.getValue() : updatedText);
                            listener.onSuccess(true);
                            break;
                        case Draft.LOCATION:
                            attachmentManager.setLocation(SignalPlace.deserialize(draft.getValue()), getCurrentMediaConstraints()).addListener(listener);
                            break;
                        case Draft.IMAGE:
                            setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE).addListener(listener);
                            break;
                        case Draft.AUDIO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO).addListener(listener);
                            break;
                        case Draft.VIDEO:
                            setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO).addListener(listener);
                            break;
                        case Draft.QUOTE:
                            SettableFuture<Boolean> quoteResult = new SettableFuture<>();
                            new QuoteRestorationTask(draft.getValue(), quoteResult).execute();
                            quoteResult.addListener(listener);
                            break;
                        case Draft.VOICE_NOTE:
                            draftViewModel.setVoiceNoteDraft(recipient.getId(), draft);
                            break;
                    }
                } catch (IOException e) {
                    Log.w(TAG, e);
                }
            }
            updateToggleButtonState();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) EmojiEventListener(org.thoughtcrime.securesms.components.emoji.EmojiEventListener) OnFocusChangeListener(android.view.View.OnFocusChangeListener) OnKeyListener(android.view.View.OnKeyListener) StickerEventListener(org.thoughtcrime.securesms.stickers.StickerEventListener) AssertedSuccessListener(org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener) OnKeyboardShownListener(org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout.OnKeyboardShownListener) OnClickListener(android.view.View.OnClickListener) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) SpannableString(android.text.SpannableString) ArrayList(java.util.ArrayList) IdentityRecordList(org.thoughtcrime.securesms.database.identity.IdentityRecordList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(org.whispersystems.libsignal.util.Pair) Context(android.content.Context) UpdatedBodyAndMentions(org.thoughtcrime.securesms.database.MentionUtil.UpdatedBodyAndMentions) IOException(java.io.IOException) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts) SpannableString(android.text.SpannableString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VoiceNoteDraft(org.thoughtcrime.securesms.components.voice.VoiceNoteDraft) Draft(org.thoughtcrime.securesms.database.DraftDatabase.Draft) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Spannable(android.text.Spannable)

Aggregations

Context (android.content.Context)2 Spannable (android.text.Spannable)2 SpannableString (android.text.SpannableString)2 OnClickListener (android.view.View.OnClickListener)2 OnFocusChangeListener (android.view.View.OnFocusChangeListener)2 OnKeyListener (android.view.View.OnKeyListener)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OnKeyboardShownListener (org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout.OnKeyboardShownListener)2 EmojiEventListener (org.thoughtcrime.securesms.components.emoji.EmojiEventListener)2 VoiceNoteDraft (org.thoughtcrime.securesms.components.voice.VoiceNoteDraft)2 DraftDatabase (org.thoughtcrime.securesms.database.DraftDatabase)2 Draft (org.thoughtcrime.securesms.database.DraftDatabase.Draft)2 Drafts (org.thoughtcrime.securesms.database.DraftDatabase.Drafts)2 UpdatedBodyAndMentions (org.thoughtcrime.securesms.database.MentionUtil.UpdatedBodyAndMentions)2 IdentityRecordList (org.thoughtcrime.securesms.database.identity.IdentityRecordList)2 StickerEventListener (org.thoughtcrime.securesms.stickers.StickerEventListener)2