1/*
2 * Copyright (C) 2015 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.media.midi;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * This is an immutable class that describes the current status of a MIDI device's ports.
24 */
25public final class MidiDeviceStatus implements Parcelable {
26
27    private static final String TAG = "MidiDeviceStatus";
28
29    private final MidiDeviceInfo mDeviceInfo;
30    // true if input ports are open
31    private final boolean mInputPortOpen[];
32    // open counts for output ports
33    private final int mOutputPortOpenCount[];
34
35    /**
36     * @hide
37     */
38    public MidiDeviceStatus(MidiDeviceInfo deviceInfo, boolean inputPortOpen[],
39            int outputPortOpenCount[]) {
40        // MidiDeviceInfo is immutable so we can share references
41        mDeviceInfo = deviceInfo;
42
43        // make copies of the arrays
44        mInputPortOpen = new boolean[inputPortOpen.length];
45        System.arraycopy(inputPortOpen, 0, mInputPortOpen, 0, inputPortOpen.length);
46        mOutputPortOpenCount = new int[outputPortOpenCount.length];
47        System.arraycopy(outputPortOpenCount, 0, mOutputPortOpenCount, 0,
48                outputPortOpenCount.length);
49    }
50
51    /**
52     * Creates a MidiDeviceStatus with zero for all port open counts
53     * @hide
54     */
55    public MidiDeviceStatus(MidiDeviceInfo deviceInfo) {
56        mDeviceInfo = deviceInfo;
57        mInputPortOpen = new boolean[deviceInfo.getInputPortCount()];
58        mOutputPortOpenCount = new int[deviceInfo.getOutputPortCount()];
59    }
60
61    /**
62     * Returns the {@link MidiDeviceInfo} of the device.
63     *
64     * @return the device info
65     */
66    public MidiDeviceInfo getDeviceInfo() {
67        return mDeviceInfo;
68    }
69
70    /**
71     * Returns true if an input port is open.
72     * An input port can only be opened by one client at a time.
73     *
74     * @param portNumber the input port's port number
75     * @return input port open status
76     */
77    public boolean isInputPortOpen(int portNumber) {
78        return mInputPortOpen[portNumber];
79    }
80
81    /**
82     * Returns the number of clients currently connected to the specified output port.
83     * Unlike input ports, an output port can be opened by multiple clients at the same time.
84     *
85     * @param portNumber the output port's port number
86     * @return output port open count
87     */
88    public int getOutputPortOpenCount(int portNumber) {
89        return mOutputPortOpenCount[portNumber];
90    }
91
92    @Override
93    public String toString() {
94        int inputPortCount = mDeviceInfo.getInputPortCount();
95        int outputPortCount = mDeviceInfo.getOutputPortCount();
96        StringBuilder builder = new StringBuilder("mInputPortOpen=[");
97        for (int i = 0; i < inputPortCount; i++) {
98            builder.append(mInputPortOpen[i]);
99            if (i < inputPortCount -1) {
100                builder.append(",");
101            }
102        }
103        builder.append("] mOutputPortOpenCount=[");
104        for (int i = 0; i < outputPortCount; i++) {
105            builder.append(mOutputPortOpenCount[i]);
106            if (i < outputPortCount -1) {
107                builder.append(",");
108            }
109        }
110        builder.append("]");
111        return builder.toString();
112    }
113
114    public static final Parcelable.Creator<MidiDeviceStatus> CREATOR =
115        new Parcelable.Creator<MidiDeviceStatus>() {
116        public MidiDeviceStatus createFromParcel(Parcel in) {
117            ClassLoader classLoader = MidiDeviceInfo.class.getClassLoader();
118            MidiDeviceInfo deviceInfo = in.readParcelable(classLoader);
119            boolean[] inputPortOpen = in.createBooleanArray();
120            int[] outputPortOpenCount = in.createIntArray();
121            return new MidiDeviceStatus(deviceInfo, inputPortOpen, outputPortOpenCount);
122        }
123
124        public MidiDeviceStatus[] newArray(int size) {
125            return new MidiDeviceStatus[size];
126        }
127    };
128
129    public int describeContents() {
130        return 0;
131    }
132
133    public void writeToParcel(Parcel parcel, int flags) {
134        parcel.writeParcelable(mDeviceInfo, flags);
135        parcel.writeBooleanArray(mInputPortOpen);
136        parcel.writeIntArray(mOutputPortOpenCount);
137   }
138}
139