package core.task.restart;

import core.command.CommandExecutor;
import core.message.DeployMessage;
import core.message.RestartAppMessage;
import core.task.Task;
import core.task.TaskPurpose;
import core.telemetry.Telemetry;

public class RestartAppTask implements Task<DeployMessage, RestartAppMessage> {

    private final String androidSdkPath;
    private final String appPackage;
    private final String mainActivity;

    public RestartAppTask(String androidSdkPath, String appPackage, String mainActivity) {
        this.androidSdkPath = androidSdkPath;
        this.appPackage = appPackage;
        this.mainActivity = mainActivity;
    }

    @Override
    public TaskPurpose getPurpose() {
        return TaskPurpose.RESTART_APPLICATION;
    }

    @Override
    public RestartAppMessage exec(Telemetry telemetry, DeployMessage message) {
        telemetry.message("Application package: %s", appPackage);
        telemetry.message("Application launcher activity: %s", mainActivity);
        telemetry.message("Restarting...");

        String cmdForceStop = String.format("adb shell am force-stop %s", appPackage);
        String cmdStart = String.format("adb shell am start -n \"%s/%s\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER", appPackage, mainActivity);
        CommandExecutor.exec(cmdForceStop);
        CommandExecutor.exec(cmdStart);
        return null;
    }
}

@@ -1032,16 +1032,201 @@
 ...%22);%0A%0A
+        String adbFilePath = androidSdkPath + %22/platform-tools/adb%22;%0A        String action = %22android.intent.action.MAIN%22;%0A        String category = %22android.intent.category.LAUNCHER%22;%0A
         
@@ -1254,35 +1254,34 @@
  String.format(%22
-adb
+%25s
  shell am force-
@@ -1285,24 +1285,37 @@
 ce-stop %25s%22,
+ adbFilePath,
  appPackage)
@@ -1357,19 +1357,18 @@
 format(%22
-adb
+%25s
  shell a
@@ -1395,71 +1395,30 @@
  -a 
-android.intent.action.MAIN -c android.intent.category.LAUNCHER%22
+%25s -c %25s%22, adbFilePath
 , ap
@@ -1431,32 +1431,50 @@
 ge, mainActivity
+, action, category
 );%0A        Comma

Fix for running adb tool.