Search in sources :

Example 1 with MediaPlayerImplementation

use of org.wikipedia.media.MediaPlayerImplementation in project apps-android-wikipedia by wikimedia.

the class PageFragment method setupMessageHandlers.

private void setupMessageHandlers() {
    linkHandler = new LinkHandler(requireActivity()) {

        @Override
        public void onPageLinkClicked(@NonNull String anchor, @NonNull String linkText) {
            dismissBottomSheet();
            JSONObject payload = new JSONObject();
            try {
                payload.put("anchor", anchor);
                payload.put("text", linkText);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
            bridge.sendMessage("handleReference", payload);
        }

        @Override
        public void onInternalLinkClicked(@NonNull PageTitle title) {
            handleInternalLink(title);
        }

        @Override
        public WikiSite getWikiSite() {
            return model.getTitle().getWikiSite();
        }
    };
    bridge.addListener("linkClicked", linkHandler);
    bridge.addListener("referenceClicked", new ReferenceHandler() {

        @Override
        protected void onReferenceClicked(int selectedIndex, @NonNull List<Reference> adjacentReferences) {
            if (!isAdded()) {
                Log.d("PageFragment", "Detached from activity, so stopping reference click.");
                return;
            }
            showBottomSheet(new ReferenceDialog(requireActivity(), selectedIndex, adjacentReferences, linkHandler));
        }
    });
    bridge.addListener("imageClicked", (String messageType, JSONObject messagePayload) -> {
        try {
            String href = decodeURL(messagePayload.getString("href"));
            if (href.startsWith("/wiki/")) {
                String filename = UriUtil.removeInternalLinkPrefix(href);
                String fileUrl = null;
                // Set the lead image url manually if the filename equals to the lead image file name.
                if (getPage() != null && !TextUtils.isEmpty(getPage().getPageProperties().getLeadImageName())) {
                    String leadImageName = addUnderscores(getPage().getPageProperties().getLeadImageName());
                    String leadImageUrl = getPage().getPageProperties().getLeadImageUrl();
                    if (filename.contains(leadImageName) && leadImageUrl != null) {
                        fileUrl = UriUtil.resolveProtocolRelativeUrl(leadImageUrl);
                    }
                }
                WikiSite wiki = model.getTitle().getWikiSite();
                requireActivity().startActivityForResult(GalleryActivity.newIntent(requireActivity(), model.getTitleOriginal(), filename, fileUrl, wiki, GalleryFunnel.SOURCE_NON_LEAD_IMAGE), Constants.ACTIVITY_REQUEST_GALLERY);
            } else {
                linkHandler.onUrlClick(href, messagePayload.optString("title"), "");
            }
        } catch (JSONException e) {
            L.logRemoteErrorIfProd(e);
        }
    });
    bridge.addListener("mediaClicked", (String messageType, JSONObject messagePayload) -> {
        try {
            String href = decodeURL(messagePayload.getString("href"));
            String filename = StringUtil.removeUnderscores(UriUtil.removeInternalLinkPrefix(href));
            WikiSite wiki = model.getTitle().getWikiSite();
            requireActivity().startActivityForResult(GalleryActivity.newIntent(requireActivity(), model.getTitleOriginal(), filename, wiki, GalleryFunnel.SOURCE_NON_LEAD_IMAGE), Constants.ACTIVITY_REQUEST_GALLERY);
        } catch (JSONException e) {
            L.logRemoteErrorIfProd(e);
        }
    });
    bridge.addListener("pronunciationClicked", (String messageType, JSONObject messagePayload) -> {
        if (avPlayer == null) {
            avPlayer = new DefaultAvPlayer(new MediaPlayerImplementation());
            avPlayer.init();
        }
        if (avCallback == null) {
            avCallback = new AvCallback();
        }
        if (!avPlayer.isPlaying()) {
            updateProgressBar(true, true, 0);
            avPlayer.play(getPage().getTitlePronunciationUrl(), avCallback, avCallback);
        } else {
            updateProgressBar(false, true, 0);
            avPlayer.stop();
        }
    });
}
Also used : MediaPlayerImplementation(org.wikipedia.media.MediaPlayerImplementation) DefaultAvPlayer(org.wikipedia.media.DefaultAvPlayer) JSONException(org.json.JSONException) WikiSite(org.wikipedia.dataclient.WikiSite) SuppressLint(android.annotation.SuppressLint) JSONObject(org.json.JSONObject)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 WikiSite (org.wikipedia.dataclient.WikiSite)1 DefaultAvPlayer (org.wikipedia.media.DefaultAvPlayer)1 MediaPlayerImplementation (org.wikipedia.media.MediaPlayerImplementation)1