package nl.rmokveld.castnotifications;

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.media.MediaRouter;
import android.util.Log;

public class DiscoveryService extends BaseCastService {

    private static final String TAG = "DiscoveryService";
    private static final String ACTION_START = BuildConfig.APPLICATION_ID + ".action.START_DISCOVERY";
    private static final String ACTION_REMOVE_TIMEOUT = BuildConfig.APPLICATION_ID + ".action_REMOVE_TIMEOUT";

    public static void start(Context context) {
        context.startService(buildIntent(context));
    }

    static Intent buildIntent(Context context) {
        return new Intent(context, DiscoveryService.class).setAction(ACTION_START);
    }

    public static void stop(Context context) {
        context.stopService(new Intent(context, DiscoveryService.class));
    }

    public static void removeTimeout(Context context) {
        context.startService(new Intent(context, DiscoveryService.class).setAction(ACTION_REMOVE_TIMEOUT));
    }

    @Override
    public String getTAG() {
        return TAG;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(getTAG(), "onStartCommand() called with: " + "intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
        if (ACTION_START.equals(intent.getAction())) {
            if (!DeviceStateHelper.isWifiConnected(this)) {
                stopDiscovery();
                return START_NOT_STICKY;
            }
            mCastNotificationManager.refreshRoutes(mMediaRouter);
            startDiscovery(false, DeviceStateHelper.isScreenTurnedOn(this) ? 10000 : 0);
        } else if (ACTION_REMOVE_TIMEOUT.equals(intent.getAction())) {
            resetTimeout(0);
        }
        return START_REDELIVER_INTENT;
    }

    @Override
    protected void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo routeInfo) {
        Log.d(getTAG(), "onRouteAdded() called with: " + "router = [" + router + "], routeInfo = [" + routeInfo + "]");
        mCastNotificationManager.getMediaRouterCallback().onRouteAdded(router, routeInfo);
    }

    @Override
    protected void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo route) {
        Log.d(getTAG(), "onRouteRemoved() called with: " + "router = [" + router + "], route = [" + route + "]");
        mCastNotificationManager.getMediaRouterCallback().onRouteRemoved(router, route);
    }

    @Override
    protected void onDiscoveryTimeout() {
        Log.d(getTAG(), "onDiscoveryTimeout() called with: " + "");
        stopSelf();
    }

    @Override
    public void onDestroy() {
        Log.d(getTAG(), "onDestroy() called with: " + "");
        mCastNotificationManager.setDiscoveryAlarm();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

@@ -456,16 +456,133 @@
 OVERY%22;%0A
+    private static final String ACTION_START_WAKEUP = BuildConfig.APPLICATION_ID + %22.action.START_DISCOVERY_WAKEUP%22;%0A
     priv
@@ -833,32 +833,148 @@
 text context) %7B%0A
+        return buildIntent(context, false);%0A    %7D%0A%0A    static Intent buildIntent(Context context, boolean wakeup) %7B%0A
         return n
@@ -1022,24 +1022,55 @@
 ).setAction(
+wakeup ? ACTION_START_WAKEUP : 
 ACTION_START
@@ -1719,32 +1719,82 @@
 ent.getAction())
+ %7C%7C ACTION_START_WAKEUP.equals(intent.getAction())
 ) %7B%0A            
@@ -1995,16 +1995,116 @@
 outer);%0A
+            if (ACTION_START_WAKEUP.equals(intent.getAction()))%0A                acquireWakeLocks();%0A
         

Add action to do discovery with device wakeup (for Discovery from Android Wear)