diff options
Diffstat (limited to 'platform_tools/android/app/src/com/skia')
5 files changed, 576 insertions, 0 deletions
diff --git a/platform_tools/android/app/src/com/skia/SkiaIntentService.java b/platform_tools/android/app/src/com/skia/SkiaIntentService.java new file mode 100644 index 0000000000..e2707f7964 --- /dev/null +++ b/platform_tools/android/app/src/com/skia/SkiaIntentService.java @@ -0,0 +1,93 @@ +// Copyright 2012 Google Inc. All Rights Reserved. + +package com.skia; + +import android.app.IntentService; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; +import android.util.Log; + +/** + * @author borenet@google.com (Eric Boren) + * + */ +public class SkiaIntentService extends IntentService { + public SkiaIntentService() { + super("SkiaIntentService"); + } + + @Override + public IBinder onBind(Intent arg0) { + return null; + } + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + } + + @Override + public void onHandleIntent(Intent intent) { + + // Extract command-line arguments + Bundle bundle = intent.getExtras(); + + // Number of times to repeat the SkiaReturnCode in the log. + int returnRepeats = bundle.getInt("returnRepeats", 1); + + // We require at least the program name to be specified. + if (!bundle.containsKey("args")) { + Log.e("skia", + "No command line arguments supplied. Unable to continue."); + SkiaReturn(-1, returnRepeats); + return; + } + + String cmd = bundle.getString("args").trim(); + String[] args = cmd.split("\\s+"); + Log.d("skia", "Executing Command: " + cmd); + + // Load the requested library + String lib = args[0]; + try { + System.loadLibrary(lib); + } catch (UnsatisfiedLinkError e) { + Log.e("skia", "Library " + lib + + " could not be linked! Unable to continue."); + SkiaReturn(-1, returnRepeats); + throw e; + } + + // JNI call to run the program + int retval = run(args); + SkiaReturn(retval, returnRepeats); + } + + /** + * Print out the exit code of the native program. Skia's buildbots watch the + * logcat output for this line. The buildbots occasionally have to restart + * a dead adb process, which causes them to miss some log output (Bug: + * https://code.google.com/p/skia/issues/detail?id=809). If this + * "SKIA_RETURN_CODE" line is missed while adb is being restarted, then the + * test may never finish. Therefore, we print the line as many times as the + * caller specifies, waiting one second in between. + */ + private void SkiaReturn(int code, int repeats) { + Log.d("skia", "SKIA_RETURN_CODE " + code); + for (int i = 1; i < repeats; ++i) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + return; + } + Log.d("skia", "SKIA_RETURN_CODE " + code); + } + } + + native int run(String[] args); +} diff --git a/platform_tools/android/app/src/com/skia/SkiaReceiver.java b/platform_tools/android/app/src/com/skia/SkiaReceiver.java new file mode 100644 index 0000000000..b62a979051 --- /dev/null +++ b/platform_tools/android/app/src/com/skia/SkiaReceiver.java @@ -0,0 +1,26 @@ +// Copyright 2012 Google Inc. All Rights Reserved. + +package com.skia; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import com.skia.SkiaIntentService; + +/** + * @author borenet@google.com (Eric Boren) + * + */ +public class SkiaReceiver extends BroadcastReceiver +{ + @Override + public void onReceive(Context context, Intent intent) { + Intent skIntent = new Intent(context, SkiaIntentService.class); + + // Forward any command-line arguments to the background service + skIntent.putExtras(intent.getExtras()); + + // Launch executable + context.startService(skIntent); + } +}
\ No newline at end of file diff --git a/platform_tools/android/app/src/com/skia/SkiaSampleActivity.java b/platform_tools/android/app/src/com/skia/SkiaSampleActivity.java new file mode 100644 index 0000000000..4b5e7d6b7f --- /dev/null +++ b/platform_tools/android/app/src/com/skia/SkiaSampleActivity.java @@ -0,0 +1,200 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +package com.skia; + +import android.app.ActionBar; +import android.app.Activity; +import android.app.DownloadManager; +import android.content.Context; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import java.io.File; + +public class SkiaSampleActivity extends Activity +{ + private TextView mTitle; + private SkiaSampleView mSampleView; + + private ArrayAdapter<String> mSlideList; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.layout); + mTitle = (TextView) findViewById(R.id.title_view); + mSampleView = new SkiaSampleView(this); + mSlideList = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1); + + try { + System.loadLibrary("SampleApp"); + + LinearLayout holder = (LinearLayout) findViewById(R.id.holder); + holder.addView(mSampleView, new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT)); + + setupActionBar(); + + } catch (UnsatisfiedLinkError e) { + mTitle.setText("ERROR: native library could not be loaded"); + } + } + + private void setupActionBar() { + ActionBar.OnNavigationListener navigationCallback = new ActionBar.OnNavigationListener() { + @Override + public boolean onNavigationItemSelected(int position, long itemId) { + mSampleView.goToSample(position); + return true; + } + }; + + ActionBar actionBar = getActionBar(); + actionBar.setDisplayShowHomeEnabled(false); + actionBar.setDisplayShowTitleEnabled(false); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + actionBar.setListNavigationCallbacks(mSlideList, navigationCallback); + } + + @Override + protected void onResume () { + super.onResume(); + if (mSampleView.getWidth() > 0 && mSampleView.getHeight() > 0) { + //TODO try mSampleView.requestRender() instead + mSampleView.inval(); + } + } + + @Override + public void onDestroy() { + mSampleView.terminate(); + super.onDestroy(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.action_bar, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.overview: + mSampleView.showOverview(); + return true; + case R.id.prev: + mSampleView.previousSample(); + return true; + case R.id.next: + mSampleView.nextSample(); + return true; + case R.id.toggle_rendering: + mSampleView.toggleRenderingMode(); + return true; + case R.id.slideshow: + mSampleView.toggleSlideshow(); + return true; + case R.id.fps: + mSampleView.toggleFPS(); + return true; + case R.id.tiling: + mSampleView.toggleTiling(); + return true; + case R.id.bbox: + mSampleView.toggleBBox(); + return true; + case R.id.save_to_pdf: + mSampleView.saveToPDF(); + return true; + default: + return false; + } + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + final int keycode = event.getKeyCode(); + if (keycode == KeyEvent.KEYCODE_BACK) { + if (event.getAction() == KeyEvent.ACTION_UP) { + finish(); + } + return true; + } + return false; + } + + private static final int SET_TITLE = 1; + private static final int SET_SLIDES = 2; + private static final int TOAST_DOWNLOAD = 3; + + private Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case SET_TITLE: + mTitle.setText((String) msg.obj); + SkiaSampleActivity.this.getActionBar().setSubtitle((String) msg.obj); + break; + case SET_SLIDES: + mSlideList.addAll((String[]) msg.obj); + break; + case TOAST_DOWNLOAD: + Toast.makeText(SkiaSampleActivity.this, (String) msg.obj, + Toast.LENGTH_SHORT).show(); + break; + default: + break; + } + } + }; + + // Called by JNI + @Override + public void setTitle(CharSequence title) { + mHandler.obtainMessage(SET_TITLE, title).sendToTarget(); + } + + // Called by JNI + public void setSlideList(String[] slideList) { + mHandler.obtainMessage(SET_SLIDES, slideList).sendToTarget(); + } + + // Called by JNI + public void addToDownloads(final String title, final String desc, final String path) { + File file = new File(path); + final long length = file.exists() ? file.length() : 0; + if (length == 0) { + String failed = getString(R.string.save_failed); + mHandler.obtainMessage(TOAST_DOWNLOAD, failed).sendToTarget(); + return; + } + String toast = getString(R.string.file_saved).replace("%s", title); + mHandler.obtainMessage(TOAST_DOWNLOAD, toast).sendToTarget(); + final DownloadManager manager = + (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + new Thread("Add PDF to downloads") { + @Override + public void run() { + final String mimeType = "application/pdf"; + manager.addCompletedDownload(title, desc, true, mimeType, path, length, true); + } + }.start(); + } +} diff --git a/platform_tools/android/app/src/com/skia/SkiaSampleRenderer.java b/platform_tools/android/app/src/com/skia/SkiaSampleRenderer.java new file mode 100644 index 0000000000..1479c92396 --- /dev/null +++ b/platform_tools/android/app/src/com/skia/SkiaSampleRenderer.java @@ -0,0 +1,92 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +package com.skia; + +import android.opengl.GLSurfaceView; +import android.os.Handler; + +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + +public class SkiaSampleRenderer implements GLSurfaceView.Renderer { + + private final SkiaSampleView mSampleView; + private Handler mHandler = new Handler(); + + SkiaSampleRenderer(SkiaSampleView view) { + mSampleView = view; + } + + @Override + public void onDrawFrame(GL10 gl) { + draw(); + } + + @Override + public void onSurfaceChanged(GL10 gl, int width, int height) { + updateSize(width, height); + } + + @Override + public void onSurfaceCreated(GL10 gl, EGLConfig config) { + gl.glClearStencil(0); + gl.glClear(GL10.GL_STENCIL_BUFFER_BIT); + init((SkiaSampleActivity)mSampleView.getContext()); + } + + // Called by JNI + private void startTimer(int ms) { + // After the delay, queue an event to the Renderer's thread + // to handle the event on the timer queue + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + mSampleView.queueEvent(new Runnable() { + @Override + public void run() { + serviceQueueTimer(); + } + }); + } + }, ms); + } + + // Called by JNI + private void queueSkEvent() { + mSampleView.queueEvent(new Runnable() { + @Override + public void run() { + processSkEvent(); + } + }); + } + + // Called by JNI + private void requestRender() { + mSampleView.requestRender(); + } + + native void init(SkiaSampleActivity activity); + native void term(); + native void draw(); + native void updateSize(int w, int h); + native void handleClick(int owner, float x, float y, int state); + native void showOverview(); + native void nextSample(); + native void previousSample(); + native void goToSample(int position); + native void toggleRenderingMode(); + native void toggleSlideshow(); + native void toggleFPS(); + native void toggleTiling(); + native void toggleBBox(); + native void processSkEvent(); + native void serviceQueueTimer(); + native void saveToPDF(); + native void postInval(); +}
\ No newline at end of file diff --git a/platform_tools/android/app/src/com/skia/SkiaSampleView.java b/platform_tools/android/app/src/com/skia/SkiaSampleView.java new file mode 100644 index 0000000000..0d493c96df --- /dev/null +++ b/platform_tools/android/app/src/com/skia/SkiaSampleView.java @@ -0,0 +1,165 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +package com.skia; + +import android.content.Context; +import android.opengl.GLSurfaceView; +import android.view.MotionEvent; + +public class SkiaSampleView extends GLSurfaceView { + + private final SkiaSampleRenderer mSampleRenderer; + + public SkiaSampleView(Context ctx) { + super(ctx); + + mSampleRenderer = new SkiaSampleRenderer(this); + + setEGLContextClientVersion(2); + setEGLConfigChooser(8,8,8,8,0,8); + setRenderer(mSampleRenderer); + setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + int count = event.getPointerCount(); + for (int i = 0; i < count; i++) { + final float x = event.getX(i); + final float y = event.getY(i); + final int owner = event.getPointerId(i); + int action = event.getAction() & MotionEvent.ACTION_MASK; + switch (action) { + case MotionEvent.ACTION_POINTER_UP: + action = MotionEvent.ACTION_UP; + break; + case MotionEvent.ACTION_POINTER_DOWN: + action = MotionEvent.ACTION_DOWN; + break; + default: + break; + } + final int finalAction = action; + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.handleClick(owner, x, y, finalAction); + } + }); + } + return true; + } + + public void inval() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.postInval(); + } + }); + } + + public void terminate() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.term(); + } + }); + } + + public void showOverview() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.showOverview(); + } + }); + } + + public void nextSample() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.nextSample(); + } + }); + } + + public void previousSample() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.previousSample(); + } + }); + } + + public void goToSample(final int position) { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.goToSample(position); + } + }); + } + + public void toggleRenderingMode() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.toggleRenderingMode(); + } + }); + } + + public void toggleSlideshow() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.toggleSlideshow(); + } + }); + } + + public void toggleFPS() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.toggleFPS(); + } + }); + } + + public void toggleTiling() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.toggleTiling(); + } + }); + } + + public void toggleBBox() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.toggleBBox(); + } + }); + } + + public void saveToPDF() { + queueEvent(new Runnable() { + @Override + public void run() { + mSampleRenderer.saveToPDF(); + } + }); + } +} |