Search in sources :

Example 1 with LbryFile

use of com.odysee.app.model.LbryFile in project odysee-android by OdyseeTeam.

the class Lbry method fileList.

public static List<LbryFile> fileList(String claimId, boolean downloads, int page, int pageSize) throws ApiCallException {
    List<LbryFile> files = new ArrayList<>();
    Map<String, Object> params = new HashMap<>();
    if (!Helper.isNullOrEmpty(claimId)) {
        params.put("claim_id", claimId);
    }
    if (downloads) {
        params.put("download_path", null);
        params.put("comparison", "ne");
    }
    if (page > 0) {
        params.put("page", page);
    }
    if (pageSize > 0) {
        params.put("page_size", pageSize);
    }
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_FILE_LIST, params));
        JSONArray items = result.getJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            JSONObject fileObject = items.getJSONObject(i);
            LbryFile file = LbryFile.fromJSONObject(fileObject);
            files.add(file);
            String fileClaimId = file.getClaimId();
            if (!Helper.isNullOrEmpty(fileClaimId)) {
                ClaimCacheKey key = new ClaimCacheKey();
                key.setClaimId(fileClaimId);
                if (claimCache.containsKey(key)) {
                    claimCache.get(key).setFile(file);
                }
            }
        }
    } catch (LbryRequestException | LbryResponseException | JSONException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return files;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) LbryFile(com.odysee.app.model.LbryFile) JSONObject(org.json.JSONObject)

Example 2 with LbryFile

use of com.odysee.app.model.LbryFile in project odysee-android by OdyseeTeam.

the class ClaimListAdapter method onBindViewHolder.

@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void onBindViewHolder(ClaimListAdapter.ViewHolder vh, int position) {
    int type = getItemViewType(position);
    if (style == STYLE_SMALL_LIST) {
        int paddingTop = position == 0 ? 16 : 8;
        int paddingBottom = position == getItemCount() - 1 ? 16 : 8;
        int paddingTopScaled = Helper.getScaledValue(paddingTop, scale);
        int paddingBottomScaled = Helper.getScaledValue(paddingBottom, scale);
        vh.itemView.setPadding(vh.itemView.getPaddingStart(), paddingTopScaled, vh.itemView.getPaddingEnd(), paddingBottomScaled);
    } else if (style == STYLE_SMALL_LIST_HORIZONTAL) {
        int paddingStart = position == 0 ? 16 : 8;
        int paddingEnd = position == getItemCount() - 1 ? 16 : 8;
        int paddingStartScaled = Helper.getScaledValue(paddingStart, scale);
        int paddingEndScaled = Helper.getScaledValue(paddingEnd, scale);
        vh.itemView.setPadding(paddingStartScaled, vh.itemView.getPaddingTop(), paddingEndScaled, vh.itemView.getPaddingBottom());
    }
    Claim original = items.get(position);
    boolean isRepost = Claim.TYPE_REPOST.equalsIgnoreCase(original.getValueType());
    final Claim item = Claim.TYPE_REPOST.equalsIgnoreCase(original.getValueType()) ? (original.getRepostedClaim() != null ? original.getRepostedClaim() : original) : original;
    Claim.GenericMetadata metadata = item.getValue();
    Claim signingChannel = item.getSigningChannel();
    Claim.StreamMetadata streamMetadata = null;
    if (metadata instanceof Claim.StreamMetadata) {
        streamMetadata = (Claim.StreamMetadata) metadata;
    }
    String thumbnailUrl = item.getThumbnailUrl(vh.thumbnailView.getLayoutParams().width, vh.thumbnailView.getLayoutParams().height, 85);
    long publishTime = (streamMetadata != null && streamMetadata.getReleaseTime() > 0) ? streamMetadata.getReleaseTime() * 1000 : item.getTimestamp() * 1000;
    int bgColor = Helper.generateRandomColorForValue(item.getClaimId());
    if (bgColor == 0) {
        bgColor = Helper.generateRandomColorForValue(item.getName());
    }
    boolean isPending = item.getConfirmations() == 0;
    boolean isSelected = isClaimSelected(original);
    vh.itemView.setSelected(isSelected);
    vh.setContextGroupId(contextGroupId);
    vh.selectedOverlayView.setVisibility(isSelected ? View.VISIBLE : View.GONE);
    vh.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (isPending) {
                Snackbar snackbar = Snackbar.make(vh.itemView, R.string.item_pending_blockchain, Snackbar.LENGTH_LONG);
                TextView snackbarText = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
                snackbarText.setMaxLines(5);
                snackbar.show();
                return;
            }
            if (inSelectionMode) {
                toggleSelectedClaim(original);
            } else {
                if (listener != null) {
                    listener.onClaimClicked(item, position);
                }
            }
        }
    });
    vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            if (!canEnterSelectionMode) {
                return false;
            }
            if (isPending) {
                Snackbar snackbar = Snackbar.make(vh.itemView, R.string.item_pending_blockchain, Snackbar.LENGTH_LONG);
                TextView snackbarText = snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
                snackbarText.setMaxLines(5);
                snackbar.show();
                return false;
            }
            if (!inSelectionMode) {
                inSelectionMode = true;
                if (selectionModeListener != null) {
                    selectionModeListener.onEnterSelectionMode();
                }
            }
            toggleSelectedClaim(original);
            return true;
        }
    });
    vh.publisherView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (listener != null && signingChannel != null) {
                listener.onClaimClicked(signingChannel, position);
            }
        }
    });
    vh.publishTimeView.setVisibility(!isPending && style != STYLE_SMALL_LIST_HORIZONTAL ? View.VISIBLE : View.GONE);
    vh.pendingTextView.setVisibility(isPending && !item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    vh.repostInfoView.setVisibility(isRepost && type != VIEW_TYPE_FEATURED ? View.VISIBLE : View.GONE);
    vh.repostChannelView.setText(isRepost && original.getSigningChannel() != null ? original.getSigningChannel().getName() : null);
    vh.repostChannelView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (listener != null) {
                listener.onClaimClicked(original.getSigningChannel(), position);
            }
        }
    });
    if (vh.optionsMenuView != null) {
        vh.optionsMenuView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                setPosition(vh.getBindingAdapterPosition());
                view.showContextMenu();
            }
        });
    }
    vh.titleView.setText(Helper.isNullOrEmpty(item.getTitle()) ? item.getName() : item.getTitle());
    if (type == VIEW_TYPE_FEATURED) {
        LbryUri vanityUrl = new LbryUri();
        vanityUrl.setClaimName(item.getName());
        vh.vanityUrlView.setText(vanityUrl.toString());
    }
    vh.feeContainer.setVisibility(item.isUnresolved() || !Claim.TYPE_STREAM.equalsIgnoreCase(item.getValueType()) ? View.GONE : View.VISIBLE);
    vh.noThumbnailView.setVisibility(Helper.isNullOrEmpty(thumbnailUrl) ? View.VISIBLE : View.GONE);
    Helper.setIconViewBackgroundColor(vh.noThumbnailView, bgColor, false, context);
    Helper.setViewVisibility(vh.loadingImagePlaceholder, item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    Helper.setViewVisibility(vh.loadingTextPlaceholder1, item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    Helper.setViewVisibility(vh.loadingTextPlaceholder2, item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    Helper.setViewVisibility(vh.titleView, !item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    Helper.setViewVisibility(vh.publisherView, !item.isLoadingPlaceholder() ? View.VISIBLE : View.GONE);
    Helper.setViewVisibility(vh.publishTimeView, !item.isLoadingPlaceholder() && !isPending && style != STYLE_SMALL_LIST_HORIZONTAL ? View.VISIBLE : View.GONE);
    if (type == VIEW_TYPE_FEATURED && item.isUnresolved()) {
        vh.durationView.setVisibility(View.GONE);
        vh.titleView.setText("Nothing here. Publish something!");
        String name = item.getName();
        if (!Helper.isNullOrEmpty(name)) {
            vh.alphaView.setText(name.substring(0, Math.min(5, name.length() - 1)));
        }
    } else {
        ViewGroup.LayoutParams lp = vh.itemView.getLayoutParams();
        if (Claim.TYPE_STREAM.equalsIgnoreCase(item.getValueType()) || Claim.TYPE_COLLECTION.equalsIgnoreCase(item.getValueType())) {
            if ((filterTimeframeFrom == 0 || publishTime >= filterTimeframeFrom) && shouldDisplayClaim(item)) {
                if (!Helper.isNullOrEmpty(thumbnailUrl)) {
                    Glide.with(context.getApplicationContext()).asBitmap().load(thumbnailUrl).placeholder(R.drawable.bg_thumbnail_placeholder).into(vh.thumbnailView);
                    vh.thumbnailView.setVisibility(View.VISIBLE);
                } else {
                    vh.thumbnailView.setVisibility(View.GONE);
                }
                BigDecimal cost = item.getActualCost(Lbryio.LBCUSDRate);
                vh.feeContainer.setVisibility(cost.doubleValue() > 0 && !hideFee ? View.VISIBLE : View.GONE);
                vh.feeView.setText(cost.doubleValue() > 0 ? Helper.shortCurrencyFormat(cost.doubleValue()) : "Paid");
                vh.alphaView.setText(item.getName().substring(0, Math.min(5, item.getName().length() - 1)));
                vh.publisherView.setText(signingChannel != null ? signingChannel.getName() : context.getString(R.string.anonymous));
                vh.publishTimeView.setText(DateUtils.getRelativeTimeSpanString(publishTime, System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_RELATIVE));
                long duration = item.getDuration();
                vh.durationView.setVisibility((duration > 0 || item.isLive() || Claim.TYPE_COLLECTION.equalsIgnoreCase(item.getValueType())) ? View.VISIBLE : View.GONE);
                if (item.isLive() && !Claim.TYPE_COLLECTION.equalsIgnoreCase(item.getValueType())) {
                    vh.durationView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
                    vh.durationView.setText(context.getResources().getString(R.string.live).toUpperCase());
                } else {
                    vh.durationView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.black));
                    if (!Claim.TYPE_COLLECTION.equalsIgnoreCase(item.getValueType())) {
                        vh.durationView.setText(Helper.formatDuration(duration));
                        vh.durationView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
                    } else {
                        List<String> claimIds = item.getClaimIds() == null ? new ArrayList<>() : item.getClaimIds();
                        vh.durationView.setText(String.valueOf(claimIds.size()));
                        vh.durationView.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_list_icon, 0, 0, 0);
                        vh.durationView.setCompoundDrawablePadding(8);
                    }
                    LbryFile claimFile = item.getFile();
                    boolean isDownloading = false;
                    int progress = 0;
                    String fileSizeString = claimFile == null ? null : Helper.formatBytes(claimFile.getTotalBytes(), false);
                    if (claimFile != null && !Helper.isNullOrEmpty(claimFile.getDownloadPath()) && !claimFile.isCompleted() && claimFile.getWrittenBytes() < claimFile.getTotalBytes()) {
                        isDownloading = true;
                        progress = claimFile.getTotalBytes() > 0 ? Double.valueOf(((double) claimFile.getWrittenBytes() / (double) claimFile.getTotalBytes()) * 100.0).intValue() : 0;
                        fileSizeString = String.format("%s / %s", Helper.formatBytes(claimFile.getWrittenBytes(), false), Helper.formatBytes(claimFile.getTotalBytes(), false));
                    }
                    Helper.setViewText(vh.fileSizeView, claimFile != null && !Helper.isNullOrEmpty(claimFile.getDownloadPath()) ? fileSizeString : null);
                    Helper.setViewVisibility(vh.downloadProgressView, isDownloading ? View.VISIBLE : View.INVISIBLE);
                    Helper.setViewProgress(vh.downloadProgressView, progress);
                    Helper.setViewText(vh.deviceView, item.getDevice());
                    lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    vh.itemView.setLayoutParams(lp);
                    vh.itemView.setVisibility(View.VISIBLE);
                    if (style != STYLE_SMALL_LIST_HORIZONTAL) {
                        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    }
                }
            } else {
                vh.itemView.setVisibility(View.GONE);
                lp.height = 0;
                lp.width = 0;
                vh.itemView.setLayoutParams(lp);
            }
        } else if (Claim.TYPE_CHANNEL.equalsIgnoreCase(item.getValueType())) {
            if ((this.filterByChannel || !this.filterByFile) && (filterTimeframeFrom == 0 || publishTime >= filterTimeframeFrom)) {
                if (!Helper.isNullOrEmpty(thumbnailUrl)) {
                    Glide.with(context.getApplicationContext()).load(thumbnailUrl).centerCrop().placeholder(R.drawable.bg_thumbnail_placeholder).apply(RequestOptions.circleCropTransform()).into(vh.thumbnailView);
                }
                vh.alphaView.setText(item.getName().substring(1, 2).toUpperCase());
                vh.publisherView.setText(item.getName());
                vh.publishTimeView.setText(DateUtils.getRelativeTimeSpanString(publishTime, System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_RELATIVE));
                lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                vh.itemView.setLayoutParams(lp);
                vh.itemView.setVisibility(View.VISIBLE);
            } else {
                vh.itemView.setVisibility(View.GONE);
                lp.height = 0;
                lp.width = 0;
                vh.itemView.setLayoutParams(lp);
            }
        }
    }
}
Also used : ViewGroup(android.view.ViewGroup) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) BigDecimal(java.math.BigDecimal) LbryFile(com.odysee.app.model.LbryFile) TextView(android.widget.TextView) LbryUri(com.odysee.app.utils.LbryUri) Claim(com.odysee.app.model.Claim) Snackbar(com.google.android.material.snackbar.Snackbar) SuppressLint(android.annotation.SuppressLint)

Example 3 with LbryFile

use of com.odysee.app.model.LbryFile in project odysee-android by OdyseeTeam.

the class FileViewFragment method tryOpenFileOrFileGet.

private void tryOpenFileOrFileGet() {
    Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
    if (actualClaim != null) {
        String claimId = actualClaim.getClaimId();
        FileListTask task = new FileListTask(claimId, null, new FileListTask.FileListResultHandler() {

            @Override
            public void onSuccess(List<LbryFile> files, boolean hasReachedEnd) {
                if (files.size() > 0) {
                    actualClaim.setFile(files.get(0));
                    handleMainActionForClaim();
                    checkIsFileComplete();
                } else {
                    checkStoragePermissionAndFileGet();
                }
            }

            @Override
            public void onError(Exception error) {
                checkStoragePermissionAndFileGet();
            }
        });
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : FileListTask(com.odysee.app.tasks.file.FileListTask) LbryFile(com.odysee.app.model.LbryFile) TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride) Claim(com.odysee.app.model.Claim) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException)

Example 4 with LbryFile

use of com.odysee.app.model.LbryFile in project odysee-android by OdyseeTeam.

the class FileViewFragment method fileGet.

private void fileGet(boolean save) {
    if (getFileTask != null && getFileTask.getStatus() != AsyncTask.Status.FINISHED) {
        return;
    }
    Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
    getFileTask = new GetFileTask(actualClaim.getPermanentUrl(), save, null, new GetFileTask.GetFileHandler() {

        @Override
        public void beforeStart() {
        }

        @Override
        public void onSuccess(LbryFile file, boolean saveFile) {
            // queue the download
            if (actualClaim != null) {
                if (actualClaim.isFree()) {
                    // paid is handled differently
                    Bundle bundle = new Bundle();
                    bundle.putString("uri", currentUrl);
                    bundle.putString("paid", "false");
                    LbryAnalytics.logEvent(LbryAnalytics.EVENT_PURCHASE_URI, bundle);
                }
                if (!actualClaim.isPlayable()) {
                    logFileView(actualClaim.getPermanentUrl(), 0);
                }
                actualClaim.setFile(file);
                playOrViewMedia();
            }
        }

        @Override
        public void onError(Exception error, boolean saveFile) {
            try {
                showError(getString(R.string.unable_to_view_url, currentUrl));
                if (saveFile) {
                    onDownloadAborted();
                }
                restoreMainActionButton();
            } catch (IllegalStateException ex) {
            // pass
            }
        }
    });
    getFileTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Bundle(android.os.Bundle) LbryFile(com.odysee.app.model.LbryFile) GetFileTask(com.odysee.app.tasks.file.GetFileTask) Claim(com.odysee.app.model.Claim) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException)

Example 5 with LbryFile

use of com.odysee.app.model.LbryFile in project odysee-android by OdyseeTeam.

the class FileViewFragment method playOrViewMedia.

private void playOrViewMedia() {
    boolean handled = false;
    Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
    String mediaType = actualClaim.getMediaType();
    if (!Helper.isNullOrEmpty(mediaType)) {
        if (actualClaim.isPlayable()) {
            startTimeMillis = System.currentTimeMillis();
            showExoplayerView();
            playMedia();
            handled = true;
        } else if (actualClaim.isViewable()) {
            // check type and display
            boolean fileExists = false;
            LbryFile claimFile = actualClaim.getFile();
            Uri fileUri = null;
            if (claimFile != null && !Helper.isNullOrEmpty(claimFile.getDownloadPath())) {
                File file = new File(claimFile.getDownloadPath());
                fileUri = Uri.fromFile(file);
                fileExists = file.exists();
            }
            if (!fileExists) {
                showError(getString(R.string.claim_file_not_found, claimFile != null ? claimFile.getDownloadPath() : ""));
            } else if (fileUri != null) {
                View root = getView();
                Context context = getContext();
                if (root != null) {
                    if (mediaType.startsWith("image")) {
                        // display the image
                        View container = root.findViewById(R.id.file_view_imageviewer_container);
                        PhotoView photoView = root.findViewById(R.id.file_view_imageviewer);
                        if (context != null) {
                            Glide.with(context.getApplicationContext()).load(fileUri).centerInside().into(photoView);
                        }
                        container.setVisibility(View.VISIBLE);
                    } else if (mediaType.startsWith("text")) {
                        // show web view (and parse markdown too)
                        View container = root.findViewById(R.id.file_view_webview_container);
                        initWebView(root);
                        applyThemeToWebView();
                        if (Arrays.asList("text/markdown", "text/md").contains(mediaType.toLowerCase())) {
                            loadMarkdownFromFile(claimFile.getDownloadPath());
                        } else {
                            webView.loadUrl(fileUri.toString());
                        }
                        container.setVisibility(View.VISIBLE);
                    }
                }
                handled = true;
            }
        } else {
            openClaimExternally(actualClaim, mediaType);
        }
    }
    if (!handled) {
        showUnsupportedView();
    }
}
Also used : AttributeProviderContext(org.commonmark.renderer.html.AttributeProviderContext) Context(android.content.Context) PhotoView(com.github.chrisbanes.photoview.PhotoView) LbryFile(com.odysee.app.model.LbryFile) LbryUri(com.odysee.app.utils.LbryUri) Uri(android.net.Uri) File(java.io.File) LbryFile(com.odysee.app.model.LbryFile) SolidIconView(com.odysee.app.ui.controls.SolidIconView) PlayerView(com.google.android.exoplayer2.ui.PlayerView) NestedScrollView(androidx.core.widget.NestedScrollView) AdapterView(android.widget.AdapterView) RecyclerView(androidx.recyclerview.widget.RecyclerView) PhotoView(com.github.chrisbanes.photoview.PhotoView) ImageView(android.widget.ImageView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) Claim(com.odysee.app.model.Claim)

Aggregations

LbryFile (com.odysee.app.model.LbryFile)17 JSONException (org.json.JSONException)11 JSONObject (org.json.JSONObject)9 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 Claim (com.odysee.app.model.Claim)6 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)5 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)5 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 LbryUriException (com.odysee.app.exceptions.LbryUriException)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 FileListTask (com.odysee.app.tasks.file.FileListTask)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 WebView (android.webkit.WebView)2 AdapterView (android.widget.AdapterView)2