blob: 9dedb35d53da429411b0c81ffc1830d03015928c [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;
Mike Lockwood00b74272010-03-26 10:41:48 -040022import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24import org.apache.http.HttpEntity;
25import org.apache.http.HttpHost;
26import org.apache.http.HttpResponse;
27import org.apache.http.StatusLine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import org.apache.http.client.methods.HttpGet;
29import org.apache.http.client.methods.HttpUriRequest;
30import org.apache.http.conn.params.ConnRouteParams;
31
32import java.io.DataInputStream;
33import java.io.IOException;
34import java.util.Properties;
35import java.util.Random;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
38 * A class for downloading GPS XTRA data.
39 *
40 * {@hide}
41 */
42public class GpsXtraDownloader {
43
44 private static final String TAG = "GpsXtraDownloader";
Joe Onorato431bb222010-10-18 19:13:23 -040045 static final boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47 private Context mContext;
48 private String[] mXtraServers;
49 // to load balance our server requests
50 private int mNextServerIndex;
51
52 GpsXtraDownloader(Context context, Properties properties) {
53 mContext = context;
54
55 // read XTRA servers from the Properties object
56 int count = 0;
57 String server1 = properties.getProperty("XTRA_SERVER_1");
58 String server2 = properties.getProperty("XTRA_SERVER_2");
59 String server3 = properties.getProperty("XTRA_SERVER_3");
60 if (server1 != null) count++;
61 if (server2 != null) count++;
62 if (server3 != null) count++;
63
64 if (count == 0) {
65 Log.e(TAG, "No XTRA servers were specified in the GPS configuration");
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070066 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 } else {
68 mXtraServers = new String[count];
69 count = 0;
70 if (server1 != null) mXtraServers[count++] = server1;
71 if (server2 != null) mXtraServers[count++] = server2;
72 if (server3 != null) mXtraServers[count++] = server3;
Mike Lockwoodb8526bfe2009-04-28 19:04:08 -040073
74 // randomize first server
75 Random random = new Random();
76 mNextServerIndex = random.nextInt(count);
77 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
79
80 byte[] downloadXtraData() {
81 String proxyHost = Proxy.getHost(mContext);
82 int proxyPort = Proxy.getPort(mContext);
83 boolean useProxy = (proxyHost != null && proxyPort != -1);
84 byte[] result = null;
85 int startIndex = mNextServerIndex;
86
87 if (mXtraServers == null) {
88 return null;
89 }
90
91 // load balance our requests among the available servers
92 while (result == null) {
93 result = doDownload(mXtraServers[mNextServerIndex], useProxy, proxyHost, proxyPort);
94
95 // increment mNextServerIndex and wrap around if necessary
96 mNextServerIndex++;
97 if (mNextServerIndex == mXtraServers.length) {
98 mNextServerIndex = 0;
99 }
100 // break if we have tried all the servers
101 if (mNextServerIndex == startIndex) break;
102 }
103
104 return result;
105 }
106
107 protected static byte[] doDownload(String url, boolean isProxySet,
108 String proxyHost, int proxyPort) {
Joe Onorato431bb222010-10-18 19:13:23 -0400109 if (DEBUG) Log.d(TAG, "Downloading XTRA data from " + url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 AndroidHttpClient client = null;
112 try {
113 client = AndroidHttpClient.newInstance("Android");
114 HttpUriRequest req = new HttpGet(url);
115
116 if (isProxySet) {
117 HttpHost proxy = new HttpHost(proxyHost, proxyPort);
118 ConnRouteParams.setDefaultProxy(req.getParams(), proxy);
119 }
120
121 req.addHeader(
122 "Accept",
123 "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
124
125 req.addHeader(
126 "x-wap-profile",
127 "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#");
128
129 HttpResponse response = client.execute(req);
130 StatusLine status = response.getStatusLine();
131 if (status.getStatusCode() != 200) { // HTTP 200 is success.
Joe Onorato431bb222010-10-18 19:13:23 -0400132 if (DEBUG) Log.d(TAG, "HTTP error: " + status.getReasonPhrase());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return null;
134 }
135
136 HttpEntity entity = response.getEntity();
137 byte[] body = null;
138 if (entity != null) {
139 try {
140 if (entity.getContentLength() > 0) {
141 body = new byte[(int) entity.getContentLength()];
142 DataInputStream dis = new DataInputStream(entity.getContent());
143 try {
144 dis.readFully(body);
145 } finally {
146 try {
147 dis.close();
148 } catch (IOException e) {
149 Log.e(TAG, "Unexpected IOException.", e);
150 }
151 }
152 }
153 } finally {
154 if (entity != null) {
155 entity.consumeContent();
156 }
157 }
158 }
159 return body;
160 } catch (Exception e) {
Joe Onorato431bb222010-10-18 19:13:23 -0400161 if (DEBUG) Log.d(TAG, "error " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 } finally {
163 if (client != null) {
164 client.close();
165 }
166 }
167 return null;
168 }
169
170}