blob: 813d2551daacfb54b75345e2f1d819b4755e5a63 [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
19import android.content.Context;
20import android.net.Proxy;
21import android.net.http.AndroidHttpClient;
22import android.util.Config;
23import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import org.apache.http.HttpEntity;
26import org.apache.http.HttpHost;
27import org.apache.http.HttpResponse;
28import org.apache.http.StatusLine;
29import org.apache.http.client.HttpClient;
30import org.apache.http.client.methods.HttpGet;
31import org.apache.http.client.methods.HttpUriRequest;
32import org.apache.http.conn.params.ConnRouteParams;
33
34import java.io.DataInputStream;
35import java.io.IOException;
36import java.util.Properties;
37import java.util.Random;
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039/**
40 * A class for downloading GPS XTRA data.
41 *
42 * {@hide}
43 */
44public class GpsXtraDownloader {
45
46 private static final String TAG = "GpsXtraDownloader";
Joe Onorato431bb222010-10-18 19:13:23 -040047 static final boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49 private Context mContext;
50 private String[] mXtraServers;
51 // to load balance our server requests
52 private int mNextServerIndex;
53
54 GpsXtraDownloader(Context context, Properties properties) {
55 mContext = context;
56
57 // read XTRA servers from the Properties object
58 int count = 0;
59 String server1 = properties.getProperty("XTRA_SERVER_1");
60 String server2 = properties.getProperty("XTRA_SERVER_2");
61 String server3 = properties.getProperty("XTRA_SERVER_3");
62 if (server1 != null) count++;
63 if (server2 != null) count++;
64 if (server3 != null) count++;
65
66 if (count == 0) {
67 Log.e(TAG, "No XTRA servers were specified in the GPS configuration");
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070068 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 } else {
70 mXtraServers = new String[count];
71 count = 0;
72 if (server1 != null) mXtraServers[count++] = server1;
73 if (server2 != null) mXtraServers[count++] = server2;
74 if (server3 != null) mXtraServers[count++] = server3;
Mike Lockwoodb8526bfe2009-04-28 19:04:08 -040075
76 // randomize first server
77 Random random = new Random();
78 mNextServerIndex = random.nextInt(count);
79 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
82 byte[] downloadXtraData() {
83 String proxyHost = Proxy.getHost(mContext);
84 int proxyPort = Proxy.getPort(mContext);
85 boolean useProxy = (proxyHost != null && proxyPort != -1);
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) {
95 result = doDownload(mXtraServers[mNextServerIndex], useProxy, proxyHost, proxyPort);
96
97 // increment mNextServerIndex and wrap around if necessary
98 mNextServerIndex++;
99 if (mNextServerIndex == mXtraServers.length) {
100 mNextServerIndex = 0;
101 }
102 // break if we have tried all the servers
103 if (mNextServerIndex == startIndex) break;
104 }
105
106 return result;
107 }
108
109 protected static byte[] doDownload(String url, boolean isProxySet,
110 String proxyHost, int proxyPort) {
Joe Onorato431bb222010-10-18 19:13:23 -0400111 if (DEBUG) Log.d(TAG, "Downloading XTRA data from " + url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 AndroidHttpClient client = null;
114 try {
115 client = AndroidHttpClient.newInstance("Android");
116 HttpUriRequest req = new HttpGet(url);
117
118 if (isProxySet) {
119 HttpHost proxy = new HttpHost(proxyHost, proxyPort);
120 ConnRouteParams.setDefaultProxy(req.getParams(), proxy);
121 }
122
123 req.addHeader(
124 "Accept",
125 "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
126
127 req.addHeader(
128 "x-wap-profile",
129 "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");
130
131 HttpResponse response = client.execute(req);
132 StatusLine status = response.getStatusLine();
133 if (status.getStatusCode() != 200) { // HTTP 200 is success.
Joe Onorato431bb222010-10-18 19:13:23 -0400134 if (DEBUG) Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 return null;
136 }
137
138 HttpEntity entity = response.getEntity();
139 byte[] body = null;
140 if (entity != null) {
141 try {
142 if (entity.getContentLength() > 0) {
143 body = new byte[(int) entity.getContentLength()];
144 DataInputStream dis = new DataInputStream(entity.getContent());
145 try {
146 dis.readFully(body);
147 } finally {
148 try {
149 dis.close();
150 } catch (IOException e) {
151 Log.e(TAG, "Unexpected IOException.", e);
152 }
153 }
154 }
155 } finally {
156 if (entity != null) {
157 entity.consumeContent();
158 }
159 }
160 }
161 return body;
162 } catch (Exception e) {
Joe Onorato431bb222010-10-18 19:13:23 -0400163 if (DEBUG) Log.d(TAG, "error " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 } finally {
165 if (client != null) {
166 client.close();
167 }
168 }
169 return null;
170 }
171
172}