• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.tools.sdkcontroller.handlers;
18 
19 import android.content.Context;
20 import android.graphics.Point;
21 import android.os.Message;
22 import android.util.Log;
23 
24 import com.android.tools.sdkcontroller.lib.EmulatorConnection;
25 
26 
27 public class MultiTouchHandler extends BaseHandler {
28 
29     @SuppressWarnings("hiding")
30     private static final String TAG = MultiTouchHandler.class.getSimpleName();
31     /**
32      * A new frame buffer has been received from the emulator.
33      * Parameter {@code obj} is a {@code byte[] array} containing the screen data.
34      */
35     public static final int EVENT_FRAME_BUFFER = 1;
36     /**
37      * A multi-touch "start" command has been received from the emulator.
38      * Parameter {@code obj} is the string parameter from the start command.
39      */
40     public static final int EVENT_MT_START = 2;
41     /**
42      * A multi-touch "stop" command has been received from the emulator.
43      * There is no {@code obj} parameter associated.
44      */
45     public static final int EVENT_MT_STOP = 3;
46 
47     private static final Point mViewSize = new Point(0, 0);
48 
MultiTouchHandler()49     public MultiTouchHandler() {
50         super(HandlerType.MultiTouch, EmulatorConnection.MULTITOUCH_PORT);
51     }
52 
setViewSize(int width, int height)53     public void setViewSize(int width, int height) {
54         mViewSize.set(width, height);
55     }
56 
57     @Override
onStart(EmulatorConnection connection, Context context)58     public void onStart(EmulatorConnection connection, Context context) {
59         super.onStart(connection, context);
60     }
61 
62     @Override
onStop()63     public void onStop() {
64         super.onStop();
65     }
66 
67     /**
68      * Called when a query is received from the emulator. NOTE: This method is
69      * called from the I/O loop.
70      *
71      * @param query Name of the query received from the emulator. The allowed
72      *            queries are: - 'start' - Starts delivering touch screen events
73      *            to the emulator. - 'stop' - Stops delivering touch screen
74      *            events to the emulator.
75      * @param param Query parameters.
76      * @return Zero-terminated reply string. String must be formatted as such:
77      *         "ok|ko[:reply data]"
78      */
79     @Override
onEmulatorQuery(String query, String param)80     public String onEmulatorQuery(String query, String param) {
81         if (query.contentEquals("start")) {
82             Message msg = Message.obtain();
83             msg.what = EVENT_MT_START;
84             msg.obj = param;
85             notifyUiHandlers(msg);
86             return "ok:" + mViewSize.x + "x" + mViewSize.y + "\0";
87 
88         } else if (query.contentEquals("stop")) {
89             notifyUiHandlers(EVENT_MT_STOP);
90             return "ok\0";
91 
92         } else {
93             Log.e(TAG, "Unknown query " + query + "(" + param + ")");
94             return "ko:Unknown query\0";
95         }
96     }
97 
98     /**
99      * Called when a BLOB query is received from the emulator.
100      * <p/>
101      * This query is used to deliver framebuffer updates in the emulator. The
102      * blob contains an update header, followed by the bitmap containing updated
103      * rectangle. The header is defined as MTFrameHeader structure in
104      * external/qemu/android/multitouch-port.h
105      * <p/>
106      * NOTE: This method is called from the I/O loop, so all communication with
107      * the emulator will be "on hold" until this method returns.
108      *
109      * @param array contains BLOB data for the query.
110      * @return Empty string: this query doesn't require any response.
111      */
112     @Override
onEmulatorBlobQuery(byte[] array)113     public String onEmulatorBlobQuery(byte[] array) {
114         Message msg = Message.obtain();
115         msg.what = EVENT_FRAME_BUFFER;
116         msg.obj = array;
117         notifyUiHandlers(msg);
118         return "";
119     }
120 
121 }
122