Index: samples/android/src/org/dartlang/example/dart/DartActivity.java |
diff --git a/samples/android/src/org/dartlang/example/dart/DartActivity.java b/samples/android/src/org/dartlang/example/dart/DartActivity.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c73c4cfae8dbadb772cb184edbe7e97bff0cb3d0 |
--- /dev/null |
+++ b/samples/android/src/org/dartlang/example/dart/DartActivity.java |
@@ -0,0 +1,177 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+package org.dartlang.example.dart; |
+ |
+import java.io.BufferedReader; |
+import java.io.File; |
+import java.io.FileOutputStream; |
+import java.io.InputStream; |
+import java.io.InputStreamReader; |
+import java.io.IOException; |
+import java.io.OutputStream; |
+ |
+import javax.microedition.khronos.egl.EGLConfig; |
+import javax.microedition.khronos.opengles.GL10; |
+ |
+import android.app.Activity; |
+import android.content.res.AssetManager; |
+import android.opengl.GLES20; |
+import android.opengl.GLSurfaceView; |
+import android.opengl.GLSurfaceView.Renderer; |
+import android.os.Bundle; |
+import android.util.Log; |
+import android.view.View; |
+import android.view.Window; |
+import android.view.WindowManager; |
+import android.widget.EditText; |
+import android.widget.TextView; |
+ |
+import org.dartlang.Dart; |
+ |
+ |
+public class DartActivity extends Activity |
+{ |
+ /** The OpenGL view */ |
+ private GLSurfaceView glSurfaceView; |
+ |
+ private static final String TAG = "DartActivity"; |
+ private static int sDartIsolate; |
+ |
+ class GlRenderer implements Renderer { |
+ @Override |
+ public void onDrawFrame(GL10 gl) { |
+ DartActivity.this.callDart("draw", new String[] {}); |
+ } |
+ |
+ @Override |
+ public void onSurfaceChanged(GL10 gl, int width, int height) { |
+ Log.i(TAG, "onSurfaceChanged: [" + width + ", " + height + "]"); |
+ DartActivity.this.callDart("resize", new Object[] {width, height}); |
+ } |
+ |
+ @Override |
+ public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
+ Log.w(TAG, "Calling start"); |
+ ensureDartStarted(); |
+ DartActivity.this.callDart("setup", new Object[] {}); |
+ } |
+ } |
+ |
+ /** Called when the activity is first created. */ |
+ @Override |
+ public void onCreate(Bundle savedInstanceState) |
+ { |
+ super.onCreate(savedInstanceState); |
+ |
+ requestWindowFeature(Window.FEATURE_NO_TITLE); |
+ getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
+ WindowManager.LayoutParams.FLAG_FULLSCREEN); |
+ glSurfaceView = new GLSurfaceView(this); |
+ // Create an OpenGL ES 2.0 context |
+ glSurfaceView.setEGLContextClientVersion(2); |
+ glSurfaceView.setRenderer(new GlRenderer()); |
+ // Render the view only when there is a change in the drawing data |
+ // glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); |
+ |
+ setContentView(glSurfaceView); |
+ } |
+ |
+ public void ensureDartStarted() { |
+ if (sDartIsolate == 0) { |
+ try { |
+ String scriptPath = unpackAssets(); |
+ startDartVM(scriptPath + "/simplegl.dart"); |
+ } catch(Exception e) { |
+ Log.e(TAG, "Could not start DartVM ", e); |
+ throw new RuntimeException("Could not start DartVM", e); |
+ } |
+ } |
+ } |
+ |
+ private String unpackAssets() throws IOException { |
+ File localDir = getApplicationContext().getDir("dart", 0); |
+ String fileSystemPath = localDir.toString(); |
+ copyAssets("dart", fileSystemPath); |
+ return fileSystemPath; |
+ } |
+ |
+ private void copyAssets(String assetPath, String fileSystemPath) |
+ throws IOException { |
+ AssetManager assetManager = getAssets(); |
+ String[] files = assetManager.list(assetPath); |
+ for (String filename : files) { |
+ InputStream in = null; |
+ OutputStream out = null; |
+ in = assetManager.open(assetPath + "/" + filename); |
+ String dest = fileSystemPath + "/" + filename; |
+ Log.w(TAG, "Copying " + dest); |
+ out = new FileOutputStream(dest); |
+ copyFile(in, out); |
+ in.close(); |
+ in = null; |
+ out.flush(); |
+ ((FileOutputStream)out).getFD().sync(); |
+ out.close(); |
+ out = null; |
+ } |
+ } |
+ |
+ private void copyFile(InputStream in, OutputStream out) throws IOException { |
+ byte[] buffer = new byte[1024]; |
+ int read; |
+ while((read = in.read(buffer)) != -1){ |
+ out.write(buffer, 0, read); |
+ } |
+ } |
+ |
+ private void startDartVM(final String scriptPath) { |
+ String[] args = new String[]{ |
+ "dart", scriptPath |
+ }; |
+ sDartIsolate = Dart.dartStart(args); |
+ if (sDartIsolate == 0) { |
+ Log.e(TAG, "Could not start Dart VM"); |
+ throw new RuntimeException("Error starting Dart VM"); |
+ } |
+ } |
+ |
+ private Object callDart(String function, Object[] args) { |
+ if (sDartIsolate == 0) { |
+ throw new RuntimeException("No isolate."); |
+ } |
+ Dart.EnterIsolate(sDartIsolate); |
+ Dart.EnterScope(); |
+ try { |
+ // Lookup the library of the root script. |
+ int library = Dart.RootLibrary(); |
+ if (Dart.IsNull(library)) { |
+ throw new RuntimeException("Unable to find root library."); |
+ } |
+ int[] dartArgs = new int[args.length]; |
+ for (int i = 0; i < args.length; i++) { |
+ if (args[i] instanceof String) { |
+ dartArgs[i] = Dart.NewString((String) args[i]); |
+ } else { |
+ Integer arg = (Integer) args[i]; |
+ dartArgs[i] = Dart.NewInteger(arg.intValue()); |
+ } |
+ } |
+ Log.i(TAG, "Calling: " + function); |
+ int result = Dart.Invoke(library, Dart.NewString(function), dartArgs); |
+ if (Dart.IsError(result)) { |
+ String error = Dart.GetError(result); |
+ Log.w(TAG, "Error: " + error); |
+ return "Dart.Invoke error: " + error; |
+ } |
+ return result; |
+ } catch (Exception e) { |
+ Log.w(TAG, "Exception: " + e); |
+ return "Error"; |
+ } finally { |
+ Dart.ExitScope(); |
+ Dart.ExitIsolate(); |
+ } |
+ } |
+} |