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.GpsNavigationMessageEvent;
20import android.location.IGpsNavigationMessageListener;
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 GpsNavigationMessageProvider
33        extends RemoteListenerHelper<IGpsNavigationMessageListener> {
34    private static final String TAG = "GpsNavigationMessageProvider";
35
36    public GpsNavigationMessageProvider(Handler handler) {
37        super(handler, TAG);
38    }
39
40    public void onNavigationMessageAvailable(final GpsNavigationMessageEvent event) {
41        ListenerOperation<IGpsNavigationMessageListener> operation =
42                new ListenerOperation<IGpsNavigationMessageListener>() {
43                    @Override
44                    public void execute(IGpsNavigationMessageListener listener)
45                            throws RemoteException {
46                        listener.onGpsNavigationMessageReceived(event);
47                    }
48                };
49        foreach(operation);
50    }
51
52    public void onCapabilitiesUpdated(boolean isGpsNavigationMessageSupported) {
53        int status = isGpsNavigationMessageSupported ?
54                GpsNavigationMessageEvent.STATUS_READY :
55                GpsNavigationMessageEvent.STATUS_NOT_SUPPORTED;
56        setSupported(isGpsNavigationMessageSupported, new StatusChangedOperation(status));
57    }
58
59    @Override
60    protected ListenerOperation<IGpsNavigationMessageListener> getHandlerOperation(int result) {
61        final int status;
62        switch (result) {
63            case RESULT_SUCCESS:
64                status = GpsNavigationMessageEvent.STATUS_READY;
65                break;
66            case RESULT_NOT_AVAILABLE:
67            case RESULT_NOT_SUPPORTED:
68            case RESULT_INTERNAL_ERROR:
69                status = GpsNavigationMessageEvent.STATUS_NOT_SUPPORTED;
70                break;
71            case RESULT_GPS_LOCATION_DISABLED:
72                status = GpsNavigationMessageEvent.STATUS_GPS_LOCATION_DISABLED;
73                break;
74            default:
75                Log.v(TAG, "Unhandled addListener result: " + result);
76                return null;
77        }
78        return new StatusChangedOperation(status);
79    }
80
81    @Override
82    protected void handleGpsEnabledChanged(boolean enabled) {
83        int status = enabled ?
84                GpsNavigationMessageEvent.STATUS_READY :
85                GpsNavigationMessageEvent.STATUS_GPS_LOCATION_DISABLED;
86        foreach(new StatusChangedOperation(status));
87    }
88
89    private class StatusChangedOperation
90            implements ListenerOperation<IGpsNavigationMessageListener> {
91        private final int mStatus;
92
93        public StatusChangedOperation(int status) {
94            mStatus = status;
95        }
96
97        @Override
98        public void execute(IGpsNavigationMessageListener listener) throws RemoteException {
99            listener.onStatusChanged(mStatus);
100        }
101    }
102}
103