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