blob: 722be3df2691be3b68589e3d25556f87a9172f39 [file] [log] [blame]
destradaa4b3e3932014-07-21 18:01:47 -07001/*
Sasha Kuznetsov94bb0092020-03-26 12:08:17 -07002 * Copyright (C) 2020 The Android Open Source Project
destradaa4b3e3932014-07-21 18:01:47 -07003 *
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 Admalefd9dc62019-03-12 17:39:20 -070014 * limitations under the License.
destradaa4b3e3932014-07-21 18:01:47 -070015 */
16
Sasha Kuznetsov94bb0092020-03-26 12:08:17 -070017package com.android.server.location.gnss;
destradaa4b3e3932014-07-21 18:01:47 -070018
Anil Admal75b9fd62018-11-28 11:22:50 -080019import android.content.Context;
Lifu Tange8abe8e2016-04-01 10:32:05 -070020import android.location.GnssNavigationMessage;
Lifu Tang818aa2c2016-02-01 01:52:00 -080021import android.location.IGnssNavigationMessageListener;
destradaa6568d702014-10-27 12:47:41 -070022import android.os.Handler;
destradaa4b3e3932014-07-21 18:01:47 -070023import android.os.RemoteException;
destradaa6568d702014-10-27 12:47:41 -070024import android.util.Log;
destradaa4b3e3932014-07-21 18:01:47 -070025
Yu-Han Yang23d92162018-04-19 06:03:00 -070026import com.android.internal.annotations.VisibleForTesting;
Sasha Kuznetsov94bb0092020-03-26 12:08:17 -070027import com.android.server.location.CallerIdentity;
28import com.android.server.location.RemoteListenerHelper;
Yu-Han Yang23d92162018-04-19 06:03:00 -070029
destradaa4b3e3932014-07-21 18:01:47 -070030/**
31 * An base implementation for GPS navigation messages provider.
32 * It abstracts out the responsibility of handling listeners, while still allowing technology
33 * specific implementations to be built.
34 *
35 * @hide
36 */
Lifu Tang818aa2c2016-02-01 01:52:00 -080037public abstract class GnssNavigationMessageProvider
Yu-Han Yang519694b2020-01-14 10:51:41 -080038 extends RemoteListenerHelper<Void, IGnssNavigationMessageListener> {
Lifu Tang818aa2c2016-02-01 01:52:00 -080039 private static final String TAG = "GnssNavigationMessageProvider";
Yu-Han Yang23d92162018-04-19 06:03:00 -070040 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
41
42 private final GnssNavigationMessageProviderNative mNative;
43 private boolean mCollectionStarted;
destradaa6568d702014-10-27 12:47:41 -070044
Anil Admal75b9fd62018-11-28 11:22:50 -080045 protected GnssNavigationMessageProvider(Context context, Handler handler) {
46 this(context, handler, new GnssNavigationMessageProviderNative());
Yu-Han Yang23d92162018-04-19 06:03:00 -070047 }
48
49 @VisibleForTesting
Sasha Kuznetsovb9f26b42019-10-03 17:30:46 -070050 public GnssNavigationMessageProvider(Context context, Handler handler,
Anil Admal75b9fd62018-11-28 11:22:50 -080051 GnssNavigationMessageProviderNative aNative) {
52 super(context, handler, TAG);
Yu-Han Yang23d92162018-04-19 06:03:00 -070053 mNative = aNative;
54 }
55
Yu-Han Yang23d92162018-04-19 06:03:00 -070056 void resumeIfStarted() {
57 if (DEBUG) {
58 Log.d(TAG, "resumeIfStarted");
59 }
60 if (mCollectionStarted) {
61 mNative.startNavigationMessageCollection();
62 }
63 }
64
65 @Override
66 protected boolean isAvailableInPlatform() {
67 return mNative.isNavigationMessageSupported();
68 }
69
70 @Override
71 protected int registerWithService() {
72 boolean result = mNative.startNavigationMessageCollection();
73 if (result) {
74 mCollectionStarted = true;
75 return RemoteListenerHelper.RESULT_SUCCESS;
76 } else {
77 return RemoteListenerHelper.RESULT_INTERNAL_ERROR;
78 }
79 }
80
81 @Override
82 protected void unregisterFromService() {
83 boolean stopped = mNative.stopNavigationMessageCollection();
84 if (stopped) {
85 mCollectionStarted = false;
86 }
destradaa4b3e3932014-07-21 18:01:47 -070087 }
88
Lifu Tange8abe8e2016-04-01 10:32:05 -070089 public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
Anil Admal08b96122019-01-30 16:55:05 -080090 foreach((IGnssNavigationMessageListener listener, CallerIdentity callerIdentity) -> {
Anil Admal75b9fd62018-11-28 11:22:50 -080091 listener.onGnssNavigationMessageReceived(event);
92 }
93 );
destradaa4b3e3932014-07-21 18:01:47 -070094 }
destradaa6568d702014-10-27 12:47:41 -070095
Anil Admalefd9dc62019-03-12 17:39:20 -070096 /** Handle GNSS capabilities update from the GNSS HAL implementation */
Anil Admal312fddb2019-03-25 12:15:43 -070097 public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
Lifu Tang818aa2c2016-02-01 01:52:00 -080098 setSupported(isGnssNavigationMessageSupported);
destradaa13a60b02015-01-15 18:36:01 -080099 updateResult();
100 }
101
102 public void onGpsEnabledChanged() {
Wyatt Rileyaa420d52017-07-03 15:14:42 -0700103 tryUpdateRegistrationWithService();
104 updateResult();
destradaa6568d702014-10-27 12:47:41 -0700105 }
106
107 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -0800108 protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) {
destradaa13a60b02015-01-15 18:36:01 -0800109 int status;
destradaa6568d702014-10-27 12:47:41 -0700110 switch (result) {
111 case RESULT_SUCCESS:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700112 status = GnssNavigationMessage.Callback.STATUS_READY;
destradaa6568d702014-10-27 12:47:41 -0700113 break;
114 case RESULT_NOT_AVAILABLE:
115 case RESULT_NOT_SUPPORTED:
116 case RESULT_INTERNAL_ERROR:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700117 status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED;
destradaa6568d702014-10-27 12:47:41 -0700118 break;
119 case RESULT_GPS_LOCATION_DISABLED:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700120 status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED;
destradaa6568d702014-10-27 12:47:41 -0700121 break;
destradaa13a60b02015-01-15 18:36:01 -0800122 case RESULT_UNKNOWN:
123 return null;
destradaa6568d702014-10-27 12:47:41 -0700124 default:
125 Log.v(TAG, "Unhandled addListener result: " + result);
126 return null;
127 }
128 return new StatusChangedOperation(status);
129 }
130
destradaa13a60b02015-01-15 18:36:01 -0800131 private static class StatusChangedOperation
Lifu Tang818aa2c2016-02-01 01:52:00 -0800132 implements ListenerOperation<IGnssNavigationMessageListener> {
destradaa6568d702014-10-27 12:47:41 -0700133 private final int mStatus;
134
135 public StatusChangedOperation(int status) {
136 mStatus = status;
137 }
138
139 @Override
Anil Admal75b9fd62018-11-28 11:22:50 -0800140 public void execute(IGnssNavigationMessageListener listener,
Anil Admal08b96122019-01-30 16:55:05 -0800141 CallerIdentity callerIdentity) throws RemoteException {
destradaa6568d702014-10-27 12:47:41 -0700142 listener.onStatusChanged(mStatus);
143 }
144 }
Yu-Han Yang23d92162018-04-19 06:03:00 -0700145
146 @VisibleForTesting
Sasha Kuznetsovb9f26b42019-10-03 17:30:46 -0700147 public static class GnssNavigationMessageProviderNative {
Yu-Han Yang23d92162018-04-19 06:03:00 -0700148 public boolean isNavigationMessageSupported() {
149 return native_is_navigation_message_supported();
150 }
151
152 public boolean startNavigationMessageCollection() {
153 return native_start_navigation_message_collection();
154 }
155
156 public boolean stopNavigationMessageCollection() {
157 return native_stop_navigation_message_collection();
158 }
159 }
160
161 private static native boolean native_is_navigation_message_supported();
162
163 private static native boolean native_start_navigation_message_collection();
164
165 private static native boolean native_stop_navigation_message_collection();
destradaa4b3e3932014-07-21 18:01:47 -0700166}