|
39 | 39 | import java.net.HttpURLConnection; |
40 | 40 | import java.net.MalformedURLException; |
41 | 41 | import java.net.URL; |
| 42 | +import java.security.MessageDigest; |
| 43 | +import java.security.NoSuchAlgorithmException; |
42 | 44 | import java.util.List; |
43 | 45 | import java.util.logging.Logger; |
44 | 46 |
|
@@ -346,6 +348,8 @@ private String getDirectionName() { |
346 | 348 |
|
347 | 349 | void destroy() { |
348 | 350 | mMidiDevice = null; |
| 351 | + setOnline(false); |
| 352 | + |
349 | 353 | mMidiFilePlayer.stop(); |
350 | 354 | synchronized (mMidiInputBuffers) { |
351 | 355 | for (int index = 0; index < mMidiInputBuffers.size(); index++) { |
@@ -399,6 +403,8 @@ synchronized void setMidiDevice(final MidiDevice device) { |
399 | 403 | for (int port = 0; port < device.getInfo().getOutputPortCount(); port++) { |
400 | 404 | connectOutputPort(port); |
401 | 405 | } |
| 406 | + |
| 407 | + setOnline(mMidiDevice != null); |
402 | 408 | } |
403 | 409 |
|
404 | 410 | private void connectOutputPort(final int port) { |
@@ -473,10 +479,13 @@ static String createServiceId(final MidiDeviceInfo deviceInfo) { |
473 | 479 | deviceType = "usb"; |
474 | 480 | UsbDevice device = props.getParcelable(MidiDeviceInfo.PROPERTY_USB_DEVICE); |
475 | 481 | if (device != null) { |
476 | | - deviceId = Integer.toString(device.getDeviceId()); |
| 482 | + LOGGER.info("createServiceId: USB Device ID = " + device.getDeviceId() + ", MIDI ID = " + deviceInfo.getId()); |
| 483 | + String deviceName = createServiceName(deviceInfo); |
| 484 | + deviceId = device.getDeviceId() + "_" + md5(deviceName); |
477 | 485 | } else { |
478 | | - LOGGER.warning("createServiceId: NO USB DEVICE INFO; id = " + deviceInfo.getId()); |
479 | | - deviceId = "midi" + deviceInfo.getId(); |
| 486 | + |
| 487 | + LOGGER.warning("createServiceId: NO USB DEVICE INFO; MIDI ID = " + deviceInfo.getId()); |
| 488 | + deviceId = "midi_" + deviceInfo.getId(); |
480 | 489 | } |
481 | 490 | break; |
482 | 491 | } |
@@ -505,6 +514,22 @@ static String createServiceId(final MidiDeviceInfo deviceInfo) { |
505 | 514 | return concat(array); |
506 | 515 | } |
507 | 516 |
|
| 517 | + public static String md5(final String s) { |
| 518 | + try { |
| 519 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 520 | + md.update(s.getBytes()); |
| 521 | + byte[] messageDigest = md.digest(); |
| 522 | + |
| 523 | + StringBuilder hex = new StringBuilder(); |
| 524 | + for (int i = 0; i < messageDigest.length; i++) { |
| 525 | + hex.append(String.format("%02x", messageDigest[i] & 0xFF)); |
| 526 | + } |
| 527 | + return hex.toString(); |
| 528 | + } catch (NoSuchAlgorithmException e) { |
| 529 | + return s; |
| 530 | + } |
| 531 | + } |
| 532 | + |
508 | 533 | private static String createServiceName(final MidiDeviceInfo deviceInfo) { |
509 | 534 | Bundle props = deviceInfo.getProperties(); |
510 | 535 | if (props != null) { |
|
0 commit comments