BLEScanActivity refactor

This commit is contained in:
Milos Kozak 2019-12-22 12:33:05 +01:00
parent 52380cbd22
commit 648b09717e
5 changed files with 50 additions and 25 deletions

View file

@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanResult;
import android.content.pm.ActivityInfo;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.view.View; import android.view.View;
@ -24,22 +25,20 @@ import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSDeviceCh
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
public class BLEScanActivity extends NoSplashAppCompatActivity { public class BLEScanActivity extends NoSplashAppCompatActivity {
private ListView listView = null;
private ListAdapter mListAdapter = null; private ListAdapter mListAdapter = null;
private ArrayList<BluetoothDeviceItem> mDevices = new ArrayList<>(); private ArrayList<BluetoothDeviceItem> mDevices = new ArrayList<>();
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothLeScanner mBluetoothLeScanner = null; private BluetoothLeScanner mBluetoothLeScanner = null;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.danars_blescanner_activity); setContentView(R.layout.danars_blescanner_activity);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mListAdapter = new ListAdapter(); mListAdapter = new ListAdapter();
listView = (ListView) findViewById(R.id.danars_blescanner_listview); ListView listView = findViewById(R.id.danars_blescanner_listview);
listView.setEmptyView(findViewById(R.id.danars_blescanner_nodevice)); listView.setEmptyView(findViewById(R.id.danars_blescanner_nodevice));
listView.setAdapter(mListAdapter); listView.setAdapter(mListAdapter);
@ -50,8 +49,9 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) { if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) mBluetoothAdapter.enable();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if (mBluetoothLeScanner == null) { if (mBluetoothLeScanner == null) {
@ -89,11 +89,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
} }
mDevices.add(item); mDevices.add(item);
new Handler().post(new Runnable() { new Handler().post(() -> mListAdapter.notifyDataSetChanged());
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
} }
private ScanCallback mBleScanCallback = new ScanCallback() { private ScanCallback mBleScanCallback = new ScanCallback() {
@ -134,19 +130,19 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
} }
BluetoothDeviceItem item = getItem(i); BluetoothDeviceItem item = getItem(i);
holder.setData(i, item); holder.setData(item);
return v; return v;
} }
private class ViewHolder implements View.OnClickListener { private class ViewHolder implements View.OnClickListener {
private BluetoothDeviceItem item = null; private BluetoothDeviceItem item = null;
private TextView mName = null; private TextView mName;
private TextView mAddress = null; private TextView mAddress;
public ViewHolder(View v) { ViewHolder(View v) {
mName = (TextView) v.findViewById(R.id.ble_name); mName = v.findViewById(R.id.ble_name);
mAddress = (TextView) v.findViewById(R.id.ble_address); mAddress = v.findViewById(R.id.ble_address);
v.setOnClickListener(ViewHolder.this); v.setOnClickListener(ViewHolder.this);
} }
@ -160,7 +156,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
finish(); finish();
} }
public void setData(int pos, BluetoothDeviceItem data) { public void setData(BluetoothDeviceItem data) {
if (data != null) { if (data != null) {
try { try {
String tTitle = data.device.getName(); String tTitle = data.device.getName();
@ -174,7 +170,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
mAddress.setText(data.device.getAddress()); mAddress.setText(data.device.getAddress());
item = data; item = data;
} catch (Exception e) { } catch (Exception ignored) {
} }
} }
} }
@ -185,14 +181,14 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
private class BluetoothDeviceItem { private class BluetoothDeviceItem {
private BluetoothDevice device; private BluetoothDevice device;
public BluetoothDeviceItem(BluetoothDevice device) { BluetoothDeviceItem(BluetoothDevice device) {
super(); super();
this.device = device; this.device = device;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (device == null || o == null || !(o instanceof BluetoothDeviceItem)) { if (device == null || !(o instanceof BluetoothDeviceItem)) {
return false; return false;
} }
BluetoothDeviceItem checkItem = (BluetoothDeviceItem) o; BluetoothDeviceItem checkItem = (BluetoothDeviceItem) o;
@ -202,7 +198,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity {
return stringEquals(device.getAddress(), checkItem.device.getAddress()); return stringEquals(device.getAddress(), checkItem.device.getAddress());
} }
public boolean stringEquals(String arg1, String arg2) { boolean stringEquals(String arg1, String arg2) {
try { try {
return arg1.equals(arg2); return arg1.equals(arg2);
} catch (Exception e) { } catch (Exception e) {

View file

@ -12,7 +12,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_gravity="center" android:layout_gravity="center"
android:background="@color/cardColorBackground" android:background="@color/activity_title_background"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="5dp"> android:padding="5dp">

View file

@ -18,7 +18,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:background="@color/cardColorBackground" android:background="@color/activity_title_background"
android:orientation="horizontal" android:orientation="horizontal"
android:padding="5dp"> android:padding="5dp">

View file

@ -1,10 +1,38 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".plugins.pump.danaRS.activities.BLEScanActivity"> tools:context=".plugins.pump.danaRS.activities.BLEScanActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/activity_title_background"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/pairing"
android:src="@drawable/ic_bluetooth_white_48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/pairing"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<ListView <ListView
android:id="@+id/danars_blescanner_listview" android:id="@+id/danars_blescanner_listview"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -19,4 +47,4 @@
android:text="@string/danars_nodeviceavailable" android:text="@string/danars_nodeviceavailable"
android:textColor="#FF7F8288" /> android:textColor="#FF7F8288" />
</FrameLayout> </LinearLayout>

View file

@ -45,6 +45,7 @@
<color name="colorInitializingBorder">#00695c</color> <color name="colorInitializingBorder">#00695c</color>
<color name="dialog_title_background">#303030</color> <color name="dialog_title_background">#303030</color>
<color name="activity_title_background">#121212</color>
<color name="cardColorBackground">#121212</color> <color name="cardColorBackground">#121212</color>
<color name="cardObjectiveText">#779ECB</color> <color name="cardObjectiveText">#779ECB</color>