blob: b2dd6ac9e9711693596a95f40e20879cdad312c4 [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 Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
Jeff Brown46b9ac02010-04-22 18:58:52 -070020import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Slog;
23
24/**
25 * An input channel specifies the file descriptors used to send input events to
Dianne Hackborna95e4cb2010-06-18 18:09:33 -070026 * a window in another process. It is Parcelable so that it can be sent
27 * to the process that is to receive events. Only one thread should be reading
28 * from an InputChannel at a time.
Jeff Brown46b9ac02010-04-22 18:58:52 -070029 * @hide
30 */
Jeff Brown7fbdc842010-06-17 20:52:56 -070031public final class InputChannel implements Parcelable {
Jeff Brown46b9ac02010-04-22 18:58:52 -070032 private static final String TAG = "InputChannel";
33
Jeff Brownc5ed5912010-07-14 18:48:53 -070034 private static final boolean DEBUG = false;
35
Mathew Inwoode5ad5982018-08-17 15:07:52 +010036 @UnsupportedAppUsage
Jeff Brown46b9ac02010-04-22 18:58:52 -070037 public static final Parcelable.Creator<InputChannel> CREATOR
38 = new Parcelable.Creator<InputChannel>() {
39 public InputChannel createFromParcel(Parcel source) {
40 InputChannel result = new InputChannel();
41 result.readFromParcel(source);
42 return result;
43 }
44
45 public InputChannel[] newArray(int size) {
46 return new InputChannel[size];
47 }
48 };
49
50 @SuppressWarnings("unused")
Mathew Inwoode5ad5982018-08-17 15:07:52 +010051 @UnsupportedAppUsage
Ashok Bhata931d5212014-01-08 14:04:51 +000052 private long mPtr; // used by native code
Jeff Brown46b9ac02010-04-22 18:58:52 -070053
Jeff Brown46b9ac02010-04-22 18:58:52 -070054 private static native InputChannel[] nativeOpenInputChannelPair(String name);
55
56 private native void nativeDispose(boolean finalized);
57 private native void nativeTransferTo(InputChannel other);
58 private native void nativeReadFromParcel(Parcel parcel);
59 private native void nativeWriteToParcel(Parcel parcel);
Jeff Brown1951ce82013-04-04 22:45:12 -070060 private native void nativeDup(InputChannel target);
Jeff Brown46b9ac02010-04-22 18:58:52 -070061
62 private native String nativeGetName();
63
64 /**
65 * Creates an uninitialized input channel.
66 * It can be initialized by reading from a Parcel or by transferring the state of
67 * another input channel into this one.
68 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +010069 @UnsupportedAppUsage
Jeff Brown46b9ac02010-04-22 18:58:52 -070070 public InputChannel() {
71 }
Jeff Brown1951ce82013-04-04 22:45:12 -070072
Jeff Brown46b9ac02010-04-22 18:58:52 -070073 @Override
74 protected void finalize() throws Throwable {
75 try {
76 nativeDispose(true);
77 } finally {
78 super.finalize();
79 }
80 }
81
82 /**
83 * Creates a new input channel pair. One channel should be provided to the input
84 * dispatcher and the other to the application's input queue.
85 * @param name The descriptive (non-unique) name of the channel pair.
Jeff Brownc28867a2013-03-26 15:42:39 -070086 * @return A pair of input channels. The first channel is designated as the
87 * server channel and should be used to publish input events. The second channel
88 * is designated as the client channel and should be used to consume input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -070089 */
90 public static InputChannel[] openInputChannelPair(String name) {
91 if (name == null) {
92 throw new IllegalArgumentException("name must not be null");
93 }
Jeff Brownc5ed5912010-07-14 18:48:53 -070094
95 if (DEBUG) {
96 Slog.d(TAG, "Opening input channel pair '" + name + "'");
97 }
Jeff Brown46b9ac02010-04-22 18:58:52 -070098 return nativeOpenInputChannelPair(name);
99 }
100
101 /**
102 * Gets the name of the input channel.
103 * @return The input channel name.
104 */
105 public String getName() {
106 String name = nativeGetName();
107 return name != null ? name : "uninitialized";
108 }
109
110 /**
111 * Disposes the input channel.
112 * Explicitly releases the reference this object is holding on the input channel.
113 * When all references are released, the input channel will be closed.
114 */
115 public void dispose() {
116 nativeDispose(false);
117 }
118
119 /**
120 * Transfers ownership of the internal state of the input channel to another
121 * instance and invalidates this instance. This is used to pass an input channel
122 * as an out parameter in a binder call.
123 * @param other The other input channel instance.
124 */
Jeff Brown0a0ab122011-08-12 18:08:08 -0700125 public void transferTo(InputChannel outParameter) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126 if (outParameter == null) {
127 throw new IllegalArgumentException("outParameter must not be null");
128 }
129
130 nativeTransferTo(outParameter);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700131 }
132
Jeff Brown1951ce82013-04-04 22:45:12 -0700133 /**
134 * Duplicates the input channel.
135 */
136 public InputChannel dup() {
137 InputChannel target = new InputChannel();
138 nativeDup(target);
139 return target;
140 }
141
Jeff Brownc28867a2013-03-26 15:42:39 -0700142 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700143 public int describeContents() {
144 return Parcelable.CONTENTS_FILE_DESCRIPTOR;
145 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700146
Jeff Brown46b9ac02010-04-22 18:58:52 -0700147 public void readFromParcel(Parcel in) {
148 if (in == null) {
149 throw new IllegalArgumentException("in must not be null");
150 }
151
152 nativeReadFromParcel(in);
153 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700154
155 @Override
Jeff Brown46b9ac02010-04-22 18:58:52 -0700156 public void writeToParcel(Parcel out, int flags) {
157 if (out == null) {
158 throw new IllegalArgumentException("out must not be null");
159 }
160
161 nativeWriteToParcel(out);
162
Jeff Brown0a0ab122011-08-12 18:08:08 -0700163 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700164 dispose();
165 }
166 }
167
168 @Override
169 public String toString() {
170 return getName();
171 }
172}