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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 19 | import android.app.AlarmManager; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 20 | import android.app.AppOpsManager; |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 21 | import android.app.PendingIntent; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 25 | import android.content.IntentFilter; |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 26 | import android.database.Cursor; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 27 | import android.hardware.location.GeofenceHardwareImpl; |
| 28 | import android.hardware.location.IGeofenceHardware; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.location.Criteria; |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 30 | import android.location.IGpsGeofenceHardware; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.location.IGpsStatusListener; |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 32 | import android.location.IGpsStatusProvider; |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 33 | import android.location.ILocationManager; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 34 | import android.location.INetInitiatedListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.location.Location; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 36 | import android.location.LocationListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.location.LocationManager; |
| 38 | import android.location.LocationProvider; |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 39 | import android.location.LocationRequest; |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 40 | import android.net.ConnectivityManager; |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 41 | import android.net.NetworkInfo; |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 42 | import android.net.Uri; |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 43 | import android.os.AsyncTask; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 44 | import android.os.BatteryStats; |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 45 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Bundle; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 47 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 49 | import android.os.Looper; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 50 | import android.os.Message; |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 51 | import android.os.PowerManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.RemoteException; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 53 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 55 | import android.os.UserHandle; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 56 | import android.os.WorkSource; |
Mike Lockwood | bcab8df | 2009-06-25 16:39:09 -0400 | [diff] [blame] | 57 | import android.provider.Settings; |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 58 | import android.provider.Telephony.Carriers; |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 59 | import android.provider.Telephony.Sms.Intents; |
Jake Hamby | b49a73d | 2011-03-15 20:09:46 -0700 | [diff] [blame] | 60 | import android.telephony.SmsMessage; |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 61 | import android.telephony.TelephonyManager; |
| 62 | import android.telephony.gsm.GsmCellLocation; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.util.Log; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 64 | import android.util.NtpTrustedTime; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 65 | |
| 66 | import com.android.internal.app.IAppOpsService; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 67 | import com.android.internal.app.IBatteryStats; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 68 | import com.android.internal.location.GpsNetInitiatedHandler; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 69 | import com.android.internal.location.ProviderProperties; |
| 70 | import com.android.internal.location.ProviderRequest; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 71 | import com.android.internal.location.GpsNetInitiatedHandler.GpsNiNotification; |
Jake Hamby | b49a73d | 2011-03-15 20:09:46 -0700 | [diff] [blame] | 72 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 73 | import com.android.internal.telephony.PhoneConstants; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import java.io.File; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 76 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import java.io.FileInputStream; |
| 78 | import java.io.IOException; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 79 | import java.io.PrintWriter; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 80 | import java.io.StringReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.util.ArrayList; |
Mike Lockwood | f1218be | 2010-01-29 09:20:06 -0500 | [diff] [blame] | 82 | import java.util.Date; |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 83 | import java.util.Map.Entry; |
Jake Hamby | b49a73d | 2011-03-15 20:09:46 -0700 | [diff] [blame] | 84 | import java.util.Properties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * A GPS implementation of LocationProvider used by LocationManager. |
| 88 | * |
| 89 | * {@hide} |
| 90 | */ |
Mike Lockwood | 8909631 | 2010-03-24 10:14:55 -0400 | [diff] [blame] | 91 | public class GpsLocationProvider implements LocationProviderInterface { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
| 93 | private static final String TAG = "GpsLocationProvider"; |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 94 | |
Brian Muramatsu | 1715cb3 | 2012-08-08 17:32:21 -0700 | [diff] [blame] | 95 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 96 | private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 97 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 98 | private static final ProviderProperties PROPERTIES = new ProviderProperties( |
| 99 | true, true, false, false, true, true, true, |
| 100 | Criteria.POWER_HIGH, Criteria.ACCURACY_FINE); |
| 101 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 102 | // these need to match GpsPositionMode enum in gps.h |
| 103 | private static final int GPS_POSITION_MODE_STANDALONE = 0; |
| 104 | private static final int GPS_POSITION_MODE_MS_BASED = 1; |
| 105 | private static final int GPS_POSITION_MODE_MS_ASSISTED = 2; |
| 106 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 107 | // these need to match GpsPositionRecurrence enum in gps.h |
| 108 | private static final int GPS_POSITION_RECURRENCE_PERIODIC = 0; |
| 109 | private static final int GPS_POSITION_RECURRENCE_SINGLE = 1; |
| 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | // these need to match GpsStatusValue defines in gps.h |
| 112 | private static final int GPS_STATUS_NONE = 0; |
| 113 | private static final int GPS_STATUS_SESSION_BEGIN = 1; |
| 114 | private static final int GPS_STATUS_SESSION_END = 2; |
| 115 | private static final int GPS_STATUS_ENGINE_ON = 3; |
| 116 | private static final int GPS_STATUS_ENGINE_OFF = 4; |
| 117 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 118 | // these need to match GpsApgsStatusValue defines in gps.h |
| 119 | /** AGPS status event values. */ |
| 120 | private static final int GPS_REQUEST_AGPS_DATA_CONN = 1; |
| 121 | private static final int GPS_RELEASE_AGPS_DATA_CONN = 2; |
| 122 | private static final int GPS_AGPS_DATA_CONNECTED = 3; |
| 123 | private static final int GPS_AGPS_DATA_CONN_DONE = 4; |
| 124 | private static final int GPS_AGPS_DATA_CONN_FAILED = 5; |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | // these need to match GpsLocationFlags enum in gps.h |
| 127 | private static final int LOCATION_INVALID = 0; |
| 128 | private static final int LOCATION_HAS_LAT_LONG = 1; |
| 129 | private static final int LOCATION_HAS_ALTITUDE = 2; |
| 130 | private static final int LOCATION_HAS_SPEED = 4; |
| 131 | private static final int LOCATION_HAS_BEARING = 8; |
| 132 | private static final int LOCATION_HAS_ACCURACY = 16; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 133 | |
| 134 | // IMPORTANT - the GPS_DELETE_* symbols here must match constants in gps.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private static final int GPS_DELETE_EPHEMERIS = 0x0001; |
| 136 | private static final int GPS_DELETE_ALMANAC = 0x0002; |
| 137 | private static final int GPS_DELETE_POSITION = 0x0004; |
| 138 | private static final int GPS_DELETE_TIME = 0x0008; |
| 139 | private static final int GPS_DELETE_IONO = 0x0010; |
| 140 | private static final int GPS_DELETE_UTC = 0x0020; |
| 141 | private static final int GPS_DELETE_HEALTH = 0x0040; |
| 142 | private static final int GPS_DELETE_SVDIR = 0x0080; |
| 143 | private static final int GPS_DELETE_SVSTEER = 0x0100; |
| 144 | private static final int GPS_DELETE_SADATA = 0x0200; |
| 145 | private static final int GPS_DELETE_RTI = 0x0400; |
| 146 | private static final int GPS_DELETE_CELLDB_INFO = 0x8000; |
| 147 | private static final int GPS_DELETE_ALL = 0xFFFF; |
| 148 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 149 | // The GPS_CAPABILITY_* flags must match the values in gps.h |
| 150 | private static final int GPS_CAPABILITY_SCHEDULING = 0x0000001; |
| 151 | private static final int GPS_CAPABILITY_MSB = 0x0000002; |
| 152 | private static final int GPS_CAPABILITY_MSA = 0x0000004; |
| 153 | private static final int GPS_CAPABILITY_SINGLE_SHOT = 0x0000008; |
Mike Lockwood | 9b9fb5c | 2011-06-29 15:09:40 -0400 | [diff] [blame] | 154 | private static final int GPS_CAPABILITY_ON_DEMAND_TIME = 0x0000010; |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 155 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 156 | // these need to match AGpsType enum in gps.h |
| 157 | private static final int AGPS_TYPE_SUPL = 1; |
| 158 | private static final int AGPS_TYPE_C2K = 2; |
| 159 | |
| 160 | // for mAGpsDataConnectionState |
| 161 | private static final int AGPS_DATA_CONNECTION_CLOSED = 0; |
| 162 | private static final int AGPS_DATA_CONNECTION_OPENING = 1; |
| 163 | private static final int AGPS_DATA_CONNECTION_OPEN = 2; |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 164 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 165 | // Handler messages |
| 166 | private static final int CHECK_LOCATION = 1; |
| 167 | private static final int ENABLE = 2; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 168 | private static final int SET_REQUEST = 3; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 169 | private static final int UPDATE_NETWORK_STATE = 4; |
| 170 | private static final int INJECT_NTP_TIME = 5; |
| 171 | private static final int DOWNLOAD_XTRA_DATA = 6; |
| 172 | private static final int UPDATE_LOCATION = 7; |
| 173 | private static final int ADD_LISTENER = 8; |
| 174 | private static final int REMOVE_LISTENER = 9; |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 175 | private static final int INJECT_NTP_TIME_FINISHED = 10; |
| 176 | private static final int DOWNLOAD_XTRA_DATA_FINISHED = 11; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 177 | |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 178 | // Request setid |
| 179 | private static final int AGPS_RIL_REQUEST_SETID_IMSI = 1; |
| 180 | private static final int AGPS_RIL_REQUEST_SETID_MSISDN = 2; |
| 181 | |
| 182 | // Request ref location |
| 183 | private static final int AGPS_RIL_REQUEST_REFLOC_CELLID = 1; |
| 184 | private static final int AGPS_RIL_REQUEST_REFLOC_MAC = 2; |
| 185 | |
| 186 | // ref. location info |
| 187 | private static final int AGPS_REF_LOCATION_TYPE_GSM_CELLID = 1; |
| 188 | private static final int AGPS_REF_LOCATION_TYPE_UMTS_CELLID = 2; |
| 189 | private static final int AGPS_REG_LOCATION_TYPE_MAC = 3; |
| 190 | |
| 191 | // set id info |
| 192 | private static final int AGPS_SETID_TYPE_NONE = 0; |
| 193 | private static final int AGPS_SETID_TYPE_IMSI = 1; |
| 194 | private static final int AGPS_SETID_TYPE_MSISDN = 2; |
| 195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | private static final String PROPERTIES_FILE = "/etc/gps.conf"; |
| 197 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 198 | /** simpler wrapper for ProviderRequest + Worksource */ |
| 199 | private static class GpsRequest { |
| 200 | public ProviderRequest request; |
| 201 | public WorkSource source; |
| 202 | public GpsRequest(ProviderRequest request, WorkSource source) { |
| 203 | this.request = request; |
| 204 | this.source = source; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private Object mLock = new Object(); |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | private int mLocationFlags = LOCATION_INVALID; |
| 211 | |
| 212 | // current status |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 213 | private int mStatus = LocationProvider.TEMPORARILY_UNAVAILABLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
| 215 | // time for last status update |
| 216 | private long mStatusUpdateTime = SystemClock.elapsedRealtime(); |
Mike Lockwood | d53ba01 | 2010-04-15 20:41:26 -0400 | [diff] [blame] | 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | // turn off GPS fix icon if we haven't received a fix in 10 seconds |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 219 | private static final long RECENT_FIX_TIMEOUT = 10 * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 221 | // stop trying if we do not receive a fix within 60 seconds |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 222 | private static final int NO_FIX_TIMEOUT = 60 * 1000; |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 223 | |
Nick Pelly | b041f23 | 2012-05-07 17:12:25 -0700 | [diff] [blame] | 224 | // if the fix interval is below this we leave GPS on, |
| 225 | // if above then we cycle the GPS driver. |
| 226 | // Typical hot TTTF is ~5 seconds, so 10 seconds seems sane. |
| 227 | private static final int GPS_POLLING_THRESHOLD_INTERVAL = 10 * 1000; |
| 228 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 229 | // how often to request NTP time, in milliseconds |
| 230 | // current setting 24 hours |
| 231 | private static final long NTP_INTERVAL = 24*60*60*1000; |
| 232 | // how long to wait if we have a network error in NTP or XTRA downloading |
| 233 | // current setting - 5 minutes |
| 234 | private static final long RETRY_INTERVAL = 5*60*1000; |
| 235 | |
| 236 | // true if we are enabled, protected by this |
| 237 | private boolean mEnabled; |
| 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | // true if we have network connectivity |
| 240 | private boolean mNetworkAvailable; |
| 241 | |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 242 | // states for injecting ntp and downloading xtra data |
| 243 | private static final int STATE_PENDING_NETWORK = 0; |
| 244 | private static final int STATE_DOWNLOADING = 1; |
| 245 | private static final int STATE_IDLE = 2; |
| 246 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 247 | // flags to trigger NTP or XTRA data download when network becomes available |
| 248 | // initialized to true so we do NTP and XTRA when the network comes up after booting |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 249 | private int mInjectNtpTimePending = STATE_PENDING_NETWORK; |
| 250 | private int mDownloadXtraDataPending = STATE_PENDING_NETWORK; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 251 | |
Mike Lockwood | 9b9fb5c | 2011-06-29 15:09:40 -0400 | [diff] [blame] | 252 | // set to true if the GPS engine does not do on-demand NTP time requests |
| 253 | private boolean mPeriodicTimeInjection; |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // true if GPS is navigating |
| 256 | private boolean mNavigating; |
Mike Lockwood | cf1d8cb | 2010-01-20 10:14:54 -0500 | [diff] [blame] | 257 | |
| 258 | // true if GPS engine is on |
| 259 | private boolean mEngineOn; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 260 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 261 | // requested frequency of fixes, in milliseconds |
| 262 | private int mFixInterval = 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | |
| 264 | // true if we started navigation |
| 265 | private boolean mStarted; |
| 266 | |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 267 | // true if single shot request is in progress |
| 268 | private boolean mSingleShot; |
| 269 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 270 | // capabilities of the GPS engine |
| 271 | private int mEngineCapabilities; |
| 272 | |
Mike Lockwood | 1a1cd3a | 2010-08-17 07:42:54 -0400 | [diff] [blame] | 273 | // true if XTRA is supported |
| 274 | private boolean mSupportsXtra; |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | // for calculating time to first fix |
| 277 | private long mFixRequestTime = 0; |
| 278 | // time to first fix for most recent session |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 279 | private int mTimeToFirstFix = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | // time we received our last fix |
| 281 | private long mLastFixTime; |
| 282 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 283 | private int mPositionMode; |
| 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | // properties loaded from PROPERTIES_FILE |
| 286 | private Properties mProperties; |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 287 | private String mSuplServerHost; |
| 288 | private int mSuplServerPort; |
| 289 | private String mC2KServerHost; |
| 290 | private int mC2KServerPort; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 292 | private final Context mContext; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 293 | private final NtpTrustedTime mNtpTime; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 294 | private final ILocationManager mILocationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | private Location mLocation = new Location(LocationManager.GPS_PROVIDER); |
| 296 | private Bundle mLocationExtras = new Bundle(); |
| 297 | private ArrayList<Listener> mListeners = new ArrayList<Listener>(); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 298 | |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 299 | // Handler for processing events |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 300 | private Handler mHandler; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 301 | |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 302 | private String mAGpsApn; |
| 303 | private int mAGpsDataConnectionState; |
Stephen Li | 8efd74d | 2011-03-01 20:56:00 -0800 | [diff] [blame] | 304 | private int mAGpsDataConnectionIpAddr; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 305 | private final ConnectivityManager mConnMgr; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 306 | private final GpsNetInitiatedHandler mNIHandler; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 307 | |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 308 | // Wakelocks |
| 309 | private final static String WAKELOCK_KEY = "GpsLocationProvider"; |
| 310 | private final PowerManager.WakeLock mWakeLock; |
| 311 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 312 | // Alarms |
| 313 | private final static String ALARM_WAKEUP = "com.android.internal.location.ALARM_WAKEUP"; |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 314 | private final static String ALARM_TIMEOUT = "com.android.internal.location.ALARM_TIMEOUT"; |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 315 | private final AlarmManager mAlarmManager; |
| 316 | private final PendingIntent mWakeupIntent; |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 317 | private final PendingIntent mTimeoutIntent; |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 318 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 319 | private final IAppOpsService mAppOpsService; |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 320 | private final IBatteryStats mBatteryStats; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 321 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 322 | // only modified on handler thread |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 323 | private WorkSource mClientSource = new WorkSource(); |
Mike Lockwood | f1218be | 2010-01-29 09:20:06 -0500 | [diff] [blame] | 324 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 325 | private GeofenceHardwareImpl mGeofenceHardwareImpl; |
| 326 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 327 | private final IGpsStatusProvider mGpsStatusProvider = new IGpsStatusProvider.Stub() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 328 | @Override |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 329 | public void addGpsStatusListener(IGpsStatusListener listener) throws RemoteException { |
| 330 | if (listener == null) { |
| 331 | throw new NullPointerException("listener is null in addGpsStatusListener"); |
| 332 | } |
| 333 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 334 | synchronized (mListeners) { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 335 | IBinder binder = listener.asBinder(); |
| 336 | int size = mListeners.size(); |
| 337 | for (int i = 0; i < size; i++) { |
| 338 | Listener test = mListeners.get(i); |
| 339 | if (binder.equals(test.mListener.asBinder())) { |
| 340 | // listener already added |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | Listener l = new Listener(listener); |
| 346 | binder.linkToDeath(l, 0); |
| 347 | mListeners.add(l); |
| 348 | } |
| 349 | } |
| 350 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 351 | @Override |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 352 | public void removeGpsStatusListener(IGpsStatusListener listener) { |
| 353 | if (listener == null) { |
| 354 | throw new NullPointerException("listener is null in addGpsStatusListener"); |
| 355 | } |
| 356 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 357 | synchronized (mListeners) { |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 358 | IBinder binder = listener.asBinder(); |
| 359 | Listener l = null; |
| 360 | int size = mListeners.size(); |
| 361 | for (int i = 0; i < size && l == null; i++) { |
| 362 | Listener test = mListeners.get(i); |
| 363 | if (binder.equals(test.mListener.asBinder())) { |
| 364 | l = test; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (l != null) { |
| 369 | mListeners.remove(l); |
| 370 | binder.unlinkToDeath(l, 0); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | public IGpsStatusProvider getGpsStatusProvider() { |
| 377 | return mGpsStatusProvider; |
| 378 | } |
| 379 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 380 | public IGpsGeofenceHardware getGpsGeofenceProxy() { |
| 381 | return mGpsGeofenceBinder; |
| 382 | } |
| 383 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 384 | private final BroadcastReceiver mBroadcastReciever = new BroadcastReceiver() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 385 | @Override public void onReceive(Context context, Intent intent) { |
| 386 | String action = intent.getAction(); |
| 387 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 388 | if (action.equals(ALARM_WAKEUP)) { |
| 389 | if (DEBUG) Log.d(TAG, "ALARM_WAKEUP"); |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 390 | startNavigating(false); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 391 | } else if (action.equals(ALARM_TIMEOUT)) { |
| 392 | if (DEBUG) Log.d(TAG, "ALARM_TIMEOUT"); |
| 393 | hibernate(); |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 394 | } else if (action.equals(Intents.DATA_SMS_RECEIVED_ACTION)) { |
| 395 | checkSmsSuplInit(intent); |
| 396 | } else if (action.equals(Intents.WAP_PUSH_RECEIVED_ACTION)) { |
| 397 | checkWapSuplInit(intent); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 398 | } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
| 399 | int networkState; |
| 400 | if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { |
| 401 | networkState = LocationProvider.TEMPORARILY_UNAVAILABLE; |
| 402 | } else { |
| 403 | networkState = LocationProvider.AVAILABLE; |
| 404 | } |
| 405 | |
| 406 | // retrieve NetworkInfo result for this UID |
| 407 | NetworkInfo info = |
| 408 | intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); |
| 409 | ConnectivityManager connManager = (ConnectivityManager) |
| 410 | mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 411 | info = connManager.getNetworkInfo(info.getType()); |
| 412 | |
| 413 | updateNetworkState(networkState, info); |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 414 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 415 | } |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 416 | }; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 417 | |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 418 | private void checkSmsSuplInit(Intent intent) { |
| 419 | SmsMessage[] messages = Intents.getMessagesFromIntent(intent); |
| 420 | for (int i=0; i <messages.length; i++) { |
| 421 | byte[] supl_init = messages[i].getUserData(); |
| 422 | native_agps_ni_message(supl_init,supl_init.length); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private void checkWapSuplInit(Intent intent) { |
| 427 | byte[] supl_init = (byte[]) intent.getExtra("data"); |
| 428 | native_agps_ni_message(supl_init,supl_init.length); |
| 429 | } |
| 430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | public static boolean isSupported() { |
| 432 | return native_is_supported(); |
| 433 | } |
| 434 | |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 435 | public GpsLocationProvider(Context context, ILocationManager ilocationManager, |
| 436 | Looper looper) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | mContext = context; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 438 | mNtpTime = NtpTrustedTime.getInstance(context); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 439 | mILocationManager = ilocationManager; |
Mike Lockwood | 79e642e | 2010-03-17 23:19:25 -0400 | [diff] [blame] | 440 | mNIHandler = new GpsNetInitiatedHandler(context); |
Mike Lockwood | 63598a0 | 2010-02-24 11:52:59 -0500 | [diff] [blame] | 441 | |
| 442 | mLocation.setExtras(mLocationExtras); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 444 | // Create a wake lock |
| 445 | PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 446 | mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_KEY); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 447 | mWakeLock.setReferenceCounted(true); |
Mike Lockwood | 0528b9b | 2009-05-07 10:12:54 -0400 | [diff] [blame] | 448 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 449 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 450 | mWakeupIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_WAKEUP), 0); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 451 | mTimeoutIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ALARM_TIMEOUT), 0); |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 452 | |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 453 | mConnMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 454 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 455 | // App ops service to keep track of who is accessing the GPS |
| 456 | mAppOpsService = IAppOpsService.Stub.asInterface(ServiceManager.getService( |
| 457 | Context.APP_OPS_SERVICE)); |
| 458 | |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 459 | // Battery statistics service to be notified when GPS turns on or off |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 460 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 461 | BatteryStats.SERVICE_NAME)); |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | mProperties = new Properties(); |
| 464 | try { |
| 465 | File file = new File(PROPERTIES_FILE); |
| 466 | FileInputStream stream = new FileInputStream(file); |
| 467 | mProperties.load(stream); |
| 468 | stream.close(); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 469 | |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 470 | mSuplServerHost = mProperties.getProperty("SUPL_HOST"); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 471 | String portString = mProperties.getProperty("SUPL_PORT"); |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 472 | if (mSuplServerHost != null && portString != null) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 473 | try { |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 474 | mSuplServerPort = Integer.parseInt(portString); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 475 | } catch (NumberFormatException e) { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 476 | Log.e(TAG, "unable to parse SUPL_PORT: " + portString); |
| 477 | } |
| 478 | } |
| 479 | |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 480 | mC2KServerHost = mProperties.getProperty("C2K_HOST"); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 481 | portString = mProperties.getProperty("C2K_PORT"); |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 482 | if (mC2KServerHost != null && portString != null) { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 483 | try { |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 484 | mC2KServerPort = Integer.parseInt(portString); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 485 | } catch (NumberFormatException e) { |
| 486 | Log.e(TAG, "unable to parse C2K_PORT: " + portString); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } catch (IOException e) { |
| 490 | Log.w(TAG, "Could not open GPS configuration file " + PROPERTIES_FILE); |
| 491 | } |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 492 | |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 493 | // construct handler, listen for events |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 494 | mHandler = new ProviderHandler(looper); |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 495 | listenForBroadcasts(); |
| 496 | |
| 497 | // also listen for PASSIVE_PROVIDER updates |
| 498 | mHandler.post(new Runnable() { |
| 499 | @Override |
| 500 | public void run() { |
| 501 | LocationManager locManager = |
| 502 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 503 | locManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, |
| 504 | 0, 0, new NetworkLocationListener(), mHandler.getLooper()); |
Mike Lockwood | 8909631 | 2010-03-24 10:14:55 -0400 | [diff] [blame] | 505 | } |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 506 | }); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 509 | private void listenForBroadcasts() { |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 510 | IntentFilter intentFilter = new IntentFilter(); |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 511 | intentFilter.addAction(Intents.DATA_SMS_RECEIVED_ACTION); |
| 512 | intentFilter.addDataScheme("sms"); |
| 513 | intentFilter.addDataAuthority("localhost","7275"); |
| 514 | mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
| 515 | |
| 516 | intentFilter = new IntentFilter(); |
| 517 | intentFilter.addAction(Intents.WAP_PUSH_RECEIVED_ACTION); |
| 518 | try { |
| 519 | intentFilter.addDataType("application/vnd.omaloc-supl-init"); |
| 520 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 521 | Log.w(TAG, "Malformed SUPL init mime type"); |
| 522 | } |
| 523 | mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
| 524 | |
| 525 | intentFilter = new IntentFilter(); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 526 | intentFilter.addAction(ALARM_WAKEUP); |
| 527 | intentFilter.addAction(ALARM_TIMEOUT); |
Brian Muramatsu | b94b41f | 2012-08-21 16:30:57 -0700 | [diff] [blame] | 528 | intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Victoria Lease | 5c24fd0 | 2012-10-01 11:00:50 -0700 | [diff] [blame] | 529 | mContext.registerReceiver(mBroadcastReciever, intentFilter, null, mHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | /** |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 533 | * Returns the name of this provider. |
| 534 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 535 | @Override |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 536 | public String getName() { |
| 537 | return LocationManager.GPS_PROVIDER; |
| 538 | } |
| 539 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 540 | @Override |
| 541 | public ProviderProperties getProperties() { |
| 542 | return PROPERTIES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 545 | public void updateNetworkState(int state, NetworkInfo info) { |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 546 | sendMessage(UPDATE_NETWORK_STATE, state, info); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | private void handleUpdateNetworkState(int state, NetworkInfo info) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | mNetworkAvailable = (state == LocationProvider.AVAILABLE); |
| 551 | |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 552 | if (DEBUG) { |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 553 | Log.d(TAG, "updateNetworkState " + (mNetworkAvailable ? "available" : "unavailable") |
| 554 | + " info: " + info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 556 | |
Mike Lockwood | 50130bb | 2010-10-11 06:22:50 -0400 | [diff] [blame] | 557 | if (info != null) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 558 | boolean dataEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 559 | Settings.Global.MOBILE_DATA, 1) == 1; |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 560 | boolean networkAvailable = info.isAvailable() && dataEnabled; |
| 561 | String defaultApn = getSelectedApn(); |
| 562 | if (defaultApn == null) { |
| 563 | defaultApn = "dummy-apn"; |
| 564 | } |
| 565 | |
Mike Lockwood | 50130bb | 2010-10-11 06:22:50 -0400 | [diff] [blame] | 566 | native_update_network_state(info.isConnected(), info.getType(), |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 567 | info.isRoaming(), networkAvailable, |
| 568 | info.getExtraInfo(), defaultApn); |
Mike Lockwood | 50130bb | 2010-10-11 06:22:50 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 571 | if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE_SUPL |
| 572 | && mAGpsDataConnectionState == AGPS_DATA_CONNECTION_OPENING) { |
| 573 | String apnName = info.getExtraInfo(); |
Stephen Li | 83b6971 | 2011-01-25 18:47:28 -0800 | [diff] [blame] | 574 | if (mNetworkAvailable) { |
| 575 | if (apnName == null) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 576 | /* Assign a dummy value in the case of C2K as otherwise we will have a runtime |
Stephen Li | 83b6971 | 2011-01-25 18:47:28 -0800 | [diff] [blame] | 577 | exception in the following call to native_agps_data_conn_open*/ |
| 578 | apnName = "dummy-apn"; |
| 579 | } |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 580 | mAGpsApn = apnName; |
Stephen Li | 8efd74d | 2011-03-01 20:56:00 -0800 | [diff] [blame] | 581 | if (DEBUG) Log.d(TAG, "mAGpsDataConnectionIpAddr " + mAGpsDataConnectionIpAddr); |
| 582 | if (mAGpsDataConnectionIpAddr != 0xffffffff) { |
| 583 | boolean route_result; |
| 584 | if (DEBUG) Log.d(TAG, "call requestRouteToHost"); |
| 585 | route_result = mConnMgr.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_SUPL, |
| 586 | mAGpsDataConnectionIpAddr); |
| 587 | if (route_result == false) Log.d(TAG, "call requestRouteToHost failed"); |
| 588 | } |
Mike Lockwood | 03d2467 | 2009-10-08 15:45:03 -0400 | [diff] [blame] | 589 | if (DEBUG) Log.d(TAG, "call native_agps_data_conn_open"); |
| 590 | native_agps_data_conn_open(apnName); |
| 591 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPEN; |
| 592 | } else { |
| 593 | if (DEBUG) Log.d(TAG, "call native_agps_data_conn_failed"); |
| 594 | mAGpsApn = null; |
| 595 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_CLOSED; |
| 596 | native_agps_data_conn_failed(); |
| 597 | } |
| 598 | } |
| 599 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 600 | if (mNetworkAvailable) { |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 601 | if (mInjectNtpTimePending == STATE_PENDING_NETWORK) { |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 602 | sendMessage(INJECT_NTP_TIME, 0, null); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 603 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 604 | if (mDownloadXtraDataPending == STATE_PENDING_NETWORK) { |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 605 | sendMessage(DOWNLOAD_XTRA_DATA, 0, null); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | private void handleInjectNtpTime() { |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 611 | if (mInjectNtpTimePending == STATE_DOWNLOADING) { |
| 612 | // already downloading data |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 613 | return; |
| 614 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 615 | if (!mNetworkAvailable) { |
| 616 | // try again when network is up |
| 617 | mInjectNtpTimePending = STATE_PENDING_NETWORK; |
| 618 | return; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 619 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 620 | mInjectNtpTimePending = STATE_DOWNLOADING; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 621 | |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 622 | // hold wake lock while task runs |
| 623 | mWakeLock.acquire(); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 624 | AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 625 | @Override |
| 626 | public void run() { |
| 627 | long delay; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 628 | |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 629 | // force refresh NTP cache when outdated |
| 630 | if (mNtpTime.getCacheAge() >= NTP_INTERVAL) { |
| 631 | mNtpTime.forceRefresh(); |
| 632 | } |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 633 | |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 634 | // only update when NTP time is fresh |
| 635 | if (mNtpTime.getCacheAge() < NTP_INTERVAL) { |
| 636 | long time = mNtpTime.getCachedNtpTime(); |
| 637 | long timeReference = mNtpTime.getCachedNtpTimeReference(); |
| 638 | long certainty = mNtpTime.getCacheCertainty(); |
| 639 | long now = System.currentTimeMillis(); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 640 | |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 641 | Log.d(TAG, "NTP server returned: " |
| 642 | + time + " (" + new Date(time) |
| 643 | + ") reference: " + timeReference |
| 644 | + " certainty: " + certainty |
| 645 | + " system time offset: " + (time - now)); |
| 646 | |
| 647 | native_inject_time(time, timeReference, (int) certainty); |
| 648 | delay = NTP_INTERVAL; |
| 649 | } else { |
| 650 | if (DEBUG) Log.d(TAG, "requestTime failed"); |
| 651 | delay = RETRY_INTERVAL; |
| 652 | } |
| 653 | |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 654 | sendMessage(INJECT_NTP_TIME_FINISHED, 0, null); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 655 | |
| 656 | if (mPeriodicTimeInjection) { |
| 657 | // send delayed message for next NTP injection |
| 658 | // since this is delayed and not urgent we do not hold a wake lock here |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 659 | mHandler.sendEmptyMessageDelayed(INJECT_NTP_TIME, delay); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 660 | } |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 661 | |
| 662 | // release wake lock held by task |
| 663 | mWakeLock.release(); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 664 | } |
| 665 | }); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | private void handleDownloadXtraData() { |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 669 | if (mDownloadXtraDataPending == STATE_DOWNLOADING) { |
| 670 | // already downloading data |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 671 | return; |
| 672 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 673 | if (!mNetworkAvailable) { |
| 674 | // try again when network is up |
| 675 | mDownloadXtraDataPending = STATE_PENDING_NETWORK; |
| 676 | return; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 677 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 678 | mDownloadXtraDataPending = STATE_DOWNLOADING; |
| 679 | |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 680 | // hold wake lock while task runs |
| 681 | mWakeLock.acquire(); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 682 | AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 683 | @Override |
| 684 | public void run() { |
| 685 | GpsXtraDownloader xtraDownloader = new GpsXtraDownloader(mContext, mProperties); |
| 686 | byte[] data = xtraDownloader.downloadXtraData(); |
| 687 | if (data != null) { |
| 688 | if (DEBUG) { |
| 689 | Log.d(TAG, "calling native_inject_xtra_data"); |
| 690 | } |
| 691 | native_inject_xtra_data(data, data.length); |
| 692 | } |
| 693 | |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 694 | sendMessage(DOWNLOAD_XTRA_DATA_FINISHED, 0, null); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 695 | |
| 696 | if (data == null) { |
| 697 | // try again later |
| 698 | // since this is delayed and not urgent we do not hold a wake lock here |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 699 | mHandler.sendEmptyMessageDelayed(DOWNLOAD_XTRA_DATA, RETRY_INTERVAL); |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 700 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 701 | |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 702 | // release wake lock held by task |
| 703 | mWakeLock.release(); |
| 704 | } |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 705 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 708 | private void handleUpdateLocation(Location location) { |
Mike Lockwood | d26ce0d | 2009-06-11 12:25:46 -0400 | [diff] [blame] | 709 | if (location.hasAccuracy()) { |
| 710 | native_inject_location(location.getLatitude(), location.getLongitude(), |
| 711 | location.getAccuracy()); |
| 712 | } |
Mike Lockwood | fd6e5f0 | 2009-05-21 11:28:20 -0400 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | * Enables this provider. When enabled, calls to getStatus() |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 717 | * must be handled. Hardware may be started up |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | * when the provider is enabled. |
| 719 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 720 | @Override |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 721 | public void enable() { |
Laurent Tu | 7b5aeca | 2013-01-24 15:10:24 -0800 | [diff] [blame] | 722 | synchronized (mLock) { |
| 723 | if (mEnabled) return; |
| 724 | mEnabled = true; |
| 725 | } |
| 726 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 727 | sendMessage(ENABLE, 1, null); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | private void handleEnable() { |
| 731 | if (DEBUG) Log.d(TAG, "handleEnable"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 733 | boolean enabled = native_init(); |
| 734 | |
| 735 | if (enabled) { |
Mike Lockwood | 1a1cd3a | 2010-08-17 07:42:54 -0400 | [diff] [blame] | 736 | mSupportsXtra = native_supports_xtra(); |
Mike Lockwood | 734d603 | 2009-07-28 18:30:25 -0700 | [diff] [blame] | 737 | if (mSuplServerHost != null) { |
| 738 | native_set_agps_server(AGPS_TYPE_SUPL, mSuplServerHost, mSuplServerPort); |
| 739 | } |
| 740 | if (mC2KServerHost != null) { |
| 741 | native_set_agps_server(AGPS_TYPE_C2K, mC2KServerHost, mC2KServerPort); |
| 742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | } else { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 744 | synchronized (mLock) { |
| 745 | mEnabled = false; |
| 746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | Log.w(TAG, "Failed to enable location provider"); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Disables this provider. When disabled, calls to getStatus() |
Mike Lockwood | 4e50b78 | 2009-04-03 08:24:43 -0700 | [diff] [blame] | 753 | * need not be handled. Hardware may be shut |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | * down while the provider is disabled. |
| 755 | */ |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 756 | @Override |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 757 | public void disable() { |
Laurent Tu | 7b5aeca | 2013-01-24 15:10:24 -0800 | [diff] [blame] | 758 | synchronized (mLock) { |
| 759 | if (!mEnabled) return; |
| 760 | mEnabled = false; |
| 761 | } |
| 762 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 763 | sendMessage(ENABLE, 0, null); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | private void handleDisable() { |
Mike Lockwood | 8909631 | 2010-03-24 10:14:55 -0400 | [diff] [blame] | 767 | if (DEBUG) Log.d(TAG, "handleDisable"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | stopNavigating(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 770 | mAlarmManager.cancel(mWakeupIntent); |
| 771 | mAlarmManager.cancel(mTimeoutIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | |
Mike Lockwood | cf1d8cb | 2010-01-20 10:14:54 -0500 | [diff] [blame] | 773 | // do this before releasing wakelock |
| 774 | native_cleanup(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 777 | @Override |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 778 | public boolean isEnabled() { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 779 | synchronized (mLock) { |
| 780 | return mEnabled; |
| 781 | } |
Mike Lockwood | d03ff94 | 2010-02-09 08:46:14 -0500 | [diff] [blame] | 782 | } |
| 783 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 784 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | public int getStatus(Bundle extras) { |
| 786 | if (extras != null) { |
| 787 | extras.putInt("satellites", mSvCount); |
| 788 | } |
| 789 | return mStatus; |
| 790 | } |
| 791 | |
| 792 | private void updateStatus(int status, int svCount) { |
| 793 | if (status != mStatus || svCount != mSvCount) { |
| 794 | mStatus = status; |
| 795 | mSvCount = svCount; |
| 796 | mLocationExtras.putInt("satellites", svCount); |
| 797 | mStatusUpdateTime = SystemClock.elapsedRealtime(); |
| 798 | } |
| 799 | } |
| 800 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 801 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | public long getStatusUpdateTime() { |
| 803 | return mStatusUpdateTime; |
| 804 | } |
| 805 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 806 | @Override |
| 807 | public void setRequest(ProviderRequest request, WorkSource source) { |
| 808 | sendMessage(SET_REQUEST, 0, new GpsRequest(request, source)); |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 811 | private void handleSetRequest(ProviderRequest request, WorkSource source) { |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 812 | boolean singleShot = false; |
| 813 | |
| 814 | // see if the request is for a single update |
| 815 | if (request.locationRequests != null && request.locationRequests.size() > 0) { |
| 816 | // if any request has zero or more than one updates |
| 817 | // requested, then this is not single-shot mode |
| 818 | singleShot = true; |
| 819 | |
| 820 | for (LocationRequest lr : request.locationRequests) { |
| 821 | if (lr.getNumUpdates() != 1) { |
| 822 | singleShot = false; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 827 | if (DEBUG) Log.d(TAG, "setRequest " + request); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 828 | if (request.reportLocation) { |
| 829 | // update client uids |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 830 | updateClientUids(source); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 832 | mFixInterval = (int) request.interval; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 833 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 834 | // check for overflow |
| 835 | if (mFixInterval != request.interval) { |
| 836 | Log.w(TAG, "interval overflow: " + request.interval); |
| 837 | mFixInterval = Integer.MAX_VALUE; |
| 838 | } |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 839 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 840 | // apply request to GPS engine |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 841 | if (mStarted && hasCapability(GPS_CAPABILITY_SCHEDULING)) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 842 | // change period |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 843 | if (!native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC, |
| 844 | mFixInterval, 0, 0)) { |
| 845 | Log.e(TAG, "set_position_mode failed in setMinTime()"); |
| 846 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 847 | } else if (!mStarted) { |
| 848 | // start GPS |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 849 | startNavigating(singleShot); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 851 | } else { |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 852 | updateClientUids(new WorkSource()); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 853 | |
| 854 | stopNavigating(); |
| 855 | mAlarmManager.cancel(mWakeupIntent); |
| 856 | mAlarmManager.cancel(mTimeoutIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | private final class Listener implements IBinder.DeathRecipient { |
| 861 | final IGpsStatusListener mListener; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | Listener(IGpsStatusListener listener) { |
| 864 | mListener = listener; |
| 865 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 866 | |
| 867 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | public void binderDied() { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 869 | if (DEBUG) Log.d(TAG, "GPS status listener died"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 871 | synchronized (mListeners) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | mListeners.remove(this); |
| 873 | } |
Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 874 | if (mListener != null) { |
| 875 | mListener.asBinder().unlinkToDeath(this, 0); |
| 876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 880 | private void updateClientUids(WorkSource source) { |
| 881 | // Update work source. |
| 882 | WorkSource[] changes = mClientSource.setReturningDiffs(source); |
Victoria Lease | ea78b85 | 2013-01-15 10:39:28 -0800 | [diff] [blame] | 883 | if (changes == null) { |
| 884 | return; |
| 885 | } |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 886 | WorkSource newWork = changes[0]; |
| 887 | WorkSource goneWork = changes[1]; |
| 888 | |
| 889 | // Update sources that were not previously tracked. |
| 890 | if (newWork != null) { |
| 891 | int lastuid = -1; |
| 892 | for (int i=0; i<newWork.size(); i++) { |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 893 | try { |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 894 | int uid = newWork.get(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 895 | mAppOpsService.startOperation(AppOpsManager.getToken(mAppOpsService), |
| 896 | AppOpsManager.OP_GPS, uid, newWork.getName(i)); |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 897 | if (uid != lastuid) { |
| 898 | lastuid = uid; |
| 899 | mBatteryStats.noteStartGps(uid); |
| 900 | } |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 901 | } catch (RemoteException e) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 902 | Log.w(TAG, "RemoteException", e); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 907 | // Update sources that are no longer tracked. |
| 908 | if (goneWork != null) { |
| 909 | int lastuid = -1; |
| 910 | for (int i=0; i<goneWork.size(); i++) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 911 | try { |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 912 | int uid = goneWork.get(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 913 | mAppOpsService.finishOperation(AppOpsManager.getToken(mAppOpsService), |
| 914 | AppOpsManager.OP_GPS, uid, goneWork.getName(i)); |
Dianne Hackborn | 002a54e | 2013-01-10 17:34:55 -0800 | [diff] [blame] | 915 | if (uid != lastuid) { |
| 916 | lastuid = uid; |
| 917 | mBatteryStats.noteStopGps(uid); |
| 918 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 919 | } catch (RemoteException e) { |
| 920 | Log.w(TAG, "RemoteException", e); |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 921 | } |
Mike Lockwood | 2f82c4e | 2009-04-17 08:24:10 -0400 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 926 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | public boolean sendExtraCommand(String command, Bundle extras) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 928 | |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 929 | long identity = Binder.clearCallingIdentity(); |
| 930 | boolean result = false; |
| 931 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | if ("delete_aiding_data".equals(command)) { |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 933 | result = deleteAidingData(extras); |
| 934 | } else if ("force_time_injection".equals(command)) { |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 935 | sendMessage(INJECT_NTP_TIME, 0, null); |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 936 | result = true; |
| 937 | } else if ("force_xtra_injection".equals(command)) { |
Mike Lockwood | 1a1cd3a | 2010-08-17 07:42:54 -0400 | [diff] [blame] | 938 | if (mSupportsXtra) { |
Mike Lockwood | 93bc44d | 2009-05-20 16:58:22 -0400 | [diff] [blame] | 939 | xtraDownloadRequest(); |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 940 | result = true; |
Mike Lockwood | 93bc44d | 2009-05-20 16:58:22 -0400 | [diff] [blame] | 941 | } |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 942 | } else { |
| 943 | Log.w(TAG, "sendExtraCommand: unknown command " + command); |
Mike Lockwood | 93bc44d | 2009-05-20 16:58:22 -0400 | [diff] [blame] | 944 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 945 | |
Mike Lockwood | 63aa5a6 | 2010-04-14 19:21:31 -0400 | [diff] [blame] | 946 | Binder.restoreCallingIdentity(identity); |
| 947 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
| 949 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 950 | private IGpsGeofenceHardware mGpsGeofenceBinder = new IGpsGeofenceHardware.Stub() { |
| 951 | public boolean isHardwareGeofenceSupported() { |
| 952 | return native_is_geofence_supported(); |
| 953 | } |
| 954 | |
| 955 | public boolean addCircularHardwareGeofence(int geofenceId, double latitude, |
| 956 | double longitude, double radius, int lastTransition, int monitorTransitions, |
| 957 | int notificationResponsiveness, int unknownTimer) { |
| 958 | return native_add_geofence(geofenceId, latitude, longitude, radius, |
| 959 | lastTransition, monitorTransitions, notificationResponsiveness, unknownTimer); |
| 960 | } |
| 961 | |
| 962 | public boolean removeHardwareGeofence(int geofenceId) { |
| 963 | return native_remove_geofence(geofenceId); |
| 964 | } |
| 965 | |
| 966 | public boolean pauseHardwareGeofence(int geofenceId) { |
| 967 | return native_pause_geofence(geofenceId); |
| 968 | } |
| 969 | |
| 970 | public boolean resumeHardwareGeofence(int geofenceId, int monitorTransition) { |
| 971 | return native_resume_geofence(geofenceId, monitorTransition); |
| 972 | } |
| 973 | }; |
| 974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | private boolean deleteAidingData(Bundle extras) { |
| 976 | int flags; |
| 977 | |
| 978 | if (extras == null) { |
| 979 | flags = GPS_DELETE_ALL; |
| 980 | } else { |
| 981 | flags = 0; |
| 982 | if (extras.getBoolean("ephemeris")) flags |= GPS_DELETE_EPHEMERIS; |
| 983 | if (extras.getBoolean("almanac")) flags |= GPS_DELETE_ALMANAC; |
| 984 | if (extras.getBoolean("position")) flags |= GPS_DELETE_POSITION; |
| 985 | if (extras.getBoolean("time")) flags |= GPS_DELETE_TIME; |
| 986 | if (extras.getBoolean("iono")) flags |= GPS_DELETE_IONO; |
| 987 | if (extras.getBoolean("utc")) flags |= GPS_DELETE_UTC; |
| 988 | if (extras.getBoolean("health")) flags |= GPS_DELETE_HEALTH; |
| 989 | if (extras.getBoolean("svdir")) flags |= GPS_DELETE_SVDIR; |
| 990 | if (extras.getBoolean("svsteer")) flags |= GPS_DELETE_SVSTEER; |
| 991 | if (extras.getBoolean("sadata")) flags |= GPS_DELETE_SADATA; |
| 992 | if (extras.getBoolean("rti")) flags |= GPS_DELETE_RTI; |
| 993 | if (extras.getBoolean("celldb-info")) flags |= GPS_DELETE_CELLDB_INFO; |
| 994 | if (extras.getBoolean("all")) flags |= GPS_DELETE_ALL; |
| 995 | } |
| 996 | |
| 997 | if (flags != 0) { |
| 998 | native_delete_aiding_data(flags); |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | return false; |
| 1003 | } |
| 1004 | |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1005 | private void startNavigating(boolean singleShot) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | if (!mStarted) { |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1007 | if (DEBUG) Log.d(TAG, "startNavigating, singleShot is " + singleShot); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1008 | mTimeToFirstFix = 0; |
| 1009 | mLastFixTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | mStarted = true; |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1011 | mSingleShot = singleShot; |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1012 | mPositionMode = GPS_POSITION_MODE_STANDALONE; |
| 1013 | |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1014 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 1015 | Settings.Global.ASSISTED_GPS_ENABLED, 1) != 0) { |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1016 | if (singleShot && hasCapability(GPS_CAPABILITY_MSA)) { |
| 1017 | mPositionMode = GPS_POSITION_MODE_MS_ASSISTED; |
| 1018 | } else if (hasCapability(GPS_CAPABILITY_MSB)) { |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1019 | mPositionMode = GPS_POSITION_MODE_MS_BASED; |
| 1020 | } |
Mike Lockwood | bcab8df | 2009-06-25 16:39:09 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1023 | if (DEBUG) { |
| 1024 | String mode; |
| 1025 | |
| 1026 | switch(mPositionMode) { |
| 1027 | case GPS_POSITION_MODE_STANDALONE: |
| 1028 | mode = "standalone"; |
| 1029 | break; |
| 1030 | case GPS_POSITION_MODE_MS_ASSISTED: |
| 1031 | mode = "MS_ASSISTED"; |
| 1032 | break; |
| 1033 | case GPS_POSITION_MODE_MS_BASED: |
| 1034 | mode = "MS_BASED"; |
| 1035 | break; |
| 1036 | default: |
| 1037 | mode = "unknown"; |
| 1038 | break; |
| 1039 | } |
| 1040 | Log.d(TAG, "setting position_mode to " + mode); |
| 1041 | } |
| 1042 | |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1043 | int interval = (hasCapability(GPS_CAPABILITY_SCHEDULING) ? mFixInterval : 1000); |
| 1044 | if (!native_set_position_mode(mPositionMode, GPS_POSITION_RECURRENCE_PERIODIC, |
| 1045 | interval, 0, 0)) { |
| 1046 | mStarted = false; |
| 1047 | Log.e(TAG, "set_position_mode failed in startNavigating()"); |
| 1048 | return; |
| 1049 | } |
| 1050 | if (!native_start()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | mStarted = false; |
| 1052 | Log.e(TAG, "native_start failed in startNavigating()"); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1053 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // reset SV count to zero |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1057 | updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, 0); |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1058 | mFixRequestTime = System.currentTimeMillis(); |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1059 | if (!hasCapability(GPS_CAPABILITY_SCHEDULING)) { |
| 1060 | // set timer to give up if we do not receive a fix within NO_FIX_TIMEOUT |
| 1061 | // and our fix interval is not short |
| 1062 | if (mFixInterval >= NO_FIX_TIMEOUT) { |
| 1063 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
| 1064 | SystemClock.elapsedRealtime() + NO_FIX_TIMEOUT, mTimeoutIntent); |
| 1065 | } |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1066 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1070 | private void stopNavigating() { |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1071 | if (DEBUG) Log.d(TAG, "stopNavigating"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | if (mStarted) { |
| 1073 | mStarted = false; |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1074 | mSingleShot = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | native_stop(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1076 | mTimeToFirstFix = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | mLastFixTime = 0; |
| 1078 | mLocationFlags = LOCATION_INVALID; |
| 1079 | |
| 1080 | // reset SV count to zero |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1081 | updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1085 | private void hibernate() { |
| 1086 | // stop GPS until our next fix interval arrives |
| 1087 | stopNavigating(); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1088 | mAlarmManager.cancel(mTimeoutIntent); |
| 1089 | mAlarmManager.cancel(mWakeupIntent); |
| 1090 | long now = SystemClock.elapsedRealtime(); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1091 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, now + mFixInterval, mWakeupIntent); |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | private boolean hasCapability(int capability) { |
| 1095 | return ((mEngineCapabilities & capability) != 0); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 1098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | /** |
| 1100 | * called from native code to update our position. |
| 1101 | */ |
| 1102 | private void reportLocation(int flags, double latitude, double longitude, double altitude, |
| 1103 | float speed, float bearing, float accuracy, long timestamp) { |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1104 | if (VERBOSE) Log.v(TAG, "reportLocation lat: " + latitude + " long: " + longitude + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | " timestamp: " + timestamp); |
| 1106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | synchronized (mLocation) { |
| 1108 | mLocationFlags = flags; |
| 1109 | if ((flags & LOCATION_HAS_LAT_LONG) == LOCATION_HAS_LAT_LONG) { |
| 1110 | mLocation.setLatitude(latitude); |
| 1111 | mLocation.setLongitude(longitude); |
| 1112 | mLocation.setTime(timestamp); |
Nick Pelly | 2eeeec2 | 2012-07-18 13:13:37 -0700 | [diff] [blame] | 1113 | // It would be nice to push the elapsed real-time timestamp |
| 1114 | // further down the stack, but this is still useful |
Philip Milne | 4118012 | 2012-09-26 11:29:25 -0700 | [diff] [blame] | 1115 | mLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | if ((flags & LOCATION_HAS_ALTITUDE) == LOCATION_HAS_ALTITUDE) { |
| 1118 | mLocation.setAltitude(altitude); |
| 1119 | } else { |
| 1120 | mLocation.removeAltitude(); |
| 1121 | } |
| 1122 | if ((flags & LOCATION_HAS_SPEED) == LOCATION_HAS_SPEED) { |
| 1123 | mLocation.setSpeed(speed); |
| 1124 | } else { |
| 1125 | mLocation.removeSpeed(); |
| 1126 | } |
| 1127 | if ((flags & LOCATION_HAS_BEARING) == LOCATION_HAS_BEARING) { |
| 1128 | mLocation.setBearing(bearing); |
| 1129 | } else { |
| 1130 | mLocation.removeBearing(); |
| 1131 | } |
| 1132 | if ((flags & LOCATION_HAS_ACCURACY) == LOCATION_HAS_ACCURACY) { |
| 1133 | mLocation.setAccuracy(accuracy); |
| 1134 | } else { |
| 1135 | mLocation.removeAccuracy(); |
| 1136 | } |
Sean Barbeau | ced2b2c | 2011-12-19 04:43:07 -0500 | [diff] [blame] | 1137 | mLocation.setExtras(mLocationExtras); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1139 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1140 | mILocationManager.reportLocation(mLocation, false); |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1141 | } catch (RemoteException e) { |
| 1142 | Log.e(TAG, "RemoteException calling reportLocation"); |
| 1143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Hakan Gustavsson | fa94ff0 | 2010-02-23 09:12:38 +0100 | [diff] [blame] | 1146 | mLastFixTime = System.currentTimeMillis(); |
| 1147 | // report time to first fix |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1148 | if (mTimeToFirstFix == 0 && (flags & LOCATION_HAS_LAT_LONG) == LOCATION_HAS_LAT_LONG) { |
| 1149 | mTimeToFirstFix = (int)(mLastFixTime - mFixRequestTime); |
| 1150 | if (DEBUG) Log.d(TAG, "TTFF: " + mTimeToFirstFix); |
Hakan Gustavsson | fa94ff0 | 2010-02-23 09:12:38 +0100 | [diff] [blame] | 1151 | |
| 1152 | // notify status listeners |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1153 | synchronized (mListeners) { |
Hakan Gustavsson | fa94ff0 | 2010-02-23 09:12:38 +0100 | [diff] [blame] | 1154 | int size = mListeners.size(); |
| 1155 | for (int i = 0; i < size; i++) { |
| 1156 | Listener listener = mListeners.get(i); |
| 1157 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1158 | listener.mListener.onFirstFix(mTimeToFirstFix); |
Hakan Gustavsson | fa94ff0 | 2010-02-23 09:12:38 +0100 | [diff] [blame] | 1159 | } catch (RemoteException e) { |
| 1160 | Log.w(TAG, "RemoteException in stopNavigating"); |
| 1161 | mListeners.remove(listener); |
| 1162 | // adjust for size of list changing |
| 1163 | size--; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1169 | if (mSingleShot) { |
| 1170 | stopNavigating(); |
| 1171 | } |
| 1172 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1173 | if (mStarted && mStatus != LocationProvider.AVAILABLE) { |
Mike Lockwood | d53ba01 | 2010-04-15 20:41:26 -0400 | [diff] [blame] | 1174 | // we want to time out if we do not receive a fix |
Mike Lockwood | b7be544 | 2010-02-24 14:34:50 -0500 | [diff] [blame] | 1175 | // within the time out and we are requesting infrequent fixes |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1176 | if (!hasCapability(GPS_CAPABILITY_SCHEDULING) && mFixInterval < NO_FIX_TIMEOUT) { |
Mike Lockwood | b7be544 | 2010-02-24 14:34:50 -0500 | [diff] [blame] | 1177 | mAlarmManager.cancel(mTimeoutIntent); |
| 1178 | } |
| 1179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | // send an intent to notify that the GPS is receiving fixes. |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 1181 | Intent intent = new Intent(LocationManager.GPS_FIX_CHANGE_ACTION); |
| 1182 | intent.putExtra(LocationManager.EXTRA_GPS_ENABLED, true); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1183 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1184 | updateStatus(LocationProvider.AVAILABLE, mSvCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1186 | |
Nick Pelly | b041f23 | 2012-05-07 17:12:25 -0700 | [diff] [blame] | 1187 | if (!hasCapability(GPS_CAPABILITY_SCHEDULING) && mStarted && |
| 1188 | mFixInterval > GPS_POLLING_THRESHOLD_INTERVAL) { |
Mike Lockwood | d53ba01 | 2010-04-15 20:41:26 -0400 | [diff] [blame] | 1189 | if (DEBUG) Log.d(TAG, "got fix, hibernating"); |
Mike Lockwood | 0632ca7 | 2009-05-14 15:51:03 -0400 | [diff] [blame] | 1190 | hibernate(); |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * called from native code to update our status |
| 1196 | */ |
| 1197 | private void reportStatus(int status) { |
Mike Lockwood | b8d9033 | 2010-10-18 17:59:48 -0400 | [diff] [blame] | 1198 | if (DEBUG) Log.v(TAG, "reportStatus status: " + status); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1200 | synchronized (mListeners) { |
Dianne Hackborn | 2e41842 | 2009-06-22 20:00:17 -0700 | [diff] [blame] | 1201 | boolean wasNavigating = mNavigating; |
Mike Lockwood | dbd6fd8 | 2009-12-07 18:43:36 -0500 | [diff] [blame] | 1202 | |
Mike Lockwood | cf1d8cb | 2010-01-20 10:14:54 -0500 | [diff] [blame] | 1203 | switch (status) { |
| 1204 | case GPS_STATUS_SESSION_BEGIN: |
| 1205 | mNavigating = true; |
Mike Lockwood | 271f9f1 | 2010-02-02 11:41:52 -0500 | [diff] [blame] | 1206 | mEngineOn = true; |
Mike Lockwood | cf1d8cb | 2010-01-20 10:14:54 -0500 | [diff] [blame] | 1207 | break; |
| 1208 | case GPS_STATUS_SESSION_END: |
| 1209 | mNavigating = false; |
| 1210 | break; |
| 1211 | case GPS_STATUS_ENGINE_ON: |
| 1212 | mEngineOn = true; |
| 1213 | break; |
| 1214 | case GPS_STATUS_ENGINE_OFF: |
| 1215 | mEngineOn = false; |
Mike Lockwood | 271f9f1 | 2010-02-02 11:41:52 -0500 | [diff] [blame] | 1216 | mNavigating = false; |
Mike Lockwood | cf1d8cb | 2010-01-20 10:14:54 -0500 | [diff] [blame] | 1217 | break; |
| 1218 | } |
| 1219 | |
Mike Lockwood | dbd6fd8 | 2009-12-07 18:43:36 -0500 | [diff] [blame] | 1220 | if (wasNavigating != mNavigating) { |
| 1221 | int size = mListeners.size(); |
| 1222 | for (int i = 0; i < size; i++) { |
| 1223 | Listener listener = mListeners.get(i); |
| 1224 | try { |
| 1225 | if (mNavigating) { |
| 1226 | listener.mListener.onGpsStarted(); |
| 1227 | } else { |
| 1228 | listener.mListener.onGpsStopped(); |
| 1229 | } |
| 1230 | } catch (RemoteException e) { |
| 1231 | Log.w(TAG, "RemoteException in reportStatus"); |
| 1232 | mListeners.remove(listener); |
| 1233 | // adjust for size of list changing |
| 1234 | size--; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Mike Lockwood | dbd6fd8 | 2009-12-07 18:43:36 -0500 | [diff] [blame] | 1238 | // send an intent to notify that the GPS has been enabled or disabled. |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 1239 | Intent intent = new Intent(LocationManager.GPS_ENABLED_CHANGE_ACTION); |
| 1240 | intent.putExtra(LocationManager.EXTRA_GPS_ENABLED, mNavigating); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1241 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * called from native code to update SV info |
| 1248 | */ |
| 1249 | private void reportSvStatus() { |
| 1250 | |
| 1251 | int svCount = native_read_sv_status(mSvs, mSnrs, mSvElevations, mSvAzimuths, mSvMasks); |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1252 | |
| 1253 | synchronized (mListeners) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | int size = mListeners.size(); |
| 1255 | for (int i = 0; i < size; i++) { |
| 1256 | Listener listener = mListeners.get(i); |
| 1257 | try { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1258 | listener.mListener.onSvStatusChanged(svCount, mSvs, mSnrs, |
| 1259 | mSvElevations, mSvAzimuths, mSvMasks[EPHEMERIS_MASK], |
| 1260 | mSvMasks[ALMANAC_MASK], mSvMasks[USED_FOR_FIX_MASK]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | } catch (RemoteException e) { |
| 1262 | Log.w(TAG, "RemoteException in reportSvInfo"); |
| 1263 | mListeners.remove(listener); |
| 1264 | // adjust for size of list changing |
| 1265 | size--; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1270 | if (VERBOSE) { |
| 1271 | Log.v(TAG, "SV count: " + svCount + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | " ephemerisMask: " + Integer.toHexString(mSvMasks[EPHEMERIS_MASK]) + |
| 1273 | " almanacMask: " + Integer.toHexString(mSvMasks[ALMANAC_MASK])); |
| 1274 | for (int i = 0; i < svCount; i++) { |
Mike Lockwood | 29c8434 | 2009-05-06 14:01:15 -0400 | [diff] [blame] | 1275 | Log.v(TAG, "sv: " + mSvs[i] + |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1276 | " snr: " + mSnrs[i]/10 + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | " elev: " + mSvElevations[i] + |
| 1278 | " azimuth: " + mSvAzimuths[i] + |
| 1279 | ((mSvMasks[EPHEMERIS_MASK] & (1 << (mSvs[i] - 1))) == 0 ? " " : " E") + |
| 1280 | ((mSvMasks[ALMANAC_MASK] & (1 << (mSvs[i] - 1))) == 0 ? " " : " A") + |
| 1281 | ((mSvMasks[USED_FOR_FIX_MASK] & (1 << (mSvs[i] - 1))) == 0 ? "" : "U")); |
| 1282 | } |
| 1283 | } |
| 1284 | |
Kevin.KY Tsai | 0881f4f | 2010-05-21 15:10:39 -0400 | [diff] [blame] | 1285 | // return number of sets used in fix instead of total |
| 1286 | updateStatus(mStatus, Integer.bitCount(mSvMasks[USED_FOR_FIX_MASK])); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1288 | if (mNavigating && mStatus == LocationProvider.AVAILABLE && mLastFixTime > 0 && |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1289 | System.currentTimeMillis() - mLastFixTime > RECENT_FIX_TIMEOUT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | // send an intent to notify that the GPS is no longer receiving fixes. |
Mike Lockwood | 00b7427 | 2010-03-26 10:41:48 -0400 | [diff] [blame] | 1291 | Intent intent = new Intent(LocationManager.GPS_FIX_CHANGE_ACTION); |
| 1292 | intent.putExtra(LocationManager.EXTRA_GPS_ENABLED, false); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1293 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Mike Lockwood | 15e3d0f | 2009-05-01 07:53:28 -0400 | [diff] [blame] | 1294 | updateStatus(LocationProvider.TEMPORARILY_UNAVAILABLE, mSvCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1297 | |
| 1298 | /** |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1299 | * called from native code to update AGPS status |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1300 | */ |
Stephen Li | 8efd74d | 2011-03-01 20:56:00 -0800 | [diff] [blame] | 1301 | private void reportAGpsStatus(int type, int status, int ipaddr) { |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1302 | switch (status) { |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1303 | case GPS_REQUEST_AGPS_DATA_CONN: |
Mike Lockwood | b6e5fa8 | 2010-10-09 20:10:46 -0400 | [diff] [blame] | 1304 | if (DEBUG) Log.d(TAG, "GPS_REQUEST_AGPS_DATA_CONN"); |
Mike Lockwood | 2acfd34 | 2010-09-22 12:13:39 -0400 | [diff] [blame] | 1305 | // Set mAGpsDataConnectionState before calling startUsingNetworkFeature |
| 1306 | // to avoid a race condition with handleUpdateNetworkState() |
| 1307 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPENING; |
| 1308 | int result = mConnMgr.startUsingNetworkFeature( |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1309 | ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_SUPL); |
Stephen Li | 8efd74d | 2011-03-01 20:56:00 -0800 | [diff] [blame] | 1310 | mAGpsDataConnectionIpAddr = ipaddr; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1311 | if (result == PhoneConstants.APN_ALREADY_ACTIVE) { |
| 1312 | if (DEBUG) Log.d(TAG, "PhoneConstants.APN_ALREADY_ACTIVE"); |
Mike Lockwood | b362a9a | 2009-06-22 16:20:39 -0400 | [diff] [blame] | 1313 | if (mAGpsApn != null) { |
Stephen Li | 8efd74d | 2011-03-01 20:56:00 -0800 | [diff] [blame] | 1314 | Log.d(TAG, "mAGpsDataConnectionIpAddr " + mAGpsDataConnectionIpAddr); |
| 1315 | if (mAGpsDataConnectionIpAddr != 0xffffffff) { |
| 1316 | boolean route_result; |
| 1317 | if (DEBUG) Log.d(TAG, "call requestRouteToHost"); |
| 1318 | route_result = mConnMgr.requestRouteToHost( |
| 1319 | ConnectivityManager.TYPE_MOBILE_SUPL, |
| 1320 | mAGpsDataConnectionIpAddr); |
| 1321 | if (route_result == false) Log.d(TAG, "call requestRouteToHost failed"); |
| 1322 | } |
Mike Lockwood | b362a9a | 2009-06-22 16:20:39 -0400 | [diff] [blame] | 1323 | native_agps_data_conn_open(mAGpsApn); |
| 1324 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPEN; |
| 1325 | } else { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1326 | Log.e(TAG, "mAGpsApn not set when receiving PhoneConstants.APN_ALREADY_ACTIVE"); |
Mike Lockwood | 2acfd34 | 2010-09-22 12:13:39 -0400 | [diff] [blame] | 1327 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_CLOSED; |
Mike Lockwood | b362a9a | 2009-06-22 16:20:39 -0400 | [diff] [blame] | 1328 | native_agps_data_conn_failed(); |
| 1329 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1330 | } else if (result == PhoneConstants.APN_REQUEST_STARTED) { |
| 1331 | if (DEBUG) Log.d(TAG, "PhoneConstants.APN_REQUEST_STARTED"); |
Mike Lockwood | 2acfd34 | 2010-09-22 12:13:39 -0400 | [diff] [blame] | 1332 | // Nothing to do here |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1333 | } else { |
Stan Chesnutt | 1d72d8c | 2013-04-15 19:18:02 -0700 | [diff] [blame] | 1334 | if (DEBUG) Log.d(TAG, "startUsingNetworkFeature failed, value is " + |
| 1335 | result); |
Mike Lockwood | 2acfd34 | 2010-09-22 12:13:39 -0400 | [diff] [blame] | 1336 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_CLOSED; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1337 | native_agps_data_conn_failed(); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1338 | } |
| 1339 | break; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1340 | case GPS_RELEASE_AGPS_DATA_CONN: |
Mike Lockwood | b6e5fa8 | 2010-10-09 20:10:46 -0400 | [diff] [blame] | 1341 | if (DEBUG) Log.d(TAG, "GPS_RELEASE_AGPS_DATA_CONN"); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1342 | if (mAGpsDataConnectionState != AGPS_DATA_CONNECTION_CLOSED) { |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1343 | mConnMgr.stopUsingNetworkFeature( |
| 1344 | ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_SUPL); |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1345 | native_agps_data_conn_closed(); |
| 1346 | mAGpsDataConnectionState = AGPS_DATA_CONNECTION_CLOSED; |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1347 | } |
| 1348 | break; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1349 | case GPS_AGPS_DATA_CONNECTED: |
Mike Lockwood | b6e5fa8 | 2010-10-09 20:10:46 -0400 | [diff] [blame] | 1350 | if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONNECTED"); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1351 | break; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1352 | case GPS_AGPS_DATA_CONN_DONE: |
Mike Lockwood | b6e5fa8 | 2010-10-09 20:10:46 -0400 | [diff] [blame] | 1353 | if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONN_DONE"); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1354 | break; |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1355 | case GPS_AGPS_DATA_CONN_FAILED: |
Mike Lockwood | b6e5fa8 | 2010-10-09 20:10:46 -0400 | [diff] [blame] | 1356 | if (DEBUG) Log.d(TAG, "GPS_AGPS_DATA_CONN_FAILED"); |
Mike Lockwood | 58bda98 | 2009-04-14 16:25:07 -0400 | [diff] [blame] | 1357 | break; |
| 1358 | } |
| 1359 | } |
| 1360 | |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 1361 | /** |
| 1362 | * called from native code to report NMEA data received |
| 1363 | */ |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1364 | private void reportNmea(long timestamp) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1365 | synchronized (mListeners) { |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 1366 | int size = mListeners.size(); |
| 1367 | if (size > 0) { |
| 1368 | // don't bother creating the String if we have no listeners |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1369 | int length = native_read_nmea(mNmeaBuffer, mNmeaBuffer.length); |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 1370 | String nmea = new String(mNmeaBuffer, 0, length); |
| 1371 | |
| 1372 | for (int i = 0; i < size; i++) { |
| 1373 | Listener listener = mListeners.get(i); |
| 1374 | try { |
| 1375 | listener.mListener.onNmeaReceived(timestamp, nmea); |
| 1376 | } catch (RemoteException e) { |
| 1377 | Log.w(TAG, "RemoteException in reportNmea"); |
| 1378 | mListeners.remove(listener); |
| 1379 | // adjust for size of list changing |
| 1380 | size--; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1387 | /** |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1388 | * called from native code to inform us what the GPS engine capabilities are |
| 1389 | */ |
| 1390 | private void setEngineCapabilities(int capabilities) { |
| 1391 | mEngineCapabilities = capabilities; |
Mike Lockwood | 9b9fb5c | 2011-06-29 15:09:40 -0400 | [diff] [blame] | 1392 | |
| 1393 | if (!hasCapability(GPS_CAPABILITY_ON_DEMAND_TIME) && !mPeriodicTimeInjection) { |
| 1394 | mPeriodicTimeInjection = true; |
| 1395 | requestUtcTime(); |
| 1396 | } |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /** |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1400 | * called from native code to request XTRA data |
| 1401 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | private void xtraDownloadRequest() { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1403 | if (DEBUG) Log.d(TAG, "xtraDownloadRequest"); |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 1404 | sendMessage(DOWNLOAD_XTRA_DATA, 0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 1407 | /** |
| 1408 | * Called from native to report GPS Geofence transition |
| 1409 | * All geofence callbacks are called on the same thread |
| 1410 | */ |
| 1411 | private void reportGeofenceTransition(int geofenceId, int flags, double latitude, |
| 1412 | double longitude, double altitude, float speed, float bearing, float accuracy, |
| 1413 | long timestamp, int transition, long transitionTimestamp) { |
| 1414 | if (mGeofenceHardwareImpl == null) { |
| 1415 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1416 | } |
| 1417 | mGeofenceHardwareImpl.reportGpsGeofenceTransition(geofenceId, flags, latitude, longitude, |
| 1418 | altitude, speed, bearing, accuracy, timestamp, transition, transitionTimestamp); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * called from native code to report GPS status change. |
| 1423 | */ |
| 1424 | private void reportGeofenceStatus(int status, int flags, double latitude, |
| 1425 | double longitude, double altitude, float speed, float bearing, float accuracy, |
| 1426 | long timestamp) { |
| 1427 | if (mGeofenceHardwareImpl == null) { |
| 1428 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1429 | } |
| 1430 | mGeofenceHardwareImpl.reportGpsGeofenceStatus(status, flags, latitude, longitude, altitude, |
| 1431 | speed, bearing, accuracy, timestamp); |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * called from native code - Geofence Add callback |
| 1436 | */ |
| 1437 | private void reportGeofenceAddStatus(int geofenceId, int status) { |
| 1438 | if (mGeofenceHardwareImpl == null) { |
| 1439 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1440 | } |
| 1441 | mGeofenceHardwareImpl.reportGpsGeofenceAddStatus(geofenceId, status); |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * called from native code - Geofence Remove callback |
| 1446 | */ |
| 1447 | private void reportGeofenceRemoveStatus(int geofenceId, int status) { |
| 1448 | if (mGeofenceHardwareImpl == null) { |
| 1449 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1450 | } |
| 1451 | mGeofenceHardwareImpl.reportGpsGeofenceRemoveStatus(geofenceId, status); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * called from native code - Geofence Pause callback |
| 1456 | */ |
| 1457 | private void reportGeofencePauseStatus(int geofenceId, int status) { |
| 1458 | if (mGeofenceHardwareImpl == null) { |
| 1459 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1460 | } |
| 1461 | mGeofenceHardwareImpl.reportGpsGeofencePauseStatus(geofenceId, status); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * called from native code - Geofence Resume callback |
| 1466 | */ |
| 1467 | private void reportGeofenceResumeStatus(int geofenceId, int status) { |
| 1468 | if (mGeofenceHardwareImpl == null) { |
| 1469 | mGeofenceHardwareImpl = GeofenceHardwareImpl.getInstance(mContext); |
| 1470 | } |
| 1471 | mGeofenceHardwareImpl.reportGpsGeofenceResumeStatus(geofenceId, status); |
| 1472 | } |
| 1473 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1474 | //============================================================= |
| 1475 | // NI Client support |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1476 | //============================================================= |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1477 | private final INetInitiatedListener mNetInitiatedListener = new INetInitiatedListener.Stub() { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1478 | // Sends a response for an NI reqeust to HAL. |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1479 | @Override |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1480 | public boolean sendNiResponse(int notificationId, int userResponse) |
| 1481 | { |
| 1482 | // TODO Add Permission check |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1483 | |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1484 | if (DEBUG) Log.d(TAG, "sendNiResponse, notifId: " + notificationId + |
| 1485 | ", response: " + userResponse); |
| 1486 | native_send_ni_response(notificationId, userResponse); |
| 1487 | return true; |
| 1488 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1489 | }; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1490 | |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1491 | public INetInitiatedListener getNetInitiatedListener() { |
| 1492 | return mNetInitiatedListener; |
| 1493 | } |
| 1494 | |
| 1495 | // Called by JNI function to report an NI request. |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1496 | public void reportNiNotification( |
| 1497 | int notificationId, |
| 1498 | int niType, |
| 1499 | int notifyFlags, |
| 1500 | int timeout, |
| 1501 | int defaultResponse, |
| 1502 | String requestorId, |
| 1503 | String text, |
| 1504 | int requestorIdEncoding, |
| 1505 | int textEncoding, |
| 1506 | String extras // Encoded extra data |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1507 | ) |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1508 | { |
| 1509 | Log.i(TAG, "reportNiNotification: entered"); |
| 1510 | Log.i(TAG, "notificationId: " + notificationId + |
| 1511 | ", niType: " + niType + |
| 1512 | ", notifyFlags: " + notifyFlags + |
| 1513 | ", timeout: " + timeout + |
| 1514 | ", defaultResponse: " + defaultResponse); |
| 1515 | |
| 1516 | Log.i(TAG, "requestorId: " + requestorId + |
| 1517 | ", text: " + text + |
| 1518 | ", requestorIdEncoding: " + requestorIdEncoding + |
| 1519 | ", textEncoding: " + textEncoding); |
| 1520 | |
| 1521 | GpsNiNotification notification = new GpsNiNotification(); |
| 1522 | |
| 1523 | notification.notificationId = notificationId; |
| 1524 | notification.niType = niType; |
| 1525 | notification.needNotify = (notifyFlags & GpsNetInitiatedHandler.GPS_NI_NEED_NOTIFY) != 0; |
| 1526 | notification.needVerify = (notifyFlags & GpsNetInitiatedHandler.GPS_NI_NEED_VERIFY) != 0; |
| 1527 | notification.privacyOverride = (notifyFlags & GpsNetInitiatedHandler.GPS_NI_PRIVACY_OVERRIDE) != 0; |
| 1528 | notification.timeout = timeout; |
| 1529 | notification.defaultResponse = defaultResponse; |
| 1530 | notification.requestorId = requestorId; |
| 1531 | notification.text = text; |
| 1532 | notification.requestorIdEncoding = requestorIdEncoding; |
| 1533 | notification.textEncoding = textEncoding; |
| 1534 | |
| 1535 | // Process extras, assuming the format is |
| 1536 | // one of more lines of "key = value" |
| 1537 | Bundle bundle = new Bundle(); |
| 1538 | |
| 1539 | if (extras == null) extras = ""; |
| 1540 | Properties extraProp = new Properties(); |
| 1541 | |
| 1542 | try { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 1543 | extraProp.load(new StringReader(extras)); |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1544 | } |
| 1545 | catch (IOException e) |
| 1546 | { |
| 1547 | Log.e(TAG, "reportNiNotification cannot parse extras data: " + extras); |
| 1548 | } |
| 1549 | |
| 1550 | for (Entry<Object, Object> ent : extraProp.entrySet()) |
| 1551 | { |
| 1552 | bundle.putString((String) ent.getKey(), (String) ent.getValue()); |
| 1553 | } |
| 1554 | |
| 1555 | notification.extras = bundle; |
| 1556 | |
| 1557 | mNIHandler.handleNiNotification(notification); |
| 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Called from native code to request set id info. |
| 1562 | * We should be careful about receiving null string from the TelephonyManager, |
| 1563 | * because sending null String to JNI function would cause a crash. |
| 1564 | */ |
| 1565 | |
| 1566 | private void requestSetID(int flags) { |
| 1567 | TelephonyManager phone = (TelephonyManager) |
| 1568 | mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 1569 | int type = AGPS_SETID_TYPE_NONE; |
| 1570 | String data = ""; |
| 1571 | |
| 1572 | if ((flags & AGPS_RIL_REQUEST_SETID_IMSI) == AGPS_RIL_REQUEST_SETID_IMSI) { |
| 1573 | String data_temp = phone.getSubscriberId(); |
| 1574 | if (data_temp == null) { |
| 1575 | // This means the framework does not have the SIM card ready. |
| 1576 | } else { |
| 1577 | // This means the framework has the SIM card. |
| 1578 | data = data_temp; |
| 1579 | type = AGPS_SETID_TYPE_IMSI; |
| 1580 | } |
| 1581 | } |
| 1582 | else if ((flags & AGPS_RIL_REQUEST_SETID_MSISDN) == AGPS_RIL_REQUEST_SETID_MSISDN) { |
| 1583 | String data_temp = phone.getLine1Number(); |
| 1584 | if (data_temp == null) { |
| 1585 | // This means the framework does not have the SIM card ready. |
| 1586 | } else { |
| 1587 | // This means the framework has the SIM card. |
| 1588 | data = data_temp; |
| 1589 | type = AGPS_SETID_TYPE_MSISDN; |
| 1590 | } |
| 1591 | } |
| 1592 | native_agps_set_id(type, data); |
| 1593 | } |
| 1594 | |
| 1595 | /** |
Mike Lockwood | 9b9fb5c | 2011-06-29 15:09:40 -0400 | [diff] [blame] | 1596 | * Called from native code to request utc time info |
| 1597 | */ |
| 1598 | |
| 1599 | private void requestUtcTime() { |
| 1600 | sendMessage(INJECT_NTP_TIME, 0, null); |
| 1601 | } |
| 1602 | |
| 1603 | /** |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1604 | * Called from native code to request reference location info |
| 1605 | */ |
| 1606 | |
| 1607 | private void requestRefLocation(int flags) { |
| 1608 | TelephonyManager phone = (TelephonyManager) |
| 1609 | mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Victoria Lease | d50d0c3 | 2012-10-29 13:16:17 -0700 | [diff] [blame] | 1610 | final int phoneType = phone.getPhoneType(); |
| 1611 | if (phoneType == TelephonyManager.PHONE_TYPE_GSM) { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1612 | GsmCellLocation gsm_cell = (GsmCellLocation) phone.getCellLocation(); |
Victoria Lease | d50d0c3 | 2012-10-29 13:16:17 -0700 | [diff] [blame] | 1613 | if ((gsm_cell != null) && (phone.getNetworkOperator() != null) |
| 1614 | && (phone.getNetworkOperator().length() > 3)) { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1615 | int type; |
| 1616 | int mcc = Integer.parseInt(phone.getNetworkOperator().substring(0,3)); |
| 1617 | int mnc = Integer.parseInt(phone.getNetworkOperator().substring(3)); |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1618 | int networkType = phone.getNetworkType(); |
| 1619 | if (networkType == TelephonyManager.NETWORK_TYPE_UMTS |
| 1620 | || networkType == TelephonyManager.NETWORK_TYPE_HSDPA |
| 1621 | || networkType == TelephonyManager.NETWORK_TYPE_HSUPA |
Arun Ravindran | 58d4612 | 2012-07-30 17:50:21 +0300 | [diff] [blame] | 1622 | || networkType == TelephonyManager.NETWORK_TYPE_HSPA |
| 1623 | || networkType == TelephonyManager.NETWORK_TYPE_HSPAP) { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1624 | type = AGPS_REF_LOCATION_TYPE_UMTS_CELLID; |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1625 | } else { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1626 | type = AGPS_REF_LOCATION_TYPE_GSM_CELLID; |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1627 | } |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1628 | native_agps_set_ref_location_cellid(type, mcc, mnc, |
| 1629 | gsm_cell.getLac(), gsm_cell.getCid()); |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1630 | } else { |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1631 | Log.e(TAG,"Error getting cell location info."); |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1632 | } |
Victoria Lease | d50d0c3 | 2012-10-29 13:16:17 -0700 | [diff] [blame] | 1633 | } else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) { |
| 1634 | Log.e(TAG, "CDMA not supported."); |
Mike Lockwood | edc0f387 | 2010-10-22 09:16:17 -0400 | [diff] [blame] | 1635 | } |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1636 | } |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1637 | |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 1638 | private void sendMessage(int message, int arg, Object obj) { |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1639 | // hold a wake lock until this message is delivered |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 1640 | // note that this assumes the message will not be removed from the queue before |
| 1641 | // it is handled (otherwise the wake lock would be leaked). |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1642 | mWakeLock.acquire(); |
| 1643 | mHandler.obtainMessage(message, arg, 1, obj).sendToTarget(); |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 1644 | } |
| 1645 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1646 | private final class ProviderHandler extends Handler { |
Victoria Lease | 5cd731a | 2012-12-19 15:04:21 -0800 | [diff] [blame] | 1647 | public ProviderHandler(Looper looper) { |
| 1648 | super(looper, null, true /*async*/); |
Jeff Brown | 028872f | 2012-08-25 13:07:01 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1651 | @Override |
Mike Lockwood | 4a7b65e | 2010-10-25 16:35:55 -0400 | [diff] [blame] | 1652 | public void handleMessage(Message msg) { |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 1653 | int message = msg.what; |
| 1654 | switch (message) { |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1655 | case ENABLE: |
| 1656 | if (msg.arg1 == 1) { |
| 1657 | handleEnable(); |
| 1658 | } else { |
| 1659 | handleDisable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | } |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1661 | break; |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1662 | case SET_REQUEST: |
| 1663 | GpsRequest gpsRequest = (GpsRequest) msg.obj; |
| 1664 | handleSetRequest(gpsRequest.request, gpsRequest.source); |
Mike Lockwood | 03ca216 | 2010-04-01 08:10:09 -0700 | [diff] [blame] | 1665 | break; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1666 | case UPDATE_NETWORK_STATE: |
| 1667 | handleUpdateNetworkState(msg.arg1, (NetworkInfo)msg.obj); |
| 1668 | break; |
| 1669 | case INJECT_NTP_TIME: |
| 1670 | handleInjectNtpTime(); |
| 1671 | break; |
| 1672 | case DOWNLOAD_XTRA_DATA: |
Mike Lockwood | 1a1cd3a | 2010-08-17 07:42:54 -0400 | [diff] [blame] | 1673 | if (mSupportsXtra) { |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1674 | handleDownloadXtraData(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | } |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1676 | break; |
Kevin Tang | 40e1baf | 2012-01-10 14:32:44 -0800 | [diff] [blame] | 1677 | case INJECT_NTP_TIME_FINISHED: |
| 1678 | mInjectNtpTimePending = STATE_IDLE; |
| 1679 | break; |
| 1680 | case DOWNLOAD_XTRA_DATA_FINISHED: |
| 1681 | mDownloadXtraDataPending = STATE_IDLE; |
| 1682 | break; |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1683 | case UPDATE_LOCATION: |
| 1684 | handleUpdateLocation((Location)msg.obj); |
| 1685 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | } |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1687 | if (msg.arg2 == 1) { |
| 1688 | // wakelock was taken for this message, release it |
| 1689 | mWakeLock.release(); |
Mike Lockwood | 98e4869 | 2010-04-07 16:32:51 -0400 | [diff] [blame] | 1690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1691 | } |
Mike Lockwood | 62a8fc1 | 2010-03-22 14:23:26 -0400 | [diff] [blame] | 1692 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1694 | private final class NetworkLocationListener implements LocationListener { |
| 1695 | @Override |
| 1696 | public void onLocationChanged(Location location) { |
| 1697 | // this callback happens on mHandler looper |
| 1698 | if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) { |
| 1699 | handleUpdateLocation(location); |
| 1700 | } |
| 1701 | } |
| 1702 | @Override |
| 1703 | public void onStatusChanged(String provider, int status, Bundle extras) { } |
| 1704 | @Override |
| 1705 | public void onProviderEnabled(String provider) { } |
| 1706 | @Override |
| 1707 | public void onProviderDisabled(String provider) { } |
| 1708 | } |
| 1709 | |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 1710 | private String getSelectedApn() { |
| 1711 | Uri uri = Uri.parse("content://telephony/carriers/preferapn"); |
| 1712 | String apn = null; |
| 1713 | |
| 1714 | Cursor cursor = mContext.getContentResolver().query(uri, new String[] {"apn"}, |
| 1715 | null, null, Carriers.DEFAULT_SORT_ORDER); |
| 1716 | |
| 1717 | if (null != cursor) { |
| 1718 | try { |
| 1719 | if (cursor.moveToFirst()) { |
| 1720 | apn = cursor.getString(0); |
| 1721 | } |
| 1722 | } finally { |
| 1723 | cursor.close(); |
| 1724 | } |
| 1725 | } |
| 1726 | return apn; |
| 1727 | } |
| 1728 | |
Nick Pelly | 6fa9ad4 | 2012-07-16 12:18:23 -0700 | [diff] [blame] | 1729 | @Override |
| 1730 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1731 | StringBuilder s = new StringBuilder(); |
| 1732 | s.append(" mFixInterval=").append(mFixInterval).append("\n"); |
| 1733 | s.append(" mEngineCapabilities=0x").append(Integer.toHexString(mEngineCapabilities)).append(" ("); |
| 1734 | if (hasCapability(GPS_CAPABILITY_SCHEDULING)) s.append("SCHED "); |
| 1735 | if (hasCapability(GPS_CAPABILITY_MSB)) s.append("MSB "); |
| 1736 | if (hasCapability(GPS_CAPABILITY_MSA)) s.append("MSA "); |
| 1737 | if (hasCapability(GPS_CAPABILITY_SINGLE_SHOT)) s.append("SINGLE_SHOT "); |
| 1738 | if (hasCapability(GPS_CAPABILITY_ON_DEMAND_TIME)) s.append("ON_DEMAND_TIME "); |
| 1739 | s.append(")\n"); |
| 1740 | |
| 1741 | s.append(native_get_internal_state()); |
| 1742 | pw.append(s); |
| 1743 | } |
| 1744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1745 | // for GPS SV statistics |
| 1746 | private static final int MAX_SVS = 32; |
| 1747 | private static final int EPHEMERIS_MASK = 0; |
| 1748 | private static final int ALMANAC_MASK = 1; |
| 1749 | private static final int USED_FOR_FIX_MASK = 2; |
| 1750 | |
| 1751 | // preallocated arrays, to avoid memory allocation in reportStatus() |
| 1752 | private int mSvs[] = new int[MAX_SVS]; |
| 1753 | private float mSnrs[] = new float[MAX_SVS]; |
| 1754 | private float mSvElevations[] = new float[MAX_SVS]; |
| 1755 | private float mSvAzimuths[] = new float[MAX_SVS]; |
| 1756 | private int mSvMasks[] = new int[3]; |
| 1757 | private int mSvCount; |
Mike Lockwood | b16e780 | 2009-08-06 09:26:02 -0400 | [diff] [blame] | 1758 | // preallocated to avoid memory allocation in reportNmea() |
| 1759 | private byte[] mNmeaBuffer = new byte[120]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | |
| 1761 | static { class_init_native(); } |
| 1762 | private static native void class_init_native(); |
| 1763 | private static native boolean native_is_supported(); |
| 1764 | |
| 1765 | private native boolean native_init(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | private native void native_cleanup(); |
Mike Lockwood | 04598b6 | 2010-04-14 17:17:24 -0400 | [diff] [blame] | 1767 | private native boolean native_set_position_mode(int mode, int recurrence, int min_interval, |
| 1768 | int preferred_accuracy, int preferred_time); |
| 1769 | private native boolean native_start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | private native boolean native_stop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | private native void native_delete_aiding_data(int flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | // returns number of SVs |
| 1773 | // mask[0] is ephemeris mask and mask[1] is almanac mask |
| 1774 | private native int native_read_sv_status(int[] svs, float[] snrs, |
| 1775 | float[] elevations, float[] azimuths, int[] masks); |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1776 | private native int native_read_nmea(byte[] buffer, int bufferSize); |
Mike Lockwood | d26ce0d | 2009-06-11 12:25:46 -0400 | [diff] [blame] | 1777 | private native void native_inject_location(double latitude, double longitude, float accuracy); |
| 1778 | |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 1779 | // XTRA Support |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | private native void native_inject_time(long time, long timeReference, int uncertainty); |
| 1781 | private native boolean native_supports_xtra(); |
| 1782 | private native void native_inject_xtra_data(byte[] data, int length); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1783 | |
Fred Fettinger | 3c8fbdf | 2010-01-04 15:38:13 -0600 | [diff] [blame] | 1784 | // DEBUG Support |
| 1785 | private native String native_get_internal_state(); |
| 1786 | |
| 1787 | // AGPS Support |
Mike Lockwood | e3635c9 | 2009-05-11 08:38:02 -0400 | [diff] [blame] | 1788 | private native void native_agps_data_conn_open(String apn); |
| 1789 | private native void native_agps_data_conn_closed(); |
| 1790 | private native void native_agps_data_conn_failed(); |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1791 | private native void native_agps_ni_message(byte [] msg, int length); |
Mike Lockwood | a9e5461 | 2009-06-19 14:54:42 -0400 | [diff] [blame] | 1792 | private native void native_set_agps_server(int type, String hostname, int port); |
Danke Xie | 22d1f9f | 2009-08-18 18:28:45 -0400 | [diff] [blame] | 1793 | |
| 1794 | // Network-initiated (NI) Support |
| 1795 | private native void native_send_ni_response(int notificationId, int userResponse); |
Miguel Torroja | 1e84da8 | 2010-07-27 07:02:24 +0200 | [diff] [blame] | 1796 | |
| 1797 | // AGPS ril suport |
| 1798 | private native void native_agps_set_ref_location_cellid(int type, int mcc, int mnc, |
| 1799 | int lac, int cid); |
| 1800 | private native void native_agps_set_id(int type, String setid); |
Mike Lockwood | 50130bb | 2010-10-11 06:22:50 -0400 | [diff] [blame] | 1801 | |
| 1802 | private native void native_update_network_state(boolean connected, int type, |
Kevin Tang | a5fe6b2 | 2011-06-05 14:25:16 -0700 | [diff] [blame] | 1803 | boolean roaming, boolean available, String extraInfo, String defaultAPN); |
Jaikumar Ganesh | 8ce470d | 2013-04-03 12:22:18 -0700 | [diff] [blame] | 1804 | |
| 1805 | // Hardware Geofence support. |
| 1806 | private static native boolean native_is_geofence_supported(); |
| 1807 | private static native boolean native_add_geofence(int geofenceId, double latitude, |
| 1808 | double longitude, double radius, int lastTransition,int monitorTransitions, |
| 1809 | int notificationResponsivenes, int unknownTimer); |
| 1810 | private static native boolean native_remove_geofence(int geofenceId); |
| 1811 | private static native boolean native_resume_geofence(int geofenceId, int transitions); |
| 1812 | private static native boolean native_pause_geofence(int geofenceId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | } |