diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt index 8edce1ece4..f6790caa25 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ActivitiesModule.kt @@ -10,7 +10,7 @@ import info.nightscout.androidaps.plugins.general.maintenance.activities.PrefImp import info.nightscout.androidaps.plugins.general.openhumans.OpenHumansLoginActivity import info.nightscout.androidaps.plugins.general.overview.activities.QuickWizardListActivity import info.nightscout.androidaps.plugins.general.smsCommunicator.activities.SmsCommunicatorOtpActivity -import info.nightscout.androidaps.plugins.pump.common.dialog.RileyLinkBLEScanActivity +import info.nightscout.androidaps.plugins.pump.common.dialog.RileyLinkBLEConfigActivity import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog.RileyLinkStatusActivity import info.nightscout.androidaps.plugins.pump.insight.activities.InsightAlertActivity import info.nightscout.androidaps.plugins.pump.insight.activities.InsightPairingActivity @@ -33,7 +33,7 @@ abstract class ActivitiesModule { @ContributesAndroidInjector abstract fun contributesQuickWizardListActivity(): QuickWizardListActivity @ContributesAndroidInjector abstract fun contributesRequestDexcomPermissionActivity(): RequestDexcomPermissionActivity @ContributesAndroidInjector abstract fun contributesRileyLinkStatusActivity(): RileyLinkStatusActivity - @ContributesAndroidInjector abstract fun contributesRileyLinkBLEScanActivity(): RileyLinkBLEScanActivity + @ContributesAndroidInjector abstract fun contributesRileyLinkBLEConfigActivity(): RileyLinkBLEConfigActivity @ContributesAndroidInjector abstract fun contributesSetupWizardActivity(): SetupWizardActivity @ContributesAndroidInjector abstract fun contributesSingleFragmentActivity(): SingleFragmentActivity @ContributesAndroidInjector abstract fun contributesSmsCommunicatorOtpActivity(): SmsCommunicatorOtpActivity diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ac2b63ce4a..a099e757ca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -918,8 +918,8 @@ Please remember: new insulin profiles require DIA at least 5h. DIA 5–6h on new profile is equal to DIA 3h on old insulin profiles. Please select source of profile. If patient is a child you should use NS profile. If there is nobody following you on Nightscout you will probably prefer Local profile. Please remember that you are only selecting the profile source. To use it you must activate it by executing \"Profile switch\" Select one from availables algorithms. They are sorted from oldest to newest. Newer algorithm is usually more powerful and more aggressive. Thus if you are new looper you may probably start with AMA and not with latest one. Do not forget to read the OpenAPS documentation and configure it before use. - Waiting for RileyLink connection…\n - Note: You can continue setup once the pump has been initialized.\n + Please configure your RileyLink below. After selecting a RileyLink, it will be possible to continue setup once the RileyLink status is \"Connected\". This might take a minute.\n + Note: You can continue setup once the pump has been set up.\n Start your first objective Permission Ask for permission diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java index 1b400b7bde..17f2932200 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/service/RileyLinkMedtronicService.java @@ -96,8 +96,9 @@ public class RileyLinkMedtronicService extends RileyLinkService { setPumpIDString(sp.getString(MedtronicConst.Prefs.PumpSerial, "000000")); - // get most recently used RileyLink address - rileyLinkServiceData.rileylinkAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); + // get most recently used RileyLink address and name + rileyLinkServiceData.rileyLinkAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); + rileyLinkServiceData.rileyLinkName = sp.getString(RileyLinkConst.Prefs.RileyLinkName, ""); rfspy.startReader(); diff --git a/medtronic/src/main/res/values/strings.xml b/medtronic/src/main/res/values/strings.xml index abcf6d97d6..994f46ef4b 100644 --- a/medtronic/src/main/res/values/strings.xml +++ b/medtronic/src/main/res/values/strings.xml @@ -111,6 +111,7 @@ Pump clock update needed On Off + %1$.1f %2$s (%3$s) Pump time updated set_neutral_temps diff --git a/medtronic/src/main/res/xml/pref_medtronic.xml b/medtronic/src/main/res/xml/pref_medtronic.xml index 342250d046..78e20e8ef8 100644 --- a/medtronic/src/main/res/xml/pref_medtronic.xml +++ b/medtronic/src/main/res/xml/pref_medtronic.xml @@ -95,7 +95,7 @@ android:key="@string/key_rileylink_mac_address" android:summary="" android:title="RileyLink Configuration"> - + %1$.2f %2$s (%3$s) + RileyLink Configuration Bolus beeps enabled Basal beeps enabled SMB beeps enabled diff --git a/omnipod/src/main/res/xml/pref_omnipod.xml b/omnipod/src/main/res/xml/pref_omnipod.xml index 79a772e94a..853d5eda9e 100644 --- a/omnipod/src/main/res/xml/pref_omnipod.xml +++ b/omnipod/src/main/res/xml/pref_omnipod.xml @@ -7,9 +7,8 @@ - + android:title="@string/omnipod_config_riley_link_configuration"> + diff --git a/rileylink/src/main/AndroidManifest.xml b/rileylink/src/main/AndroidManifest.xml index c603e9e7ef..3236de84bd 100644 --- a/rileylink/src/main/AndroidManifest.xml +++ b/rileylink/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="info.nightscout.androidaps.plugins.pump.common"> @@ -8,12 +8,13 @@ - + - + + filters; - public ListView listBTScan; - public Toolbar toolbarBTScan; - public Context mContext = this; - private BluetoothAdapter mBluetoothAdapter; - private BluetoothLeScanner mLEScanner; - private LeDeviceListAdapter mLeDeviceListAdapter; - private Handler mHandler; - - private String actionTitleStart, actionTitleStop; - private MenuItem menuItem; - - private boolean rlDisconnected; + private ScanSettings settings; + private List filters; + private TextView currentlySelectedRileyLinkName; + private TextView currentlySelectedRileyLinkAddress; + private Button buttonRemoveRileyLink; + private Button buttonStartScan; + private Button buttonStopScan; + private BluetoothAdapter bluetoothAdapter; + private BluetoothLeScanner bleScanner; + private LeDeviceListAdapter deviceListAdapter; + private Handler handler; + public boolean scanning; + private final Runnable stopScanAfterTimeoutRunnable = () -> { + if (scanning) { + stopLeDeviceScan(); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL + } + }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.rileylink_scan_activity); + setContentView(R.layout.riley_link_ble_config_activity); // Initializes Bluetooth adapter. - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - mHandler = new Handler(); - - mLeDeviceListAdapter = new LeDeviceListAdapter(); - listBTScan = findViewById(R.id.rileylink_listBTScan); - listBTScan.setAdapter(mLeDeviceListAdapter); - listBTScan.setOnItemClickListener((parent, view, position, id) -> { - + bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + deviceListAdapter = new LeDeviceListAdapter(); + handler = new Handler(); + currentlySelectedRileyLinkName = findViewById(R.id.riley_link_ble_config_currently_selected_riley_link_name); + currentlySelectedRileyLinkAddress = findViewById(R.id.riley_link_ble_config_currently_selected_riley_link_address); + buttonRemoveRileyLink = findViewById(R.id.riley_link_ble_config_button_remove_riley_link); + buttonStartScan = findViewById(R.id.riley_link_ble_config_scan_start); + buttonStopScan = findViewById(R.id.riley_link_ble_config_button_scan_stop); + ListView deviceList = findViewById(R.id.riley_link_ble_config_scan_device_list); + deviceList.setAdapter(deviceListAdapter); + deviceList.setOnItemClickListener((parent, view, position, id) -> { // stop scanning if still active - if (mScanning) { - mScanning = false; - mLEScanner.stopScan(mScanCallback2); + if (scanning) { + stopLeDeviceScan(); } - TextView textview = view.findViewById(R.id.rileylink_device_address); - String bleAddress = textview.getText().toString(); + String bleAddress = ((TextView) view.findViewById(R.id.riley_link_ble_config_scan_item_device_address)).getText().toString(); + String deviceName = ((TextView) view.findViewById(R.id.riley_link_ble_config_scan_item_device_name)).getText().toString(); sp.putString(RileyLinkConst.Prefs.RileyLinkAddress, bleAddress); + sp.putString(RileyLinkConst.Prefs.RileyLinkName, deviceName); RileyLinkPumpDevice rileyLinkPump = (RileyLinkPumpDevice) activePlugin.getActivePump(); rileyLinkPump.getRileyLinkService().verifyConfiguration(true); // force reloading of address to assure that the RL gets reconnected (even if the address didn't change) @@ -114,93 +112,88 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { finish(); }); - toolbarBTScan = findViewById(R.id.rileylink_toolbarBTScan); - toolbarBTScan.setTitle(R.string.rileylink_scanner_title); - setSupportActionBar(toolbarBTScan); + buttonStartScan.setOnClickListener(view -> { + // disable currently selected RL, so that we can discover it + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this); + startLeDeviceScan(); + }); + + buttonStopScan.setOnClickListener(view -> { + if (scanning) { + stopLeDeviceScan(); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL + } + }); + + buttonRemoveRileyLink.setOnClickListener(view -> new AlertDialog.Builder(this) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation_title)) + .setMessage(getString(R.string.riley_link_ble_config_remove_riley_link_confirmation)) + .setPositiveButton(getString(R.string.riley_link_common_yes), (dialog, which) -> { + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, RileyLinkBLEConfigActivity.this); + sp.remove(RileyLinkConst.Prefs.RileyLinkAddress); + sp.remove(RileyLinkConst.Prefs.RileyLinkName); + updateCurrentlySelectedRileyLink(); + }) + .setNegativeButton(getString(R.string.riley_link_common_no), null) + .show()); + } + + private void updateCurrentlySelectedRileyLink() { + String address = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); + if (StringUtils.isEmpty(address)) { + currentlySelectedRileyLinkName.setText(R.string.riley_link_ble_config_no_riley_link_selected); + currentlySelectedRileyLinkAddress.setVisibility(View.GONE); + buttonRemoveRileyLink.setVisibility(View.GONE); + } else { + currentlySelectedRileyLinkAddress.setVisibility(View.VISIBLE); + buttonRemoveRileyLink.setVisibility(View.VISIBLE); + + currentlySelectedRileyLinkName.setText(sp.getString(RileyLinkConst.Prefs.RileyLinkName, "RileyLink (?)")); + currentlySelectedRileyLinkAddress.setText(address); + } } @Override protected void onResume() { super.onResume(); prepareForScanning(); - } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_rileylink_ble_scan, menu); - - actionTitleStart = resourceHelper.gs(R.string.rileylink_scanner_scan_scan); - actionTitleStop = resourceHelper.gs(R.string.rileylink_scanner_scan_stop); - - menuItem = menu.getItem(0); - - menuItem.setTitle(actionTitleStart); - - return true; - } - - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getTitle().equals(actionTitleStart)) { - // disable currently selected RL, so that we can discover it - rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkDisconnect, this); - rlDisconnected = true; - - scanLeDevice(true); - return true; - } else if (item.getTitle().equals(actionTitleStop)) { - scanLeDevice(false); - return true; - } else { - return super.onOptionsItemSelected(item); - } - } - - @Override public void onBackPressed() { - super.onBackPressed(); - if (rlDisconnected) { - // Reconnect RL if it was disconnected for the scan - rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); - } + updateCurrentlySelectedRileyLink(); } @Override protected void onDestroy() { super.onDestroy(); - mScanning = false; - mLEScanner.stopScan(mScanCallback2); + if (scanning) { + stopLeDeviceScan(); + rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.RileyLinkNewAddressSet, this); // Reconnect current RL + } } private void prepareForScanning() { boolean checkOK = blePrecheck.prerequisitesCheck(this); if (checkOK) { - mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); + bleScanner = bluetoothAdapter.getBluetoothLeScanner(); settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); filters = Collections.singletonList(new ScanFilter.Builder().setServiceUuid( ParcelUuid.fromString(GattAttributes.SERVICE_RADIO)).build()); } } - - private final ScanCallback mScanCallback2 = new ScanCallback() { - + private final ScanCallback bleScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, final ScanResult scanRecord) { - Log.d(TAG, scanRecord.toString()); runOnUiThread(() -> { if (addDevice(scanRecord)) - mLeDeviceListAdapter.notifyDataSetChanged(); + deviceListAdapter.notifyDataSetChanged(); }); } - @Override public void onBatchScanResults(final List results) { - runOnUiThread(() -> { - boolean added = false; for (ScanResult result : results) { @@ -209,13 +202,11 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { } if (added) - mLeDeviceListAdapter.notifyDataSetChanged(); + deviceListAdapter.notifyDataSetChanged(); }); } - private boolean addDevice(ScanResult result) { - BluetoothDevice device = result.getDevice(); List serviceUuids = result.getScanRecord().getServiceUuids(); @@ -225,12 +216,11 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { } else if (serviceUuids.size() > 1) { Log.v(TAG, "Device " + device.getAddress() + " has too many serviceUuids (Not RileyLink)."); } else { - String uuid = serviceUuids.get(0).getUuid().toString().toLowerCase(); if (uuid.equals(GattAttributes.SERVICE_RADIO)) { Log.i(TAG, "Found RileyLink with address: " + device.getAddress()); - mLeDeviceListAdapter.addDevice(result); + deviceListAdapter.addDevice(result); return true; } else { Log.v(TAG, "Device " + device.getAddress() + " has incorrect uuid (Not RileyLink)."); @@ -240,84 +230,63 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { return false; } - - private String getDeviceDebug(BluetoothDevice device) { - return "BluetoothDevice [name=" + device.getName() + ", address=" + device.getAddress() + // - ", type=" + device.getType(); // + ", alias=" + device.getAlias(); - } - - @Override public void onScanFailed(int errorCode) { Log.e("Scan Failed", "Error Code: " + errorCode); - Toast.makeText(mContext, resourceHelper.gs(R.string.rileylink_scanner_scanning_error, errorCode), + Toast.makeText(RileyLinkBLEConfigActivity.this, resourceHelper.gs(R.string.riley_link_ble_config_scan_error, errorCode), Toast.LENGTH_LONG).show(); } }; - - private void scanLeDevice(final boolean enable) { - - if (mLEScanner == null) + private void startLeDeviceScan() { + if (bleScanner == null) { + aapsLogger.error(LTag.PUMPBTCOMM, "startLeDeviceScan failed: bleScanner is null"); return; - - if (enable) { - - mLeDeviceListAdapter.clear(); - mLeDeviceListAdapter.notifyDataSetChanged(); - - // Stops scanning after a pre-defined scan period. - mHandler.postDelayed(() -> { - - if (mScanning) { - mScanning = false; - mLEScanner.stopScan(mScanCallback2); - aapsLogger.debug("scanLeDevice: Scanning Stop"); - Toast.makeText(mContext, R.string.rileylink_scanner_scanning_finished, Toast.LENGTH_SHORT).show(); - menuItem.setTitle(actionTitleStart); - } - }, SCAN_PERIOD); - - mScanning = true; - mLEScanner.startScan(filters, settings, mScanCallback2); - aapsLogger.debug("scanLeDevice: Scanning Start"); - Toast.makeText(this, R.string.rileylink_scanner_scanning, Toast.LENGTH_SHORT).show(); - - menuItem.setTitle(actionTitleStop); - - } else { - if (mScanning) { - mScanning = false; - mLEScanner.stopScan(mScanCallback2); - - aapsLogger.debug("scanLeDevice: Scanning Stop"); - Toast.makeText(this, R.string.rileylink_scanner_scanning_finished, Toast.LENGTH_SHORT).show(); - - menuItem.setTitle(actionTitleStart); - } } + + deviceListAdapter.clear(); + deviceListAdapter.notifyDataSetChanged(); + + handler.postDelayed(stopScanAfterTimeoutRunnable, SCAN_PERIOD_MILLIS); + + buttonStartScan.setEnabled(false); + buttonStopScan.setVisibility(View.VISIBLE); + + scanning = true; + bleScanner.startScan(filters, settings, bleScanCallback); + aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start"); + Toast.makeText(RileyLinkBLEConfigActivity.this, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show(); + } + + private void stopLeDeviceScan() { + if (scanning) { + scanning = false; + + bleScanner.stopScan(bleScanCallback); + + aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop"); + Toast.makeText(this, R.string.riley_link_ble_config_scan_finished, Toast.LENGTH_SHORT).show(); + handler.removeCallbacks(stopScanAfterTimeoutRunnable); + } + + buttonStartScan.setEnabled(true); + buttonStopScan.setVisibility(View.GONE); } private class LeDeviceListAdapter extends BaseAdapter { - private final ArrayList mLeDevices; private final Map rileyLinkDevices; private final LayoutInflater mInflator; - String currentlySelectedAddress; - public LeDeviceListAdapter() { super(); mLeDevices = new ArrayList<>(); rileyLinkDevices = new HashMap<>(); - mInflator = RileyLinkBLEScanActivity.this.getLayoutInflater(); - currentlySelectedAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); + mInflator = RileyLinkBLEConfigActivity.this.getLayoutInflater(); } - public void addDevice(ScanResult result) { - if (!mLeDevices.contains(result.getDevice())) { mLeDevices.add(result.getDevice()); } @@ -325,42 +294,36 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { notifyDataSetChanged(); } - public void clear() { mLeDevices.clear(); rileyLinkDevices.clear(); notifyDataSetChanged(); } - @Override public int getCount() { return mLeDevices.size(); } - @Override public Object getItem(int i) { return mLeDevices.get(i); } - @Override public long getItemId(int i) { return i; } - @Override public View getView(int i, View view, ViewGroup viewGroup) { - ViewHolder viewHolder; // General ListView optimization code. if (view == null) { - view = mInflator.inflate(R.layout.rileylink_scan_item, null); + view = mInflator.inflate(R.layout.riley_link_ble_config_scan_item, null); viewHolder = new ViewHolder(); - viewHolder.deviceAddress = view.findViewById(R.id.rileylink_device_address); - viewHolder.deviceName = view.findViewById(R.id.rileylink_device_name); + viewHolder.deviceAddress = view.findViewById(R.id.riley_link_ble_config_scan_item_device_address); + viewHolder.deviceName = view.findViewById(R.id.riley_link_ble_config_scan_item_device_name); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); @@ -370,15 +333,15 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { String deviceName = device.getName(); if (StringUtils.isBlank(deviceName)) { - deviceName = "RileyLink"; + deviceName = "RileyLink (?)"; } - deviceName += " [" + rileyLinkDevices.get(device).intValue() + "]"; + deviceName += " [" + rileyLinkDevices.get(device) + "]"; + + String currentlySelectedAddress = sp.getString(RileyLinkConst.Prefs.RileyLinkAddress, ""); if (currentlySelectedAddress.equals(device.getAddress())) { - // viewHolder.deviceName.setTextColor(getColor(R.color.secondary_text_light)); - // viewHolder.deviceAddress.setTextColor(getColor(R.color.secondary_text_light)); - deviceName += " (" + getResources().getString(R.string.rileylink_scanner_selected_device) + ")"; + deviceName += " (" + getResources().getString(R.string.riley_link_ble_config_scan_selected) + ")"; } viewHolder.deviceName.setText(deviceName); @@ -386,11 +349,9 @@ public class RileyLinkBLEScanActivity extends NoSplashAppCompatActivity { return view; } - } static class ViewHolder { - TextView deviceName; TextView deviceAddress; } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkConst.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkConst.java index 40d17aee84..28f551e188 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkConst.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/RileyLinkConst.java @@ -33,6 +33,7 @@ public class RileyLinkConst { //public static final String PrefPrefix = "pref_rileylink_"; //public static final String RileyLinkAddress = PrefPrefix + "mac_address"; // pref_rileylink_mac_address public static final int RileyLinkAddress = R.string.key_rileylink_mac_address; + public static final int RileyLinkName = R.string.key_rileylink_name; public static final String LastGoodDeviceCommunicationTime = Prefix + "lastGoodDeviceCommunicationTime"; public static final String LastGoodDeviceFrequency = Prefix + "LastGoodDeviceFrequency"; public static final int Encoding = R.string.key_medtronic_encoding; diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java index 5a26b80dd8..7b13811a25 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.java @@ -35,6 +35,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLin import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.common.utils.ThreadUtil; +import info.nightscout.androidaps.utils.sharedPreferences.SP; /** * Created by geoff on 5/26/16. @@ -46,6 +47,7 @@ public class RileyLinkBLE { @Inject AAPSLogger aapsLogger; @Inject RileyLinkServiceData rileyLinkServiceData; @Inject RileyLinkUtil rileyLinkUtil; + @Inject SP sp; private final Context context; private final boolean gattDebugEnabled = true; @@ -393,8 +395,16 @@ public class RileyLinkBLE { aapsLogger.debug(LTag.PUMPBTCOMM, "Gatt Connected."); } - rileyLinkServiceData.rileylinkAddress = bluetoothConnectionGatt.getDevice().getAddress(); - rileyLinkServiceData.rileyLinkName = bluetoothConnectionGatt.getDevice().getName(); + String deviceName = bluetoothConnectionGatt.getDevice().getName(); + if (StringUtils.isNotEmpty(deviceName)) { + // Update stored name upon connecting (also for backwards compatibility for device where a name was not yet stored) + sp.putString(RileyLinkConst.Prefs.RileyLinkName, deviceName); + } else { + sp.remove(RileyLinkConst.Prefs.RileyLinkName); + } + + rileyLinkServiceData.rileyLinkName = deviceName; + rileyLinkServiceData.rileyLinkAddress = bluetoothConnectionGatt.getDevice().getAddress(); } } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bAbstract.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bAbstract.java index 3bab1a6d05..8d4e80d752 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bAbstract.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bAbstract.java @@ -23,10 +23,10 @@ public abstract class Encoding4b6bAbstract implements Encoding4b6b { // 21, 49, 50, 35, 52, 37, 38, 22, 26, 25, 42, 11, 44, 13, 14, 28 - public abstract byte[] encode4b6b(byte[] data); + @Override public abstract byte[] encode4b6b(byte[] data); - public abstract byte[] decode4b6b(byte[] data) throws RileyLinkCommunicationException; + @Override public abstract byte[] decode4b6b(byte[] data) throws RileyLinkCommunicationException; protected short convertUnsigned(byte x) { diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGeoff.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGeoff.java index 32176040f5..1b0dcb7dc4 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGeoff.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGeoff.java @@ -19,7 +19,7 @@ public class Encoding4b6bGeoff extends Encoding4b6bAbstract { public static final Logger LOG = StacktraceLoggerWrapper.getLogger(Encoding4b6bGeoff.class); - public byte[] encode4b6b(byte[] data) { + @Override public byte[] encode4b6b(byte[] data) { // if ((data.length % 2) != 0) { // LOG.error("Warning: data is odd number of bytes"); // } @@ -77,7 +77,7 @@ public class Encoding4b6bGeoff extends Encoding4b6bAbstract { * @return * @throws NumberFormatException */ - public byte[] decode4b6b(byte[] raw) throws RileyLinkCommunicationException { + @Override public byte[] decode4b6b(byte[] raw) throws RileyLinkCommunicationException { StringBuilder errorMessageBuilder = new StringBuilder(); diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGo.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGo.java index 3b0152455c..94ae52f788 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGo.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bGo.java @@ -20,7 +20,7 @@ public class Encoding4b6bGo extends Encoding4b6bAbstract { private static Map decodeGoMap; - public byte[] encode4b6b(byte[] src) { + @Override public byte[] encode4b6b(byte[] src) { // 2 input bytes produce 3 output bytes. // Odd final input byte, if any, produces 2 output bytes. int n = src.length; @@ -54,7 +54,7 @@ public class Encoding4b6bGo extends Encoding4b6bAbstract { * @param src * @return */ - public byte[] decode4b6b(byte[] src) throws RileyLinkCommunicationException { + @Override public byte[] decode4b6b(byte[] src) throws RileyLinkCommunicationException { int n = src.length; if (decodeGoMap == null) diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java index d7a3a5f9d1..8dfaf16b0f 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java @@ -33,7 +33,7 @@ public class Encoding4b6bLoop extends Encoding4b6bAbstract { * @param data * @return */ - public byte[] encode4b6b(byte[] data) { + @Override public byte[] encode4b6b(byte[] data) { List buffer = new ArrayList(); int bitAccumulator = 0x0; @@ -78,7 +78,7 @@ public class Encoding4b6bLoop extends Encoding4b6bAbstract { * @return * @throws RileyLinkCommunicationException */ - public byte[] decode4b6b(byte[] data) throws RileyLinkCommunicationException { + @Override public byte[] decode4b6b(byte[] data) throws RileyLinkCommunicationException { List buffer = new ArrayList(); int availBits = 0; int bitAccumulator = 0; diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java index c4e06f8ab1..45d2bbbe7b 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusActivity.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.dialog; import android.os.Bundle; -import android.widget.TextView; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -19,29 +18,18 @@ import javax.inject.Inject; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.plugins.pump.common.R; import info.nightscout.androidaps.plugins.pump.common.dialog.RefreshableInterface; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil; -import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData; import info.nightscout.androidaps.utils.resources.ResourceHelper; public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { @Inject ResourceHelper resourceHelper; - @Inject RileyLinkUtil rileyLinkUtil; - @Inject RileyLinkServiceData rileyLinkServiceData; - TextView connectionStatus; - TextView configuredAddress; - TextView connectedDevice; - TextView connectionError; - - private SectionsPagerAdapter mSectionsPagerAdapter; - private FloatingActionButton floatingActionButton; + private SectionsPagerAdapter sectionsPagerAdapter; private TabLayout tabLayout; /** * The {@link ViewPager} that will host the section contents. */ - private ViewPager mViewPager; - + private ViewPager viewPager; @Override public void onCreate(Bundle savedInstanceState) { @@ -52,36 +40,28 @@ public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { // primary sections of the activity. // Set up the ViewPager with the sections adapter. - mViewPager = findViewById(R.id.rileylink_settings_container); + viewPager = findViewById(R.id.rileylink_settings_container); // mViewPager.setAdapter(mSectionsPagerAdapter); - setupViewPager(mViewPager); + setupViewPager(); tabLayout = findViewById(R.id.rileylink_settings_tabs); - tabLayout.setupWithViewPager(mViewPager); + tabLayout.setupWithViewPager(viewPager); - floatingActionButton = findViewById(R.id.rileylink_settings_fab); + FloatingActionButton floatingActionButton = findViewById(R.id.rileylink_settings_fab); floatingActionButton.setOnClickListener(v -> { - RefreshableInterface selectableInterface = (RefreshableInterface) mSectionsPagerAdapter + RefreshableInterface selectableInterface = (RefreshableInterface) sectionsPagerAdapter .getItem(tabLayout.getSelectedTabPosition()); selectableInterface.refreshData(); }); - - this.connectionStatus = findViewById(R.id.rls_t1_connection_status); - this.configuredAddress = findViewById(R.id.rls_t1_configured_address); - this.connectedDevice = findViewById(R.id.rls_t1_connected_device); - this.connectionError = findViewById(R.id.rls_t1_connection_error); } - public void setupViewPager(ViewPager pager) { + public void setupViewPager() { + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - - mSectionsPagerAdapter.addFragment(new RileyLinkStatusGeneralFragment(), resourceHelper.gs(R.string.rileylink_settings_tab1)); - mSectionsPagerAdapter.addFragment(new RileyLinkStatusHistoryFragment(), resourceHelper.gs(R.string.rileylink_settings_tab2)); - //mSectionsPagerAdapter.addFragment(new RileyLinkStatusDevice(), "Medtronic"); - - mViewPager.setAdapter(mSectionsPagerAdapter); + sectionsPagerAdapter.addFragment(new RileyLinkStatusGeneralFragment(), resourceHelper.gs(R.string.rileylink_settings_tab1)); + sectionsPagerAdapter.addFragment(new RileyLinkStatusHistoryFragment(), resourceHelper.gs(R.string.rileylink_settings_tab2)); + viewPager.setAdapter(sectionsPagerAdapter); } /** @@ -94,32 +74,27 @@ public class RileyLinkStatusActivity extends NoSplashAppCompatActivity { List fragmentTitle = new ArrayList<>(); int lastSelectedPosition = 0; - public SectionsPagerAdapter(FragmentManager fm) { super(fm); } - @Override public Fragment getItem(int position) { this.lastSelectedPosition = position; return fragmentList.get(position); } - @Override public int getCount() { // Show 3 total pages. return fragmentList.size(); } - public void addFragment(Fragment fragment, String title) { this.fragmentList.add(fragment); this.fragmentTitle.add(title); } - @Override public CharSequence getPageTitle(int position) { return fragmentTitle.get(position); diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java index 309530cfa9..947c30a1a9 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneralFragment.java @@ -41,8 +41,8 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re @Inject DateUtil dateUtil; private TextView connectionStatus; - private TextView configuredAddress; - private TextView connectedRileyLinkName; + private TextView configuredRileyLinkAddress; + private TextView configuredRileyLinkName; private TextView connectedDevice; private TextView connectionError; private TextView deviceType; @@ -63,8 +63,8 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re super.onStart(); this.connectionStatus = getActivity().findViewById(R.id.rls_t1_connection_status); - this.configuredAddress = getActivity().findViewById(R.id.rls_t1_configured_address); - this.connectedRileyLinkName = getActivity().findViewById(R.id.rls_t1_connected_riley_link_name); + this.configuredRileyLinkAddress = getActivity().findViewById(R.id.rls_t1_configured_riley_link_address); + this.configuredRileyLinkName = getActivity().findViewById(R.id.rls_t1_configured_riley_link_name); this.connectedDevice = getActivity().findViewById(R.id.rls_t1_connected_device); this.connectionError = getActivity().findViewById(R.id.rls_t1_connection_error); this.deviceType = getActivity().findViewById(R.id.rls_t1_device_type); @@ -78,15 +78,15 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re refreshData(); } - public void refreshData() { + @Override public void refreshData() { RileyLinkTargetDevice targetDevice = rileyLinkServiceData.targetDevice; this.connectionStatus.setText(resourceHelper.gs(rileyLinkServiceData.rileyLinkServiceState.getResourceId())); // BS FIXME rileyLinkServiceData is injected so I suppose it cannot be null? if (rileyLinkServiceData != null) { - this.configuredAddress.setText(StringUtils.isEmpty(rileyLinkServiceData.rileylinkAddress) ? PLACEHOLDER : rileyLinkServiceData.rileylinkAddress); - this.connectedRileyLinkName.setText(StringUtils.isEmpty(rileyLinkServiceData.rileyLinkName) ? PLACEHOLDER : rileyLinkServiceData.rileyLinkName); + this.configuredRileyLinkAddress.setText(StringUtils.isEmpty(rileyLinkServiceData.rileyLinkAddress) ? PLACEHOLDER : rileyLinkServiceData.rileyLinkAddress); + this.configuredRileyLinkName.setText(StringUtils.isEmpty(rileyLinkServiceData.rileyLinkName) ? PLACEHOLDER : rileyLinkServiceData.rileyLinkName); this.connectionError.setText(rileyLinkServiceData.rileyLinkError == null ? // PLACEHOLDER : resourceHelper.gs(rileyLinkServiceData.rileyLinkError.getResourceId(targetDevice))); @@ -114,7 +114,7 @@ public class RileyLinkStatusGeneralFragment extends DaggerFragment implements Re long lastConnectionTimeMillis = pumpPlugin.getLastConnectionTimeMillis(); if (lastConnectionTimeMillis == 0) { - this.lastDeviceContact.setText(resourceHelper.gs(R.string.common_never)); + this.lastDeviceContact.setText(resourceHelper.gs(R.string.riley_link_ble_config_connected_never)); } else { this.lastDeviceContact.setText(StringUtil.toDateTimeString(dateUtil, new LocalDateTime(lastConnectionTimeMillis))); } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java index 7b4e11fd35..b15da2d175 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.java @@ -154,19 +154,19 @@ public abstract class RileyLinkService extends DaggerService { rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkInitializing); if (rileyLinkBLE.isConnected()) { - if (deviceAddress.equals(rileyLinkServiceData.rileylinkAddress)) { + if (deviceAddress.equals(rileyLinkServiceData.rileyLinkAddress)) { aapsLogger.info(LTag.PUMPBTCOMM, "No change to RL address. Not reconnecting."); return false; } else { - aapsLogger.warn(LTag.PUMPBTCOMM, "Disconnecting from old RL (" + rileyLinkServiceData.rileylinkAddress + aapsLogger.warn(LTag.PUMPBTCOMM, "Disconnecting from old RL (" + rileyLinkServiceData.rileyLinkAddress + "), reconnecting to new: " + deviceAddress); rileyLinkBLE.disconnect(); // prolly need to shut down listening thread too? // SP.putString(MedtronicConst.Prefs.RileyLinkAddress, deviceAddress); - rileyLinkServiceData.rileylinkAddress = deviceAddress; - rileyLinkBLE.findRileyLink(rileyLinkServiceData.rileylinkAddress); + rileyLinkServiceData.rileyLinkAddress = deviceAddress; + rileyLinkBLE.findRileyLink(rileyLinkServiceData.rileyLinkAddress); return true; } } else { @@ -229,7 +229,7 @@ public abstract class RileyLinkService extends DaggerService { if (rileyLinkBLE.isConnected()) { rileyLinkBLE.disconnect(); - rileyLinkServiceData.rileylinkAddress = null; + rileyLinkServiceData.rileyLinkAddress = null; rileyLinkServiceData.rileyLinkName = null; } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java index 10eb06373c..ed48733680 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java @@ -34,7 +34,7 @@ public class RileyLinkServiceData { private long lastServiceStateChange = 0L; public RileyLinkFirmwareVersion firmwareVersion; // here we have "compatibility level" version public RileyLinkTargetFrequency rileyLinkTargetFrequency; - public String rileylinkAddress; + public String rileyLinkAddress; public String rileyLinkName; long lastTuneUpTime = 0L; public Double lastGoodFrequency; diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/data/ServiceTransport.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/data/ServiceTransport.java index c2043708fe..99fa32f5e9 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/data/ServiceTransport.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/data/ServiceTransport.java @@ -135,7 +135,7 @@ public class ServiceTransport extends ServiceMessage { } - public ServiceTransport clone() { + @Override public ServiceTransport clone() { Parcel p = Parcel.obtain(); Parcel p2 = Parcel.obtain(); getMap().writeToParcel(p, 0); diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTaskExecutor.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTaskExecutor.java index 790bfb1535..673e3ae02c 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTaskExecutor.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ServiceTaskExecutor.java @@ -33,7 +33,7 @@ public class ServiceTaskExecutor extends ThreadPoolExecutor { } // FIXME - protected void beforeExecute(Thread t, Runnable r) { + @Override protected void beforeExecute(Thread t, Runnable r) { // This is run on either caller UI thread or Service UI thread. ServiceTask task = (ServiceTask) r; aapsLogger.debug(LTag.PUMPBTCOMM, "About to run task " + task.getClass().getSimpleName()); @@ -43,7 +43,7 @@ public class ServiceTaskExecutor extends ThreadPoolExecutor { // FIXME - protected void afterExecute(Runnable r, Throwable t) { + @Override protected void afterExecute(Runnable r, Throwable t) { // This is run on either caller UI thread or Service UI thread. ServiceTask task = (ServiceTask) r; task.postOp(); diff --git a/rileylink/src/main/res/layout/riley_link_ble_config_activity.xml b/rileylink/src/main/res/layout/riley_link_ble_config_activity.xml new file mode 100644 index 0000000000..9746ffb791 --- /dev/null +++ b/rileylink/src/main/res/layout/riley_link_ble_config_activity.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + +