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