Mholloway has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/365085 )

Change subject: Advise users when they are reading an article from an offline 
compilation
......................................................................

Advise users when they are reading an article from an offline compilation

* Shows a toast with the date downloaded upon loading the article
* Substitutes the footer "last modified" HTML with a download date string

Bug: T166652
Change-Id: I7b95f8302240234222293c860b8099f1563daf53
---
M app/src/main/java/org/wikipedia/offline/Compilation.java
M app/src/main/java/org/wikipedia/offline/OfflineManager.java
M app/src/main/java/org/wikipedia/page/Page.java
M app/src/main/java/org/wikipedia/page/PageFragmentLoadState.java
M app/src/main/java/org/wikipedia/page/bottomcontent/BottomContentHandler.java
M app/src/main/java/org/wikipedia/util/DateUtil.java
M app/src/main/res/values-qq/strings.xml
M app/src/main/res/values/strings.xml
M app/src/test/java/org/wikipedia/offline/OfflineManagerTest.java
9 files changed, 88 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/85/365085/1

diff --git a/app/src/main/java/org/wikipedia/offline/Compilation.java 
b/app/src/main/java/org/wikipedia/offline/Compilation.java
index 03d7c9a..ec354dd 100644
--- a/app/src/main/java/org/wikipedia/offline/Compilation.java
+++ b/app/src/main/java/org/wikipedia/offline/Compilation.java
@@ -57,6 +57,10 @@
         return "";
     }
 
+    public long timestamp() {
+        return file.lastModified();
+    }
+
     @NonNull public String description() {
         try {
             return reader.getZimDescription();
diff --git a/app/src/main/java/org/wikipedia/offline/OfflineManager.java 
b/app/src/main/java/org/wikipedia/offline/OfflineManager.java
index 6aeb69b..b6dd4e7 100644
--- a/app/src/main/java/org/wikipedia/offline/OfflineManager.java
+++ b/app/src/main/java/org/wikipedia/offline/OfflineManager.java
@@ -98,11 +98,11 @@
         return null;
     }
 
-    @NonNull public String getHtmlForTitle(@NonNull String title) throws 
IOException {
+    @NonNull public HtmlResult getHtmlForTitle(@NonNull String title) throws 
IOException {
         for (Compilation c : compilations) {
             ByteArrayOutputStream stream = c.getDataForTitle(title);
             if (stream != null) {
-                return stream.toString("utf-8");
+                return new HtmlResult(stream.toString("utf-8"), c.timestamp());
             }
         }
         throw new IOException("Content not found in any compilation for " + 
title);
@@ -135,6 +135,24 @@
         this.compilations = compilations;
     }
 
+    public static class HtmlResult {
+        @NonNull private String html;
+        private long timestamp;
+
+        public HtmlResult(@NonNull String html, long timestamp) {
+            this.html = html;
+            this.timestamp = timestamp;
+        }
+
+        public String html() {
+            return html;
+        }
+
+        public long timestamp() {
+            return timestamp;
+        }
+    }
+
     private OfflineManager() {
     }
 }
diff --git a/app/src/main/java/org/wikipedia/page/Page.java 
b/app/src/main/java/org/wikipedia/page/Page.java
index e3db2fd..a3f9a28 100755
--- a/app/src/main/java/org/wikipedia/page/Page.java
+++ b/app/src/main/java/org/wikipedia/page/Page.java
@@ -6,6 +6,7 @@
 
 import org.wikipedia.settings.RbSwitch;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -18,6 +19,8 @@
     @NonNull private final PageTitle title;
     @NonNull private final List<Section> sections;
     @NonNull private final PageProperties pageProperties;
+
+    @Nullable private Date zimDownloadDate;
 
     /**
      * An indicator what payload version the page content was originally 
retrieved from.
@@ -90,6 +93,18 @@
         return !isMainPage() && getTitle().namespace() == Namespace.MAIN;
     }
 
+    public boolean isFromZimCompilation() {
+        return zimDownloadDate != null;
+    }
+
+    @Nullable public Date getZimDownloadDate() {
+        return zimDownloadDate;
+    }
+
+    public void setZimDownloadDate(@NonNull Date date) {
+        this.zimDownloadDate = date;
+    }
+
     /** For old PHP API */
     public void addRemainingSections(List<Section> remainingSections) {
         sections.addAll(remainingSections);
diff --git a/app/src/main/java/org/wikipedia/page/PageFragmentLoadState.java 
b/app/src/main/java/org/wikipedia/page/PageFragmentLoadState.java
index 6cdd65d..f06ddd0 100644
--- a/app/src/main/java/org/wikipedia/page/PageFragmentLoadState.java
+++ b/app/src/main/java/org/wikipedia/page/PageFragmentLoadState.java
@@ -54,6 +54,7 @@
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -435,9 +436,13 @@
         try {
             Page page = model.getPage();
             sendMarginPayload();
+            OfflineManager.HtmlResult result = OfflineManager.instance()
+                    .getHtmlForTitle(model.getTitle().getDisplayText());
+            Date downloadDate = new Date(result.timestamp());
+            page.setZimDownloadDate(downloadDate);
             JSONObject zimPayload = setLeadSectionMetadata(new JSONObject(), 
page)
-                    .put("zimhtml", 
OfflineManager.instance().getHtmlForTitle(model.getTitle().getDisplayText()))
-                    .put("fromRestBase", false) // TODO: set to true when ZIM 
content comes from MCS
+                    .put("zimhtml", result.html())
+                    .put("fromRestBase", false)
                     .put("offlineContentProvider", 
OfflineContentProvider.getBaseUrl());
 
             if (sectionTargetFromTitle != null) {
@@ -451,9 +456,8 @@
 
             //give it our expected scroll position, in case we need the page 
to be pre-scrolled upon loading.
             zimPayload.put("scrollY", (int) (stagedScrollY / 
DimenUtil.getDensityScalar()));
-
             bridge.sendMessage("displayFromZim", zimPayload);
-
+            showZimCompilationMessage(downloadDate);
         } catch (JSONException e) {
             throw new RuntimeException(e);
         } catch (IOException e) {
@@ -597,6 +601,15 @@
         }
     }
 
+    private void showZimCompilationMessage(@NonNull Date lastModified) {
+        if (fragment.isAdded()) {
+            String dateStr = DateUtil.getShortDateString(lastModified);
+            Toast.makeText(fragment.getContext().getApplicationContext(),
+                    
fragment.getString(R.string.page_offline_notice_zim_download_date, dateStr),
+                    Toast.LENGTH_LONG).show();
+        }
+    }
+
     private JSONObject getRemoteConfig() {
         return app.getRemoteConfig().getConfig();
     }
diff --git 
a/app/src/main/java/org/wikipedia/page/bottomcontent/BottomContentHandler.java 
b/app/src/main/java/org/wikipedia/page/bottomcontent/BottomContentHandler.java
index c663d4f..d7e35ab 100644
--- 
a/app/src/main/java/org/wikipedia/page/bottomcontent/BottomContentHandler.java
+++ 
b/app/src/main/java/org/wikipedia/page/bottomcontent/BottomContentHandler.java
@@ -283,20 +283,35 @@
             pageLastUpdatedText.setVisibility(View.GONE);
         } else {
             PageTitle title = page.getTitle();
-            String lastUpdatedHtml = "<a href=\"" + 
title.getUriForAction("history")
-                    + "\">" + 
parentFragment.getContext().getString(R.string.last_updated_text,
-                    
formatDateRelative(page.getPageProperties().getLastModified())
-                            + "</a>");
+            String dateMessage = getDateMessage(page);
             // TODO: Hide the Talk link if already on a talk page
             PageTitle talkPageTitle = new PageTitle("Talk", 
title.getPrefixedText(), title.getWikiSite());
             String discussionHtml = "<a href=\"" + 
talkPageTitle.getCanonicalUri() + "\">"
                     + 
parentFragment.getContext().getString(R.string.talk_page_link_text) + "</a>";
-            pageLastUpdatedText.setText(StringUtil.fromHtml(lastUpdatedHtml + 
" &mdash; " + discussionHtml));
+            pageLastUpdatedText.setText(StringUtil.fromHtml(dateMessage + " 
&mdash; " + discussionHtml));
             pageLastUpdatedText.setMovementMethod(new 
LinkMovementMethodExt(linkHandler));
             pageLastUpdatedText.setVisibility(View.VISIBLE);
         }
     }
 
+    // Returns an HTML string consisting of the onwiki last modified date for 
network or cached
+    // content, or, for a ZIM compilation, a plain string (nothing to link) 
with the ZIM file's
+    // local last modified date (most likely, the download date).
+    private String getDateMessage(Page page) {
+        return page.isFromZimCompilation() ? zimLastModifiedString(page) : 
lastUpdatedHtml(page);
+    }
+
+    private String zimLastModifiedString(Page page) {
+        return 
parentFragment.getContext().getString(R.string.bottom_content_date_downloaded,
+                formatDateRelative(page.getZimDownloadDate()));
+    }
+
+    private String lastUpdatedHtml(Page page) {
+        return "<a href=\"" + page.getTitle().getUriForAction("history") + 
"\">"
+                + 
parentFragment.getContext().getString(R.string.last_updated_text,
+                    
formatDateRelative(page.getPageProperties().getLastModified()) + "</a>");
+    }
+
     private void preRequestReadMoreItems(final LayoutInflater layoutInflater) {
         if (parentFragment.getPage().isMainPage()) {
             new MainPageReadMoreTopicTask(app) {
diff --git a/app/src/main/java/org/wikipedia/util/DateUtil.java 
b/app/src/main/java/org/wikipedia/util/DateUtil.java
index 06aea45..eeeb892 100644
--- a/app/src/main/java/org/wikipedia/util/DateUtil.java
+++ b/app/src/main/java/org/wikipedia/util/DateUtil.java
@@ -33,6 +33,10 @@
         return getShortDateString(date);
     }
 
+    public static String getShortDateString(long timestamp) {
+        return getShortDateString(new Date(timestamp));
+    }
+
     public static String getShortDateString(@NonNull Date date) {
         // todo: consider allowing TWN date formats. It would be useful to 
have but might be
         //       difficult for translators to write correct format specifiers 
without being able to
diff --git a/app/src/main/res/values-qq/strings.xml 
b/app/src/main/res/values-qq/strings.xml
index 7090ceb..8e0dbb3 100644
--- a/app/src/main/res/values-qq/strings.xml
+++ b/app/src/main/res/values-qq/strings.xml
@@ -44,6 +44,7 @@
   <string name="nearby_zoom_to_location">Small toast (popup) message on Nearby 
screen saying that the current location of the device was not 
available.</string>
   <string name="nearby_osm_license">Copyright message with a link to the 
OpenStreetMap Copyright and License. (Please preserve the anchor tag in its 
exact form.)</string>
   <string name="last_updated_text">Message in footer of article indicating 
when the article was last updated. \'%s\' is substituted with the appropriately 
formatted human-readable timestamp, such as \"seven days ago\".</string>
+  <string name="bottom_content_date_downloaded">Message in footer of article 
from an offline compilation indicating when the file containing the compilation 
was downloaded. \'%s\' is substituted with the appropriately formatted 
human-readable timestamp, such as \"seven days ago\".</string>
   <string name="talk_page_link_text">Link text for accessing Talk pages. Use 
the same word in your language as is done on the web site. Some use the term 
discussions.\n{{Identical|Talk}}</string>
   <string name="content_license_html">HTML specifying license of the Wiki and 
link to appropriate CC page. Please preserve the %1$s parameter, since this 
will be replaced with the actual URL.</string>
   <string name="edit_save_action_license_logged_in">HTML specifying Terms of 
Use and CC license before saving edits. Please preserve the %1$s and %2$s 
parameters, since these will be replaced with the actual URLs.</string>
@@ -238,6 +239,7 @@
   <string name="page_offline_notice_cannot_load_while_offline">Message 
informing the user that an article cannot be loaded while the app is offline. 
This message is followed by page_offline_notice_add_to_reading_list.</string>
   <string name="page_offline_notice_add_to_reading_list">Message inviting the 
user to add the requested article to a reading list to be downloaded when the 
app is online. This message follows 
{{msg-wm|Wikipedia-android-strings-page_offline_notice_cannot_load_while_offline}}.</string>
   <string name="page_offline_notice_last_date">Message that tells the user 
that the current article is loaded from offline storage. The %s symbol is 
replaced with the date when the article was saved.</string>
+  <string name="page_offline_notice_zim_download_date">Message that tells the 
user that the current article is loaded from a downloaded offline compilation. 
The %s symbol is replaced with the date when the compilation was 
downloaded.</string>
   <string name="button_get_directions">Button to obtain directions to the 
location specified in the link preview.</string>
   <string name="error_no_maps_app">Error displayed when the device does not 
have any apps installed that are capable of providing directions to a 
location.</string>
   <string name="preference_title_show_link_previews">Title of the preference 
for enabling or disabling link previews.\n\nShown in the preferences screen 
along with the following:\n* 
{{msg-wikimedia|Wikipedia-android-strings-preference title show images}}\n* 
{{msg-wikimedia|Wikipedia-android-strings-preference title language}}\n* 
{{msg-wikimedia|Wikipedia-android-strings-preference title show link 
previews}}\n* {{msg-wikimedia|Wikipedia-android-strings-preference title 
eventlogging opt in}}\n* {{msg-wikimedia|Wikipedia-android-strings-zero warn 
when leaving}}</string>
diff --git a/app/src/main/res/values/strings.xml 
b/app/src/main/res/values/strings.xml
index 34c6fb3..6cd97f2 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -58,6 +58,7 @@
     <string name="nearby_osm_license"><![CDATA[&copy; &nbsp;<a 
href=\"https://www.openstreetmap.org/copyright/\";>OpenStreetMap</a> 
contributors]]></string>
 
     <string name="last_updated_text">Last updated %s</string>
+    <string name="bottom_content_date_downloaded">Downloaded %s</string>
     <string name="talk_page_link_text">Talk</string>
     <string name="content_license_html"><![CDATA[Content is available under <a 
href=\"%1$s\">CC BY-SA 3.0</a> unless otherwise noted]]></string>
     <string name="edit_save_action_license_logged_in"><![CDATA[By saving, you 
agree to the <a href="%1$s">Terms of Use</a>, and to irrevocably release your 
contributions under the <a href="%2$s">CC BY-SA 3.0</a> license.]]></string>
@@ -253,6 +254,7 @@
     <string name="page_offline_notice_cannot_load_while_offline">Article 
cannot be loaded while offline.</string>
     <string name="page_offline_notice_add_to_reading_list">Add the article to 
a reading list and it will be downloaded once you\'re back online.</string>
     <string name="page_offline_notice_last_date">You are reading an offline 
version of this article saved on %s.</string>
+    <string name="page_offline_notice_zim_download_date">You are reading an 
offline version of this article from a compilation downloaded on %s.</string>
     <string name="button_get_directions">Get directions</string>
     <string name="error_no_maps_app">Could not find any apps that provide 
directions.</string>
     <string name="preference_title_show_link_previews">Show link 
previews</string>
diff --git a/app/src/test/java/org/wikipedia/offline/OfflineManagerTest.java 
b/app/src/test/java/org/wikipedia/offline/OfflineManagerTest.java
index 3e57a6e..b2aff2d 100644
--- a/app/src/test/java/org/wikipedia/offline/OfflineManagerTest.java
+++ b/app/src/test/java/org/wikipedia/offline/OfflineManagerTest.java
@@ -47,9 +47,10 @@
 
     @Test
     public void testOfflineManagerGetDataForTitle() throws Exception {
-        String html = OfflineManager.instance().getHtmlForTitle("Ray Charles");
-        assertThat(html.startsWith("<html>"), is(true));
-        assertThat(html.endsWith("</html>"), is(true));
+        OfflineManager.HtmlResult result = 
OfflineManager.instance().getHtmlForTitle("Ray Charles");
+        assertThat(result.html().startsWith("<html>"), is(true));
+        assertThat(result.html().endsWith("</html>"), is(true));
+        assertThat(result.timestamp() > 0, is(true));
     }
 
     @Before

-- 
To view, visit https://gerrit.wikimedia.org/r/365085
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7b95f8302240234222293c860b8099f1563daf53
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to