• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.os.Parcel;
20 import android.os.Parcelable;
21 import android.util.Slog;
22 
23 /**
24  * An input channel specifies the file descriptors used to send input events to
25  * a window in another process.  It is Parcelable so that it can be sent
26  * to the process that is to receive events.  Only one thread should be reading
27  * from an InputChannel at a time.
28  * @hide
29  */
30 public final class InputChannel implements Parcelable {
31     private static final String TAG = "InputChannel";
32 
33     private static final boolean DEBUG = false;
34 
35     public static final Parcelable.Creator<InputChannel> CREATOR
36             = new Parcelable.Creator<InputChannel>() {
37         public InputChannel createFromParcel(Parcel source) {
38             InputChannel result = new InputChannel();
39             result.readFromParcel(source);
40             return result;
41         }
42 
43         public InputChannel[] newArray(int size) {
44             return new InputChannel[size];
45         }
46     };
47 
48     @SuppressWarnings("unused")
49     private int mPtr; // used by native code
50 
nativeOpenInputChannelPair(String name)51     private static native InputChannel[] nativeOpenInputChannelPair(String name);
52 
nativeDispose(boolean finalized)53     private native void nativeDispose(boolean finalized);
nativeTransferTo(InputChannel other)54     private native void nativeTransferTo(InputChannel other);
nativeReadFromParcel(Parcel parcel)55     private native void nativeReadFromParcel(Parcel parcel);
nativeWriteToParcel(Parcel parcel)56     private native void nativeWriteToParcel(Parcel parcel);
57 
nativeGetName()58     private native String nativeGetName();
59 
60     /**
61      * Creates an uninitialized input channel.
62      * It can be initialized by reading from a Parcel or by transferring the state of
63      * another input channel into this one.
64      */
InputChannel()65     public InputChannel() {
66     }
67 
68     @Override
finalize()69     protected void finalize() throws Throwable {
70         try {
71             nativeDispose(true);
72         } finally {
73             super.finalize();
74         }
75     }
76 
77     /**
78      * Creates a new input channel pair.  One channel should be provided to the input
79      * dispatcher and the other to the application's input queue.
80      * @param name The descriptive (non-unique) name of the channel pair.
81      * @return A pair of input channels.  They are symmetric and indistinguishable.
82      */
openInputChannelPair(String name)83     public static InputChannel[] openInputChannelPair(String name) {
84         if (name == null) {
85             throw new IllegalArgumentException("name must not be null");
86         }
87 
88         if (DEBUG) {
89             Slog.d(TAG, "Opening input channel pair '" + name + "'");
90         }
91         return nativeOpenInputChannelPair(name);
92     }
93 
94     /**
95      * Gets the name of the input channel.
96      * @return The input channel name.
97      */
getName()98     public String getName() {
99         String name = nativeGetName();
100         return name != null ? name : "uninitialized";
101     }
102 
103     /**
104      * Disposes the input channel.
105      * Explicitly releases the reference this object is holding on the input channel.
106      * When all references are released, the input channel will be closed.
107      */
dispose()108     public void dispose() {
109         nativeDispose(false);
110     }
111 
112     /**
113      * Transfers ownership of the internal state of the input channel to another
114      * instance and invalidates this instance.  This is used to pass an input channel
115      * as an out parameter in a binder call.
116      * @param other The other input channel instance.
117      */
transferTo(InputChannel outParameter)118     public void transferTo(InputChannel outParameter) {
119         if (outParameter == null) {
120             throw new IllegalArgumentException("outParameter must not be null");
121         }
122 
123         nativeTransferTo(outParameter);
124     }
125 
describeContents()126     public int describeContents() {
127         return Parcelable.CONTENTS_FILE_DESCRIPTOR;
128     }
129 
readFromParcel(Parcel in)130     public void readFromParcel(Parcel in) {
131         if (in == null) {
132             throw new IllegalArgumentException("in must not be null");
133         }
134 
135         nativeReadFromParcel(in);
136     }
137 
writeToParcel(Parcel out, int flags)138     public void writeToParcel(Parcel out, int flags) {
139         if (out == null) {
140             throw new IllegalArgumentException("out must not be null");
141         }
142 
143         nativeWriteToParcel(out);
144 
145         if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
146             dispose();
147         }
148     }
149 
150     @Override
toString()151     public String toString() {
152         return getName();
153     }
154 }
155