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