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 
17 package com.example.android.common.midi;
18 
19 import android.media.midi.MidiDeviceInfo;
20 import android.media.midi.MidiDeviceInfo.PortInfo;
21 import android.util.Log;
22 
23 // Wrapper for a MIDI device and port description.
24 public class MidiPortWrapper {
25     private MidiDeviceInfo mInfo;
26     private int mPortIndex;
27     private int mType;
28     private String mString;
29 
30     /**
31      * Wrapper for a MIDI device and port description.
32      * @param info
33      * @param portType
34      * @param portIndex
35      */
MidiPortWrapper(MidiDeviceInfo info, int portType, int portIndex)36     public MidiPortWrapper(MidiDeviceInfo info, int portType, int portIndex) {
37         mInfo = info;
38         mType = portType;
39         mPortIndex = portIndex;
40     }
41 
updateString()42     private void updateString() {
43         if (mInfo == null) {
44             mString = "- - - - - -";
45         } else {
46             StringBuilder sb = new StringBuilder();
47             String name = mInfo.getProperties()
48                     .getString(MidiDeviceInfo.PROPERTY_NAME);
49             if (name == null) {
50                 name = mInfo.getProperties()
51                         .getString(MidiDeviceInfo.PROPERTY_MANUFACTURER) + ", "
52                         + mInfo.getProperties()
53                                 .getString(MidiDeviceInfo.PROPERTY_PRODUCT);
54             }
55             sb.append("#" + mInfo.getId());
56             sb.append(", ").append(name);
57             PortInfo portInfo = findPortInfo();
58             sb.append("[" + mPortIndex + "]");
59             if (portInfo != null) {
60                 sb.append(", ").append(portInfo.getName());
61             } else {
62                 sb.append(", null");
63             }
64             mString = sb.toString();
65         }
66     }
67 
68     /**
69      * @param info
70      * @param portIndex
71      * @return
72      */
findPortInfo()73     private PortInfo findPortInfo() {
74         PortInfo[] ports = mInfo.getPorts();
75         for (PortInfo portInfo : ports) {
76             if (portInfo.getPortNumber() == mPortIndex
77                     && portInfo.getType() == mType) {
78                 return portInfo;
79             }
80         }
81         return null;
82     }
83 
getPortIndex()84     public int getPortIndex() {
85         return mPortIndex;
86     }
87 
getDeviceInfo()88     public MidiDeviceInfo getDeviceInfo() {
89         return mInfo;
90     }
91 
92     @Override
toString()93     public String toString() {
94         if (mString == null) {
95             updateString();
96         }
97         return mString;
98     }
99 
100     @Override
equals(Object other)101     public boolean equals(Object other) {
102         if (other == null)
103             return false;
104         if (!(other instanceof MidiPortWrapper))
105             return false;
106         MidiPortWrapper otherWrapper = (MidiPortWrapper) other;
107         if (mPortIndex != otherWrapper.mPortIndex)
108             return false;
109         if (mType != otherWrapper.mType)
110             return false;
111         if (mInfo == null)
112             return (otherWrapper.mInfo == null);
113         return mInfo.equals(otherWrapper.mInfo);
114     }
115 
116     @Override
hashCode()117     public int hashCode() {
118         int hashCode = 1;
119         hashCode = 31 * hashCode + mPortIndex;
120         hashCode = 31 * hashCode + mType;
121         hashCode = 31 * hashCode + mInfo.hashCode();
122         return hashCode;
123     }
124 
125 }
126