Search in sources :

Example 1 with AudioSlidePlayer

use of org.thoughtcrime.securesms.audio.AudioSlidePlayer in project deltachat-android by deltachat.

the class ConversationItem method setMediaAttributes.

private void setMediaAttributes(@NonNull DcMsg messageRecord, boolean showSender) {
    class SetDurationListener implements AudioSlidePlayer.Listener {

        @Override
        public void onStart() {
        }

        @Override
        public void onStop() {
        }

        @Override
        public void onProgress(double progress, long millis) {
        }

        @Override
        public void onReceivedDuration(int millis) {
            messageRecord.lateFilingMediaSize(0, 0, millis);
            audioViewStub.get().setDuration(millis);
        }
    }
    if (hasAudio(messageRecord)) {
        audioViewStub.get().setVisibility(View.VISIBLE);
        if (mediaThumbnailStub.resolved())
            mediaThumbnailStub.get().setVisibility(View.GONE);
        if (documentViewStub.resolved())
            documentViewStub.get().setVisibility(View.GONE);
        if (webxdcViewStub.resolved())
            webxdcViewStub.get().setVisibility(View.GONE);
        if (stickerStub.resolved())
            stickerStub.get().setVisibility(View.GONE);
        // noinspection ConstantConditions
        int duration = messageRecord.getDuration();
        if (duration == 0) {
            AudioSlide audio = new AudioSlide(context, messageRecord);
            AudioSlidePlayer audioSlidePlayer = AudioSlidePlayer.createFor(getContext(), audio, new SetDurationListener());
            audioSlidePlayer.requestDuration();
        }
        audioViewStub.get().setAudio(new AudioSlide(context, messageRecord), duration);
        audioViewStub.get().setOnLongClickListener(passthroughClickListener);
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    } else if (hasDocument(messageRecord)) {
        documentViewStub.get().setVisibility(View.VISIBLE);
        if (mediaThumbnailStub.resolved())
            mediaThumbnailStub.get().setVisibility(View.GONE);
        if (audioViewStub.resolved())
            audioViewStub.get().setVisibility(View.GONE);
        if (webxdcViewStub.resolved())
            webxdcViewStub.get().setVisibility(View.GONE);
        if (stickerStub.resolved())
            stickerStub.get().setVisibility(View.GONE);
        // noinspection ConstantConditions
        documentViewStub.get().setDocument(new DocumentSlide(context, messageRecord));
        documentViewStub.get().setDocumentClickListener(new ThumbnailClickListener());
        documentViewStub.get().setOnLongClickListener(passthroughClickListener);
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    } else if (hasWebxdc(messageRecord)) {
        webxdcViewStub.get().setVisibility(View.VISIBLE);
        if (mediaThumbnailStub.resolved())
            mediaThumbnailStub.get().setVisibility(View.GONE);
        if (audioViewStub.resolved())
            audioViewStub.get().setVisibility(View.GONE);
        if (documentViewStub.resolved())
            documentViewStub.get().setVisibility(View.GONE);
        if (stickerStub.resolved())
            stickerStub.get().setVisibility(View.GONE);
        webxdcViewStub.get().setWebxdc(messageRecord, "Webxdc");
        webxdcViewStub.get().setWebxdcClickListener(new ThumbnailClickListener());
        webxdcViewStub.get().setOnLongClickListener(passthroughClickListener);
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    } else if (hasThumbnail(messageRecord)) {
        mediaThumbnailStub.get().setVisibility(View.VISIBLE);
        if (audioViewStub.resolved())
            audioViewStub.get().setVisibility(View.GONE);
        if (documentViewStub.resolved())
            documentViewStub.get().setVisibility(View.GONE);
        if (webxdcViewStub.resolved())
            webxdcViewStub.get().setVisibility(View.GONE);
        if (stickerStub.resolved())
            stickerStub.get().setVisibility(View.GONE);
        Slide slide = MediaUtil.getSlideForMsg(context, messageRecord);
        MediaUtil.ThumbnailSize thumbnailSize = new MediaUtil.ThumbnailSize(messageRecord.getWidth(0), messageRecord.getHeight(0));
        if ((thumbnailSize.width <= 0 || thumbnailSize.height <= 0)) {
            if (messageRecord.getType() == DcMsg.DC_MSG_VIDEO) {
                MediaUtil.createVideoThumbnailIfNeeded(context, slide.getUri(), slide.getThumbnailUri(), thumbnailSize);
            }
            if (thumbnailSize.width <= 0 || thumbnailSize.height <= 0) {
                thumbnailSize.width = 180;
                thumbnailSize.height = 180;
            }
            messageRecord.lateFilingMediaSize(thumbnailSize.width, thumbnailSize.height, 0);
        }
        mediaThumbnailStub.get().setImageResource(glideRequests, slide, thumbnailSize.width, thumbnailSize.height);
        mediaThumbnailStub.get().setThumbnailClickListener(new ThumbnailClickListener());
        mediaThumbnailStub.get().setOnLongClickListener(passthroughClickListener);
        mediaThumbnailStub.get().setOnClickListener(passthroughClickListener);
        mediaThumbnailStub.get().showShade(TextUtils.isEmpty(messageRecord.getText()));
        setThumbnailOutlineCorners(messageRecord, showSender);
        bodyBubble.getLayoutParams().width = ViewUtil.dpToPx(readDimen(R.dimen.media_bubble_max_width));
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    } else if (hasSticker(messageRecord)) {
        stickerStub.get().setVisibility(View.VISIBLE);
        if (audioViewStub.resolved())
            audioViewStub.get().setVisibility(View.GONE);
        if (documentViewStub.resolved())
            documentViewStub.get().setVisibility(View.GONE);
        if (webxdcViewStub.resolved())
            webxdcViewStub.get().setVisibility(View.GONE);
        if (mediaThumbnailStub.resolved())
            mediaThumbnailStub.get().setVisibility(View.GONE);
        bodyBubble.setBackgroundColor(Color.TRANSPARENT);
        stickerStub.get().setSlide(glideRequests, new StickerSlide(context, messageRecord));
        stickerStub.get().setThumbnailClickListener(new StickerClickListener());
        stickerStub.get().setOnLongClickListener(passthroughClickListener);
        stickerStub.get().setOnClickListener(passthroughClickListener);
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    } else {
        if (mediaThumbnailStub.resolved())
            mediaThumbnailStub.get().setVisibility(View.GONE);
        if (audioViewStub.resolved())
            audioViewStub.get().setVisibility(View.GONE);
        if (documentViewStub.resolved())
            documentViewStub.get().setVisibility(View.GONE);
        if (webxdcViewStub.resolved())
            webxdcViewStub.get().setVisibility(View.GONE);
        ViewUtil.updateLayoutParams(bodyText, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewUtil.updateLayoutParams(groupSenderHolder, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footer.setVisibility(VISIBLE);
    }
}
Also used : MediaUtil(org.thoughtcrime.securesms.util.MediaUtil) DocumentSlide(org.thoughtcrime.securesms.mms.DocumentSlide) SlideClickListener(org.thoughtcrime.securesms.mms.SlideClickListener) AudioSlide(org.thoughtcrime.securesms.mms.AudioSlide) AudioSlidePlayer(org.thoughtcrime.securesms.audio.AudioSlidePlayer) StickerSlide(org.thoughtcrime.securesms.mms.StickerSlide) Slide(org.thoughtcrime.securesms.mms.Slide) DocumentSlide(org.thoughtcrime.securesms.mms.DocumentSlide) AudioSlide(org.thoughtcrime.securesms.mms.AudioSlide) StickerSlide(org.thoughtcrime.securesms.mms.StickerSlide)

Example 2 with AudioSlidePlayer

use of org.thoughtcrime.securesms.audio.AudioSlidePlayer in project deltachat-android by deltachat.

the class AttachmentManager method setMedia.

/*
  public ListenableFuture<Boolean> setLocation(@NonNull final SignalPlace place,
                                               @NonNull final MediaConstraints constraints)
  {
    inflateStub();

    SettableFuture<Boolean>  returnResult = new SettableFuture<>();
    ListenableFuture<Bitmap> future       = mapView.display(place);

    attachmentViewStub.get().setVisibility(View.VISIBLE);
    removableMediaView.display(mapView, false);

    future.addListener(new AssertedSuccessListener<Bitmap>() {
      @Override
      public void onSuccess(@NonNull Bitmap result) {
        byte[]        blob          = BitmapUtil.toByteArray(result);
        Uri           uri           = PersistentBlobProvider.getInstance(context)
                                                            .create(context, blob, MediaUtil.IMAGE_PNG, null);
        LocationSlide locationSlide = new LocationSlide(context, uri, blob.length, place);

        setSlide(locationSlide);
        attachmentListener.onAttachmentChanged();
        returnResult.set(true);
      }
    });

    return returnResult;
  }
  */
@SuppressLint("StaticFieldLeak")
public ListenableFuture<Boolean> setMedia(@NonNull final GlideRequests glideRequests, @NonNull final Uri uri, @Nullable final DcMsg msg, @NonNull final MediaType mediaType, final int width, final int height, final int chatId) {
    inflateStub();
    final SettableFuture<Boolean> result = new SettableFuture<>();
    new AsyncTask<Void, Void, Slide>() {

        @Override
        protected void onPreExecute() {
            thumbnail.clear(glideRequests);
            attachmentViewStub.get().setVisibility(View.VISIBLE);
        }

        @Override
        @Nullable
        protected Slide doInBackground(Void... params) {
            try {
                if (msg != null && msg.getType() == DcMsg.DC_MSG_WEBXDC) {
                    return new DocumentSlide(context, msg);
                } else if (PartAuthority.isLocalUri(uri)) {
                    return getManuallyCalculatedSlideInfo(uri, width, height);
                } else {
                    Slide result = getContentResolverSlideInfo(uri, width, height, chatId);
                    if (result == null)
                        return getManuallyCalculatedSlideInfo(uri, width, height);
                    else
                        return result;
                }
            } catch (IOException e) {
                Log.w(TAG, e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(@Nullable final Slide slide) {
            if (slide == null) {
                attachmentViewStub.get().setVisibility(View.GONE);
                result.set(false);
            } else if (slide.getFileSize() > 1 * 1024 * 1024 * 1024) {
                // this is only a rough check, videos and images may be recoded
                // and the core checks more carefully later.
                attachmentViewStub.get().setVisibility(View.GONE);
                Log.w(TAG, "File too large.");
                Toast.makeText(slide.context, "File too large.", Toast.LENGTH_LONG).show();
                result.set(false);
            } else {
                setSlide(slide);
                attachmentViewStub.get().setVisibility(View.VISIBLE);
                if (slide.hasAudio()) {
                    class SetDurationListener implements AudioSlidePlayer.Listener {

                        @Override
                        public void onStart() {
                        }

                        @Override
                        public void onStop() {
                        }

                        @Override
                        public void onProgress(double progress, long millis) {
                        }

                        @Override
                        public void onReceivedDuration(int millis) {
                            ((AudioView) removableMediaView.getCurrent()).setDuration(millis);
                        }
                    }
                    AudioSlidePlayer audioSlidePlayer = AudioSlidePlayer.createFor(context, (AudioSlide) slide, new SetDurationListener());
                    audioSlidePlayer.requestDuration();
                    audioView.setAudio((AudioSlide) slide, 0);
                    removableMediaView.display(audioView, false);
                    result.set(true);
                } else if (slide.hasDocument()) {
                    if (slide.isWebxdcDocument()) {
                        DcMsg instance = msg != null ? msg : DcHelper.getContext(context).getMsg(slide.dcMsgId);
                        webxdcView.setWebxdc(instance, context.getString(R.string.videochat_tap_to_open));
                        webxdcView.setWebxdcClickListener((v, s) -> {
                            WebxdcActivity.openWebxdcActivity(context, instance);
                        });
                        removableMediaView.display(webxdcView, false);
                    } else {
                        documentView.setDocument((DocumentSlide) slide);
                        removableMediaView.display(documentView, false);
                    }
                    result.set(true);
                } else {
                    Attachment attachment = slide.asAttachment();
                    result.deferTo(thumbnail.setImageResource(glideRequests, slide, attachment.getWidth(), attachment.getHeight()));
                    removableMediaView.display(thumbnail, mediaType == MediaType.IMAGE);
                }
                attachmentListener.onAttachmentChanged();
            }
        }

        @Nullable
        private Slide getContentResolverSlideInfo(Uri uri, int width, int height, int chatId) {
            Cursor cursor = null;
            long start = System.currentTimeMillis();
            try {
                cursor = context.getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
                    long fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
                    String mimeType = context.getContentResolver().getType(uri);
                    if (width == 0 || height == 0) {
                        Pair<Integer, Integer> dimens = MediaUtil.getDimensions(context, mimeType, uri);
                        width = dimens.first;
                        height = dimens.second;
                    }
                    Log.w(TAG, "remote slide with size " + fileSize + " took " + (System.currentTimeMillis() - start) + "ms");
                    return mediaType.createSlide(context, uri, fileName, mimeType, fileSize, width, height, chatId);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
            return null;
        }

        @NonNull
        private Slide getManuallyCalculatedSlideInfo(Uri uri, int width, int height) throws IOException {
            long start = System.currentTimeMillis();
            Long mediaSize = null;
            String fileName = null;
            String mimeType = null;
            if (PartAuthority.isLocalUri(uri)) {
                mediaSize = PartAuthority.getAttachmentSize(context, uri);
                fileName = PartAuthority.getAttachmentFileName(context, uri);
                mimeType = PartAuthority.getAttachmentContentType(context, uri);
            }
            if (mediaSize == null) {
                mediaSize = MediaUtil.getMediaSize(context, uri);
            }
            if (mimeType == null) {
                mimeType = MediaUtil.getMimeType(context, uri);
            }
            if (width == 0 || height == 0) {
                Pair<Integer, Integer> dimens = MediaUtil.getDimensions(context, mimeType, uri);
                width = dimens.first;
                height = dimens.second;
            }
            if (fileName == null) {
                try {
                    fileName = new File(uri.getPath()).getName();
                } catch (Exception e) {
                    Log.w(TAG, "Could not get file name from uri: " + e.toString());
                }
            }
            Log.w(TAG, "local slide with size " + mediaSize + " took " + (System.currentTimeMillis() - start) + "ms");
            return mediaType.createSlide(context, uri, fileName, mimeType, mediaSize, width, height, chatId);
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    return result;
}
Also used : SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) WebxdcView(org.thoughtcrime.securesms.components.WebxdcView) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) ViewUtil(org.thoughtcrime.securesms.util.ViewUtil) R(org.thoughtcrime.securesms.R) RemovableEditableMediaView(org.thoughtcrime.securesms.components.RemovableEditableMediaView) Manifest(android.Manifest) Optional(org.thoughtcrime.securesms.util.guava.Optional) MediaStore(android.provider.MediaStore) View(android.view.View) Log(android.util.Log) WebxdcActivity(org.thoughtcrime.securesms.WebxdcActivity) DcLocationManager(org.thoughtcrime.securesms.geolocation.DcLocationManager) AsyncTask(android.os.AsyncTask) ShareLocationDialog(org.thoughtcrime.securesms.ShareLocationDialog) MediaPreviewActivity(org.thoughtcrime.securesms.MediaPreviewActivity) PorterDuff(android.graphics.PorterDuff) ScribbleActivity(org.thoughtcrime.securesms.scribbles.ScribbleActivity) ApplicationContext(org.thoughtcrime.securesms.ApplicationContext) List(java.util.List) Nullable(androidx.annotation.Nullable) ActivityNotFoundException(android.content.ActivityNotFoundException) Listener(org.thoughtcrime.securesms.util.concurrent.ListenableFuture.Listener) Attachment(org.thoughtcrime.securesms.attachments.Attachment) MediaUtil(org.thoughtcrime.securesms.util.MediaUtil) Context(android.content.Context) ThumbnailView(org.thoughtcrime.securesms.components.ThumbnailView) Pair(android.util.Pair) Intent(android.content.Intent) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) ConversationActivity(org.thoughtcrime.securesms.ConversationActivity) SuppressLint(android.annotation.SuppressLint) Toast(android.widget.Toast) Build(android.os.Build) LinkedList(java.util.LinkedList) Cursor(android.database.Cursor) DcMsg(com.b44t.messenger.DcMsg) SettableFuture(org.thoughtcrime.securesms.util.concurrent.SettableFuture) ListenableFuture(org.thoughtcrime.securesms.util.concurrent.ListenableFuture) AudioView(org.thoughtcrime.securesms.components.AudioView) DcContext(com.b44t.messenger.DcContext) Iterator(java.util.Iterator) Stub(org.thoughtcrime.securesms.util.views.Stub) AudioSlidePlayer(org.thoughtcrime.securesms.audio.AudioSlidePlayer) Permissions(org.thoughtcrime.securesms.permissions.Permissions) IOException(java.io.IOException) OpenableColumns(android.provider.OpenableColumns) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) DcHelper(org.thoughtcrime.securesms.connect.DcHelper) PersistentBlobProvider(org.thoughtcrime.securesms.providers.PersistentBlobProvider) StorageUtil(org.thoughtcrime.securesms.util.StorageUtil) ThemeUtil(org.thoughtcrime.securesms.util.ThemeUtil) DocumentView(org.thoughtcrime.securesms.components.DocumentView) Activity(android.app.Activity) DocumentsContract(android.provider.DocumentsContract) Listener(org.thoughtcrime.securesms.util.concurrent.ListenableFuture.Listener) AudioSlidePlayer(org.thoughtcrime.securesms.audio.AudioSlidePlayer) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Cursor(android.database.Cursor) Uri(android.net.Uri) DcMsg(com.b44t.messenger.DcMsg) AudioView(org.thoughtcrime.securesms.components.AudioView) NonNull(androidx.annotation.NonNull) Pair(android.util.Pair) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) ActivityNotFoundException(android.content.ActivityNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint)

Aggregations

AudioSlidePlayer (org.thoughtcrime.securesms.audio.AudioSlidePlayer)2 Manifest (android.Manifest)1 SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 PorterDuff (android.graphics.PorterDuff)1 Uri (android.net.Uri)1 AsyncTask (android.os.AsyncTask)1 Build (android.os.Build)1 DocumentsContract (android.provider.DocumentsContract)1 MediaStore (android.provider.MediaStore)1 OpenableColumns (android.provider.OpenableColumns)1 Log (android.util.Log)1 Pair (android.util.Pair)1 View (android.view.View)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1