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