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