DeviceAdapter.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.epson.mobilephone.common.ble;
  2. import android.bluetooth.BluetoothDevice;
  3. import android.content.Context;
  4. import android.content.res.Resources;
  5. import android.graphics.drawable.Drawable;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import com.epson.mobilephone.common.ble.util.ScannedDevice;
  13. import epson.print.IprintApplication;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. public class DeviceAdapter extends ArrayAdapter<ScannedDevice> {
  17. private static final String PREFIX_RSSI = "RSSI:";
  18. private LayoutInflater mInflater;
  19. private List<ScannedDevice> mList;
  20. private int mResId;
  21. public DeviceAdapter(Context context, int i, List<ScannedDevice> list) {
  22. super(context, i, list);
  23. mResId = i;
  24. mList = list;
  25. mInflater = (LayoutInflater) context.getSystemService("layout_inflater");
  26. }
  27. public View getView(int i, View view, ViewGroup viewGroup) {
  28. ScannedDevice scannedDevice = (ScannedDevice) getItem(i);
  29. if (view == null) {
  30. view = mInflater.inflate(mResId, (ViewGroup) null);
  31. }
  32. TextView textView = (TextView) view.findViewById(R.id.device_name);
  33. textView.setText(scannedDevice.getDisplayName());
  34. if (IprintApplication.isReleaseUnlimited()) {
  35. ((TextView) view.findViewById(R.id.device_address)).setText(scannedDevice.getDevice().getAddress());
  36. TextView textView2 = (TextView) view.findViewById(R.id.device_rssi);
  37. textView2.setText(PREFIX_RSSI + Integer.toString(scannedDevice.getRssi()));
  38. textView2.setTextColor(-3355444);
  39. } else {
  40. view.findViewById(R.id.device_address).setVisibility(View.GONE);
  41. view.findViewById(R.id.device_rssi).setVisibility(View.GONE);
  42. textView.setPadding(0, 20, 0, 20);
  43. }
  44. ((ImageView) view.findViewById(R.id.signal)).setImageDrawable(getThresholdIcon(scannedDevice.getRssi()));
  45. return view;
  46. }
  47. private Drawable getThresholdIcon(int i) {
  48. Resources resources = getContext().getResources();
  49. int i2 = R.drawable.signal_1;
  50. if (i >= -60) {
  51. EPLog.i("HIGH_SIGNAL");
  52. i2 = R.drawable.signal_3;
  53. } else if (i >= -70) {
  54. EPLog.i("MID_SIGNAL");
  55. i2 = R.drawable.signal_2;
  56. } else if (i >= -75) {
  57. EPLog.i("LOW_SIGNAL");
  58. }
  59. return resources.getDrawable(i2);
  60. }
  61. public void update(BluetoothDevice bluetoothDevice, int i, String str, byte[] bArr) {
  62. if (bluetoothDevice != null && bluetoothDevice.getAddress() != null) {
  63. boolean z = false;
  64. Iterator<ScannedDevice> it = mList.iterator();
  65. while (true) {
  66. if (!it.hasNext()) {
  67. break;
  68. }
  69. ScannedDevice next = it.next();
  70. if (bluetoothDevice.getAddress().equals(next.getDevice().getAddress())) {
  71. z = true;
  72. next.setRssi(i);
  73. break;
  74. }
  75. }
  76. if (!z) {
  77. mList.add(new ScannedDevice(bluetoothDevice, i, str, bArr));
  78. }
  79. notifyDataSetChanged();
  80. }
  81. }
  82. }