Search in sources :

Example 11 with SettableFuture

use of org.thoughtcrime.securesms.util.concurrent.SettableFuture in project Signal-Android by signalapp.

the class PeerConnectionWrapper method setRemoteDescription.

public void setRemoteDescription(SessionDescription sdp) throws PeerConnectionException {
    final SettableFuture<Boolean> future = new SettableFuture<>();
    peerConnection.setRemoteDescription(new SdpObserver() {

        @Override
        public void onCreateSuccess(SessionDescription sdp) {
        }

        @Override
        public void onCreateFailure(String error) {
        }

        @Override
        public void onSetSuccess() {
            future.set(true);
        }

        @Override
        public void onSetFailure(String error) {
            future.setException(new PeerConnectionException(error));
        }
    }, sdp);
    try {
        future.get();
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    } catch (ExecutionException e) {
        throw new PeerConnectionException(e);
    }
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) SessionDescription(org.webrtc.SessionDescription) ExecutionException(java.util.concurrent.ExecutionException) SdpObserver(org.webrtc.SdpObserver)

Example 12 with SettableFuture

use of org.thoughtcrime.securesms.util.concurrent.SettableFuture in project Signal-Android by signalapp.

the class PeerConnectionWrapper method createAnswer.

public SessionDescription createAnswer(MediaConstraints mediaConstraints) throws PeerConnectionException {
    final SettableFuture<SessionDescription> future = new SettableFuture<>();
    peerConnection.createAnswer(new SdpObserver() {

        @Override
        public void onCreateSuccess(SessionDescription sdp) {
            future.set(sdp);
        }

        @Override
        public void onCreateFailure(String error) {
            future.setException(new PeerConnectionException(error));
        }

        @Override
        public void onSetSuccess() {
            throw new AssertionError();
        }

        @Override
        public void onSetFailure(String error) {
            throw new AssertionError();
        }
    }, mediaConstraints);
    try {
        return correctSessionDescription(future.get());
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    } catch (ExecutionException e) {
        throw new PeerConnectionException(e);
    }
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) SessionDescription(org.webrtc.SessionDescription) ExecutionException(java.util.concurrent.ExecutionException) SdpObserver(org.webrtc.SdpObserver)

Example 13 with SettableFuture

use of org.thoughtcrime.securesms.util.concurrent.SettableFuture in project Signal-Android by WhisperSystems.

the class ConversationParentFragment method saveDraft.

protected ListenableFuture<Long> saveDraft() {
    final SettableFuture<Long> future = new SettableFuture<>();
    if (this.recipient == null) {
        future.set(threadId);
        return future;
    }
    final Context context = requireContext().getApplicationContext();
    final Drafts drafts = getDraftsForCurrentState();
    final long thisThreadId = this.threadId;
    final RecipientId recipientId = this.recipient.getId();
    final int thisDistributionType = this.distributionType;
    final ListenableFuture<VoiceNoteDraft> voiceNoteDraftFuture = draftViewModel.consumeVoiceNoteDraftFuture();
    new AsyncTask<Long, Void, Long>() {

        @Override
        protected Long doInBackground(Long... params) {
            if (voiceNoteDraftFuture != null) {
                try {
                    Draft voiceNoteDraft = voiceNoteDraftFuture.get().asDraft();
                    draftViewModel.setVoiceNoteDraft(recipientId, voiceNoteDraft);
                    drafts.add(voiceNoteDraft);
                } catch (ExecutionException | InterruptedException e) {
                    Log.w(TAG, "Could not extract voice note draft data.", e);
                }
            }
            ThreadDatabase threadDatabase = SignalDatabase.threads();
            DraftDatabase draftDatabase = SignalDatabase.drafts();
            long threadId = params[0];
            if (drafts.size() > 0) {
                if (threadId == -1)
                    threadId = threadDatabase.getOrCreateThreadIdFor(getRecipient(), thisDistributionType);
                draftDatabase.replaceDrafts(threadId, drafts);
                threadDatabase.updateSnippet(threadId, drafts.getSnippet(context), drafts.getUriSnippet(), System.currentTimeMillis(), Types.BASE_DRAFT_TYPE, true);
            } else if (threadId > 0) {
                threadDatabase.update(threadId, false);
            }
            if (drafts.isEmpty()) {
                draftDatabase.clearDrafts(threadId);
            }
            return threadId;
        }

        @Override
        protected void onPostExecute(Long result) {
            future.set(result);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, thisThreadId);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) Context(android.content.Context) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) DraftDatabase(org.thoughtcrime.securesms.database.DraftDatabase) VoiceNoteDraft(org.thoughtcrime.securesms.components.voice.VoiceNoteDraft) ThreadDatabase(org.thoughtcrime.securesms.database.ThreadDatabase) SuppressLint(android.annotation.SuppressLint) Drafts(org.thoughtcrime.securesms.database.DraftDatabase.Drafts) VoiceNoteDraft(org.thoughtcrime.securesms.components.voice.VoiceNoteDraft) Draft(org.thoughtcrime.securesms.database.DraftDatabase.Draft)

Example 14 with SettableFuture

use of org.thoughtcrime.securesms.util.concurrent.SettableFuture in project Signal-Android by WhisperSystems.

the class ThumbnailView method setImageResource.

@UiThread
public ListenableFuture<Boolean> setImageResource(@NonNull GlideRequests glideRequests, @NonNull Slide slide, boolean showControls, boolean isPreview, int naturalWidth, int naturalHeight) {
    if (showControls) {
        getTransferControls().setSlide(slide);
        getTransferControls().setDownloadClickListener(new DownloadClickDispatcher());
    } else if (transferControls.isPresent()) {
        getTransferControls().setVisibility(View.GONE);
    }
    if (slide.getUri() != null && slide.hasPlayOverlay() && (slide.getTransferState() == AttachmentDatabase.TRANSFER_PROGRESS_DONE || isPreview)) {
        this.playOverlay.setVisibility(View.VISIBLE);
    } else {
        this.playOverlay.setVisibility(View.GONE);
    }
    if (Util.equals(slide, this.slide)) {
        Log.i(TAG, "Not re-loading slide " + slide.asAttachment().getUri());
        return new SettableFuture<>(false);
    }
    if (this.slide != null && this.slide.getFastPreflightId() != null && (!slide.hasVideo() || Util.equals(this.slide.getUri(), slide.getUri())) && Util.equals(this.slide.getFastPreflightId(), slide.getFastPreflightId())) {
        Log.i(TAG, "Not re-loading slide for fast preflight: " + slide.getFastPreflightId());
        this.slide = slide;
        return new SettableFuture<>(false);
    }
    Log.i(TAG, "loading part with id " + slide.asAttachment().getUri() + ", progress " + slide.getTransferState() + ", fast preflight id: " + slide.asAttachment().getFastPreflightId());
    BlurHash previousBlurhash = this.slide != null ? this.slide.getPlaceholderBlur() : null;
    this.slide = slide;
    this.captionIcon.setVisibility(slide.getCaption().isPresent() ? VISIBLE : GONE);
    dimens[WIDTH] = naturalWidth;
    dimens[HEIGHT] = naturalHeight;
    invalidate();
    SettableFuture<Boolean> result = new SettableFuture<>();
    boolean resultHandled = false;
    if (slide.hasPlaceholder() && (previousBlurhash == null || !Objects.equals(slide.getPlaceholderBlur(), previousBlurhash))) {
        buildPlaceholderGlideRequest(glideRequests, slide).into(new GlideBitmapListeningTarget(blurhash, result));
        resultHandled = true;
    } else if (!slide.hasPlaceholder()) {
        glideRequests.clear(blurhash);
        blurhash.setImageDrawable(null);
    }
    if (slide.getUri() != null) {
        if (!MediaUtil.isJpegType(slide.getContentType()) && !MediaUtil.isVideoType(slide.getContentType())) {
            SettableFuture<Boolean> thumbnailFuture = new SettableFuture<>();
            thumbnailFuture.deferTo(result);
            thumbnailFuture.addListener(new BlurhashClearListener(glideRequests, blurhash));
        }
        buildThumbnailGlideRequest(glideRequests, slide).into(new GlideDrawableListeningTarget(image, result));
        resultHandled = true;
    } else {
        glideRequests.clear(image);
        image.setImageDrawable(null);
    }
    if (!resultHandled) {
        result.set(false);
    }
    return result;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) BlurHash(org.thoughtcrime.securesms.blurhash.BlurHash) UiThread(androidx.annotation.UiThread)

Example 15 with SettableFuture

use of org.thoughtcrime.securesms.util.concurrent.SettableFuture in project Signal-Android by WhisperSystems.

the class ThumbnailView method setImageResource.

public ListenableFuture<Boolean> setImageResource(@NonNull GlideRequests glideRequests, @NonNull Uri uri, int width, int height) {
    SettableFuture<Boolean> future = new SettableFuture<>();
    if (transferControls.isPresent())
        getTransferControls().setVisibility(View.GONE);
    GlideRequest request = glideRequests.load(new DecryptableUri(uri)).diskCacheStrategy(DiskCacheStrategy.NONE).transition(withCrossFade());
    if (width > 0 && height > 0) {
        request = request.override(width, height);
    }
    if (radius > 0) {
        request = request.transforms(new CenterCrop(), new RoundedCorners(radius));
    } else {
        request = request.transforms(new CenterCrop());
    }
    request.into(new GlideDrawableListeningTarget(image, future));
    blurhash.setImageDrawable(null);
    return future;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) GlideRequest(org.thoughtcrime.securesms.mms.GlideRequest) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) CenterCrop(com.bumptech.glide.load.resource.bitmap.CenterCrop) RoundedCorners(com.bumptech.glide.load.resource.bitmap.RoundedCorners)

Aggregations

SettableFuture (org.thoughtcrime.securesms.util.concurrent.SettableFuture)37 Context (android.content.Context)9 Animation (android.view.animation.Animation)8 ExecutionException (java.util.concurrent.ExecutionException)8 SdpObserver (org.webrtc.SdpObserver)8 SessionDescription (org.webrtc.SessionDescription)8 ScaleAnimation (android.view.animation.ScaleAnimation)6 TranslateAnimation (android.view.animation.TranslateAnimation)6 Bitmap (android.graphics.Bitmap)4 NonNull (android.support.annotation.NonNull)4 OvershootInterpolator (android.view.animation.OvershootInterpolator)4 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 DraftDatabase (org.thoughtcrime.securesms.database.DraftDatabase)4 Drafts (org.thoughtcrime.securesms.database.DraftDatabase.Drafts)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)4 SuppressLint (android.annotation.SuppressLint)3 SpannableString (android.text.SpannableString)3 List (java.util.List)3 VoiceNoteDraft (org.thoughtcrime.securesms.components.voice.VoiceNoteDraft)3