Android Open Source - Processing-Android-Eclipse-Demos Emulator Controller






From Project

Back to project page Processing-Android-Eclipse-Demos.

License

The source code is released under:

MIT License

If you think the Android project Processing-Android-Eclipse-Demos 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

package processing.mode.android;
// w  ww  . j  a  va2  s . c o m
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import processing.app.Base;
import processing.app.Preferences;
import processing.app.exec.*;

import processing.core.PApplet;


class EmulatorController {
  public static enum State {
    NOT_RUNNING, WAITING_FOR_BOOT, RUNNING
  }

  private volatile State state = State.NOT_RUNNING;


  public State getState() {
    return state;
  }

  
  private void setState(final State state) {
    if (processing.app.Base.DEBUG) {
      //System.out.println("Emulator state: " + state);
      new Exception("setState(" + state + ") called").printStackTrace(System.out);
    }
    this.state = state;
  }
  

  /**
   * Blocks until emulator is running, or some catastrophe happens.
   * @throws IOException
   */
  synchronized public void launch() throws IOException {
    if (state != State.NOT_RUNNING) {
      String illegal = "You can't launch an emulator whose state is " + state;
      throw new IllegalStateException(illegal);
    }

    String portString = Preferences.get("android.emulator.port");
    if (portString == null) {
      portString = "5566";
      Preferences.set("android.emulator.port", portString);
    }

    // See http://developer.android.com/guide/developing/tools/emulator.html
    final String[] cmd = new String[] {
      "emulator",
      "-avd", AVD.defaultAVD.name,
      "-port", portString,
//      "-no-boot-anim",  // does this do anything?
      // http://code.google.com/p/processing/issues/detail?id=1059
//      "-gpu", "on"  // enable OpenGL
    };
    //System.err.println("EmulatorController: Launching emulator");
    if (Base.DEBUG) {
      System.out.println(processing.core.PApplet.join(cmd, " "));
    }
    //ProcessResult adbResult = new ProcessHelper(adbCmd).execute();
    final Process p = Runtime.getRuntime().exec(cmd);
    ProcessRegistry.watch(p);
//    new StreamPump(p.getInputStream(), "emulator: ").addTarget(System.out).start();

    // if we've gotten this far, then we've at least succeeded in finding and
    // beginning execution of the emulator, so we are now officially "Launched"
    setState(State.WAITING_FOR_BOOT);

    final String title = PApplet.join(cmd, ' ');

    // when this shows up on stdout:
    // emulator: ERROR: the cache image is used by another emulator. aborting
    // need to reset adb and try again, since it's running but adb is hosed
    StreamPump outie = new StreamPump(p.getInputStream(), "out: " + title);
    outie.addTarget(new LineProcessor() {
      public void processLine(String line) {
        if (line.contains("the cache image is used by another emulator")) {

        } else {
//          System.out.println(line);
          System.out.println(title + ": " + line);
        }
      }
    });
    //new StreamPump(p.getInputStream(), "out: " + title).addTarget(System.out).start();

    // suppress this warning on OS X, otherwise we're gonna get a lot of reports:
    // 2010-04-13 15:26:56.380 emulator[91699:903] Warning once: This
    // application, or a library it uses, is using NSQuickDrawView, which has
    // been deprecated. Apps should cease use of QuickDraw and move to Quartz.
    StreamPump errie = new StreamPump(p.getErrorStream(), "err: " + title);
    errie.addTarget(new LineProcessor() {
      public void processLine(String line) {
        if (line.contains("This application, or a library it uses, is using NSQuickDrawView")) {
          // i don't really care
        } else {
//          System.err.println(line);
          System.err.println(title + ": " + line);
        }
      }
    });
    //new StreamPump(p.getErrorStream(), "err: " + title).addTarget(System.err).start();

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {
      public void run() {
        try {
          //System.err.println("EmulatorController: Waiting for boot.");
          while (state == State.WAITING_FOR_BOOT) {
            if (processing.app.Base.DEBUG) {
              System.out.println("sleeping for 2 seconds " + new java.util.Date().toString());
            }
            Thread.sleep(2000);
            //System.out.println("done sleeping");
            for (final String device : Devices.list()) {
              if (device.contains("emulator")) {
                //System.err.println("EmulatorController: Emulator booted.");
                setState(State.RUNNING);
                return;
              }
            }
          }
          System.err.println("EmulatorController: Emulator never booted. " + state);
        } catch (Exception e) {
          System.err.println("Exception while waiting for emulator to boot:");
          e.printStackTrace();
          p.destroy();
        } finally {
          latch.countDown();
        }
      }
    }, "EmulatorController: Wait for emulator to boot").start();
    new Thread(new Runnable() {
      public void run() {
        try {
          final int result = p.waitFor();
          // On Windows (as of SDK tools 15), emulator.exe process will terminate
          // immediately, even though the emulator itself is launching correctly.
          // However on OS X and Linux the process will stay open.
          if (result != 0) {
            System.err.println("Emulator process exited with status " + result + ".");
            setState(State.NOT_RUNNING);
          }
        } catch (InterruptedException e) {
          System.err.println("Emulator was interrupted.");
          setState(State.NOT_RUNNING);
        } finally {
          p.destroy();
          ProcessRegistry.unwatch(p);
        }
      }
    }, "EmulatorController: emulator process waitFor()").start();
    try {
      latch.await();
    } catch (final InterruptedException drop) {
      System.err.println("Interrupted while waiting for emulator to launch.");
    }
  }

  
  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  
  
  // whoever called them "design patterns" certainly wasn't a f*king designer.
  
  public static EmulatorController getInstance() {
    return INSTANCE;
  }

  private static final EmulatorController INSTANCE = new EmulatorController();
}




Java Source Code List

.AccelerometerManager.java
.CompassManager.java
com.processing.core.PApplet.java
com.processing.core.PConstants.java
com.processing.core.PFont.java
com.processing.core.PGraphicsAndroid2D.java
com.processing.core.PGraphics.java
com.processing.core.PImage.java
com.processing.core.PMatrix2D.java
com.processing.core.PMatrix3D.java
com.processing.core.PMatrix.java
com.processing.core.PShapeOBJ.java
com.processing.core.PShapeSVG.java
com.processing.core.PShape.java
com.processing.core.PStyle.java
com.processing.core.PVector.java
com.processing.data.FloatDict.java
com.processing.data.FloatList.java
com.processing.data.IntDict.java
com.processing.data.IntList.java
com.processing.data.JSONArray.java
com.processing.data.JSONObject.java
com.processing.data.JSONTokener.java
com.processing.data.Sort.java
com.processing.data.StringDict.java
com.processing.data.StringList.java
com.processing.data.TableRow.java
com.processing.data.Table.java
com.processing.data.XML.java
com.processing.event.Event.java
com.processing.event.KeyEvent.java
com.processing.event.MouseEvent.java
com.processing.event.TouchEvent.java
com.processing.opengl.FontTexture.java
com.processing.opengl.FrameBuffer.java
com.processing.opengl.LinePath.java
com.processing.opengl.LineStroker.java
com.processing.opengl.PGLES.java
com.processing.opengl.PGL.java
com.processing.opengl.PGraphics2D.java
com.processing.opengl.PGraphics3D.java
com.processing.opengl.PGraphicsOpenGL.java
com.processing.opengl.PShader.java
com.processing.opengl.PShapeOpenGL.java
com.processing.opengl.Texture.java
com.processing.opengl.tess.ActiveRegion.java
com.processing.opengl.tess.CachedVertex.java
com.processing.opengl.tess.DictNode.java
com.processing.opengl.tess.Dict.java
com.processing.opengl.tess.GLUface.java
com.processing.opengl.tess.GLUhalfEdge.java
com.processing.opengl.tess.GLUmesh.java
com.processing.opengl.tess.GLUtessellatorImpl.java
com.processing.opengl.tess.GLUvertex.java
com.processing.opengl.tess.Geom.java
com.processing.opengl.tess.Mesh.java
com.processing.opengl.tess.Normal.java
com.processing.opengl.tess.PGLU.java
com.processing.opengl.tess.PGLUtessellatorCallbackAdapter.java
com.processing.opengl.tess.PGLUtessellatorCallback.java
com.processing.opengl.tess.PGLUtessellator.java
com.processing.opengl.tess.PriorityQHeap.java
com.processing.opengl.tess.PriorityQSort.java
com.processing.opengl.tess.PriorityQ.java
com.processing.opengl.tess.Render.java
com.processing.opengl.tess.Sweep.java
com.processing.opengl.tess.TessMono.java
com.processing.opengl.tess.TessState.java
processing.mode.android.AVD.java
processing.mode.android.AndroidBuild.java
processing.mode.android.AndroidEditor.java
processing.mode.android.AndroidMode.java
processing.mode.android.AndroidPreprocessor.java
processing.mode.android.AndroidRunner.java
processing.mode.android.AndroidSDK.java
processing.mode.android.AndroidToolbar.java
processing.mode.android.BadSDKException.java
processing.mode.android.Commander.java
processing.mode.android.DeviceListener.java
processing.mode.android.Device.java
processing.mode.android.Devices.java
processing.mode.android.EmulatorController.java
processing.mode.android.Export.java
processing.mode.android.Keys.java
processing.mode.android.LogEntry.java
processing.mode.android.Manifest.java
processing.mode.android.Permissions.java