Android Open Source - Processing-Android-Eclipse-Demos Android Runner






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

/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
//  ww w. j  a va  2 s. c  o  m
/*
 Part of the Processing project - http://processing.org

 Copyright (c) 2011 Ben Fry and Casey Reas

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License version 2
 as published by the Free Software Foundation.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software Foundation,
 Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package processing.mode.android;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import processing.app.RunnerListener;
import processing.app.SketchException;
import processing.mode.java.runner.Runner;


public class AndroidRunner implements DeviceListener {
  AndroidBuild build;
  RunnerListener listener;
  
  
  public AndroidRunner(AndroidBuild build, RunnerListener listener) {
    this.build = build;
    this.listener = listener;
  }
  
  
  public void launch(Future<Device> deviceFuture) {
//    try {
//      runSketchOnDevice(Devices.getInstance().getEmulator(), "debug", AndroidEditor.this);
//    } catch (final MonitorCanceled ok) {
//      sketchStopped();
//      statusNotice("Canceled.");
//    }

    listener.statusNotice("Waiting for device to become available...");
//  final Device device = waitForDevice(deviceFuture, monitor);
    final Device device = waitForDevice(deviceFuture, listener);
    if (device == null || !device.isAlive()) {
      listener.statusError("Lost connection with device while launching. Try again.");
      // Reset the server, in case that's the problem. Sometimes when 
      // launching the emulator times out, the device list refuses to update.
      Devices.killAdbServer();
      return;
    }

    device.addListener(this);

//  if (listener.isHalted()) {
////  if (monitor.isCanceled()) {
//    throw new MonitorCanceled();
//  }

//  monitor.setNote("Installing sketch on " + device.getId());
    listener.statusNotice("Installing sketch on " + device.getId());
    // this stopped working with Android SDK tools revision 17
    if (!device.installApp(build.getPathForAPK(), listener)) {
      listener.statusError("Lost connection with device while installing. Try again.");
      Devices.killAdbServer();  // see above
      return;
    }
//    if (!build.antInstall()) {      
//    }

//  if (monitor.isCanceled()) {
//    throw new MonitorCanceled();
//  }
//  monitor.setNote("Starting sketch on " + device.getId());
    listener.statusNotice("Starting sketch on " + device.getId());
    if (startSketch(build, device)) {
      listener.statusNotice("Sketch launched on the "
                            + (device.isEmulator() ? "emulator" : "device") + ".");
    } else {
      listener.statusError("Could not start the sketch.");
    }
    listener.stopIndeterminate();
    lastRunDevice = device;
//} finally {
//  build.cleanup();
//}
//} finally {
////monitor.close();
//listener.stopIndeterminate();
//}
  }
  
  
  private volatile Device lastRunDevice = null;

  /**
   * @param target "debug" or "release"
   */
  /*
  private void runSketchOnDevice(Sketch sketch,
                                 Future<Device> deviceFuture,
                                 String target,
                                 RunnerListener listener) {
//    final IndeterminateProgressMonitor monitor =
//      new IndeterminateProgressMonitor(this,
//                                       "Building and launching...",
//                                       "Creating project...");

    
    AndroidBuild build = new AndroidBuild(sketch, listener);
    try {
      try {
        if (build.createProject(target) == null) {
          return;
        }
      } catch (SketchException se) {
        listener.statusError(se);
      } catch (IOException e) {
        listener.statusError(e);
      }
      try {
//        if (monitor.isCanceled()) {
//          throw new MonitorCanceled();
//        }
//        monitor.setNote("Building...");
        listener.statusNotice("Building...");
        try {
          if (!build.antBuild(target)) {
            return;
          }
        } catch (SketchException se) {
          listener.statusError(se);
        }

//        if (monitor.isCanceled()) {
//          throw new MonitorCanceled();
//        }
//        monitor.setNote("Waiting for device to become available...");
        listener.statusNotice("Waiting for device to become available...");
//        final Device device = waitForDevice(deviceFuture, monitor);
        final Device device = waitForDevice(deviceFuture, listener);
        if (device == null || !device.isAlive()) {
          listener.statusError("Device killed or disconnected.");
          return;
        }

        device.addListener(this);

//        if (listener.isHalted()) {
////        if (monitor.isCanceled()) {
//          throw new MonitorCanceled();
//        }

//        monitor.setNote("Installing sketch on " + device.getId());
        listener.statusNotice("Installing sketch on " + device.getId());
        if (!device.installApp(build.getPathForAPK(target), listener)) {
          listener.statusError("Device killed or disconnected.");
          return;
        }

//        if (monitor.isCanceled()) {
//          throw new MonitorCanceled();
//        }
//        monitor.setNote("Starting sketch on " + device.getId());
        listener.statusNotice("Starting sketch on " + device.getId());
        if (startSketch(build, device)) {
          listener.statusNotice("Sketch launched on the "
              + (device.isEmulator() ? "emulator" : "device") + ".");
        } else {
          listener.statusError("Could not start the sketch.");
        }

        lastRunDevice = device;
      } finally {
        build.cleanup();
      }
    } finally {
//      monitor.close();
      listener.stopIndeterminate();
    }
  }
  */

  
  // if user asks for 480x320, 320x480, 854x480 etc, then launch like that
  // though would need to query the emulator to see if it can do that

  private boolean startSketch(AndroidBuild build, final Device device) {
    final String packageName = build.getPackageName();
    final String className = build.getSketchClassName();
    try {
      if (device.launchApp(packageName, className)) {
        return true;
      }
    } catch (final Exception e) {
      e.printStackTrace(System.err);
    }
    return false;
  }


  private Device waitForDevice(Future<Device> deviceFuture, RunnerListener listener) {
    for (int i = 0; i < 120; i++) {
//      if (monitor.isCanceled()) {
      if (listener.isHalted()) {
        deviceFuture.cancel(true);
//        throw new MonitorCanceled();
        return null;
      }
      try {
        return deviceFuture.get(1, TimeUnit.SECONDS);
      } catch (final InterruptedException e) {
        listener.statusError("Interrupted.");
        return null;
      } catch (final ExecutionException e) {
        listener.statusError(e);
        return null;
      } catch (final TimeoutException expected) {
      }
    }
    listener.statusError("No, on second thought, I'm giving up " +
                         "on waiting for that device to show up.");
    return null;
  }
  
  
  private static final Pattern LOCATION =
    Pattern.compile("\\(([^:]+):(\\d+)\\)");
  private static final Pattern EXCEPTION_PARSER =
    Pattern.compile("^\\s*([a-z]+(?:\\.[a-z]+)+)(?:: .+)?$",
                    Pattern.CASE_INSENSITIVE);

  /**
   * Currently figures out the first relevant stack trace line
   * by looking for the telltale presence of "processing.android"
   * in the package. If the packaging for droid sketches changes,
   * this method will have to change too.
   */
  public void stackTrace(final List<String> trace) {
    final Iterator<String> frames = trace.iterator();
    final String exceptionLine = frames.next();

    final Matcher m = EXCEPTION_PARSER.matcher(exceptionLine);
    if (!m.matches()) {
      System.err.println("Can't parse this exception line:");
      System.err.println(exceptionLine);
      listener.statusError("Unknown exception");
      return;
    }
    final String exceptionClass = m.group(1);
//    if (Runner.handleCommonErrors(exceptionClass, exceptionLine, listener)) {
//      return;
//    }
    Runner.handleCommonErrors(exceptionClass, exceptionLine, listener);

    while (frames.hasNext()) {
      final String line = frames.next();
      if (line.contains("processing.android")) {
        final Matcher lm = LOCATION.matcher(line);
        if (lm.find()) {
          final String filename = lm.group(1);
          final int lineNumber = Integer.parseInt(lm.group(2)) - 1;
          final SketchException rex =
            build.placeException(exceptionLine, filename, lineNumber);
          listener.statusError(rex == null ? new SketchException(exceptionLine, false) : rex);
          return;
        }
      }
    }
  }


  // called by AndroidMode.handleStop()...
  public void close() {
    if (lastRunDevice != null) {
      lastRunDevice.bringLauncherToFront();
    }
  }


  // sketch stopped on the device 
  public void sketchStopped() {
    listener.stopIndeterminate();
    listener.statusHalt();
  }
}




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