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.IGpsStatusListener;
20import android.os.Handler;
21import android.os.RemoteException;
22
23/**
24 * Implementation of a handler for {@link IGpsStatusListener}.
25 */
26abstract class GpsStatusListenerHelper extends RemoteListenerHelper<IGpsStatusListener> {
27    public GpsStatusListenerHelper(Handler handler) {
28        super(handler, "GpsStatusListenerHelper");
29
30        Operation nullOperation = new Operation() {
31            @Override
32            public void execute(IGpsStatusListener iGpsStatusListener) throws RemoteException {}
33        };
34        setSupported(GpsLocationProvider.isSupported(), nullOperation);
35    }
36
37    @Override
38    protected boolean registerWithService() {
39        return true;
40    }
41
42    @Override
43    protected void unregisterFromService() {}
44
45    @Override
46    protected ListenerOperation<IGpsStatusListener> getHandlerOperation(int result) {
47        return null;
48    }
49
50    @Override
51    protected void handleGpsEnabledChanged(boolean enabled) {
52        Operation operation;
53        if (enabled) {
54            operation = new Operation() {
55                @Override
56                public void execute(IGpsStatusListener listener) throws RemoteException {
57                    listener.onGpsStarted();
58                }
59            };
60        } else {
61            operation = new Operation() {
62                @Override
63                public void execute(IGpsStatusListener listener) throws RemoteException {
64                    listener.onGpsStopped();
65                }
66            };
67        }
68        foreach(operation);
69    }
70
71    public void onFirstFix(final int timeToFirstFix) {
72        Operation operation = new Operation() {
73            @Override
74            public void execute(IGpsStatusListener listener) throws RemoteException {
75                listener.onFirstFix(timeToFirstFix);
76            }
77        };
78        foreach(operation);
79    }
80
81    public void onSvStatusChanged(
82            final int svCount,
83            final int[] prns,
84            final float[] snrs,
85            final float[] elevations,
86            final float[] azimuths,
87            final int ephemerisMask,
88            final int almanacMask,
89            final int usedInFixMask) {
90        Operation operation = new Operation() {
91            @Override
92            public void execute(IGpsStatusListener listener) throws RemoteException {
93                listener.onSvStatusChanged(
94                        svCount,
95                        prns,
96                        snrs,
97                        elevations,
98                        azimuths,
99                        ephemerisMask,
100                        almanacMask,
101                        usedInFixMask);
102            }
103        };
104        foreach(operation);
105    }
106
107    public void onNmeaReceived(final long timestamp, final String nmea) {
108        Operation operation = new Operation() {
109            @Override
110            public void execute(IGpsStatusListener listener) throws RemoteException {
111                listener.onNmeaReceived(timestamp, nmea);
112            }
113        };
114        foreach(operation);
115    }
116
117    private abstract class Operation implements ListenerOperation<IGpsStatusListener> { }
118}
119