The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 17 | package com.android.server.location; |
| 18 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 19 | import android.net.TrafficStats; |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 20 | import android.text.TextUtils; |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 21 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 23 | import java.io.ByteArrayOutputStream; |
destradaa | ac6d896 | 2015-07-31 18:01:55 -0700 | [diff] [blame] | 24 | import java.io.IOException; |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 25 | import java.io.InputStream; |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 26 | import java.net.HttpURLConnection; |
| 27 | import java.net.URL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import java.util.Properties; |
| 29 | import java.util.Random; |
destradaa | ac6d896 | 2015-07-31 18:01:55 -0700 | [diff] [blame] | 30 | import java.util.concurrent.TimeUnit; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** |
| 33 | * A class for downloading GPS XTRA data. |
| 34 | * |
| 35 | * {@hide} |
| 36 | */ |
| 37 | public class GpsXtraDownloader { |
| 38 | |
| 39 | private static final String TAG = "GpsXtraDownloader"; |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 40 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
David Christie | dde12c6 | 2016-07-25 17:13:23 -0700 | [diff] [blame] | 41 | private static final long MAXIMUM_CONTENT_LENGTH_BYTES = 1000000; // 1MB. |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 42 | private static final String DEFAULT_USER_AGENT = "Android"; |
destradaa | ac6d896 | 2015-07-31 18:01:55 -0700 | [diff] [blame] | 43 | private static final int CONNECTION_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(30); |
z00208928 | f7dd375 | 2017-07-21 22:26:29 +0800 | [diff] [blame] | 44 | private static final int READ_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(60); |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 45 | |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 46 | private final String[] mXtraServers; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | // to load balance our server requests |
| 48 | private int mNextServerIndex; |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 49 | private final String mUserAgent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 51 | GpsXtraDownloader(Properties properties) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | // read XTRA servers from the Properties object |
| 53 | int count = 0; |
| 54 | String server1 = properties.getProperty("XTRA_SERVER_1"); |
| 55 | String server2 = properties.getProperty("XTRA_SERVER_2"); |
| 56 | String server3 = properties.getProperty("XTRA_SERVER_3"); |
| 57 | if (server1 != null) count++; |
| 58 | if (server2 != null) count++; |
| 59 | if (server3 != null) count++; |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 60 | |
| 61 | // Set User Agent from properties, if possible. |
| 62 | String agent = properties.getProperty("XTRA_USER_AGENT"); |
| 63 | if (TextUtils.isEmpty(agent)) { |
| 64 | mUserAgent = DEFAULT_USER_AGENT; |
| 65 | } else { |
| 66 | mUserAgent = agent; |
| 67 | } |
| 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | if (count == 0) { |
| 70 | Log.e(TAG, "No XTRA servers were specified in the GPS configuration"); |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 71 | mXtraServers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } else { |
| 73 | mXtraServers = new String[count]; |
| 74 | count = 0; |
| 75 | if (server1 != null) mXtraServers[count++] = server1; |
| 76 | if (server2 != null) mXtraServers[count++] = server2; |
| 77 | if (server3 != null) mXtraServers[count++] = server3; |
Mike Lockwood | b8526bfe | 2009-04-28 19:04:08 -0400 | [diff] [blame] | 78 | |
| 79 | // randomize first server |
| 80 | Random random = new Random(); |
| 81 | mNextServerIndex = random.nextInt(count); |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 82 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | byte[] downloadXtraData() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | byte[] result = null; |
| 87 | int startIndex = mNextServerIndex; |
| 88 | |
| 89 | if (mXtraServers == null) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | // load balance our requests among the available servers |
| 94 | while (result == null) { |
Jeff Sharkey | 619a511 | 2017-01-19 11:55:54 -0700 | [diff] [blame] | 95 | final int oldTag = TrafficStats.getAndSetThreadStatsTag(TrafficStats.TAG_SYSTEM_GPS); |
| 96 | try { |
| 97 | result = doDownload(mXtraServers[mNextServerIndex]); |
| 98 | } finally { |
| 99 | TrafficStats.setThreadStatsTag(oldTag); |
| 100 | } |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | // increment mNextServerIndex and wrap around if necessary |
| 103 | mNextServerIndex++; |
| 104 | if (mNextServerIndex == mXtraServers.length) { |
| 105 | mNextServerIndex = 0; |
| 106 | } |
| 107 | // break if we have tried all the servers |
| 108 | if (mNextServerIndex == startIndex) break; |
| 109 | } |
Tsuwei Chen | f86f0e1 | 2014-09-13 15:00:40 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | return result; |
| 112 | } |
| 113 | |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 114 | protected byte[] doDownload(String url) { |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 115 | if (DEBUG) Log.d(TAG, "Downloading XTRA data from " + url); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 117 | HttpURLConnection connection = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | try { |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 119 | connection = (HttpURLConnection) (new URL(url)).openConnection(); |
| 120 | connection.setRequestProperty( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | "Accept", |
| 122 | "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 123 | connection.setRequestProperty( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | "x-wap-profile", |
| 125 | "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"); |
destradaa | ac6d896 | 2015-07-31 18:01:55 -0700 | [diff] [blame] | 126 | connection.setConnectTimeout(CONNECTION_TIMEOUT_MS); |
z00208928 | f7dd375 | 2017-07-21 22:26:29 +0800 | [diff] [blame] | 127 | connection.setReadTimeout(READ_TIMEOUT_MS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 129 | connection.connect(); |
| 130 | int statusCode = connection.getResponseCode(); |
| 131 | if (statusCode != HttpURLConnection.HTTP_OK) { |
| 132 | if (DEBUG) Log.d(TAG, "HTTP error downloading gps XTRA: " + statusCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | return null; |
| 134 | } |
| 135 | |
David Christie | a2399b4 | 2016-08-23 16:19:51 -0700 | [diff] [blame] | 136 | try (InputStream in = connection.getInputStream()) { |
| 137 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 138 | byte[] buffer = new byte[1024]; |
| 139 | int count; |
| 140 | while ((count = in.read(buffer)) != -1) { |
| 141 | bytes.write(buffer, 0, count); |
| 142 | if (bytes.size() > MAXIMUM_CONTENT_LENGTH_BYTES) { |
| 143 | if (DEBUG) Log.d(TAG, "XTRA file too large"); |
| 144 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
David Christie | a2399b4 | 2016-08-23 16:19:51 -0700 | [diff] [blame] | 147 | return bytes.toByteArray(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 149 | } catch (IOException ioe) { |
| 150 | if (DEBUG) Log.d(TAG, "Error downloading gps XTRA: ", ioe); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | } finally { |
Andreas Gampe | dfdc6ac | 2014-10-28 20:42:53 -0700 | [diff] [blame] | 152 | if (connection != null) { |
| 153 | connection.disconnect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | } |
David Christie | a2399b4 | 2016-08-23 16:19:51 -0700 | [diff] [blame] | 160 | |