FileDocCategorySizeDatePackage
LocalListenerHelper.javaAPI DocAndroid 5.1 API3768Thu Mar 12 22:22:30 GMT 2015android.location

LocalListenerHelper

public abstract class LocalListenerHelper extends Object
A base handler class to manage transport and local listeners.
hide

Fields Summary
private final HashSet
mListeners
private final String
mTag
private final android.content.Context
mContext
Constructors Summary
protected LocalListenerHelper(android.content.Context context, String name)


         
        Preconditions.checkNotNull(name);
        mContext = context;
        mTag = name;
    
Methods Summary
public booleanadd(TListener listener)

        Preconditions.checkNotNull(listener);
        synchronized (mListeners) {
            // we need to register with the service first, because we need to find out if the
            // service will actually support the request before we attempt anything
            if (mListeners.isEmpty()) {
                boolean registeredWithService;
                try {
                    registeredWithService = registerWithServer();
                } catch (RemoteException e) {
                    Log.e(mTag, "Error handling first listener.", e);
                    return false;
                }
                if (!registeredWithService) {
                    Log.e(mTag, "Unable to register listener transport.");
                    return false;
                }
            }
            if (mListeners.contains(listener)) {
                return true;
            }
            return mListeners.add(listener);
        }
    
protected voidforeach(android.location.LocalListenerHelper$ListenerOperation operation)

        Collection<TListener> listeners;
        synchronized (mListeners) {
            listeners = new ArrayList<>(mListeners);
        }
        for (TListener listener : listeners) {
            try {
                operation.execute(listener);
            } catch (RemoteException e) {
                Log.e(mTag, "Error in monitored listener.", e);
                // don't return, give a fair chance to all listeners to receive the event
            }
        }
    
protected android.content.ContextgetContext()

        return mContext;
    
protected abstract booleanregisterWithServer()

public voidremove(TListener listener)

        Preconditions.checkNotNull(listener);
        synchronized (mListeners) {
            boolean removed = mListeners.remove(listener);
            boolean isLastRemoved = removed && mListeners.isEmpty();
            if (isLastRemoved) {
                try {
                    unregisterFromServer();
                } catch (RemoteException e) {
                    Log.v(mTag, "Error handling last listener removal", e);
                }
            }
        }
    
protected abstract voidunregisterFromServer()