Search in sources :

Example 1 with ThumbnailsCacheManager

use of com.owncloud.android.datamodel.ThumbnailsCacheManager in project android by owncloud.

the class LocalFileListAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflator.inflate(R.layout.list_item, null);
    }
    if (mFiles != null && mFiles.length > position) {
        File file = mFiles[position];
        TextView fileName = (TextView) view.findViewById(R.id.Filename);
        String name = file.getName();
        fileName.setText(name);
        ImageView fileIcon = (ImageView) view.findViewById(R.id.thumbnail);
        /** Cancellation needs do be checked and done before changing the drawable in fileIcon, or
             * {@link ThumbnailsCacheManager#cancelPotentialThumbnailWork} will NEVER cancel any task.
             **/
        boolean allowedToCreateNewThumbnail = (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, fileIcon));
        if (!file.isDirectory()) {
            fileIcon.setImageResource(R.drawable.file);
        } else {
            fileIcon.setImageResource(R.drawable.ic_menu_archive);
        }
        fileIcon.setTag(file.hashCode());
        TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
        TextView fileSizeSeparatorV = (TextView) view.findViewById(R.id.file_separator);
        TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
        ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
        lastModV.setVisibility(View.VISIBLE);
        lastModV.setText(DisplayUtils.getRelativeTimestamp(mContext, file.lastModified()));
        if (!file.isDirectory()) {
            fileSizeSeparatorV.setVisibility(View.VISIBLE);
            fileSizeV.setVisibility(View.VISIBLE);
            fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.length(), mContext));
            ListView parentList = (ListView) parent;
            if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
                checkBoxV.setVisibility(View.GONE);
            } else {
                if (parentList.isItemChecked(position)) {
                    checkBoxV.setImageResource(R.drawable.ic_checkbox_marked);
                } else {
                    checkBoxV.setImageResource(R.drawable.ic_checkbox_blank_outline);
                }
                checkBoxV.setVisibility(View.VISIBLE);
            }
            // get Thumbnail if file is image
            if (BitmapUtils.isImage(file)) {
                // Thumbnail in Cache?
                Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.hashCode()));
                if (thumbnail != null) {
                    fileIcon.setImageBitmap(thumbnail);
                } else {
                    // generate new Thumbnail
                    if (allowedToCreateNewThumbnail) {
                        final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon);
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                        final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mContext.getResources(), thumbnail, task);
                        fileIcon.setImageDrawable(asyncDrawable);
                        task.execute(file);
                        Log_OC.v(TAG, "Executing task to generate a new thumbnail");
                    }
                // else, already being generated, don't restart it
                }
            } else {
                fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(null, file.getName()));
            }
        } else {
            fileSizeSeparatorV.setVisibility(View.GONE);
            fileSizeV.setVisibility(View.GONE);
            checkBoxV.setVisibility(View.GONE);
        }
        // not GONE; the alignment changes; ugly way to keep it
        view.findViewById(R.id.localFileIndicator).setVisibility(View.INVISIBLE);
        view.findViewById(R.id.favoriteIcon).setVisibility(View.GONE);
        view.findViewById(R.id.sharedIcon).setVisibility(View.GONE);
    }
    return view;
}
Also used : ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) Bitmap(android.graphics.Bitmap) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) ListView(android.widget.ListView) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) ImageView(android.widget.ImageView) File(java.io.File)

Example 2 with ThumbnailsCacheManager

use of com.owncloud.android.datamodel.ThumbnailsCacheManager in project android by nextcloud.

the class LocalFileListAdapter method setThumbnail.

private void setThumbnail(File file, ImageView thumbnailView) {
    if (file.isDirectory()) {
        thumbnailView.setImageDrawable(MimeTypeUtil.getDefaultFolderIcon());
    } else {
        thumbnailView.setImageResource(R.drawable.file);
        /**
         * Cancellation needs do be checked and done before changing the drawable in fileIcon, or
         * {@link ThumbnailsCacheManager#cancelPotentialThumbnailWork} will NEVER cancel any task.
         */
        boolean allowedToCreateNewThumbnail = (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, thumbnailView));
        // get Thumbnail if file is image
        if (MimeTypeUtil.isImage(file)) {
            // Thumbnail in Cache?
            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(ThumbnailsCacheManager.PREFIX_THUMBNAIL + String.valueOf(file.hashCode()));
            if (thumbnail != null) {
                thumbnailView.setImageBitmap(thumbnail);
            } else {
                // generate new Thumbnail
                if (allowedToCreateNewThumbnail) {
                    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(thumbnailView);
                    if (MimeTypeUtil.isVideo(file)) {
                        thumbnail = ThumbnailsCacheManager.mDefaultVideo;
                    } else {
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    }
                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mContext.getResources(), thumbnail, task);
                    thumbnailView.setImageDrawable(asyncDrawable);
                    task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, null));
                    Log_OC.v(TAG, "Executing task to generate a new thumbnail");
                }
            // else, already being generated, don't restart it
            }
        } else {
            thumbnailView.setImageDrawable(MimeTypeUtil.getFileTypeIcon(null, file.getName(), null));
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager)

Example 3 with ThumbnailsCacheManager

use of com.owncloud.android.datamodel.ThumbnailsCacheManager in project android by nextcloud.

the class UploadListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(SectionedViewHolder holder, int section, int relativePosition, int absolutePosition) {
    ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
    OCUpload item = mUploadGroups[section].getItem(relativePosition);
    itemViewHolder.name.setText(item.getLocalPath());
    // local file name
    File remoteFile = new File(item.getRemotePath());
    String fileName = remoteFile.getName();
    if (fileName.length() == 0) {
        fileName = File.separator;
    }
    itemViewHolder.name.setText(fileName);
    // remote path to parent folder
    itemViewHolder.remotePath.setText(new File(item.getRemotePath()).getParent());
    // file size
    if (item.getFileSize() != 0) {
        itemViewHolder.fileSize.setText(String.format("%s, ", DisplayUtils.bytesToHumanReadable(item.getFileSize())));
    } else {
        itemViewHolder.fileSize.setText("");
    }
    // upload date
    long updateTime = item.getUploadEndTimestamp();
    CharSequence dateString = DisplayUtils.getRelativeDateTimeString(mParentActivity, updateTime, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
    itemViewHolder.date.setText(dateString);
    Account account = AccountUtils.getOwnCloudAccountByName(mParentActivity, item.getAccountName());
    if (account != null) {
        itemViewHolder.account.setText(DisplayUtils.getAccountNameDisplayText(mParentActivity, account, account.name, item.getAccountName()));
    } else {
        itemViewHolder.account.setText(item.getAccountName());
    }
    // Reset fields visibility
    itemViewHolder.date.setVisibility(View.VISIBLE);
    itemViewHolder.remotePath.setVisibility(View.VISIBLE);
    itemViewHolder.fileSize.setVisibility(View.VISIBLE);
    itemViewHolder.account.setVisibility(View.VISIBLE);
    itemViewHolder.status.setVisibility(View.VISIBLE);
    itemViewHolder.progressBar.setVisibility(View.GONE);
    // Update information depending of upload details
    String status = getStatusText(item);
    switch(item.getUploadStatus()) {
        case UPLOAD_IN_PROGRESS:
            ThemeUtils.colorHorizontalProgressBar(itemViewHolder.progressBar, ThemeUtils.primaryAccentColor());
            itemViewHolder.progressBar.setProgress(0);
            itemViewHolder.progressBar.setVisibility(View.VISIBLE);
            FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
            if (binder != null) {
                if (binder.isUploadingNow(item)) {
                    // ... unbind the old progress bar, if any; ...
                    if (mProgressListener != null) {
                        binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                        mProgressListener.getUpload());
                    }
                    // ... then, bind the current progress bar to listen for updates
                    mProgressListener = new ProgressListener(item, itemViewHolder.progressBar);
                    binder.addDatatransferProgressListener(mProgressListener, item);
                } else {
                    // not really uploading; stop listening progress if view is reused!
                    if (mProgressListener != null && mProgressListener.isWrapping(itemViewHolder.progressBar)) {
                        binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                        mProgressListener.getUpload());
                        mProgressListener = null;
                    }
                }
            } else {
                Log_OC.w(TAG, "FileUploaderBinder not ready yet for upload " + item.getRemotePath());
            }
            itemViewHolder.date.setVisibility(View.GONE);
            itemViewHolder.fileSize.setVisibility(View.GONE);
            itemViewHolder.progressBar.invalidate();
            break;
        case UPLOAD_FAILED:
            itemViewHolder.date.setVisibility(View.GONE);
            break;
        case UPLOAD_SUCCEEDED:
            itemViewHolder.status.setVisibility(View.GONE);
            break;
    }
    itemViewHolder.status.setText(status);
    // bind listeners to perform actions
    if (item.getUploadStatus() == UploadStatus.UPLOAD_IN_PROGRESS) {
        // Cancel
        itemViewHolder.button.setImageResource(R.drawable.ic_action_cancel_grey);
        itemViewHolder.button.setVisibility(View.VISIBLE);
        itemViewHolder.button.setOnClickListener(v -> {
            FileUploader.FileUploaderBinder uploaderBinder = mParentActivity.getFileUploaderBinder();
            if (uploaderBinder != null) {
                uploaderBinder.cancel(item);
                loadUploadItemsFromDb();
            }
        });
    } else if (item.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
        // Delete
        itemViewHolder.button.setImageResource(R.drawable.ic_action_delete_grey);
        itemViewHolder.button.setVisibility(View.VISIBLE);
        itemViewHolder.button.setOnClickListener(v -> {
            mUploadsStorageManager.removeUpload(item);
            loadUploadItemsFromDb();
        });
    } else {
        // UploadStatus.UPLOAD_SUCCESS
        itemViewHolder.button.setVisibility(View.INVISIBLE);
    }
    itemViewHolder.itemLayout.setOnClickListener(null);
    // click on item
    if (item.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
        if (UploadResult.CREDENTIAL_ERROR.equals(item.getLastResult())) {
            itemViewHolder.itemLayout.setOnClickListener(v -> mParentActivity.getFileOperationsHelper().checkCurrentCredentials(item.getAccount(mParentActivity)));
        } else {
            // not a credentials error
            itemViewHolder.itemLayout.setOnClickListener(v -> {
                File file = new File(item.getLocalPath());
                if (file.exists()) {
                    FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
                    requester.retry(mParentActivity, item);
                    loadUploadItemsFromDb();
                } else {
                    Snackbar.make(v.getRootView().findViewById(android.R.id.content), mParentActivity.getString(R.string.local_file_not_found_message), Snackbar.LENGTH_LONG).show();
                }
            });
        }
    } else {
        itemViewHolder.itemLayout.setOnClickListener(v -> onUploadItemClick(item));
    }
    // Set icon or thumbnail
    itemViewHolder.thumbnail.setImageResource(R.drawable.file);
    /*
         * Cancellation needs do be checked and done before changing the drawable in fileIcon, or
         * {@link ThumbnailsCacheManager#cancelPotentialWork} will NEVER cancel any task.
         */
    OCFile fakeFileToCheatThumbnailsCacheManagerInterface = new OCFile(item.getRemotePath());
    fakeFileToCheatThumbnailsCacheManagerInterface.setStoragePath(item.getLocalPath());
    fakeFileToCheatThumbnailsCacheManagerInterface.setMimetype(item.getMimeType());
    boolean allowedToCreateNewThumbnail = (ThumbnailsCacheManager.cancelPotentialThumbnailWork(fakeFileToCheatThumbnailsCacheManagerInterface, itemViewHolder.thumbnail));
    // TODO this code is duplicated; refactor to a common place
    if ((MimeTypeUtil.isImage(fakeFileToCheatThumbnailsCacheManagerInterface) && fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null && item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED)) {
        // Thumbnail in Cache?
        Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId()));
        if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.needsUpdateThumbnail()) {
            itemViewHolder.thumbnail.setImageBitmap(thumbnail);
        } else {
            // generate new Thumbnail
            if (allowedToCreateNewThumbnail) {
                final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.thumbnail, mParentActivity.getStorageManager(), mParentActivity.getAccount());
                if (thumbnail == null) {
                    if (MimeTypeUtil.isVideo(fakeFileToCheatThumbnailsCacheManagerInterface)) {
                        thumbnail = ThumbnailsCacheManager.mDefaultVideo;
                    } else {
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    }
                }
                final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                itemViewHolder.thumbnail.setImageDrawable(asyncDrawable);
                task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(fakeFileToCheatThumbnailsCacheManagerInterface, null));
            }
        }
        if ("image/png".equals(item.getMimeType())) {
            itemViewHolder.thumbnail.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
        }
    } else if (MimeTypeUtil.isImage(fakeFileToCheatThumbnailsCacheManagerInterface)) {
        File file = new File(item.getLocalPath());
        // Thumbnail in Cache?
        Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.hashCode()));
        if (thumbnail != null) {
            itemViewHolder.thumbnail.setImageBitmap(thumbnail);
        } else {
            // generate new Thumbnail
            if (allowedToCreateNewThumbnail) {
                final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.thumbnail);
                if (MimeTypeUtil.isVideo(file)) {
                    thumbnail = ThumbnailsCacheManager.mDefaultVideo;
                } else {
                    thumbnail = ThumbnailsCacheManager.mDefaultImg;
                }
                final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                itemViewHolder.thumbnail.setImageDrawable(asyncDrawable);
                task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, null));
                Log_OC.v(TAG, "Executing task to generate a new thumbnail");
            }
        }
        if ("image/png".equalsIgnoreCase(item.getMimeType())) {
            itemViewHolder.thumbnail.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
        }
    } else {
        itemViewHolder.thumbnail.setImageDrawable(MimeTypeUtil.getFileTypeIcon(item.getMimeType(), fileName, account));
    }
}
Also used : ImageButton(android.widget.ImageButton) LinearLayout(android.widget.LinearLayout) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) Arrays(java.util.Arrays) DateUtils(android.text.format.DateUtils) ProgressBar(android.widget.ProgressBar) Uri(android.net.Uri) ImageView(android.widget.ImageView) OCFile(com.owncloud.android.datamodel.OCFile) FileActivity(com.owncloud.android.ui.activity.FileActivity) Intent(android.content.Intent) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) View(android.view.View) DisplayUtils(com.owncloud.android.utils.DisplayUtils) ThemeUtils(com.owncloud.android.utils.ThemeUtils) Account(android.accounts.Account) LayoutInflater(android.view.LayoutInflater) SectionedRecyclerViewAdapter(com.afollestad.sectionedrecyclerview.SectionedRecyclerViewAdapter) UploadStatus(com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus) OCUpload(com.owncloud.android.db.OCUpload) SectionedViewHolder(com.afollestad.sectionedrecyclerview.SectionedViewHolder) AccountUtils(com.owncloud.android.authentication.AccountUtils) ViewGroup(android.view.ViewGroup) File(java.io.File) Log_OC(com.owncloud.android.lib.common.utils.Log_OC) TextView(android.widget.TextView) ActivityNotFoundException(android.content.ActivityNotFoundException) Bitmap(android.graphics.Bitmap) UploadResult(com.owncloud.android.db.UploadResult) FileUploader(com.owncloud.android.files.services.FileUploader) MimeTypeUtil(com.owncloud.android.utils.MimeTypeUtil) Snackbar(android.support.design.widget.Snackbar) Comparator(java.util.Comparator) R(com.owncloud.android.R) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Account(android.accounts.Account) FileUploader(com.owncloud.android.files.services.FileUploader) OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) Bitmap(android.graphics.Bitmap) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 4 with ThumbnailsCacheManager

use of com.owncloud.android.datamodel.ThumbnailsCacheManager in project android by owncloud.

the class ExpandableUploadListAdapter method getView.

private View getView(OCUpload[] uploadsItems, int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflator = (LayoutInflater) mParentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflator.inflate(R.layout.upload_list_item, parent, false);
        // Allow or disallow touches with other visible windows
        view.setFilterTouchesWhenObscured(PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(mParentActivity));
    }
    if (uploadsItems != null && uploadsItems.length > position) {
        final OCUpload upload = uploadsItems[position];
        // local file name
        TextView fileTextView = view.findViewById(R.id.upload_name);
        File remoteFile = new File(upload.getRemotePath());
        String fileName = remoteFile.getName();
        if (fileName.length() == 0) {
            fileName = File.separator;
        }
        fileTextView.setText(fileName);
        // remote path to parent folder
        TextView pathTextView = view.findViewById(R.id.upload_remote_path);
        pathTextView.setText(PREF__CAMERA_UPLOADS_DEFAULT_PATH);
        String remoteParentPath = upload.getRemotePath();
        remoteParentPath = new File(remoteParentPath).getParent();
        String pathText = mParentActivity.getString(R.string.app_name) + remoteParentPath;
        pathTextView.setText(pathText);
        // file size
        TextView fileSizeTextView = view.findViewById(R.id.upload_file_size);
        String fileSize = DisplayUtils.bytesToHumanReadable(upload.getFileSize(), mParentActivity) + ", ";
        fileSizeTextView.setText(fileSize);
        // * upload date
        TextView uploadDateTextView = view.findViewById(R.id.upload_date);
        long updateTime = upload.getUploadEndTimestamp();
        CharSequence dateString = DisplayUtils.getRelativeDateTimeString(mParentActivity, updateTime, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
        uploadDateTextView.setText(dateString);
        TextView accountNameTextView = view.findViewById(R.id.upload_account);
        try {
            Account account = AccountUtils.getOwnCloudAccountByName(mParentActivity, upload.getAccountName());
            OwnCloudAccount oca = new OwnCloudAccount(account, mParentActivity);
            String accountName = oca.getDisplayName() + " @ " + DisplayUtils.convertIdn(account.name.substring(account.name.lastIndexOf("@") + 1), false);
            accountNameTextView.setText(accountName);
        } catch (Exception e) {
            Timber.w("Couldn't get display name for account, using old style");
            accountNameTextView.setText(upload.getAccountName());
        }
        TextView statusTextView = view.findViewById(R.id.upload_status);
        ProgressBar progressBar = view.findViewById(R.id.upload_progress_bar);
        // / Reset fields visibility
        uploadDateTextView.setVisibility(View.VISIBLE);
        pathTextView.setVisibility(View.VISIBLE);
        fileSizeTextView.setVisibility(View.VISIBLE);
        accountNameTextView.setVisibility(View.VISIBLE);
        statusTextView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        // / Update information depending of upload details
        String status = getStatusText(upload);
        switch(upload.getUploadStatus()) {
            case UPLOAD_IN_PROGRESS:
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                FileUploader.FileUploaderBinder binder = mParentActivity.getFileUploaderBinder();
                if (binder != null) {
                    if (binder.isUploadingNow(upload)) {
                        // / ... unbind the old progress bar, if any; ...
                        if (mProgressListener != null) {
                            binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                            mProgressListener.getUpload());
                        }
                        // / ... then, bind the current progress bar to listen for updates
                        mProgressListener = new ProgressListener(upload, progressBar);
                        binder.addDatatransferProgressListener(mProgressListener, upload);
                    } else {
                        // / not really uploading; stop listening progress if view is reused!
                        if (convertView != null && mProgressListener != null && mProgressListener.isWrapping(progressBar)) {
                            binder.removeDatatransferProgressListener(mProgressListener, // the one that was added
                            mProgressListener.getUpload());
                            mProgressListener = null;
                        }
                    }
                } else {
                    Timber.w("FileUploaderBinder not ready yet for upload %s", upload.getRemotePath());
                }
                uploadDateTextView.setVisibility(View.GONE);
                pathTextView.setVisibility(View.GONE);
                progressBar.invalidate();
                break;
            case UPLOAD_FAILED:
                uploadDateTextView.setVisibility(View.GONE);
                break;
            case UPLOAD_SUCCEEDED:
                statusTextView.setVisibility(View.GONE);
                break;
        }
        statusTextView.setText(status);
        // / bind listeners to perform actions
        ImageButton rightButton = view.findViewById(R.id.upload_right_button);
        if (upload.getUploadStatus() == UploadStatus.UPLOAD_IN_PROGRESS) {
            // Cancel
            rightButton.setImageResource(R.drawable.ic_action_cancel_grey);
            rightButton.setVisibility(View.VISIBLE);
            rightButton.setOnClickListener(v -> {
                String localPath = upload.getLocalPath();
                boolean isDocumentUri = DocumentFile.isDocumentUri(parent.getContext(), Uri.parse(localPath));
                if (isDocumentUri) {
                    CancelUploadWithIdUseCase cancelUploadWithIdUseCase = new CancelUploadWithIdUseCase(WorkManager.getInstance(parent.getContext()));
                    cancelUploadWithIdUseCase.execute(new CancelUploadWithIdUseCase.Params(upload));
                } else {
                    FileUploader.FileUploaderBinder uploaderBinder = mParentActivity.getFileUploaderBinder();
                    if (uploaderBinder != null) {
                        uploaderBinder.cancel(upload);
                    }
                }
                refreshView();
            });
        } else if (upload.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
            // Delete
            rightButton.setImageResource(R.drawable.ic_action_delete_grey);
            rightButton.setVisibility(View.VISIBLE);
            rightButton.setOnClickListener(v -> {
                mUploadsStorageManager.removeUpload(upload);
                refreshView();
            });
        } else {
            // UploadStatus.UPLOAD_SUCCESS
            rightButton.setVisibility(View.INVISIBLE);
        }
        // retry
        if (upload.getUploadStatus() == UploadStatus.UPLOAD_FAILED) {
            if (UploadResult.CREDENTIAL_ERROR.equals(upload.getLastResult())) {
                view.setOnClickListener(v -> mParentActivity.getFileOperationsHelper().checkCurrentCredentials(upload.getAccount(mParentActivity)));
            } else {
                // not a credentials error
                view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        File file = new File(upload.getLocalPath());
                        if (file.exists()) {
                            TransferRequester requester = new TransferRequester();
                            requester.retry(mParentActivity, upload, false);
                            refreshView();
                        } else if (DocumentFile.isDocumentUri(v.getContext(), Uri.parse(upload.getLocalPath()))) {
                            WorkManager workManager = WorkManager.getInstance(MainApp.Companion.getAppContext());
                            RetryUploadFromContentUriUseCase retryUploadFromContentUriUseCase = new RetryUploadFromContentUriUseCase(workManager);
                            RetryUploadFromContentUriUseCase.Params useCaseParams = new RetryUploadFromContentUriUseCase.Params(upload.getUploadId());
                            retryUploadFromContentUriUseCase.execute(useCaseParams);
                        } else {
                            Snackbar snackbar = Snackbar.make(v.getRootView().findViewById(android.R.id.content), mParentActivity.getString(R.string.local_file_not_found_toast), Snackbar.LENGTH_LONG);
                            snackbar.show();
                        }
                    }
                });
            }
        } else {
            view.setOnClickListener(null);
        }
        // / Set icon or thumbnail
        ImageView fileIcon = view.findViewById(R.id.thumbnail);
        fileIcon.setImageResource(R.drawable.file);
        /* Cancellation needs do be checked and done before changing the drawable in fileIcon, or
             * {@link ThumbnailsCacheManager#cancelPotentialWork} will NEVER cancel any task.
             */
        OCFile fakeFileToCheatThumbnailsCacheManagerInterface = new OCFile(upload.getRemotePath());
        fakeFileToCheatThumbnailsCacheManagerInterface.setStoragePath(upload.getLocalPath());
        fakeFileToCheatThumbnailsCacheManagerInterface.setMimetype(upload.getMimeType());
        boolean allowedToCreateNewThumbnail = (ThumbnailsCacheManager.cancelPotentialThumbnailWork(fakeFileToCheatThumbnailsCacheManagerInterface, fileIcon));
        // TODO this code is duplicated; refactor to a common place
        if ((fakeFileToCheatThumbnailsCacheManagerInterface.isImage() && fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null && upload.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED)) {
            // Thumbnail in Cache?
            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId()));
            if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.needsUpdateThumbnail()) {
                fileIcon.setImageBitmap(thumbnail);
            } else {
                // generate new Thumbnail
                if (allowedToCreateNewThumbnail) {
                    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon, mParentActivity.getStorageManager(), mParentActivity.getAccount());
                    if (thumbnail == null) {
                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    }
                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                    fileIcon.setImageDrawable(asyncDrawable);
                    task.execute(fakeFileToCheatThumbnailsCacheManagerInterface);
                }
            }
            if ("image/png".equals(upload.getMimeType())) {
                fileIcon.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
            }
        } else if (fakeFileToCheatThumbnailsCacheManagerInterface.isImage()) {
            File file = new File(upload.getLocalPath());
            // Thumbnail in Cache?
            Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(String.valueOf(file.hashCode()));
            if (thumbnail != null) {
                fileIcon.setImageBitmap(thumbnail);
            } else {
                // generate new Thumbnail
                if (allowedToCreateNewThumbnail) {
                    final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(fileIcon);
                    thumbnail = ThumbnailsCacheManager.mDefaultImg;
                    final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(mParentActivity.getResources(), thumbnail, task);
                    fileIcon.setImageDrawable(asyncDrawable);
                    task.execute(file);
                    Timber.v("Executing task to generate a new thumbnail");
                }
            }
            if ("image/png".equalsIgnoreCase(upload.getMimeType())) {
                fileIcon.setBackgroundColor(mParentActivity.getResources().getColor(R.color.background_color));
            }
        } else {
            fileIcon.setImageResource(MimetypeIconUtil.getFileTypeIconId(upload.getMimeType(), fileName));
        }
    }
    return view;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) Observer(java.util.Observer) ImageButton(android.widget.ImageButton) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) Arrays(java.util.Arrays) DateUtils(android.text.format.DateUtils) ProgressBar(android.widget.ProgressBar) Uri(android.net.Uri) ImageView(android.widget.ImageView) OCFile(com.owncloud.android.datamodel.OCFile) PreferenceUtils(com.owncloud.android.utils.PreferenceUtils) BaseExpandableListAdapter(android.widget.BaseExpandableListAdapter) CancelUploadWithIdUseCase(com.owncloud.android.usecases.CancelUploadWithIdUseCase) View(android.view.View) DisplayUtils(com.owncloud.android.utils.DisplayUtils) PREF__CAMERA_UPLOADS_DEFAULT_PATH(com.owncloud.android.db.PreferenceManager.PREF__CAMERA_UPLOADS_DEFAULT_PATH) DataSetObserver(android.database.DataSetObserver) RetryUploadFromContentUriUseCase(com.owncloud.android.usecases.RetryUploadFromContentUriUseCase) TransferRequester(com.owncloud.android.files.services.TransferRequester) Account(android.accounts.Account) UploadStatus(com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus) AppCompatButton(androidx.appcompat.widget.AppCompatButton) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) OCUpload(com.owncloud.android.datamodel.OCUpload) TextView(android.widget.TextView) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) MainApp(com.owncloud.android.MainApp) DocumentFile(androidx.documentfile.provider.DocumentFile) UploadResult(com.owncloud.android.db.UploadResult) FileUploader(com.owncloud.android.files.services.FileUploader) Snackbar(com.google.android.material.snackbar.Snackbar) R(com.owncloud.android.R) MimetypeIconUtil(com.owncloud.android.utils.MimetypeIconUtil) Context(android.content.Context) FileActivity(com.owncloud.android.ui.activity.FileActivity) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) WorkManager(androidx.work.WorkManager) UploadListFragment(com.owncloud.android.ui.fragment.UploadListFragment) WeakReference(java.lang.ref.WeakReference) LayoutInflater(android.view.LayoutInflater) AccountUtils(com.owncloud.android.authentication.AccountUtils) File(java.io.File) OptionsInUploadListClickListener(com.owncloud.android.ui.fragment.OptionsInUploadListClickListener) Bitmap(android.graphics.Bitmap) ExpandableListView(android.widget.ExpandableListView) Comparator(java.util.Comparator) Observable(java.util.Observable) OnClickListener(android.view.View.OnClickListener) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) FileUploader(com.owncloud.android.files.services.FileUploader) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.datamodel.OCUpload) ImageButton(android.widget.ImageButton) Bitmap(android.graphics.Bitmap) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) WorkManager(androidx.work.WorkManager) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ProgressBar(android.widget.ProgressBar) RetryUploadFromContentUriUseCase(com.owncloud.android.usecases.RetryUploadFromContentUriUseCase) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ExpandableListView(android.widget.ExpandableListView) CancelUploadWithIdUseCase(com.owncloud.android.usecases.CancelUploadWithIdUseCase) TransferRequester(com.owncloud.android.files.services.TransferRequester) OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) LayoutInflater(android.view.LayoutInflater) OnClickListener(android.view.View.OnClickListener) OCFile(com.owncloud.android.datamodel.OCFile) DocumentFile(androidx.documentfile.provider.DocumentFile) File(java.io.File) Snackbar(com.google.android.material.snackbar.Snackbar)

Aggregations

Bitmap (android.graphics.Bitmap)4 ThumbnailsCacheManager (com.owncloud.android.datamodel.ThumbnailsCacheManager)4 LayoutInflater (android.view.LayoutInflater)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 Account (android.accounts.Account)2 Uri (android.net.Uri)2 DateUtils (android.text.format.DateUtils)2 ViewGroup (android.view.ViewGroup)2 ImageButton (android.widget.ImageButton)2 ProgressBar (android.widget.ProgressBar)2 R (com.owncloud.android.R)2 AccountUtils (com.owncloud.android.authentication.AccountUtils)2 OCFile (com.owncloud.android.datamodel.OCFile)2 UploadsStorageManager (com.owncloud.android.datamodel.UploadsStorageManager)2 UploadStatus (com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus)2 UploadResult (com.owncloud.android.db.UploadResult)2 FileUploader (com.owncloud.android.files.services.FileUploader)2 FileActivity (com.owncloud.android.ui.activity.FileActivity)2