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;
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations