Search in sources :

Example 41 with WikiSite

use of org.wikipedia.dataclient.WikiSite in project apps-android-wikipedia by wikimedia.

the class PageActivity method handleIntent.

private void handleIntent(@NonNull Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() != null) {
        WikiSite wiki = new WikiSite(intent.getData());
        PageTitle title = wiki.titleForUri(intent.getData());
        HistoryEntry historyEntry = new HistoryEntry(title, intent.hasExtra(Constants.INTENT_EXTRA_VIEW_FROM_NOTIFICATION) ? HistoryEntry.SOURCE_NOTIFICATION_SYSTEM : HistoryEntry.SOURCE_EXTERNAL_LINK);
        if (intent.hasExtra(Intent.EXTRA_REFERRER)) {
            // Populate the referrer with the externally-referring URL, e.g. an external Browser URL.
            // This can be a Uri or a String, so let's extract it safely as an Object.
            historyEntry.setReferrer(intent.getExtras().get(Intent.EXTRA_REFERRER).toString());
        }
        if (title.isSpecial()) {
            visitInExternalBrowser(this, intent.getData());
            finish();
            return;
        }
        loadPageInForegroundTab(title, historyEntry);
    } else if (ACTION_LOAD_IN_NEW_TAB.equals(intent.getAction()) || ACTION_LOAD_IN_CURRENT_TAB.equals(intent.getAction())) {
        PageTitle title = intent.getParcelableExtra(EXTRA_PAGETITLE);
        HistoryEntry historyEntry = intent.getParcelableExtra(EXTRA_HISTORYENTRY);
        if (ACTION_LOAD_IN_NEW_TAB.equals(intent.getAction())) {
            loadPageInForegroundTab(title, historyEntry);
        } else if (ACTION_LOAD_IN_CURRENT_TAB.equals(intent.getAction())) {
            loadPage(title, historyEntry, TabPosition.CURRENT_TAB);
        }
        if (intent.hasExtra(Constants.INTENT_EXTRA_REVERT_QNUMBER)) {
            showDescriptionEditRevertDialog(intent.getStringExtra(Constants.INTENT_EXTRA_REVERT_QNUMBER));
        }
    } else if (ACTION_LOAD_FROM_EXISTING_TAB.equals(intent.getAction())) {
        PageTitle title = intent.getParcelableExtra(EXTRA_PAGETITLE);
        HistoryEntry historyEntry = intent.getParcelableExtra(EXTRA_HISTORYENTRY);
        loadPage(title, historyEntry, TabPosition.EXISTING_TAB);
    } else if (ACTION_RESUME_READING.equals(intent.getAction()) || intent.hasExtra(Constants.INTENT_APP_SHORTCUT_CONTINUE_READING)) {
    // do nothing, since this will be handled indirectly by PageFragment.
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        PageTitle title = new PageTitle(query, app.getWikiSite());
        HistoryEntry historyEntry = new HistoryEntry(title, HistoryEntry.SOURCE_SEARCH);
        loadPageInForegroundTab(title, historyEntry);
    } else if (intent.hasExtra(Constants.INTENT_FEATURED_ARTICLE_FROM_WIDGET)) {
        new IntentFunnel(app).logFeaturedArticleWidgetTap();
        PageTitle title = intent.getParcelableExtra(EXTRA_PAGETITLE);
        HistoryEntry historyEntry = new HistoryEntry(title, HistoryEntry.SOURCE_WIDGET);
        loadPageInForegroundTab(title, historyEntry);
    } else {
        loadMainPageInCurrentTab();
    }
}
Also used : HistoryEntry(org.wikipedia.history.HistoryEntry) WikiSite(org.wikipedia.dataclient.WikiSite) IntentFunnel(org.wikipedia.analytics.IntentFunnel)

Example 42 with WikiSite

use of org.wikipedia.dataclient.WikiSite 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)

Example 43 with WikiSite

use of org.wikipedia.dataclient.WikiSite in project apps-android-wikipedia by wikimedia.

the class NotificationActivity method deleteItems.

private void deleteItems(List<NotificationListItemContainer> items, boolean markUnread) {
    Map<WikiSite, List<Notification>> notificationsPerWiki = new HashMap<>();
    Long selectionKey = items.size() > 1 ? new Random().nextLong() : null;
    for (NotificationListItemContainer item : items) {
        WikiSite wiki = dbNameMap.containsKey(item.notification.wiki()) ? dbNameMap.get(item.notification.wiki()) : WikipediaApp.getInstance().getWikiSite();
        if (!notificationsPerWiki.containsKey(wiki)) {
            notificationsPerWiki.put(wiki, new ArrayList<>());
        }
        notificationsPerWiki.get(wiki).add(item.notification);
        if (markUnread && !displayArchived) {
            notificationList.add(item.notification);
        } else {
            notificationList.remove(item.notification);
            new NotificationFunnel(WikipediaApp.getInstance(), item.notification).logMarkRead(selectionKey);
        }
    }
    for (WikiSite wiki : notificationsPerWiki.keySet()) {
        if (markUnread) {
            NotificationPollBroadcastReceiver.markRead(wiki, notificationsPerWiki.get(wiki), true);
        } else {
            NotificationPollBroadcastReceiver.markRead(wiki, notificationsPerWiki.get(wiki), false);
            showDeleteItemsUndoSnackbar(items);
        }
    }
    postprocessAndDisplay();
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) NotificationFunnel(org.wikipedia.analytics.NotificationFunnel) List(java.util.List) ArrayList(java.util.ArrayList) WikiSite(org.wikipedia.dataclient.WikiSite)

Example 44 with WikiSite

use of org.wikipedia.dataclient.WikiSite in project apps-android-wikipedia by wikimedia.

the class NotificationPollBroadcastReceiver method markRead.

public static void markRead(@NonNull WikiSite wiki, @NonNull List<Notification> notifications, boolean unread) {
    final String idListStr = TextUtils.join("|", notifications);
    CsrfTokenClient editTokenClient = new CsrfTokenClient(wiki, WikipediaApp.getInstance().getWikiSite());
    editTokenClient.request(new CsrfTokenClient.DefaultCallback() {

        @SuppressLint("CheckResult")
        @Override
        public void success(@NonNull String token) {
            ServiceFactory.get(wiki).markRead(token, unread ? null : idListStr, unread ? idListStr : null).subscribeOn(Schedulers.io()).subscribe(response -> {
            }, L::e);
        }
    });
}
Also used : Context(android.content.Context) ServiceFactory(org.wikipedia.dataclient.ServiceFactory) AccountUtil(org.wikipedia.auth.AccountUtil) AlarmManager(android.app.AlarmManager) R(org.wikipedia.R) Intent(android.content.Intent) SystemClock(android.os.SystemClock) TextUtils(android.text.TextUtils) CsrfTokenClient(org.wikipedia.csrf.CsrfTokenClient) HashMap(java.util.HashMap) PendingIntent(android.app.PendingIntent) NonNull(android.support.annotation.NonNull) BroadcastReceiver(android.content.BroadcastReceiver) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) WikiSite(org.wikipedia.dataclient.WikiSite) Prefs(org.wikipedia.settings.Prefs) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) SuppressLint(android.annotation.SuppressLint) List(java.util.List) WikipediaApp(org.wikipedia.WikipediaApp) Map(java.util.Map) L(org.wikipedia.util.log.L) Schedulers(io.reactivex.schedulers.Schedulers) CsrfTokenClient(org.wikipedia.csrf.CsrfTokenClient) SuppressLint(android.annotation.SuppressLint)

Example 45 with WikiSite

use of org.wikipedia.dataclient.WikiSite in project apps-android-wikipedia by wikimedia.

the class NotificationPollBroadcastReceiver method markItemsAsRead.

private void markItemsAsRead(List<Notification> items) {
    Map<WikiSite, List<Notification>> notificationsPerWiki = new HashMap<>();
    for (Notification item : items) {
        WikiSite wiki = dbNameWikiSiteMap.containsKey(item.wiki()) ? dbNameWikiSiteMap.get(item.wiki()) : WikipediaApp.getInstance().getWikiSite();
        if (!notificationsPerWiki.containsKey(wiki)) {
            notificationsPerWiki.put(wiki, new ArrayList<>());
        }
        notificationsPerWiki.get(wiki).add(item);
    }
    for (WikiSite wiki : notificationsPerWiki.keySet()) {
        markRead(wiki, notificationsPerWiki.get(wiki), false);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) WikiSite(org.wikipedia.dataclient.WikiSite)

Aggregations

WikiSite (org.wikipedia.dataclient.WikiSite)46 Test (org.junit.Test)23 PageTitle (org.wikipedia.page.PageTitle)13 ArrayList (java.util.ArrayList)7 HistoryEntry (org.wikipedia.history.HistoryEntry)6 List (java.util.List)5 SuppressLint (android.annotation.SuppressLint)4 NonNull (android.support.annotation.NonNull)4 View (android.view.View)4 Date (java.util.Date)4 Uri (android.net.Uri)3 HashMap (java.util.HashMap)3 PageProperties (org.wikipedia.page.PageProperties)3 Context (android.content.Context)2 Bundle (android.os.Bundle)2 RecyclerView (android.support.v7.widget.RecyclerView)2 BindView (butterknife.BindView)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Schedulers (io.reactivex.schedulers.Schedulers)2 JSONException (org.json.JSONException)2