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