DeviceAdapter.java 3.4 KB

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