blob: 8d2192892f7416e8f71098fd608c813f06bc2447 [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
25/**
26 * An base implementation for GPS navigation messages provider.
27 * It abstracts out the responsibility of handling listeners, while still allowing technology
28 * specific implementations to be built.
29 *
30 * @hide
31 */
Lifu Tang818aa2c2016-02-01 01:52:00 -080032public abstract class GnssNavigationMessageProvider
33 extends RemoteListenerHelper<IGnssNavigationMessageListener> {
34 private static final String TAG = "GnssNavigationMessageProvider";
destradaa6568d702014-10-27 12:47:41 -070035
Lifu Tang818aa2c2016-02-01 01:52:00 -080036 protected GnssNavigationMessageProvider(Handler handler) {
destradaa6568d702014-10-27 12:47:41 -070037 super(handler, TAG);
destradaa4b3e3932014-07-21 18:01:47 -070038 }
39
Lifu Tange8abe8e2016-04-01 10:32:05 -070040 public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
Lifu Tang818aa2c2016-02-01 01:52:00 -080041 ListenerOperation<IGnssNavigationMessageListener> operation =
42 new ListenerOperation<IGnssNavigationMessageListener>() {
destradaa4b3e3932014-07-21 18:01:47 -070043 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080044 public void execute(IGnssNavigationMessageListener listener)
destradaa4b3e3932014-07-21 18:01:47 -070045 throws RemoteException {
Lifu Tang818aa2c2016-02-01 01:52:00 -080046 listener.onGnssNavigationMessageReceived(event);
destradaa4b3e3932014-07-21 18:01:47 -070047 }
48 };
destradaa4b3e3932014-07-21 18:01:47 -070049 foreach(operation);
50 }
destradaa6568d702014-10-27 12:47:41 -070051
Lifu Tang818aa2c2016-02-01 01:52:00 -080052 public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
53 setSupported(isGnssNavigationMessageSupported);
destradaa13a60b02015-01-15 18:36:01 -080054 updateResult();
55 }
56
57 public void onGpsEnabledChanged() {
58 if (tryUpdateRegistrationWithService()) {
59 updateResult();
60 }
destradaa6568d702014-10-27 12:47:41 -070061 }
62
63 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080064 protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) {
destradaa13a60b02015-01-15 18:36:01 -080065 int status;
destradaa6568d702014-10-27 12:47:41 -070066 switch (result) {
67 case RESULT_SUCCESS:
Lifu Tange8abe8e2016-04-01 10:32:05 -070068 status = GnssNavigationMessage.Callback.STATUS_READY;
destradaa6568d702014-10-27 12:47:41 -070069 break;
70 case RESULT_NOT_AVAILABLE:
71 case RESULT_NOT_SUPPORTED:
72 case RESULT_INTERNAL_ERROR:
Lifu Tange8abe8e2016-04-01 10:32:05 -070073 status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED;
destradaa6568d702014-10-27 12:47:41 -070074 break;
75 case RESULT_GPS_LOCATION_DISABLED:
Lifu Tange8abe8e2016-04-01 10:32:05 -070076 status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED;
destradaa6568d702014-10-27 12:47:41 -070077 break;
destradaa13a60b02015-01-15 18:36:01 -080078 case RESULT_UNKNOWN:
79 return null;
destradaa6568d702014-10-27 12:47:41 -070080 default:
81 Log.v(TAG, "Unhandled addListener result: " + result);
82 return null;
83 }
84 return new StatusChangedOperation(status);
85 }
86
destradaa13a60b02015-01-15 18:36:01 -080087 private static class StatusChangedOperation
Lifu Tang818aa2c2016-02-01 01:52:00 -080088 implements ListenerOperation<IGnssNavigationMessageListener> {
destradaa6568d702014-10-27 12:47:41 -070089 private final int mStatus;
90
91 public StatusChangedOperation(int status) {
92 mStatus = status;
93 }
94
95 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080096 public void execute(IGnssNavigationMessageListener listener) throws RemoteException {
destradaa6568d702014-10-27 12:47:41 -070097 listener.onStatusChanged(mStatus);
98 }
99 }
destradaa4b3e3932014-07-21 18:01:47 -0700100}