mobile/android/base/background/announcements/AnnouncementsFetcher.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.announcements;
michael@0 6
michael@0 7 import java.io.UnsupportedEncodingException;
michael@0 8 import java.net.URI;
michael@0 9 import java.net.URISyntaxException;
michael@0 10 import java.net.URLEncoder;
michael@0 11
michael@0 12 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 13 import org.mozilla.gecko.background.common.log.Logger;
michael@0 14 import org.mozilla.gecko.sync.net.BaseResource;
michael@0 15
michael@0 16 public class AnnouncementsFetcher {
michael@0 17 private static final String LOG_TAG = "AnnounceFetch";
michael@0 18 private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
michael@0 19
michael@0 20 public static URI getSnippetURI(String base, String channel,
michael@0 21 String version, String platform,
michael@0 22 int idleDays)
michael@0 23 throws URISyntaxException {
michael@0 24 try {
michael@0 25 final String c = URLEncoder.encode(channel, "UTF-8");
michael@0 26 final String v = URLEncoder.encode(version, "UTF-8");
michael@0 27 final String p = URLEncoder.encode(platform, "UTF-8");
michael@0 28 final String s = base + c + "/" + v + "/" + p + ((idleDays == -1) ? "" : ("?idle=" + idleDays));
michael@0 29 return new URI(s);
michael@0 30 } catch (UnsupportedEncodingException e) {
michael@0 31 // Nonsense.
michael@0 32 return null;
michael@0 33 }
michael@0 34 }
michael@0 35
michael@0 36 public static URI getAnnounceURI(final String baseURL, final long lastLaunch) throws URISyntaxException {
michael@0 37 final String channel = getChannel();
michael@0 38 final String version = getVersion();
michael@0 39 final String platform = getPlatform();
michael@0 40 final int idleDays = getIdleDays(lastLaunch);
michael@0 41
michael@0 42 Logger.debug(LOG_TAG, "Fetch URI: idle for " + idleDays + " days.");
michael@0 43 return getSnippetURI(baseURL, channel, version, platform, idleDays);
michael@0 44 }
michael@0 45
michael@0 46 protected static String getChannel() {
michael@0 47 return AnnouncementsConstants.ANNOUNCE_CHANNEL;
michael@0 48 }
michael@0 49
michael@0 50 protected static String getVersion() {
michael@0 51 return GlobalConstants.MOZ_APP_VERSION;
michael@0 52 }
michael@0 53
michael@0 54 protected static String getPlatform() {
michael@0 55 return GlobalConstants.ANDROID_CPU_ARCH;
michael@0 56 }
michael@0 57
michael@0 58 /**
michael@0 59 * Return the number of days that we've been idle, assuming that we have a
michael@0 60 * sane last launch time and the current time is within range. If no sane idle
michael@0 61 * time can be returned, we return -1.
michael@0 62 *
michael@0 63 * @param lastLaunch
michael@0 64 * Time at which the browser was last launched, in milliseconds since epoch.
michael@0 65 * @param now
michael@0 66 * Milliseconds since epoch for which idle time should be calculated.
michael@0 67 * @return number of idle days, or -1 if out of range.
michael@0 68 */
michael@0 69 protected static int getIdleDays(final long lastLaunch, final long now) {
michael@0 70 if (lastLaunch <= 0) {
michael@0 71 return -1;
michael@0 72 }
michael@0 73
michael@0 74 if (now < GlobalConstants.BUILD_TIMESTAMP_MSEC) {
michael@0 75 Logger.warn(LOG_TAG, "Current time " + now + " earlier than build date. Not calculating idle.");
michael@0 76 return -1;
michael@0 77 }
michael@0 78
michael@0 79 if (now < lastLaunch) {
michael@0 80 Logger.warn(LOG_TAG, "Current time " + now + " earlier than last launch! Not calculating idle.");
michael@0 81 return -1;
michael@0 82 }
michael@0 83
michael@0 84 final long idleMillis = now - lastLaunch;
michael@0 85 final int idleDays = (int) (idleMillis / MILLISECONDS_PER_DAY);
michael@0 86
michael@0 87 if (idleDays > AnnouncementsConstants.MAX_SANE_IDLE_DAYS) {
michael@0 88 Logger.warn(LOG_TAG, "Idle from " + lastLaunch + " until " + now +
michael@0 89 ", which is insane. Not calculating idle.");
michael@0 90 return -1;
michael@0 91 }
michael@0 92
michael@0 93 return idleDays;
michael@0 94 }
michael@0 95
michael@0 96 /**
michael@0 97 * Return the number of days that we've been idle, assuming that we have a
michael@0 98 * sane last launch time and the current time is within range. If no sane idle
michael@0 99 * time can be returned, we return -1.
michael@0 100 * The current time will be calculated from {@link System#currentTimeMillis()}.
michael@0 101 *
michael@0 102 * @param lastLaunch
michael@0 103 * Unix timestamp at which the browser was last launched.
michael@0 104 * @return number of idle days, or -1 if out of range.
michael@0 105 */
michael@0 106 protected static int getIdleDays(final long lastLaunch) {
michael@0 107 final long now = System.currentTimeMillis();
michael@0 108 return getIdleDays(lastLaunch, now);
michael@0 109 }
michael@0 110
michael@0 111 public static void fetchAnnouncements(URI uri, AnnouncementsFetchDelegate delegate) {
michael@0 112 BaseResource r = new BaseResource(uri);
michael@0 113 r.delegate = new AnnouncementsFetchResourceDelegate(r, delegate);
michael@0 114 r.getBlocking();
michael@0 115 }
michael@0 116
michael@0 117 /**
michael@0 118 * Synchronous.
michael@0 119 */
michael@0 120 public static void fetchAndProcessAnnouncements(long lastLaunch,
michael@0 121 AnnouncementsFetchDelegate delegate) {
michael@0 122 final long now = System.currentTimeMillis();
michael@0 123 Logger.debug(LOG_TAG, "Fetching announcements. Last launch: " + lastLaunch + "; now: " + now);
michael@0 124 try {
michael@0 125 final String base = delegate.getServiceURL();
michael@0 126 final URI uri = getAnnounceURI(base, lastLaunch);
michael@0 127 Logger.info(LOG_TAG, "Fetching announcements from " + uri.toASCIIString());
michael@0 128 fetchAnnouncements(uri, delegate);
michael@0 129 } catch (URISyntaxException e) {
michael@0 130 Logger.warn(LOG_TAG, "Couldn't create URL.", e);
michael@0 131 return;
michael@0 132 }
michael@0 133 }
michael@0 134 }

mercurial