Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(974)

Side by Side Diff: samples/android/src/org/dartlang/example/dart/DartActivity.java

Issue 11362103: Rotating spheres sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More cleanup Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/android/src/org/dartlang/Dart.java ('k') | samples/android/tools/build.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 package org.dartlang.example.dart;
6
7 import java.io.BufferedReader;
8 import java.io.File;
9 import java.io.FileOutputStream;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.IOException;
13 import java.io.OutputStream;
14
15 import javax.microedition.khronos.egl.EGLConfig;
16 import javax.microedition.khronos.opengles.GL10;
17
18 import android.app.Activity;
19 import android.content.res.AssetManager;
20 import android.opengl.GLES20;
21 import android.opengl.GLSurfaceView;
22 import android.opengl.GLSurfaceView.Renderer;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.View;
26 import android.view.Window;
27 import android.view.WindowManager;
28 import android.widget.EditText;
29 import android.widget.TextView;
30
31 import org.dartlang.Dart;
32
33
34 public class DartActivity extends Activity
35 {
36 /** The OpenGL view */
37 private GLSurfaceView glSurfaceView;
38
39 private static final String TAG = "DartActivity";
40 private static int sDartIsolate;
41
42 class GlRenderer implements Renderer {
43 @Override
44 public void onDrawFrame(GL10 gl) {
45 DartActivity.this.callDart("draw", new String[] {});
46 }
47
48 @Override
49 public void onSurfaceChanged(GL10 gl, int width, int height) {
50 Log.i(TAG, "onSurfaceChanged: [" + width + ", " + height + "]");
51 DartActivity.this.callDart("resize", new Object[] {width, height});
52 }
53
54 @Override
55 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
56 Log.w(TAG, "Calling start");
57 ensureDartStarted();
58 DartActivity.this.callDart("setup", new Object[] {});
59 }
60 }
61
62 /** Called when the activity is first created. */
63 @Override
64 public void onCreate(Bundle savedInstanceState)
65 {
66 super.onCreate(savedInstanceState);
67
68 requestWindowFeature(Window.FEATURE_NO_TITLE);
69 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
70 WindowManager.LayoutParams.FLAG_FULLSCREEN);
71 glSurfaceView = new GLSurfaceView(this);
72 // Create an OpenGL ES 2.0 context
73 glSurfaceView.setEGLContextClientVersion(2);
74 glSurfaceView.setRenderer(new GlRenderer());
75 // Render the view only when there is a change in the drawing data
76 // glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
77
78 setContentView(glSurfaceView);
79 }
80
81 public void ensureDartStarted() {
82 if (sDartIsolate == 0) {
83 try {
84 String scriptPath = unpackAssets();
85 startDartVM(scriptPath + "/simplegl.dart");
86 } catch(Exception e) {
87 Log.e(TAG, "Could not start DartVM ", e);
88 throw new RuntimeException("Could not start DartVM", e);
89 }
90 }
91 }
92
93 private String unpackAssets() throws IOException {
94 File localDir = getApplicationContext().getDir("dart", 0);
95 String fileSystemPath = localDir.toString();
96 copyAssets("dart", fileSystemPath);
97 return fileSystemPath;
98 }
99
100 private void copyAssets(String assetPath, String fileSystemPath)
101 throws IOException {
102 AssetManager assetManager = getAssets();
103 String[] files = assetManager.list(assetPath);
104 for (String filename : files) {
105 InputStream in = null;
106 OutputStream out = null;
107 in = assetManager.open(assetPath + "/" + filename);
108 String dest = fileSystemPath + "/" + filename;
109 Log.w(TAG, "Copying " + dest);
110 out = new FileOutputStream(dest);
111 copyFile(in, out);
112 in.close();
113 in = null;
114 out.flush();
115 ((FileOutputStream)out).getFD().sync();
116 out.close();
117 out = null;
118 }
119 }
120
121 private void copyFile(InputStream in, OutputStream out) throws IOException {
122 byte[] buffer = new byte[1024];
123 int read;
124 while((read = in.read(buffer)) != -1){
125 out.write(buffer, 0, read);
126 }
127 }
128
129 private void startDartVM(final String scriptPath) {
130 String[] args = new String[]{
131 "dart", scriptPath
132 };
133 sDartIsolate = Dart.dartStart(args);
134 if (sDartIsolate == 0) {
135 Log.e(TAG, "Could not start Dart VM");
136 throw new RuntimeException("Error starting Dart VM");
137 }
138 }
139
140 private Object callDart(String function, Object[] args) {
141 if (sDartIsolate == 0) {
142 throw new RuntimeException("No isolate.");
143 }
144 Dart.EnterIsolate(sDartIsolate);
145 Dart.EnterScope();
146 try {
147 // Lookup the library of the root script.
148 int library = Dart.RootLibrary();
149 if (Dart.IsNull(library)) {
150 throw new RuntimeException("Unable to find root library.");
151 }
152 int[] dartArgs = new int[args.length];
153 for (int i = 0; i < args.length; i++) {
154 if (args[i] instanceof String) {
155 dartArgs[i] = Dart.NewString((String) args[i]);
156 } else {
157 Integer arg = (Integer) args[i];
158 dartArgs[i] = Dart.NewInteger(arg.intValue());
159 }
160 }
161 Log.i(TAG, "Calling: " + function);
162 int result = Dart.Invoke(library, Dart.NewString(function), dartArgs );
163 if (Dart.IsError(result)) {
164 String error = Dart.GetError(result);
165 Log.w(TAG, "Error: " + error);
166 return "Dart.Invoke error: " + error;
167 }
168 return result;
169 } catch (Exception e) {
170 Log.w(TAG, "Exception: " + e);
171 return "Error";
172 } finally {
173 Dart.ExitScope();
174 Dart.ExitIsolate();
175 }
176 }
177 }
OLDNEW
« no previous file with comments | « samples/android/src/org/dartlang/Dart.java ('k') | samples/android/tools/build.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698