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.content.Context;
23import android.os.Handler;
24import android.os.RemoteException;
25import android.util.Log;
26
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.HashMap;
30import java.util.Map;
31
32/**
33 * A base handler class to manage transport and local listeners.
34 *
35 * @hide
36 */
37abstract class LocalListenerHelper<TListener> {
38    private final HashMap<TListener, Handler> mListeners = new HashMap<>();
39
40    private final String mTag;
41    private final Context mContext;
42
43    protected LocalListenerHelper(Context context, String name) {
44        Preconditions.checkNotNull(name);
45        mContext = context;
46        mTag = name;
47    }
48
49    public boolean add(@NonNull TListener listener, Handler handler) {
50        Preconditions.checkNotNull(listener);
51        synchronized (mListeners) {
52            // we need to register with the service first, because we need to find out if the
53            // service will actually support the request before we attempt anything
54            if (mListeners.isEmpty()) {
55                boolean registeredWithService;
56                try {
57                    registeredWithService = registerWithServer();
58                } catch (RemoteException e) {
59                    Log.e(mTag, "Error handling first listener.", e);
60                    return false;
61                }
62                if (!registeredWithService) {
63                    Log.e(mTag, "Unable to register listener transport.");
64                    return false;
65                }
66            }
67            if (mListeners.containsKey(listener)) {
68                return true;
69            }
70            mListeners.put(listener, handler);
71            return true;
72        }
73    }
74
75    public void remove(@NonNull TListener listener) {
76        Preconditions.checkNotNull(listener);
77        synchronized (mListeners) {
78            boolean removed = mListeners.containsKey(listener);
79            mListeners.remove(listener);
80            boolean isLastRemoved = removed && mListeners.isEmpty();
81            if (isLastRemoved) {
82                try {
83                    unregisterFromServer();
84                } catch (RemoteException e) {
85                    Log.v(mTag, "Error handling last listener removal", e);
86                }
87            }
88        }
89    }
90
91    protected abstract boolean registerWithServer() throws RemoteException;
92    protected abstract void unregisterFromServer() throws RemoteException;
93
94    protected interface ListenerOperation<TListener> {
95        void execute(TListener listener) throws RemoteException;
96    }
97
98    protected Context getContext() {
99        return mContext;
100    }
101
102    private void executeOperation(ListenerOperation<TListener> operation, TListener listener) {
103        try {
104            operation.execute(listener);
105        } catch (RemoteException e) {
106            Log.e(mTag, "Error in monitored listener.", e);
107            // don't return, give a fair chance to all listeners to receive the event
108        }
109    }
110
111    protected void foreach(final ListenerOperation<TListener> operation) {
112        Collection<Map.Entry<TListener, Handler>> listeners;
113        synchronized (mListeners) {
114            listeners = new ArrayList<>(mListeners.entrySet());
115        }
116        for (final Map.Entry<TListener, Handler> listener : listeners) {
117            if (listener.getValue() == null) {
118                executeOperation(operation, listener.getKey());
119            } else {
120                listener.getValue().post(new Runnable() {
121                    @Override
122                    public void run() {
123                        executeOperation(operation, listener.getKey());
124                    }
125                });
126            }
127        }
128    }
129}
130