Android Open Source - android-tetheringfixer Wait Command






From Project

Back to project page android-tetheringfixer.

License

The source code is released under:

MIT License

If you think the Android project android-tetheringfixer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * The MIT License (MIT)//from w  ww  . ja  v a  2s . c  o  m
 *
 * Copyright (c) 2014 Eugenio Marletti
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package me.eugeniomarletti.tetheringfixer.command;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.execution.Command;

import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;

// don't make final
public class WaitCommand extends Command
{
    private static final int LOG_TYPE_DEBUG = 3;

    private static int lastId = -1;

    private static final Field timeout;

    public final int id;
    private       CommandResult  result    = null;
    private final StringBuilder  output    = new StringBuilder();
    private final CountDownLatch countdown = new CountDownLatch(1);

    static
    {
        final String timeoutFieldName = "timeout";
        Field _timeout;
        try
        {
            _timeout = Command.class.getDeclaredField(timeoutFieldName);
            _timeout.setAccessible(true);
        }
        catch (NoSuchFieldException e)
        {
            _timeout = null;
            RootTools.log(String.format("WARNING! Field '%s' not found in super class.", timeoutFieldName),
                          LOG_TYPE_DEBUG, e);
        }
        timeout = _timeout;
    }

    public int getTimeout()
    {
        if (timeout != null)
        {
            try
            {
                return timeout.getInt(this);
            }
            catch (IllegalAccessException ignore)
            {
            }
        }
        return RootTools.default_Command_Timeout;
    }

    public void setTimeout(int timeout)
    {
        if (WaitCommand.timeout != null)
        {
            try
            {
                WaitCommand.timeout.setInt(this, timeout);
            }
            catch (IllegalAccessException ignore)
            {
            }
        }
    }

    private static synchronized int getNextId()
    {
        if (lastId == Integer.MAX_VALUE) lastId = -1;
        return ++lastId;
    }

    public WaitCommand(int id, String... command)
    {
        super(id, false, command);
        this.id = id;
    }

    public WaitCommand(String... command)
    {
        this(getNextId(), command);
    }

    public String getOutput()
    {
        return output.toString();
    }

    @Override
    public void commandOutput(int id, String s)
    {
        output.append(s).append('\n');
    }

    @Override
    protected void startExecution()
    {
        if (isFinished()) throw new RuntimeException(String.format("Can't start finished command (id: %d).", id));
        super.startExecution();
    }

    public CommandResult getResult()
    {
        return result;
    }

    protected void commandCompletedOrTerminated(int id, int exitcode, boolean terminated, String terminatedReason)
    {
        RootTools.log(String.format("Output (id:%d exitcode:%d):\n%s", id, exitcode, getOutput()));
        result = new CommandResult(id, exitcode, terminated, terminatedReason);
        countdown.countDown();
    }

    @Override
    public void commandTerminated(int id, String reason)
    {
        commandCompletedOrTerminated(id, getExitCode(), true, reason);
    }

    @Override
    public void commandCompleted(int id, int exitcode)
    {
        commandCompletedOrTerminated(id, exitcode, false, null);
    }

    public CommandResult waitForFinish()
    {
        RootTools.log(String.format("Waiting for command %d...", id));
        do
        {
            try
            {
                countdown.await();
            }
            catch (InterruptedException e)
            {
                RootTools.log(String.format("...InterruptedException while waiting for command %d...", id),
                              LOG_TYPE_DEBUG, e);
            }
        }
        while (countdown.getCount() > 0);
        RootTools.log(String.format("...Done waiting for command %d", id));

        return getResult();
    }
}




Java Source Code List

me.eugeniomarletti.tetheringfixer.Async.java
me.eugeniomarletti.tetheringfixer.Fixer.java
me.eugeniomarletti.tetheringfixer.Steps.java
me.eugeniomarletti.tetheringfixer.Utils.java
me.eugeniomarletti.tetheringfixer.android.Application.java
me.eugeniomarletti.tetheringfixer.android.BootBroadcastReceiver.java
me.eugeniomarletti.tetheringfixer.android.BulletExpandEffect.java
me.eugeniomarletti.tetheringfixer.android.Bullet.java
me.eugeniomarletti.tetheringfixer.android.DrawableState.java
me.eugeniomarletti.tetheringfixer.android.PackageManager.java
me.eugeniomarletti.tetheringfixer.android.SimpleAnimatorListener.java
me.eugeniomarletti.tetheringfixer.android.StepsActivity.java
me.eugeniomarletti.tetheringfixer.android.StepsService.java
me.eugeniomarletti.tetheringfixer.android.Switch.java
me.eugeniomarletti.tetheringfixer.android.TextView.java
me.eugeniomarletti.tetheringfixer.command.CommandException.java
me.eugeniomarletti.tetheringfixer.command.CommandNotFoundException.java
me.eugeniomarletti.tetheringfixer.command.CommandNotOkException.java
me.eugeniomarletti.tetheringfixer.command.CommandResult.java
me.eugeniomarletti.tetheringfixer.command.WaitCommand.java