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 17package com.android.server.location; 18 19import android.text.TextUtils; 20import android.util.Log; 21 22import java.io.IOException; 23import java.net.HttpURLConnection; 24import java.net.URL; 25import java.util.Properties; 26import java.util.Random; 27import java.util.concurrent.TimeUnit; 28 29import libcore.io.Streams; 30 31/** 32 * A class for downloading GPS XTRA data. 33 * 34 * {@hide} 35 */ 36public class GpsXtraDownloader { 37 38 private static final String TAG = "GpsXtraDownloader"; 39 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 40 private static final String DEFAULT_USER_AGENT = "Android"; 41 private static final int CONNECTION_TIMEOUT_MS = (int) TimeUnit.SECONDS.toMillis(30); 42 43 private final String[] mXtraServers; 44 // to load balance our server requests 45 private int mNextServerIndex; 46 private final String mUserAgent; 47 48 GpsXtraDownloader(Properties properties) { 49 // read XTRA servers from the Properties object 50 int count = 0; 51 String server1 = properties.getProperty("XTRA_SERVER_1"); 52 String server2 = properties.getProperty("XTRA_SERVER_2"); 53 String server3 = properties.getProperty("XTRA_SERVER_3"); 54 if (server1 != null) count++; 55 if (server2 != null) count++; 56 if (server3 != null) count++; 57 58 // Set User Agent from properties, if possible. 59 String agent = properties.getProperty("XTRA_USER_AGENT"); 60 if (TextUtils.isEmpty(agent)) { 61 mUserAgent = DEFAULT_USER_AGENT; 62 } else { 63 mUserAgent = agent; 64 } 65 66 if (count == 0) { 67 Log.e(TAG, "No XTRA servers were specified in the GPS configuration"); 68 mXtraServers = null; 69 } 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; 75 76 // randomize first server 77 Random random = new Random(); 78 mNextServerIndex = random.nextInt(count); 79 } 80 } 81 82 byte[] downloadXtraData() { 83 byte[] result = null; 84 int startIndex = mNextServerIndex; 85 86 if (mXtraServers == null) { 87 return null; 88 } 89 90 // load balance our requests among the available servers 91 while (result == null) { 92 result = doDownload(mXtraServers[mNextServerIndex]); 93 94 // increment mNextServerIndex and wrap around if necessary 95 mNextServerIndex++; 96 if (mNextServerIndex == mXtraServers.length) { 97 mNextServerIndex = 0; 98 } 99 // break if we have tried all the servers 100 if (mNextServerIndex == startIndex) break; 101 } 102 103 return result; 104 } 105 106 protected byte[] doDownload(String url) { 107 if (DEBUG) Log.d(TAG, "Downloading XTRA data from " + url); 108 109 HttpURLConnection connection = null; 110 try { 111 connection = (HttpURLConnection) (new URL(url)).openConnection(); 112 connection.setRequestProperty( 113 "Accept", 114 "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); 115 connection.setRequestProperty( 116 "x-wap-profile", 117 "http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"); 118 connection.setConnectTimeout(CONNECTION_TIMEOUT_MS); 119 120 connection.connect(); 121 int statusCode = connection.getResponseCode(); 122 if (statusCode != HttpURLConnection.HTTP_OK) { 123 if (DEBUG) Log.d(TAG, "HTTP error downloading gps XTRA: " + statusCode); 124 return null; 125 } 126 127 return Streams.readFully(connection.getInputStream()); 128 } catch (IOException ioe) { 129 if (DEBUG) Log.d(TAG, "Error downloading gps XTRA: ", ioe); 130 } finally { 131 if (connection != null) { 132 connection.disconnect(); 133 } 134 } 135 return null; 136 } 137 138} 139