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);
}
}
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;
}
Aggregations