Skip to content
Snippets Groups Projects
Unverified Commit c0fd2dfd authored by Johannes Mockenhaupt's avatar Johannes Mockenhaupt
Browse files

Extract method acquireUsbDevice.

parent cdd5f6dd
No related branches found
No related tags found
No related merge requests found
...@@ -128,30 +128,7 @@ public class MedtronicCnlIntentService extends IntentService { ...@@ -128,30 +128,7 @@ public class MedtronicCnlIntentService extends IntentService {
pollInterval = configurationStore.getLowBatteryPollInterval(); pollInterval = configurationStore.getLowBatteryPollInterval();
} }
if (!hasUsbHostFeature()) { if (!acquireUsbDevice()) return;
sendStatus("It appears that this device doesn't support USB OTG.");
Log.e(TAG, "Device does not support USB OTG");
// TODO - throw, don't return
return;
}
UsbDevice cnlStick = UsbHidDriver.getUsbDevice(mUsbManager, USB_VID, USB_PID);
if (cnlStick == null) {
sendStatus("USB connection error. Is the Contour Next Link plugged in?");
Log.w(TAG, "USB connection error. Is the CNL plugged in?");
// TODO - set status if offline or Nightscout not reachable
uploadToNightscout();
// TODO - throw, don't return
return;
}
if (!mUsbManager.hasPermission(UsbHidDriver.getUsbDevice(mUsbManager, USB_VID, USB_PID))) {
sendMessage(Constants.ACTION_NO_USB_PERMISSION);
// TODO - throw, don't return
return;
}
mHidDevice = UsbHidDriver.acquire(mUsbManager, cnlStick);
try { try {
mHidDevice.open(); mHidDevice.open();
...@@ -366,6 +343,34 @@ public class MedtronicCnlIntentService extends IntentService { ...@@ -366,6 +343,34 @@ public class MedtronicCnlIntentService extends IntentService {
} }
} }
private boolean acquireUsbDevice() {
if (!hasUsbHostFeature()) {
sendStatus("It appears that this device doesn't support USB OTG.");
Log.e(TAG, "Device does not support USB OTG");
// TODO - throw, don't return
return false;
}
UsbDevice cnlStick = UsbHidDriver.getUsbDevice(mUsbManager, USB_VID, USB_PID);
if (cnlStick == null) {
sendStatus("USB connection error. Is the Contour Next Link plugged in?");
Log.w(TAG, "USB connection error. Is the CNL plugged in?");
// TODO - set status if offline or Nightscout not reachable
uploadToNightscout();
// TODO - throw, don't return
return false;
}
if (!mUsbManager.hasPermission(UsbHidDriver.getUsbDevice(mUsbManager, USB_VID, USB_PID))) {
sendMessage(Constants.ACTION_NO_USB_PERMISSION);
// TODO - throw, don't return
return false;
}
mHidDevice = UsbHidDriver.acquire(mUsbManager, cnlStick);
return true;
}
// reliable wake alarm manager wake up for all android versions // reliable wake alarm manager wake up for all android versions
public static void wakeUpIntent(Context context, long wakeTime, PendingIntent pendingIntent) { public static void wakeUpIntent(Context context, long wakeTime, PendingIntent pendingIntent) {
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment