blob: c012ee41b29b63b7849881ca47103b8e7b1d0b59 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Lockwood00b74272010-03-26 10:41:48 -040017package com.android.server.location;
18
Jeff Sharkey619a5112017-01-19 11:55:54 -070019import android.net.TrafficStats;
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070020import android.text.TextUtils;
Mike Lockwood00b74272010-03-26 10:41:48 -040021import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Jeff Sharkey619a5112017-01-19 11:55:54 -070023import java.io.ByteArrayOutputStream;
destradaaac6d8962015-07-31 18:01:55 -070024import java.io.IOException;
Jeff Sharkey619a5112017-01-19 11:55:54 -070025import java.io.InputStream;
Andreas Gampedfdc6ac2014-10-28 20:42:53 -070026import java.net.HttpURLConnection;
27import java.net.URL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.util.Properties;
29import java.util.Random;
destradaaac6d8962015-07-31 18:01:55 -070030import java.util.concurrent.TimeUnit;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * A class for downloading GPS XTRA data.
34 *
35 * {@hide}
36 */
37public class GpsXtraDownloader {
38
39 private static final String TAG = "GpsXtraDownloader";
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070040 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
David Christiedde12c62016-07-25 17:13:23 -070041 private static final long MAXIMUM_CONTENT_LENGTH_BYTES = 1000000; // 1MB.
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070042 private static final String DEFAULT_USER_AGENT = "Android";
destradaaac6d8962015-07-31 18:01:55 -070043 private static final int CONNECTION_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(30);
z00208928f7dd3752017-07-21 22:26:29 +080044 private static final int READ_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(60);
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070045
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070046 private final String[] mXtraServers;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 // to load balance our server requests
48 private int mNextServerIndex;
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070049 private final String mUserAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Andreas Gampedfdc6ac2014-10-28 20:42:53 -070051 GpsXtraDownloader(Properties properties) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 // 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 Chenf86f0e12014-09-13 15:00:40 -070060
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 Project9066cfe2009-03-03 19:31:44 -080069 if (count == 0) {
70 Log.e(TAG, "No XTRA servers were specified in the GPS configuration");
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070071 mXtraServers = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 } 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 Lockwoodb8526bfe2009-04-28 19:04:08 -040078
79 // randomize first server
80 Random random = new Random();
81 mNextServerIndex = random.nextInt(count);
Tsuwei Chenf86f0e12014-09-13 15:00:40 -070082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
84
85 byte[] downloadXtraData() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 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 Sharkey619a5112017-01-19 11:55:54 -070095 final int oldTag = TrafficStats.getAndSetThreadStatsTag(TrafficStats.TAG_SYSTEM_GPS);
96 try {
97 result = doDownload(mXtraServers[mNextServerIndex]);
98 } finally {
99 TrafficStats.setThreadStatsTag(oldTag);
100 }
Tsuwei Chenf86f0e12014-09-13 15:00:40 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 // 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 Chenf86f0e12014-09-13 15:00:40 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 return result;
112 }
113
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700114 protected byte[] doDownload(String url) {
Joe Onorato431bb222010-10-18 19:13:23 -0400115 if (DEBUG) Log.d(TAG, "Downloading XTRA data from " + url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700117 HttpURLConnection connection = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 try {
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700119 connection = (HttpURLConnection) (new URL(url)).openConnection();
120 connection.setRequestProperty(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 "Accept",
122 "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700123 connection.setRequestProperty(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 "x-wap-profile",
125 "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");
destradaaac6d8962015-07-31 18:01:55 -0700126 connection.setConnectTimeout(CONNECTION_TIMEOUT_MS);
z00208928f7dd3752017-07-21 22:26:29 +0800127 connection.setReadTimeout(READ_TIMEOUT_MS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700129 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 Project9066cfe2009-03-03 19:31:44 -0800133 return null;
134 }
135
David Christiea2399b42016-08-23 16:19:51 -0700136 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 Project9066cfe2009-03-03 19:31:44 -0800145 }
146 }
David Christiea2399b42016-08-23 16:19:51 -0700147 return bytes.toByteArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700149 } catch (IOException ioe) {
150 if (DEBUG) Log.d(TAG, "Error downloading gps XTRA: ", ioe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 } finally {
Andreas Gampedfdc6ac2014-10-28 20:42:53 -0700152 if (connection != null) {
153 connection.disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 }
155 }
156 return null;
157 }
158
159}
David Christiea2399b42016-08-23 16:19:51 -0700160