diff options
Diffstat (limited to 'apps/SdkController/SdkControllerMultitouch/src')
2 files changed, 0 insertions, 654 deletions
diff --git a/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/MultiTouchView.java b/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/MultiTouchView.java deleted file mode 100755 index fc3f994..0000000 --- a/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/MultiTouchView.java +++ /dev/null @@ -1,228 +0,0 @@ -/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.android.tools.sdkcontroller.sdkcontrollermultitouch;
-
-import java.io.InputStream;
-
-import android.content.Context;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.graphics.Canvas;
-import android.graphics.Matrix;
-import android.graphics.Paint;
-import android.util.AttributeSet;
-import android.util.Log;
-import android.view.MotionEvent;
-import android.view.View;
-
-/**
- * Implements a main view for the application providing multi-touch emulation.
- */
-public class MultiTouchView extends View {
- /** Tag for logging messages. */
- private static final String TAG = "SdkControllerMultitouch";
- /**
- * Back-end bitmap. Initialized in onSizeChanged(), updated in
- * onTouchEvent() and drawn in onDraw().
- */
- private Bitmap mBitmap;
- /** Default Paint instance for drawing the bitmap. */
- private final Paint mPaint = new Paint();
- /** Canvas instance for this view. */
- private Canvas mCanvas;
- /** Emulator screen width to this view width ratio. */
- private float mDx = 1;
- /** Emulator screen height to this view height ratio. */
- private float mDy = 1;
- /**
- * Flags whether or not image received from the emulator should be rotated.
- * Rotation is required when display orientation state of the emulator and
- * the device doesn't match.
- */
- private boolean mRotateDisplay;
- /** Base matrix that keep emulator->device display scaling */
- private Matrix mBaseMatrix = new Matrix();
- /** Matrix that is used to draw emulator's screen on the device. */
- private Matrix mDrawMatrix = new Matrix();
-
- /**
- * Simple constructor to use when creating a view from code.
- *
- * @see View#View(Context)
- */
- public MultiTouchView(Context context) {
- this(context, null);
- }
-
- /**
- * Constructor that is called when inflating a view from XML.
- *
- * @see View#View(Context, AttributeSet)
- */
- public MultiTouchView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
-
- /**
- * Perform inflation from XML and apply a class-specific base style.
- *
- * @see View#View(Context, AttributeSet, int)
- */
- public MultiTouchView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
-
- // TODO Add constructor-time code here.
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
-
- mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- mCanvas = new Canvas(mBitmap);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- // Just draw the back-end bitmap without zooming or scaling.
- if (mBitmap != null) {
- canvas.drawBitmap(mBitmap, 0, 0, null);
- }
- }
-
- /**
- * Sets emulator screen width and height to this view width and height
- * ratio.
- *
- * @param dx Emulator screen width to this view width ratio.
- * @param dy Emulator screen height to this view height ratio.
- * @param rotateDisplay Flags whether image received from the emulator
- * should be rotated when drawn on the device.
- */
- public void setDxDy(float dx, float dy, boolean rotateDisplay) {
- mDx = dx;
- mDy = dy;
- mRotateDisplay = rotateDisplay;
-
- mBaseMatrix.setScale(dx, dy);
- if (mRotateDisplay) {
- mBaseMatrix.postRotate(90);
- mBaseMatrix.postTranslate(getWidth(), 0);
- }
- }
-
- /**
- * Computes draw matrix for the emulator screen update.
- *
- * @param x Left screen coordinate of the bitmap on emulator screen.
- * @param y Top screen coordinate of the bitmap on emulator screen.
- */
- private void computeDrawMatrix(int x, int y) {
- mDrawMatrix.set(mBaseMatrix);
- if (mRotateDisplay) {
- mDrawMatrix.postTranslate(-y * mDy, x * mDx);
- } else {
- mDrawMatrix.postTranslate(x * mDx, y * mDy);
- }
- }
-
- /**
- * Draws a bitmap on the screen.
- *
- * @param x Left screen coordinate of the bitmap on emulator screen.
- * @param y Top screen coordinate of the bitmap on emulator screen.
- * @param w Width of the bitmap on the emulator screen.
- * @param h Height of the bitmap on the emulator screen.
- * @param colors Bitmap to draw.
- */
- public void drawBitmap(int x, int y, int w, int h, int[] colors) {
- if (mCanvas != null) {
- final Bitmap bmp = Bitmap.createBitmap(colors, 0, w, w, h, Bitmap.Config.ARGB_8888);
-
- computeDrawMatrix(x, y);
-
- /* Draw the bitmap and invalidate the updated region. */
- mCanvas.drawBitmap(bmp, mDrawMatrix, mPaint);
- invalidate();
- }
- }
-
- /**
- * Draws a JPEG bitmap on the screen.
- *
- * @param x Left screen coordinate of the bitmap on emulator screen.
- * @param y Top screen coordinate of the bitmap on emulator screen.
- * @param w Width of the bitmap on the emulator screen.
- * @param h Height of the bitmap on the emulator screen.
- * @param jpeg JPEG bitmap to draw.
- */
- public void drawJpeg(int x, int y, int w, int h, InputStream jpeg) {
- if (mCanvas != null) {
- final Bitmap bmp = BitmapFactory.decodeStream(jpeg);
-
- computeDrawMatrix(x, y);
-
- /* Draw the bitmap and invalidate the updated region. */
- mCanvas.drawBitmap(bmp, mDrawMatrix, mPaint);
- invalidate();
- }
- }
-
- /**
- * Constructs touch event message to be send to emulator.
- *
- * @param sb String builder where to construct the message.
- * @param event Event for which to construct the message.
- * @param ptr_index Index of the motion pointer for which to construct the
- * message.
- */
- void constructEventMessage(StringBuilder sb, MotionEvent event, int ptr_index) {
- sb.append(" pid=").append(event.getPointerId(ptr_index));
- if (mRotateDisplay == false) {
- sb.append(" x=").append((int) (event.getX(ptr_index) / mDx));
- sb.append(" y=").append((int) (event.getY(ptr_index) / mDy));
- } else {
- sb.append(" x=").append((int) (event.getY(ptr_index) / mDy));
- sb.append(" y=").append((int) (getWidth() - event.getX(ptr_index) / mDx));
- }
- // At the system level the input reader takes integers in the range
- // 0 - 100 for the pressure.
- int pressure = (int) (event.getPressure(ptr_index) * 100);
- // Make sure it doesn't exceed 100...
- if (pressure > 100) {
- pressure = 100;
- }
- sb.append(" pressure=").append(pressure);
- }
-
- /***************************************************************************
- * Logging wrappers
- **************************************************************************/
-
- private void Loge(String log) {
- Log.e(TAG, log);
- }
-
- private void Logw(String log) {
- Log.w(TAG, log);
- }
-
- private void Logv(String log) {
- Log.v(TAG, log);
- }
-}
diff --git a/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/SdkControllerMultitouchActivity.java b/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/SdkControllerMultitouchActivity.java deleted file mode 100644 index 4715ac4..0000000 --- a/apps/SdkController/SdkControllerMultitouch/src/com/android/tools/sdkcontroller/sdkcontrollermultitouch/SdkControllerMultitouchActivity.java +++ /dev/null @@ -1,426 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.android.tools.sdkcontroller.sdkcontrollermultitouch; - -import java.io.ByteArrayInputStream; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -import android.app.Activity; -import android.graphics.Color; -import android.os.Bundle; -import android.util.Log; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnTouchListener; -import android.view.WindowManager; -import android.widget.Toast; - -import com.android.tools.sdkcontroller.lib.EmulatorConnection; -import com.android.tools.sdkcontroller.lib.EmulatorConnection.EmulatorConnectionType; -import com.android.tools.sdkcontroller.lib.EmulatorListener; - -/** - * Encapsulates an application that monitors multi-touch activities on a device, - * and reports them to an Android Emulator application running on the host - * machine. This application is used to provide a realistic multi-touch - * emulation in Android Emulator. - */ -public class SdkControllerMultitouchActivity extends Activity implements EmulatorListener { - /** Tag for logging messages. */ - private static final String TAG = "SdkControllerMultitouch"; - /** Received frame is JPEG image. */ - private static final int FRAME_JPEG = 1; - /** Received frame is RGB565 bitmap. */ - private static final int FRAME_RGB565 = 2; - /** Received frame is RGB888 bitmap. */ - private static final int FRAME_RGB888 = 3; - - /** TCP over USB connection to the emulator. */ - private EmulatorConnection mEmulator; - /** View for this application. */ - private MultiTouchView mView; - /** Listener to touch events. */ - private TouchListener mTouchListener; - /** Width of the emulator's display. */ - private int mEmulatorWidth = 0; - /** Height of the emulator's display. */ - private int mEmulatorHeight = 0; - /** Bitmap storage. */ - private int[] mColors; - - /** - * Implements OnTouchListener interface that receives touch screen events, - * and reports them to the emulator application. - */ - class TouchListener implements OnTouchListener { - /** - * Touch screen event handler. - */ - @Override - public boolean onTouch(View v, MotionEvent event) { - StringBuilder sb = new StringBuilder(); - final int action = event.getAction(); - final int action_code = action & MotionEvent.ACTION_MASK; - final int action_pid_index = action >> MotionEvent.ACTION_POINTER_ID_SHIFT; - - /* - * Build message for the emulator. - */ - - switch (action_code) { - case MotionEvent.ACTION_MOVE: - sb.append("action=move"); - for (int n = 0; n < event.getPointerCount(); n++) { - mView.constructEventMessage(sb, event, n); - } - break; - case MotionEvent.ACTION_DOWN: - sb.append("action=down"); - mView.constructEventMessage(sb, event, action_pid_index); - break; - case MotionEvent.ACTION_UP: - sb.append("action=up pid=").append(event.getPointerId(action_pid_index)); - break; - case MotionEvent.ACTION_POINTER_DOWN: - sb.append("action=pdown"); - mView.constructEventMessage(sb, event, action_pid_index); - break; - case MotionEvent.ACTION_POINTER_UP: - sb.append("action=pup pid=").append(event.getPointerId(action_pid_index)); - break; - default: - Logw("Unknown action type: " + action_code); - return true; - } - - Logv(sb.toString()); - mEmulator.sendNotification(sb.toString() + '\0'); - return true; - } - } // TouchListener - - /** Called when the activity is first created. */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - mView = (MultiTouchView) findViewById(R.id.imageView); - getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - - // Create listener for touch events. - mTouchListener = new TouchListener(); - } - - @Override - protected void onResume() { - super.onResume(); - - // Instantiate emulator connector. - // This will call onEmulatorBindResult with the result. - mEmulator = new EmulatorConnection(this); - mEmulator.connect(EmulatorConnection.MULTITOUCH_PORT, - EmulatorConnectionType.SYNC_CONNECTION); - } - - @Override - protected void onPause() { - super.onPause(); - - if (mEmulator != null) { - mEmulator.setEmulatorListener(null); - mEmulator.disconnect(); - mEmulator = null; - } - } - - /** - * Updates application's screen accordingly to the emulator screen. - * - * @param e_width Width of the emulator screen. - * @param e_height Height of the emulator screen. - */ - private void updateDisplay(int e_width, int e_height) { - if (e_width != mEmulatorWidth || e_height != mEmulatorHeight) { - mEmulatorWidth = e_width; - mEmulatorHeight = e_height; - - boolean rotateDisplay = false; - int w = mView.getWidth(); - int h = mView.getHeight(); - if (w > h != e_width > e_height) { - rotateDisplay = true; - int tmp = w; - w = h; - h = tmp; - } - - float dx = (float) w / (float) e_width; - float dy = (float) h / (float) e_height; - mView.setDxDy(dx, dy, rotateDisplay); - Logv("Dispay updated: " + e_width + " x " + e_height + - " -> " + w + " x " + h + " ratio: " + - dx + " x " + dy); - } - } - - /*************************************************************************** - * EmulatorListener implementation - **************************************************************************/ - - /** - * Called when emulator is connected. NOTE: This method is called from the - * I/O loop, so all communication with the emulator will be "on hold" until - * this method returns. - */ - @Override - public void onEmulatorConnected() { - Logv("Emulator is connected"); - } - - /** - * Called when emulator is disconnected. - */ - @Override - public void onEmulatorDisconnected() { - Logv("Emulator is disconnected."); - // Stop listening on events, and let it cool for a sec... - onStopEvents(); - try { - Thread.sleep(500); - } catch (Exception e) { - } - - // Instantiate emulator connector for the next client. - // This will call onEmulatorBindResult with the result. - mEmulator = new EmulatorConnection(this); - mEmulator.connect(EmulatorConnection.MULTITOUCH_PORT, - EmulatorConnectionType.SYNC_CONNECTION); - } - - /** - * Called with the result from {@code new EmulatorConnection} - */ - @Override - public void onEmulatorBindResult(boolean success, Exception e) { - if (!success) { - String msg = "Failed to connect to server socket"; - if (e != null) msg += ": " + e.toString(); - Loge(msg); - Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); - finish(); - } - } - - /** - * Called when a query is received from the emulator. NOTE: This method is - * called from the I/O loop. - * - * @param query Name of the query received from the emulator. The allowed - * queries are: - 'start' - Starts delivering touch screen events - * to the emulator. - 'stop' - Stops delivering touch screen - * events to the emulator. - * @param param Query parameters. - * @return Zero-terminated reply string. String must be formatted as such: - * "ok|ko[:reply data]" - */ - @Override - public String onEmulatorQuery(String query, String param) { - if (query.contentEquals("start")) { - return onQueryStart(param); - } else if (query.contentEquals("stop")) { - return onQueryStop(); - } else { - Loge("Unknown query " + query + "(" + param + ")"); - return "ko:Unknown query\0"; - } - } - - /** - * Called when a BLOB query is received from the emulator. - * <p/> - * This query is used to deliver framebuffer updates in the emulator. The - * blob contains an update header, followed by the bitmap containing updated - * rectangle. The header is defined as MTFrameHeader structure in - * external/qemu/android/multitouch-port.h - * <p/> - * NOTE: This method is called from the I/O loop, so all communication with - * the emulator will be "on hold" until this method returns. - * - * @param array contains BLOB data for the query. - * @return Empty string: this query doesn't require any response. - */ - @Override - public String onEmulatorBlobQuery(byte[] array) { - final ByteBuffer bb = ByteBuffer.wrap(array); - bb.order(ByteOrder.LITTLE_ENDIAN); - - // Read frame header. - final int header_size = bb.getInt(); - final int disp_width = bb.getInt(); - final int disp_height = bb.getInt(); - final int x = bb.getInt(); - final int y = bb.getInt(); - final int w = bb.getInt(); - final int h = bb.getInt(); - final int bpl = bb.getInt(); - final int bpp = bb.getInt(); - final int format = bb.getInt(); - - // Update application display. - updateDisplay(disp_width, disp_height); - - mView.post(new Runnable() { - @Override - public void run() { - if (format == FRAME_JPEG) { - /* - * Framebuffer is in JPEG format. - */ - - final ByteArrayInputStream jpg = new ByteArrayInputStream(bb.array()); - // Advance input stream to JPEG image. - jpg.skip(header_size); - // Draw the image. - mView.drawJpeg(x, y, w, h, jpg); - } else { - /* - * Framebuffer is in a raw RGB format. - */ - - final int pixel_num = h * w; - // Advance stream to the beginning of framebuffer data. - bb.position(header_size); - - // Make sure that mColors is large enough to contain the - // update bitmap. - if (mColors == null || mColors.length < pixel_num) { - mColors = new int[pixel_num]; - } - - // Convert the blob bitmap into bitmap that we will display. - if (format == FRAME_RGB565) { - for (int n = 0; n < pixel_num; n++) { - // Blob bitmap is in RGB565 format. - final int color = bb.getShort(); - final int r = ((color & 0xf800) >> 8) | ((color & 0xf800) >> 14); - final int g = ((color & 0x7e0) >> 3) | ((color & 0x7e0) >> 9); - final int b = ((color & 0x1f) << 3) | ((color & 0x1f) >> 2); - mColors[n] = Color.rgb(r, g, b); - } - } else if (format == FRAME_RGB888) { - for (int n = 0; n < pixel_num; n++) { - // Blob bitmap is in RGB565 format. - final int r = bb.getChar(); - final int g = bb.getChar(); - final int b = bb.getChar(); - mColors[n] = Color.rgb(r, g, b); - } - } else { - Logw("Invalid framebuffer format: " + format); - return; - } - mView.drawBitmap(x, y, w, h, mColors); - } - } - }); - - return ""; - } - - /*************************************************************************** - * Emulator query handlers - **************************************************************************/ - - /** - * Handles 'start' query. - * - * @return 'ok:<WidthxHeight> on success, or 'ko:<reason>' on failure. Width - * and height returned on success represent width and height of the - * application view. - */ - private String onQueryStart(String param) { - // Lets see if query has parameters. - int sep = param.indexOf('x'); - if (sep != -1) { - final String dx = param.substring(0, sep); - final String dy = param.substring(sep + 1); - final int x = Integer.parseInt(dx); - final int y = Integer.parseInt(dy); - - updateDisplay(x, y); - } - onStartEvents(); - return "ok:" + mView.getWidth() + "x" + mView.getHeight() + "\0"; - } - - /** - * Handles 'stop' query. - * - * @return 'ok'. - */ - private String onQueryStop() { - onStopEvents(); - return "ok\0"; - } - - /*************************************************************************** - * Internals - **************************************************************************/ - - /** - * Registers touch screen event listener, and starts receiving touch screen - * events. - */ - private void onStartEvents() { - mView.post(new Runnable() { - @Override - public void run() { - mView.setOnTouchListener(mTouchListener); - } - }); - } - - /** - * Unregisters touch screen event listener, and stops receiving touch screen - * events. - */ - private void onStopEvents() { - mView.post(new Runnable() { - @Override - public void run() { - mView.setOnTouchListener(null); - } - }); - } - - /*************************************************************************** - * Logging wrappers - **************************************************************************/ - - private void Loge(String log) { - Log.e(TAG, log); - } - - private void Logw(String log) { - Log.w(TAG, log); - } - - private void Logv(String log) { - Log.v(TAG, log); - } -} |