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
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
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 */
30public 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
53    private static native InputChannel[] nativeOpenInputChannelPair(String name);
54
55    private native void nativeDispose(boolean finalized);
56    private native void nativeTransferTo(InputChannel other);
57    private native void nativeReadFromParcel(Parcel parcel);
58    private native void nativeWriteToParcel(Parcel parcel);
59
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     */
67    public InputChannel() {
68    }
69
70    @Override
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     */
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     */
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     */
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     */
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
129    public int describeContents() {
130        return Parcelable.CONTENTS_FILE_DESCRIPTOR;
131    }
132
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
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
154    public String toString() {
155        return getName();
156    }
157}
158