package com.stericson.RootShell.execution;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.stericson.RootShell.RootShell;
import java.io.IOException;

public class Command
{
  String[] command = new String[0];
  protected Context context = null;
  boolean executing = false;
  ExecutionMonitor executionMonitor = null;
  int exitCode = -1;
  boolean finished = false;
  boolean handlerEnabled = true;
  int id = 0;
  protected boolean javaCommand = false;
  Handler mHandler = null;
  boolean terminated = false;
  int timeout = RootShell.defaultCommandTimeout;
  public int totalOutput = 0;
  public int totalOutputProcessed = 0;
  
  public Command(int paramInt1, int paramInt2, String... paramVarArgs)
  {
    this.command = paramVarArgs;
    this.id = paramInt1;
    this.timeout = paramInt2;
    createHandler(RootShell.handlerEnabled);
  }
  
  public Command(int paramInt, boolean paramBoolean, String... paramVarArgs)
  {
    this.command = paramVarArgs;
    this.id = paramInt;
    createHandler(paramBoolean);
  }
  
  public Command(int paramInt, String... paramVarArgs)
  {
    this.command = paramVarArgs;
    this.id = paramInt;
    createHandler(RootShell.handlerEnabled);
  }
  
  private void createHandler(boolean paramBoolean)
  {
    this.handlerEnabled = paramBoolean;
    if ((Looper.myLooper() != null) && (paramBoolean))
    {
      RootShell.log("CommandHandler created");
      this.mHandler = new CommandHandler(null);
      return;
    }
    RootShell.log("CommandHandler not created");
  }
  
  public void commandCompleted(int paramInt1, int paramInt2) {}
  
  protected final void commandFinished()
  {
    if (!this.terminated) {
      try
      {
        if ((this.mHandler != null) && (this.handlerEnabled))
        {
          Message localMessage = this.mHandler.obtainMessage();
          Bundle localBundle = new Bundle();
          localBundle.putInt("action", 2);
          localMessage.setData(localBundle);
          this.mHandler.sendMessage(localMessage);
        }
        for (;;)
        {
          RootShell.log("Command " + this.id + " finished.");
          finishCommand();
          return;
          commandCompleted(this.id, this.exitCode);
        }
        return;
      }
      finally {}
    }
  }
  
  public void commandOutput(int paramInt, String paramString)
  {
    RootShell.log("Command", "ID: " + paramInt + ", " + paramString);
    this.totalOutputProcessed += 1;
  }
  
  public void commandTerminated(int paramInt, String paramString) {}
  
  public final void finish()
  {
    RootShell.log("Command finished at users request!");
    commandFinished();
  }
  
  protected final void finishCommand()
  {
    this.executing = false;
    this.finished = true;
    notifyAll();
  }
  
  public final String getCommand()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    int i = 0;
    while (i < this.command.length)
    {
      if (i > 0) {
        localStringBuilder.append('\n');
      }
      localStringBuilder.append(this.command[i]);
      i += 1;
    }
    return localStringBuilder.toString();
  }
  
  public final int getExitCode()
  {
    return this.exitCode;
  }
  
  public final boolean isExecuting()
  {
    return this.executing;
  }
  
  public final boolean isFinished()
  {
    return this.finished;
  }
  
  public final boolean isHandlerEnabled()
  {
    return this.handlerEnabled;
  }
  
  protected final void output(int paramInt, String paramString)
  {
    this.totalOutput += 1;
    if ((this.mHandler != null) && (this.handlerEnabled))
    {
      Message localMessage = this.mHandler.obtainMessage();
      Bundle localBundle = new Bundle();
      localBundle.putInt("action", 1);
      localBundle.putString("text", paramString);
      localMessage.setData(localBundle);
      this.mHandler.sendMessage(localMessage);
      return;
    }
    commandOutput(paramInt, paramString);
  }
  
  public final void resetCommand()
  {
    this.finished = false;
    this.totalOutput = 0;
    this.totalOutputProcessed = 0;
    this.executing = false;
    this.terminated = false;
    this.exitCode = -1;
  }
  
  protected final void setExitCode(int paramInt)
  {
    try
    {
      this.exitCode = paramInt;
      return;
    }
    finally {}
  }
  
  protected final void startExecution()
  {
    this.executionMonitor = new ExecutionMonitor(null);
    this.executionMonitor.setPriority(1);
    this.executionMonitor.start();
    this.executing = true;
  }
  
  public final void terminate()
  {
    RootShell.log("Terminating command at users request!");
    terminated("Terminated at users request!");
  }
  
  protected final void terminate(String paramString)
  {
    try
    {
      Shell.closeAll();
      RootShell.log("Terminating all shells.");
      terminated(paramString);
      return;
    }
    catch (IOException paramString) {}
  }
  
  /* Error */
  protected final void terminated(String paramString)
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: aload_0
    //   3: getfield 49	com/stericson/RootShell/execution/Command:mHandler	Landroid/os/Handler;
    //   6: ifnull +105 -> 111
    //   9: aload_0
    //   10: getfield 61	com/stericson/RootShell/execution/Command:handlerEnabled	Z
    //   13: ifeq +98 -> 111
    //   16: aload_0
    //   17: getfield 49	com/stericson/RootShell/execution/Command:mHandler	Landroid/os/Handler;
    //   20: invokevirtual 106	android/os/Handler:obtainMessage	()Landroid/os/Message;
    //   23: astore_2
    //   24: new 108	android/os/Bundle
    //   27: dup
    //   28: invokespecial 109	android/os/Bundle:<init>	()V
    //   31: astore_3
    //   32: aload_3
    //   33: ldc 111
    //   35: iconst_3
    //   36: invokevirtual 115	android/os/Bundle:putInt	(Ljava/lang/String;I)V
    //   39: aload_3
    //   40: ldc -75
    //   42: aload_1
    //   43: invokevirtual 184	android/os/Bundle:putString	(Ljava/lang/String;Ljava/lang/String;)V
    //   46: aload_2
    //   47: aload_3
    //   48: invokevirtual 121	android/os/Message:setData	(Landroid/os/Bundle;)V
    //   51: aload_0
    //   52: getfield 49	com/stericson/RootShell/execution/Command:mHandler	Landroid/os/Handler;
    //   55: aload_2
    //   56: invokevirtual 125	android/os/Handler:sendMessage	(Landroid/os/Message;)Z
    //   59: pop
    //   60: new 127	java/lang/StringBuilder
    //   63: dup
    //   64: invokespecial 128	java/lang/StringBuilder:<init>	()V
    //   67: ldc -126
    //   69: invokevirtual 134	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   72: aload_0
    //   73: getfield 65	com/stericson/RootShell/execution/Command:id	I
    //   76: invokevirtual 137	java/lang/StringBuilder:append	(I)Ljava/lang/StringBuilder;
    //   79: ldc -41
    //   81: invokevirtual 134	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   84: aload_1
    //   85: invokevirtual 134	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   88: invokevirtual 143	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   91: invokestatic 92	com/stericson/RootShell/RootShell:log	(Ljava/lang/String;)V
    //   94: aload_0
    //   95: iconst_m1
    //   96: invokevirtual 217	com/stericson/RootShell/execution/Command:setExitCode	(I)V
    //   99: aload_0
    //   100: iconst_1
    //   101: putfield 59	com/stericson/RootShell/execution/Command:terminated	Z
    //   104: aload_0
    //   105: invokevirtual 146	com/stericson/RootShell/execution/Command:finishCommand	()V
    //   108: aload_0
    //   109: monitorexit
    //   110: return
    //   111: aload_0
    //   112: aload_0
    //   113: getfield 65	com/stericson/RootShell/execution/Command:id	I
    //   116: aload_1
    //   117: invokevirtual 219	com/stericson/RootShell/execution/Command:commandTerminated	(ILjava/lang/String;)V
    //   120: goto -60 -> 60
    //   123: astore_1
    //   124: aload_0
    //   125: monitorexit
    //   126: aload_1
    //   127: athrow
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	128	0	this	Command
    //   0	128	1	paramString	String
    //   23	33	2	localMessage	Message
    //   31	17	3	localBundle	Bundle
    // Exception table:
    //   from	to	target	type
    //   2	60	123	finally
    //   60	110	123	finally
    //   111	120	123	finally
    //   124	126	123	finally
  }
  
  private class CommandHandler
    extends Handler
  {
    public static final String ACTION = "action";
    public static final int COMMAND_COMPLETED = 2;
    public static final int COMMAND_OUTPUT = 1;
    public static final int COMMAND_TERMINATED = 3;
    public static final String TEXT = "text";
    
    private CommandHandler() {}
    
    public final void handleMessage(Message paramMessage)
    {
      int i = paramMessage.getData().getInt("action");
      paramMessage = paramMessage.getData().getString("text");
      switch (i)
      {
      default: 
        return;
      case 1: 
        Command.this.commandOutput(Command.this.id, paramMessage);
        return;
      case 2: 
        Command.this.commandCompleted(Command.this.id, Command.this.exitCode);
        return;
      }
      Command.this.commandTerminated(Command.this.id, paramMessage);
    }
  }
  
  private class ExecutionMonitor
    extends Thread
  {
    private ExecutionMonitor() {}
    
    public void run()
    {
      for (;;)
      {
        if ((Command.this.timeout > 0) && (!Command.this.finished)) {}
        try
        {
          synchronized (Command.this)
          {
            Command.this.wait(Command.this.timeout);
            if (Command.this.finished) {
              continue;
            }
            RootShell.log("Timeout Exception has occurred.");
            Command.this.terminate("Timeout Exception");
          }
          return;
        }
        catch (InterruptedException localInterruptedException)
        {
          for (;;) {}
        }
      }
    }
  }
}


/* Location:              /home/enzo/Documents/hacking/malwares/viking-horde/classes.jar!/com/stericson/RootShell/execution/Command.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */