Search in sources :

Example 6 with LbryUriException

use of com.odysee.app.exceptions.LbryUriException in project odysee-android by OdyseeTeam.

the class ChannelFragment method onCreateView.

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_channel, container, false);
    layoutLoadingState = root.findViewById(R.id.channel_view_loading_state);
    layoutNothingAtLocation = root.findViewById(R.id.container_nothing_at_location);
    layoutDisplayArea = root.findViewById(R.id.channel_view_claim_display_area);
    layoutResolving = root.findViewById(R.id.channel_view_loading_container);
    imageCover = root.findViewById(R.id.channel_view_cover_image);
    imageThumbnail = root.findViewById(R.id.channel_view_thumbnail);
    noThumbnailView = root.findViewById(R.id.channel_view_no_thumbnail);
    textAlpha = root.findViewById(R.id.channel_view_icon_alpha);
    textTitle = root.findViewById(R.id.channel_view_title);
    textFollowerCount = root.findViewById(R.id.channel_view_follower_count);
    buttonEdit = root.findViewById(R.id.channel_view_edit);
    buttonDelete = root.findViewById(R.id.channel_view_delete);
    buttonShare = root.findViewById(R.id.channel_view_share);
    buttonTip = root.findViewById(R.id.channel_view_tip);
    buttonReport = root.findViewById(R.id.channel_view_report);
    buttonFollowUnfollow = root.findViewById(R.id.channel_view_follow_unfollow);
    textFollow = root.findViewById(R.id.channel_view_text_follow);
    iconFollow = root.findViewById(R.id.channel_view_icon_follow);
    iconUnfollow = root.findViewById(R.id.channel_view_icon_unfollow);
    buttonBell = root.findViewById(R.id.channel_view_subscribe_notify);
    iconBell = root.findViewById(R.id.channel_view_icon_bell);
    blockUnblock = root.findViewById(R.id.channel_view_block_unblock);
    blockUnblockText = root.findViewById(R.id.channel_view_block_unblock_text);
    blockUnblock.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            boolean blocked = Lbryio.isChannelBlocked(claim);
            Context context = getContext();
            if (context instanceof MainActivity) {
                if (blocked) {
                    // handle unblock
                    ((MainActivity) context).handleUnblockChannel(claim);
                } else {
                    ((MainActivity) context).handleBlockChannel(claim);
                }
            }
        }
    });
    tabPager = root.findViewById(R.id.channel_view_pager);
    tabLayout = root.findViewById(R.id.channel_view_tabs);
    tabPager.setSaveEnabled(false);
    buttonEdit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (claim != null) {
                Context context = getContext();
                if (context instanceof MainActivity) {
                    ((MainActivity) context).openChannelForm(claim);
                }
            }
        }
    });
    buttonDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (claim != null) {
                Context c = getContext();
                if (c != null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(c).setTitle(R.string.delete_channel).setMessage(R.string.confirm_delete_channel).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            deleteCurrentClaim();
                        }
                    }).setNegativeButton(R.string.no, null);
                    builder.show();
                }
            }
        }
    });
    buttonShare.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (claim != null) {
                try {
                    String shareUrl = LbryUri.parse(!Helper.isNullOrEmpty(claim.getCanonicalUrl()) ? claim.getCanonicalUrl() : (!Helper.isNullOrEmpty(claim.getShortUrl()) ? claim.getShortUrl() : claim.getPermanentUrl())).toOdyseeString();
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.setType("text/plain");
                    shareIntent.putExtra(Intent.EXTRA_TEXT, shareUrl);
                    MainActivity.startingShareActivity = true;
                    Intent shareUrlIntent = Intent.createChooser(shareIntent, getString(R.string.share_lbry_content));
                    shareUrlIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(shareUrlIntent);
                } catch (LbryUriException ex) {
                // pass
                }
            }
        }
    });
    buttonTip.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            MainActivity activity = (MainActivity) getActivity();
            if (activity != null && activity.isSignedIn()) {
                if (claim != null) {
                    CreateSupportDialogFragment dialog = CreateSupportDialogFragment.newInstance(claim, (amount, isTip) -> {
                        double sentAmount = amount.doubleValue();
                        View view1 = getView();
                        if (view1 != null) {
                            String message = getResources().getQuantityString(isTip ? R.plurals.you_sent_a_tip : R.plurals.you_sent_a_support, sentAmount == 1.0 ? 1 : 2, new DecimalFormat("#,###.##").format(sentAmount));
                            Snackbar.make(view1, message, Snackbar.LENGTH_LONG).show();
                        }
                    });
                    Context context = getContext();
                    if (context instanceof MainActivity) {
                        dialog.show(((MainActivity) context).getSupportFragmentManager(), CreateSupportDialogFragment.TAG);
                    }
                }
            } else {
                if (activity != null) {
                    activity.simpleSignIn(0);
                }
            }
        }
    });
    buttonReport.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (claim != null) {
                Context context = getContext();
                CustomTabColorSchemeParams.Builder ctcspb = new CustomTabColorSchemeParams.Builder();
                ctcspb.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
                CustomTabColorSchemeParams ctcsp = ctcspb.build();
                CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder().setDefaultColorSchemeParams(ctcsp);
                CustomTabsIntent intent = builder.build();
                intent.launchUrl(context, Uri.parse(String.format("https://odysee.com/$/report_content?claimId=%s", claim.getClaimId())));
            }
        }
    });
    buttonBell.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (claim != null) {
                boolean isNotificationsDisabled = Lbryio.isNotificationsDisabled(claim);
                final Subscription subscription = Subscription.fromClaim(claim);
                subscription.setNotificationsDisabled(!isNotificationsDisabled);
                view.setEnabled(false);
                Context context = getContext();
                new ChannelSubscribeTask(context, claim.getClaimId(), subscription, false, new ChannelSubscribeTask.ChannelSubscribeHandler() {

                    @Override
                    public void onSuccess() {
                        view.setEnabled(true);
                        Lbryio.updateSubscriptionNotificationsDisabled(subscription);
                        Context context = getContext();
                        if (context instanceof MainActivity) {
                            ((MainActivity) context).showMessage(subscription.isNotificationsDisabled() ? R.string.receive_no_notifications : R.string.receive_all_notifications);
                        }
                        checkIsFollowing();
                        if (context != null) {
                            context.sendBroadcast(new Intent(MainActivity.ACTION_SAVE_SHARED_USER_STATE));
                        }
                    }

                    @Override
                    public void onError(Exception exception) {
                        view.setEnabled(true);
                    }
                }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        }
    });
    buttonFollowUnfollow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            MainActivity activity = (MainActivity) getActivity();
            if (activity != null && activity.isSignedIn()) {
                if (claim != null) {
                    if (subscribing) {
                        return;
                    }
                    boolean isFollowing = Lbryio.isFollowing(claim);
                    if (isFollowing) {
                        Context context = getContext();
                        if (context != null) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(R.string.confirm_unfollow).setMessage(R.string.confirm_unfollow_message).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    doFollowUnfollow(isFollowing, view);
                                }
                            }).setNegativeButton(R.string.no, null);
                            builder.show();
                        }
                    } else {
                        doFollowUnfollow(isFollowing, view);
                    }
                }
            } else {
                if (activity != null) {
                    activity.simpleSignIn(0);
                }
            }
        }
    });
    return root;
}
Also used : Context(android.content.Context) AlertDialog(androidx.appcompat.app.AlertDialog) Bundle(android.os.Bundle) SneakyThrows(lombok.SneakyThrows) NonNull(androidx.annotation.NonNull) ChannelSubscribeTask(com.odysee.app.tasks.lbryinc.ChannelSubscribeTask) Uri(android.net.Uri) ImageView(android.widget.ImageView) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) FetchChannelsListener(com.odysee.app.listener.FetchChannelsListener) Handler(android.os.Handler) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) CreateSupportDialogFragment(com.odysee.app.dialog.CreateSupportDialogFragment) ContextCompat(androidx.core.content.ContextCompat) AsyncTask(android.os.AsyncTask) TabLayout(com.google.android.material.tabs.TabLayout) AbandonHandler(com.odysee.app.tasks.claim.AbandonHandler) Helper(com.odysee.app.utils.Helper) Claim(com.odysee.app.model.Claim) ViewGroup(android.view.ViewGroup) Lbryio(com.odysee.app.utils.Lbryio) List(java.util.List) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) TextView(android.widget.TextView) BaseFragment(com.odysee.app.ui.BaseFragment) Subscription(com.odysee.app.model.lbryinc.Subscription) Snackbar(com.google.android.material.snackbar.Snackbar) Context(android.content.Context) AlertDialog(androidx.appcompat.app.AlertDialog) ResolveTask(com.odysee.app.tasks.claim.ResolveTask) UrlSuggestion(com.odysee.app.model.UrlSuggestion) ClaimListResultHandler(com.odysee.app.tasks.claim.ClaimListResultHandler) Lbry(com.odysee.app.utils.Lbry) SolidIconView(com.odysee.app.ui.controls.SolidIconView) Intent(android.content.Intent) ViewPager2(androidx.viewpager2.widget.ViewPager2) LbryUriException(com.odysee.app.exceptions.LbryUriException) NumberFormat(java.text.NumberFormat) FragmentActivity(androidx.fragment.app.FragmentActivity) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) MainActivity(com.odysee.app.MainActivity) OutlineIconView(com.odysee.app.ui.controls.OutlineIconView) LbryUri(com.odysee.app.utils.LbryUri) FollowingFragment(com.odysee.app.ui.findcontent.FollowingFragment) DialogInterface(android.content.DialogInterface) CustomTabColorSchemeParams(androidx.browser.customtabs.CustomTabColorSchemeParams) RequestOptions(com.bumptech.glide.request.RequestOptions) LayoutInflater(android.view.LayoutInflater) DecimalFormat(java.text.DecimalFormat) FetchStatCountTask(com.odysee.app.tasks.lbryinc.FetchStatCountTask) Color(android.graphics.Color) Glide(com.bumptech.glide.Glide) LbryAnalytics(com.odysee.app.utils.LbryAnalytics) Collections(java.util.Collections) AbandonChannelTask(com.odysee.app.tasks.claim.AbandonChannelTask) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) R(com.odysee.app.R) DialogInterface(android.content.DialogInterface) CustomTabColorSchemeParams(androidx.browser.customtabs.CustomTabColorSchemeParams) DecimalFormat(java.text.DecimalFormat) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) Intent(android.content.Intent) MainActivity(com.odysee.app.MainActivity) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) SolidIconView(com.odysee.app.ui.controls.SolidIconView) OutlineIconView(com.odysee.app.ui.controls.OutlineIconView) LbryUriException(com.odysee.app.exceptions.LbryUriException) CreateSupportDialogFragment(com.odysee.app.dialog.CreateSupportDialogFragment) CustomTabsIntent(androidx.browser.customtabs.CustomTabsIntent) LbryUriException(com.odysee.app.exceptions.LbryUriException) ChannelSubscribeTask(com.odysee.app.tasks.lbryinc.ChannelSubscribeTask) Subscription(com.odysee.app.model.lbryinc.Subscription)

Example 7 with LbryUriException

use of com.odysee.app.exceptions.LbryUriException in project odysee-android by OdyseeTeam.

the class FollowingFragment method buildChannelIdsAndUrls.

private void buildChannelIdsAndUrls() {
    channelIds = new ArrayList<>();
    channelUrls = new ArrayList<>();
    if (subscriptionsList != null) {
        for (Subscription subscription : subscriptionsList) {
            try {
                String url = subscription.getUrl();
                LbryUri uri = LbryUri.parse(url);
                String claimId = uri.getClaimId();
                if (Helper.isNullOrEmpty(claimId) || Helper.isNullOrEmpty(url)) {
                    // don't add null / empty claim IDs or URLs
                    continue;
                }
                channelIds.add(claimId);
                channelUrls.add(url);
            } catch (LbryUriException ex) {
            // pass
            }
        }
    }
    excludeChannelIdsForDiscover = new ArrayList<>(channelIds);
}
Also used : LbryUriException(com.odysee.app.exceptions.LbryUriException) Subscription(com.odysee.app.model.lbryinc.Subscription) LbryUri(com.odysee.app.utils.LbryUri)

Example 8 with LbryUriException

use of com.odysee.app.exceptions.LbryUriException 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 9 with LbryUriException

use of com.odysee.app.exceptions.LbryUriException in project odysee-android by OdyseeTeam.

the class MainActivity method launchSearch.

/*
    private void setupUriBar() {
        findViewById(R.id.wunderbar_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearWunderbarFocus(view);
            }
        });


        EditText wunderbar = findViewById(R.id.wunderbar);
        wunderbar.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    hideNotifications();
                    findViewById(R.id.wunderbar_notifications).setVisibility(View.INVISIBLE);

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(view, 0);
                } else {
                    findViewById(R.id.wunderbar_notifications).setVisibility(View.VISIBLE);
                }

                if (canShowUrlSuggestions()) {
                    toggleUrlSuggestions(hasFocus);
                    if (hasFocus && Helper.isNullOrEmpty(Helper.getValue(((EditText) view).getText()))) {
                        displayUrlSuggestionsForNoInput();
                    }
                }
            }
        });

        wunderbar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence != null && canShowUrlSuggestions()) {
                    handleUriInputChanged(charSequence.toString().trim());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        wunderbar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    String input = Helper.getValue(wunderbar.getText());
                    boolean handled = false;
                    if (input.startsWith(LbryUri.PROTO_DEFAULT) && !input.equalsIgnoreCase(LbryUri.PROTO_DEFAULT)) {
                        try {
                            LbryUri uri = LbryUri.parse(input);
                            if (uri.isChannel()) {
                                openChannelUrl(uri.toString());
                                clearWunderbarFocus(wunderbar);
                                handled = true;
                            } else {
                                openFileUrl(uri.toString());
                                clearWunderbarFocus(wunderbar);
                                handled = true;
                            }
                        } catch (LbryUriException ex) {
                            // pass
                        }
                    }
                    if (!handled) {
                        // search
                        launchSearch(input);
                        clearWunderbarFocus(wunderbar);
                    }

                    return true;
                }

                return false;
            }
        });


        urlSuggestionListAdapter = new UrlSuggestionListAdapter(this);
        urlSuggestionListAdapter.setListener(new UrlSuggestionListAdapter.UrlSuggestionClickListener() {
            @Override
            public void onUrlSuggestionClicked(UrlSuggestion urlSuggestion) {
                switch (urlSuggestion.getType()) {
                    case UrlSuggestion.TYPE_CHANNEL:
                        // open channel page
                        if (urlSuggestion.getClaim() != null) {
                            openChannelClaim(urlSuggestion.getClaim());
                        } else {
                            openChannelUrl(urlSuggestion.getUri().toString());
                        }
                        break;
                    case UrlSuggestion.TYPE_FILE:
                        if (urlSuggestion.getClaim() != null) {
                            openFileClaim(urlSuggestion.getClaim());
                        } else {
                            openFileUrl(urlSuggestion.getUri().toString());
                        }
                        break;
                    case UrlSuggestion.TYPE_SEARCH:
                        launchSearch(urlSuggestion.getText());
                        break;
                    case UrlSuggestion.TYPE_TAG:
                        // open tag page
                        openAllContentFragmentWithTag(urlSuggestion.getText());
                        break;
                }
                clearWunderbarFocus(findViewById(R.id.wunderbar));
            }
        });

        RecyclerView urlSuggestionList = findViewById(R.id.url_suggestions);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        urlSuggestionList.setLayoutManager(llm);
        urlSuggestionList.setAdapter(urlSuggestionListAdapter);


    }

    public void clearWunderbarFocus(View view) {
        findViewById(R.id.wunderbar).clearFocus();
        findViewById(R.id.appbar).requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    public View getWunderbar() {
        return findViewById(R.id.wunderbar);
    }
*/
private void launchSearch(String text) {
    Fragment currentFragment = getCurrentFragment();
    if (currentFragment instanceof SearchFragment) {
        ((SearchFragment) currentFragment).search(text, 0);
    } else {
        try {
            SearchFragment fragment = SearchFragment.class.newInstance();
            fragment.setCurrentQuery(text);
            openFragment(fragment, true);
        } catch (Exception ex) {
        // pass
        }
    }
}
Also used : SearchFragment(com.odysee.app.ui.findcontent.SearchFragment) FileViewFragment(com.odysee.app.ui.findcontent.FileViewFragment) DialogFragment(androidx.fragment.app.DialogFragment) SettingsFragment(com.odysee.app.ui.other.SettingsFragment) FollowingFragment(com.odysee.app.ui.findcontent.FollowingFragment) LibraryFragment(com.odysee.app.ui.library.LibraryFragment) ContentScopeDialogFragment(com.odysee.app.dialog.ContentScopeDialogFragment) SearchFragment(com.odysee.app.ui.findcontent.SearchFragment) Fragment(androidx.fragment.app.Fragment) PublishFragment(com.odysee.app.ui.publish.PublishFragment) InvitesFragment(com.odysee.app.ui.wallet.InvitesFragment) PlaylistFragment(com.odysee.app.ui.library.PlaylistFragment) RewardsFragment(com.odysee.app.ui.wallet.RewardsFragment) WalletFragment(com.odysee.app.ui.wallet.WalletFragment) BaseFragment(com.odysee.app.ui.BaseFragment) AddToListsDialogFragment(com.odysee.app.dialog.AddToListsDialogFragment) AllContentFragment(com.odysee.app.ui.findcontent.AllContentFragment) PublishesFragment(com.odysee.app.ui.publish.PublishesFragment) 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)

Example 10 with LbryUriException

use of com.odysee.app.exceptions.LbryUriException in project odysee-android by OdyseeTeam.

the class LbryUri method parse.

public static LbryUri parse(String url, boolean requireProto) throws LbryUriException {
    Pattern componentsPattern = Pattern.compile(String.format("%s%s%s%s(/?)%s%s", REGEX_PART_PROTOCOL, REGEX_PART_HOST, REGEX_PART_STREAM_OR_CHANNEL_NAME, REGEX_PART_MODIFIER_SEPARATOR, REGEX_PART_STREAM_OR_CHANNEL_NAME, REGEX_PART_MODIFIER_SEPARATOR));
    String cleanUrl = url, queryString = null;
    if (Helper.isNullOrEmpty(url)) {
        throw new LbryUriException("Invalid url parameter.");
    }
    Matcher qsMatcher = PATTERN_SEPARATE_QUERY_STRING.matcher(url);
    if (qsMatcher.matches()) {
        queryString = qsMatcher.group(2);
        cleanUrl = !Helper.isNullOrEmpty(queryString) ? url.substring(0, url.indexOf(queryString)) : url;
        if (queryString != null && queryString.length() > 0) {
            queryString = queryString.substring(1);
        }
    }
    List<String> components = new ArrayList<>();
    Matcher matcher = componentsPattern.matcher(cleanUrl);
    if (matcher.matches()) {
        // Note: For Java regex, group index 0 is always the full match
        for (int i = 1; i <= matcher.groupCount(); i++) {
            components.add(matcher.group(i));
        }
    }
    if (components.size() == 0) {
        throw new LbryUriException("Regular expression error occurred while trying to parse the value");
    }
    // components[8] = secondaryModValue
    if (requireProto && Helper.isNullOrEmpty(components.get(0))) {
        throw new LbryUriException("LBRY URLs must include a protocol prefix (lbry://).");
    }
    if (Helper.isNullOrEmpty(components.get(2))) {
        throw new LbryUriException("URL does not include name.");
    }
    for (String component : components.subList(2, components.size())) {
        if (component.indexOf(' ') > -1) {
            throw new LbryUriException("URL cannot include a space.");
        }
    }
    String streamOrChannelName = components.get(2);
    String primaryModSeparator = components.get(3);
    String primaryModValue = components.get(4);
    String possibleStreamName = components.get(6);
    String secondaryModSeparator = components.get(7);
    String secondaryModValue = components.get(8);
    boolean includesChannel = streamOrChannelName.startsWith("@");
    boolean isChannel = includesChannel && Helper.isNullOrEmpty(possibleStreamName);
    String channelName = includesChannel && streamOrChannelName.length() > 1 ? streamOrChannelName.substring(1) : null;
    if (includesChannel) {
        if (Helper.isNullOrEmpty(channelName)) {
            throw new LbryUriException("No channel name after @.");
        }
        if (channelName.length() < CHANNEL_NAME_MIN_LENGTH) {
            throw new LbryUriException(String.format("Channel names must be at least %d character long.", CHANNEL_NAME_MIN_LENGTH));
        }
    }
    UriModifier primaryMod = null, secondaryMod = null;
    if (!Helper.isNullOrEmpty(primaryModSeparator) && !Helper.isNullOrEmpty(primaryModValue)) {
        primaryMod = UriModifier.parse(primaryModSeparator, primaryModValue);
    }
    if (!Helper.isNullOrEmpty(secondaryModSeparator) && !Helper.isNullOrEmpty(secondaryModValue)) {
        secondaryMod = UriModifier.parse(secondaryModSeparator, secondaryModValue);
    }
    String streamName = includesChannel ? possibleStreamName : streamOrChannelName;
    String streamClaimId = (includesChannel && secondaryMod != null) ? secondaryMod.getClaimId() : primaryMod != null ? primaryMod.getClaimId() : null;
    String channelClaimId = (includesChannel && primaryMod != null) ? primaryMod.getClaimId() : null;
    LbryUri uri = new LbryUri();
    uri.setChannel(isChannel);
    uri.setPath(Helper.join(components.subList(2, components.size()), ""));
    uri.setStreamName(streamName);
    uri.setStreamClaimId(streamClaimId);
    uri.setChannelName(channelName);
    uri.setChannelClaimId(channelClaimId);
    uri.setPrimaryClaimSequence(primaryMod != null ? primaryMod.getClaimSequence() : -1);
    uri.setSecondaryClaimSequence(secondaryMod != null ? secondaryMod.getClaimSequence() : -1);
    uri.setPrimaryBidPosition(primaryMod != null ? primaryMod.getBidPosition() : -1);
    uri.setSecondaryBidPosition(secondaryMod != null ? secondaryMod.getBidPosition() : -1);
    // Values that will not work properly with canonical urls
    uri.setClaimName(streamOrChannelName);
    uri.setClaimId(primaryMod != null ? primaryMod.getClaimId() : null);
    uri.setContentName(streamName);
    uri.setQueryString(queryString);
    return uri;
}
Also used : Pattern(java.util.regex.Pattern) LbryUriException(com.odysee.app.exceptions.LbryUriException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Aggregations

LbryUriException (com.odysee.app.exceptions.LbryUriException)10 LbryUri (com.odysee.app.utils.LbryUri)8 Subscription (com.odysee.app.model.lbryinc.Subscription)6 ArrayList (java.util.ArrayList)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 SQLiteException (android.database.sqlite.SQLiteException)3 Uri (android.net.Uri)3 MainActivity (com.odysee.app.MainActivity)3 ApiCallException (com.odysee.app.exceptions.ApiCallException)3 UrlSuggestion (com.odysee.app.model.UrlSuggestion)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 Intent (android.content.Intent)2 Color (android.graphics.Color)2 AsyncTask (android.os.AsyncTask)2 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2