development
Revisión | 1f49fa9b7c6b2b603345fe74e6ec76969ba6f00d (tree) |
---|---|
Tiempo | 2009-08-20 08:14:18 |
Autor | Xavier Ducrohet <xav@andr...> |
Commiter | Xavier Ducrohet |
Add OS version to ping service (win/mac only)
@@ -44,6 +44,8 @@ import java.net.HttpURLConnection; | ||
44 | 44 | import java.net.URL; |
45 | 45 | import java.net.URLEncoder; |
46 | 46 | import java.util.Random; |
47 | +import java.util.regex.Matcher; | |
48 | +import java.util.regex.Pattern; | |
47 | 49 | |
48 | 50 | /** Utility class to send "ping" usage reports to the server. */ |
49 | 51 | public class SdkStatsService { |
@@ -93,14 +95,14 @@ public class SdkStatsService { | ||
93 | 95 | "kfmclient openURL %URL%", // $NON-NLS-1$ Konqueror |
94 | 96 | "opera -newwindow %URL%", // $NON-NLS-1$ Opera |
95 | 97 | }; |
96 | - | |
98 | + | |
97 | 99 | public final static String PING_OPT_IN = "pingOptIn"; //$NON-NLS-1$ |
98 | 100 | public final static String PING_TIME = "pingTime"; //$NON-NLS-1$ |
99 | 101 | public final static String PING_ID = "pingId"; //$NON-NLS-1$ |
100 | 102 | |
101 | 103 | |
102 | 104 | private static PreferenceStore sPrefStore; |
103 | - | |
105 | + | |
104 | 106 | /** |
105 | 107 | * Send a "ping" to the Google toolbar server, if enough time has |
106 | 108 | * elapsed since the last ping, and if the user has not opted out. |
@@ -123,7 +125,7 @@ public class SdkStatsService { | ||
123 | 125 | if (!prefs.contains(PING_ID)) { |
124 | 126 | // First time: make up a new ID. TODO: Use something more random? |
125 | 127 | prefs.setValue(PING_ID, new Random().nextLong()); |
126 | - | |
128 | + | |
127 | 129 | // Also give them a chance to opt out. |
128 | 130 | prefs.setValue(PING_OPT_IN, getUserPermission(display)); |
129 | 131 | try { |
@@ -132,13 +134,13 @@ public class SdkStatsService { | ||
132 | 134 | catch (IOException ioe) { |
133 | 135 | } |
134 | 136 | } |
135 | - | |
137 | + | |
136 | 138 | // If the user has not opted in, do nothing and quietly return. |
137 | 139 | if (!prefs.getBoolean(PING_OPT_IN)) { |
138 | 140 | // user opted out. |
139 | 141 | return; |
140 | 142 | } |
141 | - | |
143 | + | |
142 | 144 | // If the last ping *for this app* was too recent, do nothing. |
143 | 145 | String timePref = PING_TIME + "." + app; // $NON-NLS-1$ |
144 | 146 | long now = System.currentTimeMillis(); |
@@ -147,7 +149,7 @@ public class SdkStatsService { | ||
147 | 149 | // too soon after a ping. |
148 | 150 | return; |
149 | 151 | } |
150 | - | |
152 | + | |
151 | 153 | // Record the time of the attempt, whether or not it succeeds. |
152 | 154 | prefs.setValue(timePref, now); |
153 | 155 | try { |
@@ -155,7 +157,7 @@ public class SdkStatsService { | ||
155 | 157 | } |
156 | 158 | catch (IOException ioe) { |
157 | 159 | } |
158 | - | |
160 | + | |
159 | 161 | // Send the ping itself in the background (don't block if the |
160 | 162 | // network is down or slow or confused). |
161 | 163 | final long id = prefs.getLong(PING_ID); |
@@ -171,7 +173,7 @@ public class SdkStatsService { | ||
171 | 173 | }.start(); |
172 | 174 | } |
173 | 175 | } |
174 | - | |
176 | + | |
175 | 177 | /** |
176 | 178 | * Returns the DDMS {@link PreferenceStore}. |
177 | 179 | */ |
@@ -187,7 +189,7 @@ public class SdkStatsService { | ||
187 | 189 | |
188 | 190 | if (homeDir != null) { |
189 | 191 | String rcFileName = homeDir + "ddms.cfg"; //$NON-NLS-1$ |
190 | - | |
192 | + | |
191 | 193 | // also look for an old pref file in the previous location |
192 | 194 | String oldPrefPath = System.getProperty("user.home") //$NON-NLS-1$ |
193 | 195 | + File.separator + ".ddmsrc"; //$NON-NLS-1$ |
@@ -196,10 +198,10 @@ public class SdkStatsService { | ||
196 | 198 | try { |
197 | 199 | PreferenceStore oldStore = new PreferenceStore(oldPrefPath); |
198 | 200 | oldStore.load(); |
199 | - | |
201 | + | |
200 | 202 | oldStore.save(new FileOutputStream(rcFileName), ""); |
201 | 203 | oldPrefFile.delete(); |
202 | - | |
204 | + | |
203 | 205 | PreferenceStore newStore = new PreferenceStore(rcFileName); |
204 | 206 | newStore.load(); |
205 | 207 | sPrefStore = newStore; |
@@ -220,10 +222,10 @@ public class SdkStatsService { | ||
220 | 222 | sPrefStore = new PreferenceStore(); |
221 | 223 | } |
222 | 224 | } |
223 | - | |
225 | + | |
224 | 226 | return sPrefStore; |
225 | 227 | } |
226 | - | |
228 | + | |
227 | 229 | /** |
228 | 230 | * Unconditionally send a "ping" request to the Google toolbar server. |
229 | 231 | * |
@@ -239,8 +241,16 @@ public class SdkStatsService { | ||
239 | 241 | String os = System.getProperty("os.name"); // $NON-NLS-1$ |
240 | 242 | if (os.startsWith("Mac OS")) { // $NON-NLS-1$ |
241 | 243 | os = "mac"; // $NON-NLS-1$ |
244 | + String osVers = getVersion(); | |
245 | + if (osVers != null) { | |
246 | + os = os + "-" + osVers; // $NON-NLS-1$ | |
247 | + } | |
242 | 248 | } else if (os.startsWith("Windows")) { // $NON-NLS-1$ |
243 | 249 | os = "win"; // $NON-NLS-1$ |
250 | + String osVers = getVersion(); | |
251 | + if (osVers != null) { | |
252 | + os = os + "-" + osVers; // $NON-NLS-1$ | |
253 | + } | |
244 | 254 | } else if (os.startsWith("Linux")) { // $NON-NLS-1$ |
245 | 255 | os = "linux"; // $NON-NLS-1$ |
246 | 256 | } else { |
@@ -272,6 +282,24 @@ public class SdkStatsService { | ||
272 | 282 | } |
273 | 283 | |
274 | 284 | /** |
285 | + * Returns the version of the os if it is defined as X.Y, or null otherwise. | |
286 | + * <p/> | |
287 | + * Example of returned versions can be found at http://lopica.sourceforge.net/os.html | |
288 | + * <p/> | |
289 | + * This method removes any exiting micro versions. | |
290 | + */ | |
291 | + private static String getVersion() { | |
292 | + Pattern p = Pattern.compile("(\\d+)\\.(\\d+).*"); // $NON-NLS-1$ | |
293 | + String osVers = System.getProperty("os.version"); // $NON-NLS-1$ | |
294 | + Matcher m = p.matcher(osVers); | |
295 | + if (m.matches()) { | |
296 | + return m.group(1) + "." + m.group(2); // $NON-NLS-1$ | |
297 | + } | |
298 | + | |
299 | + return null; | |
300 | + } | |
301 | + | |
302 | + /** | |
275 | 303 | * Prompt the user for whether they want to opt out of reporting. |
276 | 304 | * @return whether the user allows reporting (they do not opt out). |
277 | 305 | */ |