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 51 private boolean mDisposeAfterWriteToParcel; 52 nativeOpenInputChannelPair(String name)53 private static native InputChannel[] nativeOpenInputChannelPair(String name); 54 nativeDispose(boolean finalized)55 private native void nativeDispose(boolean finalized); nativeTransferTo(InputChannel other)56 private native void nativeTransferTo(InputChannel other); nativeReadFromParcel(Parcel parcel)57 private native void nativeReadFromParcel(Parcel parcel); nativeWriteToParcel(Parcel parcel)58 private native void nativeWriteToParcel(Parcel parcel); 59 nativeGetName()60 private native String nativeGetName(); 61 62 /** 63 * Creates an uninitialized input channel. 64 * It can be initialized by reading from a Parcel or by transferring the state of 65 * another input channel into this one. 66 */ InputChannel()67 public InputChannel() { 68 } 69 70 @Override finalize()71 protected void finalize() throws Throwable { 72 try { 73 nativeDispose(true); 74 } finally { 75 super.finalize(); 76 } 77 } 78 79 /** 80 * Creates a new input channel pair. One channel should be provided to the input 81 * dispatcher and the other to the application's input queue. 82 * @param name The descriptive (non-unique) name of the channel pair. 83 * @return A pair of input channels. They are symmetric and indistinguishable. 84 */ openInputChannelPair(String name)85 public static InputChannel[] openInputChannelPair(String name) { 86 if (name == null) { 87 throw new IllegalArgumentException("name must not be null"); 88 } 89 90 if (DEBUG) { 91 Slog.d(TAG, "Opening input channel pair '" + name + "'"); 92 } 93 return nativeOpenInputChannelPair(name); 94 } 95 96 /** 97 * Gets the name of the input channel. 98 * @return The input channel name. 99 */ getName()100 public String getName() { 101 String name = nativeGetName(); 102 return name != null ? name : "uninitialized"; 103 } 104 105 /** 106 * Disposes the input channel. 107 * Explicitly releases the reference this object is holding on the input channel. 108 * When all references are released, the input channel will be closed. 109 */ dispose()110 public void dispose() { 111 nativeDispose(false); 112 } 113 114 /** 115 * Transfers ownership of the internal state of the input channel to another 116 * instance and invalidates this instance. This is used to pass an input channel 117 * as an out parameter in a binder call. 118 * @param other The other input channel instance. 119 */ transferToBinderOutParameter(InputChannel outParameter)120 public void transferToBinderOutParameter(InputChannel outParameter) { 121 if (outParameter == null) { 122 throw new IllegalArgumentException("outParameter must not be null"); 123 } 124 125 nativeTransferTo(outParameter); 126 outParameter.mDisposeAfterWriteToParcel = true; 127 } 128 describeContents()129 public int describeContents() { 130 return Parcelable.CONTENTS_FILE_DESCRIPTOR; 131 } 132 readFromParcel(Parcel in)133 public void readFromParcel(Parcel in) { 134 if (in == null) { 135 throw new IllegalArgumentException("in must not be null"); 136 } 137 138 nativeReadFromParcel(in); 139 } 140 writeToParcel(Parcel out, int flags)141 public void writeToParcel(Parcel out, int flags) { 142 if (out == null) { 143 throw new IllegalArgumentException("out must not be null"); 144 } 145 146 nativeWriteToParcel(out); 147 148 if (mDisposeAfterWriteToParcel) { 149 dispose(); 150 } 151 } 152 153 @Override toString()154 public String toString() { 155 return getName(); 156 } 157 } 158