blob: af2b992ccba2da8597371dd63a9e95db31064ba6 [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
Mathew Inwooda570dee2018-08-17 14:56:00 +010019import android.annotation.UnsupportedAppUsage;
Jeff Brown46b9ac02010-04-22 18:58:52 -070020import android.os.Parcel;
Robert Carre0a353c2018-08-02 16:38:04 -070021import android.os.IBinder;
Jeff Brown46b9ac02010-04-22 18:58:52 -070022import android.os.Parcelable;
23import android.util.Slog;
24
25/**
26 * An input channel specifies the file descriptors used to send input events to
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070027 * a window in another process. It is Parcelable so that it can be sent
28 * to the process that is to receive events. Only one thread should be reading
29 * from an InputChannel at a time.
Jeff Brown46b9ac02010-04-22 18:58:52 -070030 * @hide
31 */
Jeff Brown7fbdc842010-06-17 20:52:56 -070032public final class InputChannel implements Parcelable {
Jeff Brown46b9ac02010-04-22 18:58:52 -070033 private static final String TAG = "InputChannel";
34
Jeff Brownc5ed5912010-07-14 18:48:53 -070035 private static final boolean DEBUG = false;
36
Mathew Inwooda570dee2018-08-17 14:56:00 +010037 @UnsupportedAppUsage
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070038 public static final @android.annotation.NonNull Parcelable.Creator<InputChannel> CREATOR
Jeff Brown46b9ac02010-04-22 18:58:52 -070039 = new Parcelable.Creator<InputChannel>() {
40 public InputChannel createFromParcel(Parcel source) {
41 InputChannel result = new InputChannel();
42 result.readFromParcel(source);
43 return result;
44 }
45
46 public InputChannel[] newArray(int size) {
47 return new InputChannel[size];
48 }
49 };
50
51 @SuppressWarnings("unused")
Mathew Inwooda570dee2018-08-17 14:56:00 +010052 @UnsupportedAppUsage
Ashok Bhata931d5212014-01-08 14:04:51 +000053 private long mPtr; // used by native code
Robert Carre0a353c2018-08-02 16:38:04 -070054
Jeff Brown46b9ac02010-04-22 18:58:52 -070055 private static native InputChannel[] nativeOpenInputChannelPair(String name);
Robert Carre0a353c2018-08-02 16:38:04 -070056
Jeff Brown46b9ac02010-04-22 18:58:52 -070057 private native void nativeDispose(boolean finalized);
58 private native void nativeTransferTo(InputChannel other);
59 private native void nativeReadFromParcel(Parcel parcel);
60 private native void nativeWriteToParcel(Parcel parcel);
Jeff Brown1951ce82013-04-04 22:45:12 -070061 private native void nativeDup(InputChannel target);
Robert Carre0a353c2018-08-02 16:38:04 -070062 private native IBinder nativeGetToken();
63 private native void nativeSetToken(IBinder token);
64
Jeff Brown46b9ac02010-04-22 18:58:52 -070065 private native String nativeGetName();
66
67 /**
68 * Creates an uninitialized input channel.
69 * It can be initialized by reading from a Parcel or by transferring the state of
70 * another input channel into this one.
71 */
Mathew Inwooda570dee2018-08-17 14:56:00 +010072 @UnsupportedAppUsage
Jeff Brown46b9ac02010-04-22 18:58:52 -070073 public InputChannel() {
74 }
Jeff Brown1951ce82013-04-04 22:45:12 -070075
Jeff Brown46b9ac02010-04-22 18:58:52 -070076 @Override
77 protected void finalize() throws Throwable {
78 try {
79 nativeDispose(true);
80 } finally {
81 super.finalize();
82 }
83 }
84
85 /**
86 * Creates a new input channel pair. One channel should be provided to the input
87 * dispatcher and the other to the application's input queue.
88 * @param name The descriptive (non-unique) name of the channel pair.
Jeff Brownc28867a2013-03-26 15:42:39 -070089 * @return A pair of input channels. The first channel is designated as the
90 * server channel and should be used to publish input events. The second channel
91 * is designated as the client channel and should be used to consume input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -070092 */
93 public static InputChannel[] openInputChannelPair(String name) {
94 if (name == null) {
95 throw new IllegalArgumentException("name must not be null");
96 }
Jeff Brownc5ed5912010-07-14 18:48:53 -070097
98 if (DEBUG) {
99 Slog.d(TAG, "Opening input channel pair '" + name + "'");
100 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700101 return nativeOpenInputChannelPair(name);
102 }
103
104 /**
105 * Gets the name of the input channel.
106 * @return The input channel name.
107 */
108 public String getName() {
109 String name = nativeGetName();
110 return name != null ? name : "uninitialized";
111 }
112
113 /**
114 * Disposes the input channel.
115 * Explicitly releases the reference this object is holding on the input channel.
116 * When all references are released, the input channel will be closed.
117 */
118 public void dispose() {
119 nativeDispose(false);
120 }
121
122 /**
123 * Transfers ownership of the internal state of the input channel to another
124 * instance and invalidates this instance. This is used to pass an input channel
125 * as an out parameter in a binder call.
126 * @param other The other input channel instance.
127 */
Jeff Brown0a0ab122011-08-12 18:08:08 -0700128 public void transferTo(InputChannel outParameter) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700129 if (outParameter == null) {
130 throw new IllegalArgumentException("outParameter must not be null");
131 }
132
133 nativeTransferTo(outParameter);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700134 }
135
Jeff Brown1951ce82013-04-04 22:45:12 -0700136 /**
137 * Duplicates the input channel.
138 */
139 public InputChannel dup() {
140 InputChannel target = new InputChannel();
141 nativeDup(target);
142 return target;
143 }
144
Jeff Brownc28867a2013-03-26 15:42:39 -0700145 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146 public int describeContents() {
147 return Parcelable.CONTENTS_FILE_DESCRIPTOR;
148 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700149
Jeff Brown46b9ac02010-04-22 18:58:52 -0700150 public void readFromParcel(Parcel in) {
151 if (in == null) {
152 throw new IllegalArgumentException("in must not be null");
153 }
154
155 nativeReadFromParcel(in);
156 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700157
158 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700159 public void writeToParcel(Parcel out, int flags) {
160 if (out == null) {
161 throw new IllegalArgumentException("out must not be null");
162 }
163
164 nativeWriteToParcel(out);
Robert Carre0a353c2018-08-02 16:38:04 -0700165
Jeff Brown0a0ab122011-08-12 18:08:08 -0700166 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700167 dispose();
168 }
169 }
Robert Carre0a353c2018-08-02 16:38:04 -0700170
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171 @Override
172 public String toString() {
173 return getName();
174 }
Robert Carre0a353c2018-08-02 16:38:04 -0700175
176 public IBinder getToken() {
177 return nativeGetToken();
178 }
179
180 public void setToken(IBinder token) {
181 nativeSetToken(token);
182 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700183}