1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/announcements/AnnouncementsFetcher.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.announcements; 1.9 + 1.10 +import java.io.UnsupportedEncodingException; 1.11 +import java.net.URI; 1.12 +import java.net.URISyntaxException; 1.13 +import java.net.URLEncoder; 1.14 + 1.15 +import org.mozilla.gecko.background.common.GlobalConstants; 1.16 +import org.mozilla.gecko.background.common.log.Logger; 1.17 +import org.mozilla.gecko.sync.net.BaseResource; 1.18 + 1.19 +public class AnnouncementsFetcher { 1.20 + private static final String LOG_TAG = "AnnounceFetch"; 1.21 + private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; 1.22 + 1.23 + public static URI getSnippetURI(String base, String channel, 1.24 + String version, String platform, 1.25 + int idleDays) 1.26 + throws URISyntaxException { 1.27 + try { 1.28 + final String c = URLEncoder.encode(channel, "UTF-8"); 1.29 + final String v = URLEncoder.encode(version, "UTF-8"); 1.30 + final String p = URLEncoder.encode(platform, "UTF-8"); 1.31 + final String s = base + c + "/" + v + "/" + p + ((idleDays == -1) ? "" : ("?idle=" + idleDays)); 1.32 + return new URI(s); 1.33 + } catch (UnsupportedEncodingException e) { 1.34 + // Nonsense. 1.35 + return null; 1.36 + } 1.37 + } 1.38 + 1.39 + public static URI getAnnounceURI(final String baseURL, final long lastLaunch) throws URISyntaxException { 1.40 + final String channel = getChannel(); 1.41 + final String version = getVersion(); 1.42 + final String platform = getPlatform(); 1.43 + final int idleDays = getIdleDays(lastLaunch); 1.44 + 1.45 + Logger.debug(LOG_TAG, "Fetch URI: idle for " + idleDays + " days."); 1.46 + return getSnippetURI(baseURL, channel, version, platform, idleDays); 1.47 + } 1.48 + 1.49 + protected static String getChannel() { 1.50 + return AnnouncementsConstants.ANNOUNCE_CHANNEL; 1.51 + } 1.52 + 1.53 + protected static String getVersion() { 1.54 + return GlobalConstants.MOZ_APP_VERSION; 1.55 + } 1.56 + 1.57 + protected static String getPlatform() { 1.58 + return GlobalConstants.ANDROID_CPU_ARCH; 1.59 + } 1.60 + 1.61 + /** 1.62 + * Return the number of days that we've been idle, assuming that we have a 1.63 + * sane last launch time and the current time is within range. If no sane idle 1.64 + * time can be returned, we return -1. 1.65 + * 1.66 + * @param lastLaunch 1.67 + * Time at which the browser was last launched, in milliseconds since epoch. 1.68 + * @param now 1.69 + * Milliseconds since epoch for which idle time should be calculated. 1.70 + * @return number of idle days, or -1 if out of range. 1.71 + */ 1.72 + protected static int getIdleDays(final long lastLaunch, final long now) { 1.73 + if (lastLaunch <= 0) { 1.74 + return -1; 1.75 + } 1.76 + 1.77 + if (now < GlobalConstants.BUILD_TIMESTAMP_MSEC) { 1.78 + Logger.warn(LOG_TAG, "Current time " + now + " earlier than build date. Not calculating idle."); 1.79 + return -1; 1.80 + } 1.81 + 1.82 + if (now < lastLaunch) { 1.83 + Logger.warn(LOG_TAG, "Current time " + now + " earlier than last launch! Not calculating idle."); 1.84 + return -1; 1.85 + } 1.86 + 1.87 + final long idleMillis = now - lastLaunch; 1.88 + final int idleDays = (int) (idleMillis / MILLISECONDS_PER_DAY); 1.89 + 1.90 + if (idleDays > AnnouncementsConstants.MAX_SANE_IDLE_DAYS) { 1.91 + Logger.warn(LOG_TAG, "Idle from " + lastLaunch + " until " + now + 1.92 + ", which is insane. Not calculating idle."); 1.93 + return -1; 1.94 + } 1.95 + 1.96 + return idleDays; 1.97 + } 1.98 + 1.99 + /** 1.100 + * Return the number of days that we've been idle, assuming that we have a 1.101 + * sane last launch time and the current time is within range. If no sane idle 1.102 + * time can be returned, we return -1. 1.103 + * The current time will be calculated from {@link System#currentTimeMillis()}. 1.104 + * 1.105 + * @param lastLaunch 1.106 + * Unix timestamp at which the browser was last launched. 1.107 + * @return number of idle days, or -1 if out of range. 1.108 + */ 1.109 + protected static int getIdleDays(final long lastLaunch) { 1.110 + final long now = System.currentTimeMillis(); 1.111 + return getIdleDays(lastLaunch, now); 1.112 + } 1.113 + 1.114 + public static void fetchAnnouncements(URI uri, AnnouncementsFetchDelegate delegate) { 1.115 + BaseResource r = new BaseResource(uri); 1.116 + r.delegate = new AnnouncementsFetchResourceDelegate(r, delegate); 1.117 + r.getBlocking(); 1.118 + } 1.119 + 1.120 + /** 1.121 + * Synchronous. 1.122 + */ 1.123 + public static void fetchAndProcessAnnouncements(long lastLaunch, 1.124 + AnnouncementsFetchDelegate delegate) { 1.125 + final long now = System.currentTimeMillis(); 1.126 + Logger.debug(LOG_TAG, "Fetching announcements. Last launch: " + lastLaunch + "; now: " + now); 1.127 + try { 1.128 + final String base = delegate.getServiceURL(); 1.129 + final URI uri = getAnnounceURI(base, lastLaunch); 1.130 + Logger.info(LOG_TAG, "Fetching announcements from " + uri.toASCIIString()); 1.131 + fetchAnnouncements(uri, delegate); 1.132 + } catch (URISyntaxException e) { 1.133 + Logger.warn(LOG_TAG, "Couldn't create URL.", e); 1.134 + return; 1.135 + } 1.136 + } 1.137 +}