destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 |
Anil Admal | efd9dc6 | 2019-03-12 17:39:20 -0700 | [diff] [blame] | 14 | * limitations under the License. |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.location; |
| 18 | |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 20 | import android.location.GnssMeasurementsEvent; |
| 21 | import android.location.IGnssMeasurementsListener; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 23 | import android.os.RemoteException; |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 24 | import android.provider.Settings; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 25 | import android.util.Log; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 26 | |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 27 | import com.android.internal.annotations.VisibleForTesting; |
| 28 | |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 29 | /** |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 30 | * An base implementation for GPS measurements provider. It abstracts out the responsibility of |
| 31 | * handling listeners, while still allowing technology specific implementations to be built. |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 32 | * |
| 33 | * @hide |
| 34 | */ |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 35 | public abstract class GnssMeasurementsProvider |
| 36 | extends RemoteListenerHelper<IGnssMeasurementsListener> { |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 37 | private static final String TAG = "GnssMeasurementsProvider"; |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 38 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 39 | |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 40 | private final GnssMeasurementProviderNative mNative; |
| 41 | |
| 42 | private boolean mIsCollectionStarted; |
| 43 | private boolean mEnableFullTracking; |
| 44 | |
| 45 | protected GnssMeasurementsProvider(Context context, Handler handler) { |
| 46 | this(context, handler, new GnssMeasurementProviderNative()); |
| 47 | } |
| 48 | |
| 49 | @VisibleForTesting |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 50 | public GnssMeasurementsProvider( |
gomo | 226b7b7 | 2018-12-12 16:49:39 -0800 | [diff] [blame] | 51 | Context context, Handler handler, GnssMeasurementProviderNative aNative) { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 52 | super(context, handler, TAG); |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 53 | mNative = aNative; |
| 54 | } |
| 55 | |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 56 | void resumeIfStarted() { |
| 57 | if (DEBUG) { |
| 58 | Log.d(TAG, "resumeIfStarted"); |
| 59 | } |
| 60 | if (mIsCollectionStarted) { |
| 61 | mNative.startMeasurementCollection(mEnableFullTracking); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean isAvailableInPlatform() { |
| 67 | return mNative.isMeasurementSupported(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected int registerWithService() { |
| 72 | int devOptions = Settings.Secure.getInt(mContext.getContentResolver(), |
| 73 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); |
| 74 | int fullTrackingToggled = Settings.Global.getInt(mContext.getContentResolver(), |
| 75 | Settings.Global.ENABLE_GNSS_RAW_MEAS_FULL_TRACKING, 0); |
| 76 | boolean enableFullTracking = (devOptions == 1 /* Developer Mode enabled */) |
| 77 | && (fullTrackingToggled == 1 /* Raw Measurements Full Tracking enabled */); |
| 78 | boolean result = mNative.startMeasurementCollection(enableFullTracking); |
| 79 | if (result) { |
| 80 | mIsCollectionStarted = true; |
| 81 | mEnableFullTracking = enableFullTracking; |
| 82 | return RemoteListenerHelper.RESULT_SUCCESS; |
| 83 | } else { |
| 84 | return RemoteListenerHelper.RESULT_INTERNAL_ERROR; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void unregisterFromService() { |
| 90 | boolean stopped = mNative.stopMeasurementCollection(); |
| 91 | if (stopped) { |
| 92 | mIsCollectionStarted = false; |
| 93 | } |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 94 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 95 | |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 96 | public void onMeasurementsAvailable(final GnssMeasurementsEvent event) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 97 | foreach((IGnssMeasurementsListener listener, CallerIdentity callerIdentity) -> { |
| 98 | if (!hasPermission(mContext, callerIdentity)) { |
| 99 | logPermissionDisabledEventNotReported( |
| 100 | TAG, callerIdentity.mPackageName, "GNSS measurements"); |
| 101 | return; |
| 102 | } |
| 103 | listener.onGnssMeasurementsReceived(event); |
| 104 | }); |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 105 | } |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 106 | |
Anil Admal | efd9dc6 | 2019-03-12 17:39:20 -0700 | [diff] [blame] | 107 | /** Handle GNSS capabilities update from the GNSS HAL implementation. */ |
Anil Admal | 312fddb | 2019-03-25 12:15:43 -0700 | [diff] [blame] | 108 | public void onCapabilitiesUpdated(boolean isGnssMeasurementsSupported) { |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 109 | setSupported(isGnssMeasurementsSupported); |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 110 | updateResult(); |
| 111 | } |
| 112 | |
| 113 | public void onGpsEnabledChanged() { |
Wyatt Riley | aa420d5 | 2017-07-03 15:14:42 -0700 | [diff] [blame] | 114 | tryUpdateRegistrationWithService(); |
| 115 | updateResult(); |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 119 | protected ListenerOperation<IGnssMeasurementsListener> getHandlerOperation(int result) { |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 120 | int status; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 121 | switch (result) { |
| 122 | case RESULT_SUCCESS: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 123 | status = GnssMeasurementsEvent.Callback.STATUS_READY; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 124 | break; |
| 125 | case RESULT_NOT_AVAILABLE: |
| 126 | case RESULT_NOT_SUPPORTED: |
| 127 | case RESULT_INTERNAL_ERROR: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 128 | status = GnssMeasurementsEvent.Callback.STATUS_NOT_SUPPORTED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 129 | break; |
gomo | 48f1a64 | 2017-11-10 20:35:46 -0800 | [diff] [blame] | 130 | case RESULT_NOT_ALLOWED: |
| 131 | status = GnssMeasurementsEvent.Callback.STATUS_NOT_ALLOWED; |
| 132 | break; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 133 | case RESULT_GPS_LOCATION_DISABLED: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 134 | status = GnssMeasurementsEvent.Callback.STATUS_LOCATION_DISABLED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 135 | break; |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 136 | case RESULT_UNKNOWN: |
| 137 | return null; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 138 | default: |
| 139 | Log.v(TAG, "Unhandled addListener result: " + result); |
| 140 | return null; |
| 141 | } |
| 142 | return new StatusChangedOperation(status); |
| 143 | } |
| 144 | |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 145 | private static class StatusChangedOperation |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 146 | implements ListenerOperation<IGnssMeasurementsListener> { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 147 | private final int mStatus; |
| 148 | |
| 149 | public StatusChangedOperation(int status) { |
| 150 | mStatus = status; |
| 151 | } |
| 152 | |
| 153 | @Override |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 154 | public void execute(IGnssMeasurementsListener listener, |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 155 | CallerIdentity callerIdentity) throws RemoteException { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 156 | listener.onStatusChanged(mStatus); |
| 157 | } |
| 158 | } |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 159 | |
| 160 | @VisibleForTesting |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 161 | public static class GnssMeasurementProviderNative { |
Yu-Han Yang | 8de2150 | 2018-04-23 01:40:25 -0700 | [diff] [blame] | 162 | public boolean isMeasurementSupported() { |
| 163 | return native_is_measurement_supported(); |
| 164 | } |
| 165 | |
| 166 | public boolean startMeasurementCollection(boolean enableFullTracking) { |
| 167 | return native_start_measurement_collection(enableFullTracking); |
| 168 | } |
| 169 | |
| 170 | public boolean stopMeasurementCollection() { |
| 171 | return native_stop_measurement_collection(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private static native boolean native_is_measurement_supported(); |
| 176 | |
| 177 | private static native boolean native_start_measurement_collection(boolean enableFullTracking); |
| 178 | |
| 179 | private static native boolean native_stop_measurement_collection(); |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 180 | } |