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 | |
| 19 | import android.content.Context; |
| 20 | import android.net.Proxy; |
| 21 | import android.net.http.AndroidHttpClient; |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 22 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | import org.apache.http.HttpEntity; |
| 25 | import org.apache.http.HttpHost; |
| 26 | import org.apache.http.HttpResponse; |
| 27 | import org.apache.http.StatusLine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import org.apache.http.client.methods.HttpGet; |
| 29 | import org.apache.http.client.methods.HttpUriRequest; |
| 30 | import org.apache.http.conn.params.ConnRouteParams; |
| 31 | |
| 32 | import java.io.DataInputStream; |
| 33 | import java.io.IOException; |
| 34 | import java.util.Properties; |
| 35 | import java.util.Random; |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | /** |
| 38 | * A class for downloading GPS XTRA data. |
| 39 | * |
| 40 | * {@hide} |
| 41 | */ |
| 42 | public class GpsXtraDownloader { |
| 43 | |
| 44 | private static final String TAG = "GpsXtraDownloader"; |
Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 45 | static final boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 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 Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 66 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } 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 Lockwood | b8526bfe | 2009-04-28 19:04:08 -0400 | [diff] [blame] | 73 | |
| 74 | // randomize first server |
| 75 | Random random = new Random(); |
| 76 | mNextServerIndex = random.nextInt(count); |
| 77 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } |
| 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 Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 109 | 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] | 110 | |
| 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 Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 132 | if (DEBUG) Log.d(TAG, "HTTP error: " + status.getReasonPhrase()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | 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 Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 161 | if (DEBUG) Log.d(TAG, "error " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } finally { |
| 163 | if (client != null) { |
| 164 | client.close(); |
| 165 | } |
| 166 | } |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | } |