blob: 1b4fd187051a22355300ba1a28adcd84777488cd [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
Lifu Tange8abe8e2016-04-01 10:32:05 -070019import android.location.GnssNavigationMessage;
Lifu Tang818aa2c2016-02-01 01:52:00 -080020import android.location.IGnssNavigationMessageListener;
destradaa6568d702014-10-27 12:47:41 -070021import android.os.Handler;
destradaa4b3e3932014-07-21 18:01:47 -070022import android.os.RemoteException;
destradaa6568d702014-10-27 12:47:41 -070023import android.util.Log;
destradaa4b3e3932014-07-21 18:01:47 -070024
Yu-Han Yang23d92162018-04-19 06:03:00 -070025import com.android.internal.annotations.VisibleForTesting;
26
destradaa4b3e3932014-07-21 18:01:47 -070027/**
28 * An base implementation for GPS navigation messages provider.
29 * It abstracts out the responsibility of handling listeners, while still allowing technology
30 * specific implementations to be built.
31 *
32 * @hide
33 */
Lifu Tang818aa2c2016-02-01 01:52:00 -080034public abstract class GnssNavigationMessageProvider
35 extends RemoteListenerHelper<IGnssNavigationMessageListener> {
36 private static final String TAG = "GnssNavigationMessageProvider";
Yu-Han Yang23d92162018-04-19 06:03:00 -070037 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
38
39 private final GnssNavigationMessageProviderNative mNative;
40 private boolean mCollectionStarted;
destradaa6568d702014-10-27 12:47:41 -070041
Lifu Tang818aa2c2016-02-01 01:52:00 -080042 protected GnssNavigationMessageProvider(Handler handler) {
Yu-Han Yang23d92162018-04-19 06:03:00 -070043 this(handler, new GnssNavigationMessageProviderNative());
44 }
45
46 @VisibleForTesting
47 GnssNavigationMessageProvider(Handler handler, GnssNavigationMessageProviderNative aNative) {
destradaa6568d702014-10-27 12:47:41 -070048 super(handler, TAG);
Yu-Han Yang23d92162018-04-19 06:03:00 -070049 mNative = aNative;
50 }
51
52 // TODO(b/37460011): Use this with death recovery logic.
53 void resumeIfStarted() {
54 if (DEBUG) {
55 Log.d(TAG, "resumeIfStarted");
56 }
57 if (mCollectionStarted) {
58 mNative.startNavigationMessageCollection();
59 }
60 }
61
62 @Override
63 protected boolean isAvailableInPlatform() {
64 return mNative.isNavigationMessageSupported();
65 }
66
67 @Override
68 protected int registerWithService() {
69 boolean result = mNative.startNavigationMessageCollection();
70 if (result) {
71 mCollectionStarted = true;
72 return RemoteListenerHelper.RESULT_SUCCESS;
73 } else {
74 return RemoteListenerHelper.RESULT_INTERNAL_ERROR;
75 }
76 }
77
78 @Override
79 protected void unregisterFromService() {
80 boolean stopped = mNative.stopNavigationMessageCollection();
81 if (stopped) {
82 mCollectionStarted = false;
83 }
destradaa4b3e3932014-07-21 18:01:47 -070084 }
85
Lifu Tange8abe8e2016-04-01 10:32:05 -070086 public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
Lifu Tang818aa2c2016-02-01 01:52:00 -080087 ListenerOperation<IGnssNavigationMessageListener> operation =
88 new ListenerOperation<IGnssNavigationMessageListener>() {
destradaa4b3e3932014-07-21 18:01:47 -070089 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080090 public void execute(IGnssNavigationMessageListener listener)
destradaa4b3e3932014-07-21 18:01:47 -070091 throws RemoteException {
Lifu Tang818aa2c2016-02-01 01:52:00 -080092 listener.onGnssNavigationMessageReceived(event);
destradaa4b3e3932014-07-21 18:01:47 -070093 }
94 };
destradaa4b3e3932014-07-21 18:01:47 -070095 foreach(operation);
96 }
destradaa6568d702014-10-27 12:47:41 -070097
Lifu Tang818aa2c2016-02-01 01:52:00 -080098 public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
99 setSupported(isGnssNavigationMessageSupported);
destradaa13a60b02015-01-15 18:36:01 -0800100 updateResult();
101 }
102
103 public void onGpsEnabledChanged() {
Wyatt Rileyaa420d52017-07-03 15:14:42 -0700104 tryUpdateRegistrationWithService();
105 updateResult();
destradaa6568d702014-10-27 12:47:41 -0700106 }
107
108 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -0800109 protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) {
destradaa13a60b02015-01-15 18:36:01 -0800110 int status;
destradaa6568d702014-10-27 12:47:41 -0700111 switch (result) {
112 case RESULT_SUCCESS:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700113 status = GnssNavigationMessage.Callback.STATUS_READY;
destradaa6568d702014-10-27 12:47:41 -0700114 break;
115 case RESULT_NOT_AVAILABLE:
116 case RESULT_NOT_SUPPORTED:
117 case RESULT_INTERNAL_ERROR:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700118 status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED;
destradaa6568d702014-10-27 12:47:41 -0700119 break;
120 case RESULT_GPS_LOCATION_DISABLED:
Lifu Tange8abe8e2016-04-01 10:32:05 -0700121 status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED;
destradaa6568d702014-10-27 12:47:41 -0700122 break;
destradaa13a60b02015-01-15 18:36:01 -0800123 case RESULT_UNKNOWN:
124 return null;
destradaa6568d702014-10-27 12:47:41 -0700125 default:
126 Log.v(TAG, "Unhandled addListener result: " + result);
127 return null;
128 }
129 return new StatusChangedOperation(status);
130 }
131
destradaa13a60b02015-01-15 18:36:01 -0800132 private static class StatusChangedOperation
Lifu Tang818aa2c2016-02-01 01:52:00 -0800133 implements ListenerOperation<IGnssNavigationMessageListener> {
destradaa6568d702014-10-27 12:47:41 -0700134 private final int mStatus;
135
136 public StatusChangedOperation(int status) {
137 mStatus = status;
138 }
139
140 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -0800141 public void execute(IGnssNavigationMessageListener listener) 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
147 static class GnssNavigationMessageProviderNative {
148 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}