Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.view;
     18 
     19 import dalvik.system.CloseGuard;
     20 
     21 import android.os.Handler;
     22 import android.os.Looper;
     23 import android.os.MessageQueue;
     24 import android.util.Pools.Pool;
     25 import android.util.Pools.SimplePool;
     26 import android.util.SparseArray;
     27 
     28 import java.lang.ref.WeakReference;
     29 
     30 /**
     31  * An input queue provides a mechanism for an application to receive incoming
     32  * input events.  Currently only usable from native code.
     33  */
     34 public final class InputQueue {
     35     private final SparseArray<ActiveInputEvent> mActiveEventArray =
     36             new SparseArray<ActiveInputEvent>(20);
     37     private final Pool<ActiveInputEvent> mActiveInputEventPool =
     38             new SimplePool<ActiveInputEvent>(20);
     39 
     40     private final CloseGuard mCloseGuard = CloseGuard.get();
     41 
     42     private int mPtr;
     43 
     44     private static native int nativeInit(WeakReference<InputQueue> weakQueue,
     45             MessageQueue messageQueue);
     46     private static native int nativeSendKeyEvent(int ptr, KeyEvent e, boolean preDispatch);
     47     private static native int nativeSendMotionEvent(int ptr, MotionEvent e);
     48     private static native void nativeDispose(int ptr);
     49 
     50     /** @hide */
     51     public InputQueue() {
     52         mPtr = nativeInit(new WeakReference<InputQueue>(this), Looper.myQueue());
     53 
     54         mCloseGuard.open("dispose");
     55     }
     56 
     57     @Override
     58     protected void finalize() throws Throwable {
     59         try {
     60             dispose(true);
     61         } finally {
     62             super.finalize();
     63         }
     64     }
     65 
     66     /** @hide */
     67     public void dispose() {
     68         dispose(false);
     69     }
     70 
     71     /** @hide */
     72     public void dispose(boolean finalized) {
     73         if (mCloseGuard != null) {
     74             if (finalized) {
     75                 mCloseGuard.warnIfOpen();
     76             }
     77             mCloseGuard.close();
     78         }
     79 
     80         if (mPtr != 0) {
     81             nativeDispose(mPtr);
     82             mPtr = 0;
     83         }
     84     }
     85 
     86     /** @hide */
     87     public int getNativePtr() {
     88         return mPtr;
     89     }
     90 
     91     /** @hide */
     92     public void sendInputEvent(InputEvent e, Object token, boolean predispatch,
     93             FinishedInputEventCallback callback) {
     94         ActiveInputEvent event = obtainActiveInputEvent(token, callback);
     95         int id;
     96         if (e instanceof KeyEvent) {
     97             id = nativeSendKeyEvent(mPtr, (KeyEvent) e, predispatch);
     98         } else {
     99             id = nativeSendMotionEvent(mPtr, (MotionEvent) e);
    100         }
    101         mActiveEventArray.put(id, event);
    102     }
    103 
    104     private void finishInputEvent(int id, boolean handled) {
    105         int index = mActiveEventArray.indexOfKey(id);
    106         if (index >= 0) {
    107             ActiveInputEvent e = mActiveEventArray.valueAt(index);
    108             mActiveEventArray.removeAt(index);
    109             e.mCallback.onFinishedInputEvent(e.mToken, handled);
    110             recycleActiveInputEvent(e);
    111         }
    112     }
    113 
    114     private ActiveInputEvent obtainActiveInputEvent(Object token,
    115             FinishedInputEventCallback callback) {
    116         ActiveInputEvent e = mActiveInputEventPool.acquire();
    117         if (e == null) {
    118             e = new ActiveInputEvent();
    119         }
    120         e.mToken = token;
    121         e.mCallback = callback;
    122         return e;
    123     }
    124 
    125     private void recycleActiveInputEvent(ActiveInputEvent e) {
    126         e.recycle();
    127         mActiveInputEventPool.release(e);
    128     }
    129 
    130     private final class ActiveInputEvent {
    131         public Object mToken;
    132         public FinishedInputEventCallback mCallback;
    133 
    134         public void recycle() {
    135             mToken = null;
    136             mCallback = null;
    137         }
    138     }
    139 
    140     /**
    141      * Interface to receive notification of when an InputQueue is associated
    142      * and dissociated with a thread.
    143      */
    144     public static interface Callback {
    145         /**
    146          * Called when the given InputQueue is now associated with the
    147          * thread making this call, so it can start receiving events from it.
    148          */
    149         void onInputQueueCreated(InputQueue queue);
    150 
    151         /**
    152          * Called when the given InputQueue is no longer associated with
    153          * the thread and thus not dispatching events.
    154          */
    155         void onInputQueueDestroyed(InputQueue queue);
    156     }
    157 
    158     /** @hide */
    159     public static interface FinishedInputEventCallback {
    160         void onFinishedInputEvent(Object token, boolean handled);
    161     }
    162 
    163 }
    164