blob: 679919f8fc67921f3935d2430fbaac2627c0a9b2 [file] [log] [blame]
destradaa4b3e3932014-07-21 18:01:47 -07001/*
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
17package com.android.server.location;
18
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;
27
destradaa4b3e3932014-07-21 18:01:47 -070028/**
29 * An base implementation for GPS navigation messages provider.
30 * It abstracts out the responsibility of handling listeners, while still allowing technology
31 * specific implementations to be built.
32 *
33 * @hide
34 */
Lifu Tang818aa2c2016-02-01 01:52:00 -080035public abstract class GnssNavigationMessageProvider
36 extends RemoteListenerHelper<IGnssNavigationMessageListener> {
37 private static final String TAG = "GnssNavigationMessageProvider";
Yu-Han Yang23d92162018-04-19 06:03:00 -070038 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
39
40 private final GnssNavigationMessageProviderNative mNative;
41 private boolean mCollectionStarted;
destradaa6568d702014-10-27 12:47:41 -070042
Anil Admal75b9fd62018-11-28 11:22:50 -080043 protected GnssNavigationMessageProvider(Context context, Handler handler) {
44 this(context, handler, new GnssNavigationMessageProviderNative());
Yu-Han Yang23d92162018-04-19 06:03:00 -070045 }
46
47 @VisibleForTesting
Anil Admal75b9fd62018-11-28 11:22:50 -080048 GnssNavigationMessageProvider(Context context, Handler handler,
49 GnssNavigationMessageProviderNative aNative) {
50 super(context, handler, TAG);
Yu-Han Yang23d92162018-04-19 06:03:00 -070051 mNative = aNative;
52 }
53
54 // TODO(b/37460011): Use this with death recovery logic.
55 void resumeIfStarted() {
56 if (DEBUG) {
57 Log.d(TAG, "resumeIfStarted");
58 }
59 if (mCollectionStarted) {
60 mNative.startNavigationMessageCollection();
61 }
62 }
63
64 @Override
65 protected boolean isAvailableInPlatform() {
66 return mNative.isNavigationMessageSupported();
67 }
68
69 @Override
70 protected int registerWithService() {
71 boolean result = mNative.startNavigationMessageCollection();
72 if (result) {
73 mCollectionStarted = true;
74 return RemoteListenerHelper.RESULT_SUCCESS;
75 } else {
76 return RemoteListenerHelper.RESULT_INTERNAL_ERROR;
77 }
78 }
79
80 @Override
81 protected void unregisterFromService() {
82 boolean stopped = mNative.stopNavigationMessageCollection();
83 if (stopped) {
84 mCollectionStarted = false;
85 }
destradaa4b3e3932014-07-21 18:01:47 -070086 }
87
Lifu Tange8abe8e2016-04-01 10:32:05 -070088 public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
Anil Admal75b9fd62018-11-28 11:22:50 -080089 foreach((IGnssNavigationMessageListener listener, int uid, String packageName) -> {
90 listener.onGnssNavigationMessageReceived(event);
91 }
92 );
destradaa4b3e3932014-07-21 18:01:47 -070093 }
destradaa6568d702014-10-27 12:47:41 -070094
Lifu Tang818aa2c2016-02-01 01:52:00 -080095 public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
96 setSupported(isGnssNavigationMessageSupported);
destradaa13a60b02015-01-15 18:36:01 -080097 updateResult();
98 }
99
100 public void onGpsEnabledChanged() {
Wyatt Rileyaa420d52017-07-03 15:14:42 -0700101 tryUpdateRegistrationWithService();
102 updateResult();
destradaa6568d702014-10-27 12:47:41 -0700103 }
104
105 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -0800106 protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) {
destradaa13a60b02015-01-15 18:36:01 -0800107 int status;
destradaa6568d702014-10-27 12:47:41 -0700108 switch (result) {
109 case RESULT_SUCCESS:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700110 status = GnssNavigationMessage.Callback.STATUS_READY;
destradaa6568d702014-10-27 12:47:41 -0700111 break;
112 case RESULT_NOT_AVAILABLE:
113 case RESULT_NOT_SUPPORTED:
114 case RESULT_INTERNAL_ERROR:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700115 status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED;
destradaa6568d702014-10-27 12:47:41 -0700116 break;
117 case RESULT_GPS_LOCATION_DISABLED:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700118 status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED;
destradaa6568d702014-10-27 12:47:41 -0700119 break;
destradaa13a60b02015-01-15 18:36:01 -0800120 case RESULT_UNKNOWN:
121 return null;
destradaa6568d702014-10-27 12:47:41 -0700122 default:
123 Log.v(TAG, "Unhandled addListener result: " + result);
124 return null;
125 }
126 return new StatusChangedOperation(status);
127 }
128
destradaa13a60b02015-01-15 18:36:01 -0800129 private static class StatusChangedOperation
Lifu Tang818aa2c2016-02-01 01:52:00 -0800130 implements ListenerOperation<IGnssNavigationMessageListener> {
destradaa6568d702014-10-27 12:47:41 -0700131 private final int mStatus;
132
133 public StatusChangedOperation(int status) {
134 mStatus = status;
135 }
136
137 @Override
Anil Admal75b9fd62018-11-28 11:22:50 -0800138 public void execute(IGnssNavigationMessageListener listener,
139 int uid, String packageName) throws RemoteException {
destradaa6568d702014-10-27 12:47:41 -0700140 listener.onStatusChanged(mStatus);
141 }
142 }
Yu-Han Yang23d92162018-04-19 06:03:00 -0700143
144 @VisibleForTesting
145 static class GnssNavigationMessageProviderNative {
146 public boolean isNavigationMessageSupported() {
147 return native_is_navigation_message_supported();
148 }
149
150 public boolean startNavigationMessageCollection() {
151 return native_start_navigation_message_collection();
152 }
153
154 public boolean stopNavigationMessageCollection() {
155 return native_stop_navigation_message_collection();
156 }
157 }
158
159 private static native boolean native_is_navigation_message_supported();
160
161 private static native boolean native_start_navigation_message_collection();
162
163 private static native boolean native_stop_navigation_message_collection();
destradaa4b3e3932014-07-21 18:01:47 -0700164}