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 android.location;
18
19import com.android.internal.util.Preconditions;
20
21import android.annotation.NonNull;
22import android.os.RemoteException;
23import android.util.Log;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.HashSet;
28
29/**
30 * A base handler class to manage transport and local listeners.
31 *
32 * @hide
33 */
34abstract class LocalListenerHelper<TListener> {
35    private final HashSet<TListener> mListeners = new HashSet<TListener>();
36    private final String mTag;
37
38    protected LocalListenerHelper(String name) {
39        Preconditions.checkNotNull(name);
40        mTag = name;
41    }
42
43    public boolean add(@NonNull TListener listener) {
44        Preconditions.checkNotNull(listener);
45
46        synchronized (mListeners) {
47            // we need to register with the service first, because we need to find out if the
48            // service will actually support the request before we attempt anything
49            if (mListeners.isEmpty()) {
50                boolean registeredWithService;
51                try {
52                    registeredWithService = registerWithServer();
53                } catch (RemoteException e) {
54                    Log.e(mTag, "Error handling first listener.", e);
55                    return false;
56                }
57                if (!registeredWithService) {
58                    Log.e(mTag, "Unable to register listener transport.");
59                    return false;
60                }
61            }
62
63            if (mListeners.contains(listener)) {
64                return true;
65            }
66            mListeners.add(listener);
67        }
68        return true;
69    }
70
71    public void remove(@NonNull TListener listener) {
72        Preconditions.checkNotNull(listener);
73
74        synchronized (mListeners) {
75            boolean removed = mListeners.remove(listener);
76            boolean isLastRemoved = removed && mListeners.isEmpty();
77            if (isLastRemoved) {
78                try {
79                    unregisterFromServer();
80                } catch (RemoteException e) {
81
82                }
83            }
84        }
85    }
86
87    protected abstract boolean registerWithServer() throws RemoteException;
88    protected abstract void unregisterFromServer() throws RemoteException;
89
90    protected interface ListenerOperation<TListener> {
91        void execute(TListener listener) throws RemoteException;
92    }
93
94    protected void foreach(ListenerOperation operation) {
95        Collection<TListener> listeners;
96        synchronized (mListeners) {
97            listeners = new ArrayList<TListener>(mListeners);
98        }
99
100        for (TListener listener : listeners) {
101            try {
102                operation.execute(listener);
103            } catch (RemoteException e) {
104                Log.e(mTag, "Error in monitored listener.", e);
105                // don't return, give a fair chance to all listeners to receive the event
106            }
107        }
108    }
109}
110