Search in sources :

Example 6 with FileProvider.getUriForFile

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);
    }
}
Also used : Context(android.content.Context) MenuInflater(android.view.MenuInflater) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) Uri(android.net.Uri) ActivityOptionsCompat(android.support.v4.app.ActivityOptionsCompat) DataTransfer(cx.ring.model.DataTransfer) ImageView(android.widget.ImageView) File(java.io.File) LinearLayout(android.widget.LinearLayout)

Aggregations

Uri (android.net.Uri)6 Intent (android.content.Intent)4 File (java.io.File)4 NotificationManager (android.app.NotificationManager)2 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 NonNull (android.support.annotation.NonNull)2 NotificationCompat (android.support.v4.app.NotificationCompat)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 LinearLayout (android.widget.LinearLayout)2 Animator (android.animation.Animator)1 AnimatorListener (android.animation.Animator.AnimatorListener)1 AnimatorSet (android.animation.AnimatorSet)1 ArgbEvaluator (android.animation.ArgbEvaluator)1 ValueAnimator (android.animation.ValueAnimator)1 SharedPreferences (android.content.SharedPreferences)1 Configuration (android.content.res.Configuration)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1