Search in sources :

Example 16 with LbryUri

use of com.odysee.app.utils.LbryUri in project odysee-android by OdyseeTeam.

the class ViewHistory method fromClaimWithUrlAndDeviceName.

public static ViewHistory fromClaimWithUrlAndDeviceName(Claim claim, String url, String deviceName) {
    ViewHistory history = new ViewHistory();
    LbryUri uri = LbryUri.tryParse(url);
    if (uri == null) {
        uri = LbryUri.tryParse(claim.getPermanentUrl());
    }
    history.setUri(uri);
    history.setClaimId(claim.getClaimId());
    history.setClaimName(claim.getName());
    history.setTitle(claim.getTitle());
    history.setThumbnailUrl(claim.getThumbnailUrl());
    Claim.GenericMetadata metadata = claim.getValue();
    if (metadata instanceof Claim.StreamMetadata) {
        Claim.StreamMetadata value = (Claim.StreamMetadata) metadata;
        history.setReleaseTime(value.getReleaseTime());
        if (value.getFee() != null) {
            Fee fee = value.getFee();
            history.setCost(new BigDecimal(fee.getAmount()));
            history.setCurrency(fee.getCurrency());
        }
    }
    if (history.getReleaseTime() == 0) {
        history.setReleaseTime(claim.getTimestamp());
    }
    Claim signingChannel = claim.getSigningChannel();
    if (signingChannel != null) {
        history.setPublisherClaimId(signingChannel.getClaimId());
        history.setPublisherName(signingChannel.getName());
        history.setPublisherTitle(signingChannel.getTitle());
    }
    history.setDevice(deviceName);
    return history;
}
Also used : LbryUri(com.odysee.app.utils.LbryUri) BigDecimal(java.math.BigDecimal)

Example 17 with LbryUri

use of com.odysee.app.utils.LbryUri in project odysee-android by OdyseeTeam.

the class FileViewFragment method checkParams.

private void checkParams() {
    boolean updateRequired = false;
    Context context = getContext();
    // claim.setClaimId(FileViewFragmentArgs.fromBundle(getArguments()).getClaimId());
    try {
        Map<String, Object> params = getParams();
        Claim newClaim = null;
        String newUrl = null;
        if (params != null) {
            if (params.containsKey("collection")) {
                handlePlayCollection(params);
                return;
            }
            if (params.containsKey("claim")) {
                newClaim = (Claim) params.get("claim");
                // Only update fragment if new claim is different than currently being played
                if (newClaim != null && !newClaim.equals(this.fileClaim)) {
                    updateRequired = true;
                }
            }
            if (params.containsKey("url")) {
                LbryUri newLbryUri = LbryUri.tryParse(params.get("url").toString());
                if (newLbryUri != null) {
                    newUrl = newLbryUri.toString();
                    String qs = newLbryUri.getQueryString();
                    if (!Helper.isNullOrEmpty(qs)) {
                        String[] qsPairs = qs.split("&");
                        for (String pair : qsPairs) {
                            String[] parts = pair.split("=");
                            if (parts.length < 2) {
                                continue;
                            }
                            if ("comment_hash".equalsIgnoreCase(parts[0])) {
                                commentHash = parts[1];
                                break;
                            }
                        }
                    }
                    if (fileClaim == null || !newUrl.equalsIgnoreCase(currentUrl)) {
                        updateRequired = true;
                    }
                }
            }
        } else if (currentUrl != null) {
            updateRequired = true;
        } else if (context instanceof MainActivity) {
            ((MainActivity) context).onBackPressed();
        }
        boolean invalidRepost = false;
        if (updateRequired) {
            resetViewCount();
            resetFee();
            checkNewClaimAndUrl(newClaim, newUrl);
            // This is required to recycle current fragment with new claim from related content
            fileClaim = null;
            if (newClaim != null) {
                fileClaim = newClaim;
            }
            if (fileClaim == null && !Helper.isNullOrEmpty(newUrl)) {
                // check if the claim is already cached
                currentUrl = newUrl;
                ClaimCacheKey key = new ClaimCacheKey();
                key.setUrl(currentUrl);
                onNewClaim(currentUrl);
                if (Lbry.claimCache.containsKey(key)) {
                    fileClaim = Lbry.claimCache.get(key);
                }
            }
            if (fileClaim != null && Claim.TYPE_REPOST.equalsIgnoreCase(fileClaim.getValueType())) {
                fileClaim = fileClaim.getRepostedClaim();
                if (fileClaim == null || Helper.isNullOrEmpty(fileClaim.getClaimId())) {
                    // Invalid repost, probably
                    invalidRepost = true;
                    renderNothingAtLocation();
                } else if (fileClaim.getName().startsWith("@")) {
                    // this is a reposted channel, so launch the channel url
                    if (context instanceof MainActivity) {
                        MainActivity activity = (MainActivity) context;
                        // activity.onBackPressed(); // remove the reposted url page from the back stack
                        activity.getSupportFragmentManager().popBackStack();
                        activity.openChannelUrl(!Helper.isNullOrEmpty(fileClaim.getShortUrl()) ? fileClaim.getShortUrl() : fileClaim.getPermanentUrl());
                    }
                    return;
                }
            }
            if (fileClaim == null) {
                resolveUrl(currentUrl);
            }
        } else {
            checkAndResetNowPlayingClaim();
        }
        if (!Helper.isNullOrEmpty(currentUrl)) {
            Helper.saveUrlHistory(currentUrl, fileClaim != null ? fileClaim.getTitle() : null, UrlSuggestion.TYPE_FILE);
        }
        if (fileClaim != null && !invalidRepost) {
            if (Claim.TYPE_COLLECTION.equalsIgnoreCase(fileClaim.getValueType()) && fileClaim.getClaimIds() != null && fileClaim.getClaimIds().size() > 0) {
                resolvePlaylistClaimsAndPlayFirst();
                return;
            }
            Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
            if (!Claim.TYPE_COLLECTION.equalsIgnoreCase(actualClaim.getType())) {
                // We don't want to save actual collections to the view history
                Helper.saveViewHistory(currentUrl, actualClaim);
            }
            if (Helper.isClaimBlocked(actualClaim)) {
                renderClaimBlocked();
            } else {
                checkAndLoadRelatedContent();
                checkAndLoadComments();
                renderClaim();
                if (actualClaim.getFile() == null) {
                    loadFile();
                } else {
                // initialFileLoadDone = true;
                }
            }
        }
        checkIsFileComplete();
    } catch (Exception ex) {
        android.util.Log.e(TAG, ex.getMessage(), ex);
    }
}
Also used : AttributeProviderContext(org.commonmark.renderer.html.AttributeProviderContext) Context(android.content.Context) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) MainActivity(com.odysee.app.MainActivity) LbryUri(com.odysee.app.utils.LbryUri) 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 18 with LbryUri

use of com.odysee.app.utils.LbryUri in project odysee-android by OdyseeTeam.

the class LoadSharedUserStateTask method doInBackground.

protected Boolean doInBackground(Void... params) {
    // Get the previous saved state
    try {
        SQLiteDatabase db = null;
        JSONObject result = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_PREFERENCE_GET, Lbry.buildSingleParam("key", KEY), authToken);
        if (result != null) {
            MainActivity activity = null;
            if (context instanceof MainActivity) {
                activity = (MainActivity) context;
                db = activity.getDbHelper().getWritableDatabase();
            }
            // get the built in collections
            Map<String, OdyseeCollection> allCollections = null;
            OdyseeCollection favoritesPlaylist = null;
            OdyseeCollection watchlaterPlaylist = null;
            if (db != null) {
                allCollections = DatabaseHelper.loadAllCollections(db);
                favoritesPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_FAVORITES);
                watchlaterPlaylist = allCollections.get(OdyseeCollection.BUILT_IN_ID_WATCHLATER);
            }
            JSONObject shared = result.getJSONObject("shared");
            if (shared.has("type") && "object".equalsIgnoreCase(shared.getString("type")) && shared.has("value")) {
                JSONObject value = shared.getJSONObject("value");
                JSONArray tags = value.has("tags") && !value.isNull("tags") ? value.getJSONArray("tags") : null;
                JSONArray blocked = value.has("blocked") && !value.isNull("blocked") ? value.getJSONArray("blocked") : null;
                JSONObject builtInCollections = Helper.getJSONObject("builtinCollections", value);
                OdyseeCollection favoritesCollection = OdyseeCollection.fromJSONObject(OdyseeCollection.BUILT_IN_ID_FAVORITES, OdyseeCollection.VISIBILITY_PRIVATE, Helper.getJSONObject(OdyseeCollection.BUILT_IN_ID_FAVORITES, builtInCollections));
                OdyseeCollection watchLaterCollection = OdyseeCollection.fromJSONObject(OdyseeCollection.BUILT_IN_ID_WATCHLATER, OdyseeCollection.VISIBILITY_PRIVATE, Helper.getJSONObject(OdyseeCollection.BUILT_IN_ID_WATCHLATER, builtInCollections));
                if (activity != null) {
                    if (favoritesPlaylist == null || favoritesCollection.getUpdatedAtTimestamp() > favoritesPlaylist.getUpdatedAtTimestamp()) {
                        // only replace the locally saved collections if there are items
                        DatabaseHelper.saveCollection(favoritesCollection, db);
                    }
                    if (watchlaterPlaylist == null || watchLaterCollection.getUpdatedAtTimestamp() > watchlaterPlaylist.getUpdatedAtTimestamp()) {
                        DatabaseHelper.saveCollection(watchLaterCollection, db);
                    }
                }
                JSONObject unpublishedCollections = Helper.getJSONObject("unpublishedCollections", value);
                Iterator<String> pcIdsIterator = unpublishedCollections.keys();
                while (pcIdsIterator.hasNext()) {
                    String collectionId = pcIdsIterator.next();
                    JSONObject jsonCollection = Helper.getJSONObject(collectionId, unpublishedCollections);
                    OdyseeCollection thisCollection = OdyseeCollection.fromJSONObject(collectionId, OdyseeCollection.VISIBILITY_PRIVATE, jsonCollection);
                    boolean shouldSave = true;
                    if (allCollections.containsKey(collectionId)) {
                        OdyseeCollection priorLocalCollection = allCollections.get(collectionId);
                        shouldSave = thisCollection.getUpdatedAtTimestamp() > priorLocalCollection.getUpdatedAtTimestamp();
                    }
                    if (shouldSave) {
                        DatabaseHelper.saveCollection(thisCollection, db);
                    }
                }
                subscriptions = loadSubscriptionsFromSharedUserState(shared);
                if (db != null) {
                    for (Subscription subscription : subscriptions) {
                        try {
                            DatabaseHelper.createOrUpdateSubscription(subscription, db);
                        } catch (IllegalStateException ex) {
                        // pass
                        }
                    }
                }
                if (tags != null) {
                    if (db != null && tags.length() > 0) {
                        try {
                            DatabaseHelper.setAllTagsUnfollowed(db);
                        } catch (IllegalStateException ex) {
                        // pass
                        }
                    }
                    followedTags = new ArrayList<>();
                    for (int i = 0; i < tags.length(); i++) {
                        String tagName = tags.getString(i);
                        Tag tag = new Tag(tagName);
                        tag.setFollowed(true);
                        followedTags.add(tag);
                        try {
                            if (db != null) {
                                DatabaseHelper.createOrUpdateTag(tag, db);
                            }
                        } catch (SQLiteException | IllegalStateException ex) {
                        // pass
                        }
                    }
                }
                if (blocked != null) {
                    blockedChannels = new ArrayList<>();
                    if (db != null) {
                        for (int i = 0; i < blocked.length(); i++) {
                            LbryUri uri = LbryUri.tryParse(blocked.getString(i));
                            if (uri != null) {
                                blockedChannels.add(uri);
                                DatabaseHelper.createOrUpdateBlockedChannel(uri.getClaimId(), uri.getClaimName(), db);
                            }
                        }
                    }
                }
            }
        }
        return true;
    } catch (ApiCallException | JSONException ex) {
        // failed
        error = ex;
    }
    return false;
}
Also used : ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) MainActivity(com.odysee.app.MainActivity) OdyseeCollection(com.odysee.app.model.OdyseeCollection) SQLiteException(android.database.sqlite.SQLiteException) JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Tag(com.odysee.app.model.Tag) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 19 with LbryUri

use of com.odysee.app.utils.LbryUri in project odysee-android by OdyseeTeam.

the class MainActivity method buildDefaultSuggestions.

private List<UrlSuggestion> buildDefaultSuggestions(String text) {
    List<UrlSuggestion> suggestions = new ArrayList<UrlSuggestion>();
    if (LbryUri.PROTO_DEFAULT.equalsIgnoreCase(text)) {
        loadDefaultSuggestionsForBlankUrl();
        return recentUrlHistory != null ? recentUrlHistory : new ArrayList<>();
    }
    // First item is always search
    if (!text.startsWith(LbryUri.PROTO_DEFAULT)) {
        UrlSuggestion searchSuggestion = new UrlSuggestion(UrlSuggestion.TYPE_SEARCH, text);
        suggestions.add(searchSuggestion);
    }
    if (!text.matches(LbryUri.REGEX_INVALID_URI)) {
        boolean isUrlWithScheme = text.startsWith(LbryUri.PROTO_DEFAULT);
        boolean isChannel = text.startsWith("@");
        LbryUri uri = null;
        if (isUrlWithScheme && text.length() > 7) {
            try {
                uri = LbryUri.parse(text);
                isChannel = uri.isChannel();
            } catch (LbryUriException ex) {
            // pass
            }
        }
        if (!isChannel) {
            if (uri == null) {
                uri = new LbryUri();
                uri.setStreamName(text);
            }
            UrlSuggestion fileSuggestion = new UrlSuggestion(UrlSuggestion.TYPE_FILE, text);
            fileSuggestion.setUri(uri);
            suggestions.add(fileSuggestion);
        }
        if (text.indexOf(' ') == -1) {
            // channels should not contain spaces
            if (isChannel) {
                if (uri == null) {
                    uri = new LbryUri();
                    uri.setChannelName(text);
                }
                UrlSuggestion suggestion = new UrlSuggestion(UrlSuggestion.TYPE_CHANNEL, text);
                suggestion.setUri(uri);
                suggestions.add(suggestion);
            }
        }
        if (!isUrlWithScheme && !isChannel) {
            UrlSuggestion suggestion = new UrlSuggestion(UrlSuggestion.TYPE_TAG, text);
            suggestions.add(suggestion);
        }
    }
    return suggestions;
}
Also used : LbryUriException(com.odysee.app.exceptions.LbryUriException) ArrayList(java.util.ArrayList) LbryUri(com.odysee.app.utils.LbryUri) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 20 with LbryUri

use of com.odysee.app.utils.LbryUri in project odysee-android by OdyseeTeam.

the class MainActivity method resolveUrlSuggestions.

private void resolveUrlSuggestions(List<String> urls) {
    ResolveTask task = new ResolveTask(urls, Lbry.API_CONNECTION_STRING, null, new ClaimListResultHandler() {

        @Override
        public void onSuccess(List<Claim> claims) {
            if (findViewById(R.id.url_suggestions_container).getVisibility() == View.VISIBLE) {
                for (int i = 0; i < claims.size(); i++) {
                    // build a simple url from the claim for matching
                    Claim claim = claims.get(i);
                    Claim actualClaim = claim;
                    boolean isRepost = false;
                    if (Claim.TYPE_REPOST.equalsIgnoreCase(claim.getValueType())) {
                        actualClaim = claim.getRepostedClaim();
                        isRepost = true;
                    }
                    if (Helper.isNullOrEmpty(claim.getName())) {
                        continue;
                    }
                    LbryUri simpleUrl = new LbryUri();
                    if (actualClaim.getName().startsWith("@") && !isRepost) {
                        // channel
                        simpleUrl.setChannelName(actualClaim.getName());
                    } else {
                        simpleUrl.setStreamName(claim.getName());
                    }
                    urlSuggestionListAdapter.setClaimForUrl(simpleUrl, actualClaim);
                }
                urlSuggestionListAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onError(Exception error) {
        }
    });
    task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
Also used : ClaimListResultHandler(com.odysee.app.tasks.claim.ClaimListResultHandler) LbryUri(com.odysee.app.utils.LbryUri) Claim(com.odysee.app.model.Claim) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) ExecutionException(java.util.concurrent.ExecutionException) SQLiteException(android.database.sqlite.SQLiteException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) ParseException(java.text.ParseException) ResolveTask(com.odysee.app.tasks.claim.ResolveTask)

Aggregations

LbryUri (com.odysee.app.utils.LbryUri)30 LbryUriException (com.odysee.app.exceptions.LbryUriException)10 ArrayList (java.util.ArrayList)10 JSONException (org.json.JSONException)9 Subscription (com.odysee.app.model.lbryinc.Subscription)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 Claim (com.odysee.app.model.Claim)7 JSONObject (org.json.JSONObject)7 SQLiteException (android.database.sqlite.SQLiteException)6 MainActivity (com.odysee.app.MainActivity)6 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)6 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)6 ExecutionException (java.util.concurrent.ExecutionException)6 SuppressLint (android.annotation.SuppressLint)4 Context (android.content.Context)4 RecyclerView (androidx.recyclerview.widget.RecyclerView)4 Tag (com.odysee.app.model.Tag)4 HashMap (java.util.HashMap)4 AccountManager (android.accounts.AccountManager)3 View (android.view.View)3