@@ -0,0 +1,2876 @@
+package nl.rmokveld.castnotifications;%0A%0Aimport android.os.Handler;%0Aimport android.os.SystemClock;%0Aimport android.support.v7.media.MediaRouter;%0A%0Aimport com.google.android.gms.cast.CastDevice;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * CastAvailabilityHelper delays removal of routes and keeps track of current rounds%0A */%0Apublic abstract class CastAvailabilityHelper %7B%0A%0A    private Map%3CString, String%3E mAvailableRoutes = new HashMap%3C%3E();%0A    private final Handler mHandler = new Handler();%0A    private final Runnable mOnRouteRemovedRunnable = new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A%0A        %7D%0A    %7D;%0A%0A    public CastAvailabilityHelper() %7B%0A    %7D%0A%0A    public void onRouteAdded(MediaRouter.RouteInfo route) %7B%0A        if (route.isDefault()) return;%0A        CastDevice castDevice = CastDevice.getFromBundle(route.getExtras());%0A        if (castDevice == null %7C%7C !castDevice.isOnLocalNetwork()) return;%0A        if (!mAvailableRoutes.containsKey(route.getId())) %7B%0A            mHandler.removeCallbacksAndMessages(route.getId());%0A            mAvailableRoutes.put(route.getId(), castDevice.getFriendlyName());%0A            onRoutesChanged(mAvailableRoutes);%0A        %7D%0A    %7D%0A%0A    public void onRouteRemoved(final MediaRouter router, final MediaRouter.RouteInfo route) %7B%0A        if (!mAvailableRoutes.containsKey(route.getId())) return;%0A        mHandler.postAtTime(new OnRouteRemovedRunnable(router, route.getId()), route.getId(), SystemClock.uptimeMillis() + 3000);%0A    %7D%0A%0A    public void onWifiDisconnected() %7B%0A        mHandler.removeCallbacksAndMessages(null);%0A        mAvailableRoutes.clear();%0A        onRoutesChanged(mAvailableRoutes);%0A    %7D%0A%0A    public Map%3CString, String%3E getAvailableRoutes() %7B%0A        return mAvailableRoutes;%0A    %7D%0A%0A    abstract void onRoutesChanged(Map%3CString, String%3E availableRoutes);%0A%0A    public void refreshRoutes(MediaRouter mediaRouter) %7B%0A        for (String routeId : mAvailableRoutes.keySet()) %7B%0A            mHandler.postAtTime(new OnRouteRemovedRunnable(mediaRouter, routeId), routeId, SystemClock.uptimeMillis() + 3000);%0A        %7D%0A        for (MediaRouter.RouteInfo routeInfo : mediaRouter.getRoutes()) %7B%0A            onRouteAdded(routeInfo);%0A        %7D%0A    %7D%0A%0A    public class OnRouteRemovedRunnable implements Runnable %7B%0A%0A        private MediaRouter mMediaRouter;%0A        private String mRouteId;%0A%0A        public OnRouteRemovedRunnable(MediaRouter mediaRouter, String routeId) %7B%0A            mMediaRouter = mediaRouter;%0A            mRouteId = routeId;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            for (MediaRouter.RouteInfo routeInfo : mMediaRouter.getRoutes()) %7B%0A                if (mRouteId.equals(routeInfo.getId())) %7B%0A                    return;%0A                %7D%0A            %7D%0A            mAvailableRoutes.remove(mRouteId);%0A            onRoutesChanged(mAvailableRoutes);%0A        %7D%0A    %7D%0A%7D%0A

Add CastAvailability helper which keeps track of all the available routes and delays onRouteRemoved calls and