From 611fa49b5affde39f92febcc4d4eee25cb4a7977 Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Fri, 19 May 2017 19:44:53 +0100 Subject: [PATCH] Changed from list to HashMap --- .../BluetoothPairingActivity.java | 4 +--- .../DataObjects/ServicesDataHolder.java | 22 ++++++++++--------- .../locmessclient/Services/MasterService.java | 7 +++--- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/BluetoothPairingActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/BluetoothPairingActivity.java index 746f812..a4a177a 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/BluetoothPairingActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/BluetoothPairingActivity.java @@ -39,9 +39,7 @@ public void onReceive(Context context, Intent intent) { if (action.equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevice device = intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE); - List list = dataHolder.getBleContent(); - list.add(device); - dataHolder.setBleContent(list); + dataHolder.addBleContent(device); names = dataHolder.getBleNames(); mDeviceListAdapter.notifyDataSetChanged(); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/DataObjects/ServicesDataHolder.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/DataObjects/ServicesDataHolder.java index c0b7eec..c07fd1e 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/DataObjects/ServicesDataHolder.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/DataObjects/ServicesDataHolder.java @@ -1,9 +1,12 @@ package pt.ulisboa.tecnico.cmu.tg14.locmessclient.DataObjects; +import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import java.util.AbstractMap; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -15,7 +18,7 @@ public class ServicesDataHolder { - private List bleContent; + private AbstractMap bleContent; private AbstractMap ssidContent; private Float longitude; private Float latitude; @@ -40,7 +43,7 @@ public static ServicesDataHolder getInstance() { private ServicesDataHolder() { - bleContent = new ArrayList<>(); + bleContent = new HashMap<>(); ssidContent = new HashMap<>(); latitude = new Float(0); longitude = new Float(0); @@ -112,13 +115,17 @@ public void setUsername(String username) { public void setPassword(String password) { this.password = password; } - public List getBleContent() { + public AbstractMap getBleContent() { return bleContent; } - public void setBleContent(List bleContent) { + public void setBleContent(AbstractMap bleContent) { this.bleContent = bleContent; } + public void addBleContent(BluetoothDevice device){ + String name = (device.getName() == null) ? device.getAddress() : device.getName(); + this.bleContent.put(name,device); + } public AbstractMap getSsidContent() { return ssidContent; @@ -145,12 +152,7 @@ public void setLatitude(Float latitude) { } public List getBleNames(){ - List bleNames = new ArrayList<>(); - for(BluetoothDevice device : bleContent){ - String name = (device.getName() == null) ? device.getAddress() : device.getName(); - bleNames.add(name); - } - return bleNames; + return new ArrayList<>(bleContent.keySet()); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java index 02c6dbd..82d0e75 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java @@ -63,9 +63,8 @@ public void onWifiReceived(String name, String ssid) { @Override public void onBleReceived(BluetoothDevice device) { - List devices = dataHolder.getBleContent(); - devices.add(device); - dataHolder.setBleContent(devices); + dataHolder.addBleContent(device); + } @Override @@ -81,7 +80,7 @@ public void clearWifiList() { @Override public void clearBluetoothList() { - dataHolder.setBleContent(new ArrayList()); + dataHolder.setBleContent(new HashMap()); } @Override