blob: de195ae524c8b273ad32ddb95d22d629c2b99ab8 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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
17package android.view;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.Slog;
22
23/**
24 * An input channel specifies the file descriptors used to send input events to
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070025 * 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.
Jeff Brown46b9ac02010-04-22 18:58:52 -070028 * @hide
29 */
Jeff Brown7fbdc842010-06-17 20:52:56 -070030public final class InputChannel implements Parcelable {
Jeff Brown46b9ac02010-04-22 18:58:52 -070031 private static final String TAG = "InputChannel";
32
Jeff Brownc5ed5912010-07-14 18:48:53 -070033 private static final boolean DEBUG = false;
34
Jeff Brown46b9ac02010-04-22 18:58:52 -070035 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")
Ashok Bhata931d5212014-01-08 14:04:51 +000049 private long mPtr; // used by native code
Jeff Brown46b9ac02010-04-22 18:58:52 -070050
Jeff Brown46b9ac02010-04-22 18:58:52 -070051 private static native InputChannel[] nativeOpenInputChannelPair(String name);
52
53 private native void nativeDispose(boolean finalized);
54 private native void nativeTransferTo(InputChannel other);
55 private native void nativeReadFromParcel(Parcel parcel);
56 private native void nativeWriteToParcel(Parcel parcel);
Jeff Brown1951ce82013-04-04 22:45:12 -070057 private native void nativeDup(InputChannel target);
Jeff Brown46b9ac02010-04-22 18:58:52 -070058
59 private native String nativeGetName();
60
61 /**
62 * Creates an uninitialized input channel.
63 * It can be initialized by reading from a Parcel or by transferring the state of
64 * another input channel into this one.
65 */
66 public InputChannel() {
67 }
Jeff Brown1951ce82013-04-04 22:45:12 -070068
Jeff Brown46b9ac02010-04-22 18:58:52 -070069 @Override
70 protected void finalize() throws Throwable {
71 try {
72 nativeDispose(true);
73 } finally {
74 super.finalize();
75 }
76 }
77
78 /**
79 * Creates a new input channel pair. One channel should be provided to the input
80 * dispatcher and the other to the application's input queue.
81 * @param name The descriptive (non-unique) name of the channel pair.
Jeff Brownc28867a2013-03-26 15:42:39 -070082 * @return A pair of input channels. The first channel is designated as the
83 * server channel and should be used to publish input events. The second channel
84 * is designated as the client channel and should be used to consume input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -070085 */
86 public static InputChannel[] openInputChannelPair(String name) {
87 if (name == null) {
88 throw new IllegalArgumentException("name must not be null");
89 }
Jeff Brownc5ed5912010-07-14 18:48:53 -070090
91 if (DEBUG) {
92 Slog.d(TAG, "Opening input channel pair '" + name + "'");
93 }
Jeff Brown46b9ac02010-04-22 18:58:52 -070094 return nativeOpenInputChannelPair(name);
95 }
96
97 /**
98 * Gets the name of the input channel.
99 * @return The input channel name.
100 */
101 public String getName() {
102 String name = nativeGetName();
103 return name != null ? name : "uninitialized";
104 }
105
106 /**
107 * Disposes the input channel.
108 * Explicitly releases the reference this object is holding on the input channel.
109 * When all references are released, the input channel will be closed.
110 */
111 public void dispose() {
112 nativeDispose(false);
113 }
114
115 /**
116 * Transfers ownership of the internal state of the input channel to another
117 * instance and invalidates this instance. This is used to pass an input channel
118 * as an out parameter in a binder call.
119 * @param other The other input channel instance.
120 */
Jeff Brown0a0ab122011-08-12 18:08:08 -0700121 public void transferTo(InputChannel outParameter) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700122 if (outParameter == null) {
123 throw new IllegalArgumentException("outParameter must not be null");
124 }
125
126 nativeTransferTo(outParameter);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700127 }
128
Jeff Brown1951ce82013-04-04 22:45:12 -0700129 /**
130 * Duplicates the input channel.
131 */
132 public InputChannel dup() {
133 InputChannel target = new InputChannel();
134 nativeDup(target);
135 return target;
136 }
137
Jeff Brownc28867a2013-03-26 15:42:39 -0700138 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700139 public int describeContents() {
140 return Parcelable.CONTENTS_FILE_DESCRIPTOR;
141 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700142
Jeff Brown46b9ac02010-04-22 18:58:52 -0700143 public void readFromParcel(Parcel in) {
144 if (in == null) {
145 throw new IllegalArgumentException("in must not be null");
146 }
147
148 nativeReadFromParcel(in);
149 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700150
151 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700152 public void writeToParcel(Parcel out, int flags) {
153 if (out == null) {
154 throw new IllegalArgumentException("out must not be null");
155 }
156
157 nativeWriteToParcel(out);
158
Jeff Brown0a0ab122011-08-12 18:08:08 -0700159 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160 dispose();
161 }
162 }
163
164 @Override
165 public String toString() {
166 return getName();
167 }
168}