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.RemoteException; 24import android.util.Log; 25 26import java.util.ArrayList; 27import java.util.Collection; 28import java.util.HashSet; 29 30/** 31 * A base handler class to manage transport and local listeners. 32 * 33 * @hide 34 */ 35abstract class LocalListenerHelper<TListener> { 36 private final HashSet<TListener> mListeners = new HashSet<>(); 37 38 private final String mTag; 39 private final Context mContext; 40 41 protected LocalListenerHelper(Context context, String name) { 42 Preconditions.checkNotNull(name); 43 mContext = context; 44 mTag = name; 45 } 46 47 public boolean add(@NonNull TListener listener) { 48 Preconditions.checkNotNull(listener); 49 synchronized (mListeners) { 50 // we need to register with the service first, because we need to find out if the 51 // service will actually support the request before we attempt anything 52 if (mListeners.isEmpty()) { 53 boolean registeredWithService; 54 try { 55 registeredWithService = registerWithServer(); 56 } catch (RemoteException e) { 57 Log.e(mTag, "Error handling first listener.", e); 58 return false; 59 } 60 if (!registeredWithService) { 61 Log.e(mTag, "Unable to register listener transport."); 62 return false; 63 } 64 } 65 if (mListeners.contains(listener)) { 66 return true; 67 } 68 return mListeners.add(listener); 69 } 70 } 71 72 public void remove(@NonNull TListener listener) { 73 Preconditions.checkNotNull(listener); 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 Log.v(mTag, "Error handling last listener removal", e); 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 Context getContext() { 95 return mContext; 96 } 97 98 protected void foreach(ListenerOperation<TListener> operation) { 99 Collection<TListener> listeners; 100 synchronized (mListeners) { 101 listeners = new ArrayList<>(mListeners); 102 } 103 for (TListener listener : listeners) { 104 try { 105 operation.execute(listener); 106 } catch (RemoteException e) { 107 Log.e(mTag, "Error in monitored listener.", e); 108 // don't return, give a fair chance to all listeners to receive the event 109 } 110 } 111 } 112} 113