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