/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2021 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brltty.android.settings;
import org.a11y.brltty.android.*;

import android.util.Log;
import android.app.Activity;
import android.os.Bundle;

import android.content.Intent;
import android.hardware.usb.*;
import java.util.Map;

public class UsbDeviceAttachedMonitor extends Activity {
  private final static String LOG_TAG = UsbDeviceAttachedMonitor.class.getName();

  private static void addParameter (Map<String, String> parameters, String name, String value) {
    if ((value != null) && !value.isEmpty()) {
      parameters.put(name, value);
    }
  }

  private static String makeDescription (UsbDevice device) {
    Map<String, String> parameters = DeviceCollection.newParameters();
    UsbDeviceCollection.putParameters(parameters, device);

    addParameter(parameters, "manufacturerName", device.getManufacturerName());
    addParameter(parameters, "productName", device.getProductName());

    return UsbDeviceCollection.makeReference(parameters);
  }

  @Override
  protected void onCreate (Bundle savedState) {
    super.onCreate(savedState);

    Intent intent = getIntent();
    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    Log.i(LOG_TAG, ("device attached: " + makeDescription(device)));
  }

  @Override
  protected void onResume () {
    super.onResume();
    finish();
  }
}

@@ -877,16 +877,48 @@
 til.Map;
+%0Aimport java.util.LinkedHashMap;
 %0A%0Apublic
@@ -1077,24 +1077,207 @@
 oid addP
-arameter
+roperty (Map%3CString, String%3E properties, String name, int value) %7B%0A    if (value != 0) %7B%0A      properties.put(name, String.format(%22%2504X%22, value));%0A    %7D%0A  %7D%0A%0A  private static void addProperty
  (Map%3CSt
@@ -1291,24 +1291,24 @@
 tring%3E p
-arameter
+ropertie
 s, Strin
@@ -1338,17 +1338,16 @@
 %0A    if 
-(
 (value !
@@ -1358,11 +1358,20 @@
 ll) 
-&& 
+%7B%0A      if (
 !val
@@ -1396,36 +1396,111 @@
     
-parameters.put(name, value);
+  char quote = '%22';%0A        properties.put(name, String.format(%22%25c%25s%25c%22, quote, value, quote));%0A      %7D
 %0A   
@@ -1528,30 +1528,41 @@
 tic 
+Map%3C
 String
- makeDescription
+, String%3E getProperties
  (Us
@@ -1608,122 +1608,234 @@
 g%3E p
-arameters = DeviceCollection.newParameters
+roperties = new LinkedHashMap%3C%3E();%0A%0A    addProperty(properties, %22VID%22, device.getVendorId
 ()
+)
 ;%0A    
-UsbDeviceCollection.putParameters(parameters, device
+addProperty(properties, %22PID%22, device.getProductId());%0A    addProperty(properties, %22Serial%22, device.getSerialNumber()
 );%0A
-%0A
     addP
 aram
@@ -1834,31 +1834,30 @@
 addP
-arameter(parameter
+roperty(propertie
 s, %22
-m
+M
 anuf
@@ -1863,20 +1863,16 @@
 facturer
-Name
 %22, devic
@@ -1909,41 +1909,36 @@
 addP
-arameter(parameter
+roperty(propertie
 s, %22
-p
+P
 roduct
-Name
 %22, d
@@ -1978,52 +1978,447 @@
 urn 
-UsbDeviceCollection.makeReference(parameters
+properties;%0A  %7D%0A%0A  private static String makeDescription (UsbDevice device) %7B%0A    Map%3CString, String%3E properties = getProperties(device);%0A    StringBuilder description = new StringBuilder();%0A%0A    for (String name : properties.keySet()) %7B%0A      String value = properties.get(name);%0A      if (description.length() %3E 0) description.append(' ');%0A      description.append(name).append(':').append(value);%0A    %7D%0A%0A    return description.toString(
 );%0A 

Improve the USB device attached log. (dm)