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