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
17package com.android.server.location;
18
19import android.location.GnssNavigationMessage;
20import android.location.IGnssNavigationMessageListener;
21import android.os.Handler;
22import android.os.RemoteException;
23import android.util.Log;
24
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 */
32public abstract class GnssNavigationMessageProvider
33        extends RemoteListenerHelper<IGnssNavigationMessageListener> {
34    private static final String TAG = "GnssNavigationMessageProvider";
35
36    protected GnssNavigationMessageProvider(Handler handler) {
37        super(handler, TAG);
38    }
39
40    public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
41        ListenerOperation<IGnssNavigationMessageListener> operation =
42                new ListenerOperation<IGnssNavigationMessageListener>() {
43                    @Override
44                    public void execute(IGnssNavigationMessageListener listener)
45                            throws RemoteException {
46                        listener.onGnssNavigationMessageReceived(event);
47                    }
48                };
49        foreach(operation);
50    }
51
52    public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
53        setSupported(isGnssNavigationMessageSupported);
54        updateResult();
55    }
56
57    public void onGpsEnabledChanged() {
58        if (tryUpdateRegistrationWithService()) {
59            updateResult();
60        }
61    }
62
63    @Override
64    protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) {
65        int status;
66        switch (result) {
67            case RESULT_SUCCESS:
68                status = GnssNavigationMessage.Callback.STATUS_READY;
69                break;
70            case RESULT_NOT_AVAILABLE:
71            case RESULT_NOT_SUPPORTED:
72            case RESULT_INTERNAL_ERROR:
73                status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED;
74                break;
75            case RESULT_GPS_LOCATION_DISABLED:
76                status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED;
77                break;
78            case RESULT_UNKNOWN:
79                return null;
80            default:
81                Log.v(TAG, "Unhandled addListener result: " + result);
82                return null;
83        }
84        return new StatusChangedOperation(status);
85    }
86
87    private static class StatusChangedOperation
88            implements ListenerOperation<IGnssNavigationMessageListener> {
89        private final int mStatus;
90
91        public StatusChangedOperation(int status) {
92            mStatus = status;
93        }
94
95        @Override
96        public void execute(IGnssNavigationMessageListener listener) throws RemoteException {
97            listener.onStatusChanged(mStatus);
98        }
99    }
100}
101