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

View file

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

View file

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

View file

@ -1,10 +1,38 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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
android:id="@+id/danars_blescanner_listview"
android:layout_width="match_parent"
@ -19,4 +47,4 @@
android:text="@string/danars_nodeviceavailable"
android:textColor="#FF7F8288" />
</FrameLayout>
</LinearLayout>

View file

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