use of android.support.v4.content.FileProvider.getUriForFile in project ring-client-android by savoirfairelinux.
the class ConversationAdapter method configureForFileInfoTextMessage.
private void configureForFileInfoTextMessage(final ConversationViewHolder conversationViewHolder, final IConversationElement conversationElement) {
if (conversationViewHolder == null || conversationElement == null) {
return;
}
DataTransfer file = (DataTransfer) conversationElement;
String timeSeparationString = computeTimeSeparationStringFromMsgTimeStamp(conversationViewHolder.itemView.getContext(), file.getDate());
if (file.getEventCode() == DataTransferEventCode.FINISHED) {
conversationViewHolder.mMsgDetailTxt.setText(String.format("%s - %s", timeSeparationString, FileUtils.readableFileSize(file.getTotalSize())));
} else {
conversationViewHolder.mMsgDetailTxt.setText(String.format("%s - %s - %s", timeSeparationString, FileUtils.readableFileSize(file.getTotalSize()), ResourceMapper.getReadableFileTransferStatus(conversationFragment.getActivity(), file.getEventCode())));
}
boolean showPicture = file.showPicture();
View longPressView = showPicture ? conversationViewHolder.mPhoto : conversationViewHolder.itemView;
longPressView.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
menu.setHeaderTitle(file.getDisplayName());
conversationFragment.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = conversationFragment.getActivity().getMenuInflater();
inflater.inflate(R.menu.conversation_item_actions, menu);
if (!file.isComplete())
menu.removeItem(R.id.conv_action_download);
});
longPressView.setOnLongClickListener(v -> {
mCurrentLongItem = new RecyclerViewContextMenuInfo(conversationViewHolder.getLayoutPosition(), v.getId());
return false;
});
if (showPicture) {
Context context = conversationViewHolder.mPhoto.getContext();
File path = presenter.getDeviceRuntimeService().getConversationPath(file.getPeerId(), file.getStoragePath());
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) conversationViewHolder.mAnswerLayout.getLayoutParams();
params.gravity = (file.isOutgoing() ? Gravity.END : Gravity.START) | Gravity.BOTTOM;
conversationViewHolder.mAnswerLayout.setLayoutParams(params);
LinearLayout.LayoutParams imageParams = (LinearLayout.LayoutParams) conversationViewHolder.mPhoto.getLayoutParams();
imageParams.height = mPictureMaxSize;
conversationViewHolder.mPhoto.setLayoutParams(imageParams);
GlideApp.with(context).load(path).apply(PICTURE_OPTIONS).into(new ImageViewTarget<Drawable>(conversationViewHolder.mPhoto) {
@Override
protected void setResource(@Nullable Drawable resource) {
ImageView view = getView();
runJustBeforeBeingDrawn(view, () -> {
if (view.getDrawable() != null) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
}
});
view.setImageDrawable(resource);
}
});
((LinearLayout) conversationViewHolder.mAnswerLayout).setGravity(file.isOutgoing() ? Gravity.END : Gravity.START);
conversationViewHolder.mPhoto.setOnClickListener(v -> {
Uri contentUri = FileProvider.getUriForFile(v.getContext(), ContentUriHandler.AUTHORITY_FILES, path);
Intent i = new Intent(context, MediaViewerActivity.class);
i.setAction(Intent.ACTION_VIEW).setDataAndType(contentUri, "image/*").setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(conversationFragment.getActivity(), conversationViewHolder.mPhoto, "picture");
conversationFragment.startActivityForResult(i, 3006, options.toBundle());
});
return;
}
if (file.getEventCode().isError()) {
conversationViewHolder.icon.setImageResource(R.drawable.ic_warning);
} else {
conversationViewHolder.icon.setImageResource(R.drawable.ic_clip_black);
}
conversationViewHolder.mMsgTxt.setText(file.getDisplayName());
((LinearLayout) conversationViewHolder.mLayout).setGravity(file.isOutgoing() ? Gravity.END : Gravity.START);
if (file.getEventCode() == DataTransferEventCode.WAIT_HOST_ACCEPTANCE) {
conversationViewHolder.mAnswerLayout.setVisibility(View.VISIBLE);
conversationViewHolder.btnAccept.setOnClickListener(v -> {
if (!presenter.getDeviceRuntimeService().hasWriteExternalStoragePermission()) {
conversationFragment.askWriteExternalStoragePermission();
return;
}
Context context = v.getContext();
File cacheDir = context.getCacheDir();
long spaceLeft = AndroidFileUtils.getSpaceLeft(cacheDir.toString());
if (spaceLeft == -1L || file.getTotalSize() > spaceLeft) {
presenter.noSpaceLeft();
return;
}
context.startService(new Intent(DRingService.ACTION_FILE_ACCEPT).setClass(context.getApplicationContext(), DRingService.class).putExtra(DRingService.KEY_TRANSFER_ID, file.getDataTransferId()));
});
conversationViewHolder.btnRefuse.setOnClickListener(v -> {
Context context = v.getContext();
context.startService(new Intent(DRingService.ACTION_FILE_CANCEL).setClass(context.getApplicationContext(), DRingService.class).putExtra(DRingService.KEY_TRANSFER_ID, file.getDataTransferId()));
});
} else {
conversationViewHolder.mAnswerLayout.setVisibility(View.GONE);
}
}
Aggregations