Handling Battery Service updates in CGMS service and activity

This commit is contained in:
Aleksander Nowakowski
2018-04-19 17:10:04 +02:00
parent 1a81c11ad4
commit bc8cac6b63
2 changed files with 30 additions and 0 deletions

View File

@@ -35,17 +35,20 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import java.util.UUID;
import no.nordicsemi.android.nrftoolbox.R;
import no.nordicsemi.android.nrftoolbox.profile.BleProfileService;
import no.nordicsemi.android.nrftoolbox.profile.BleProfileServiceReadyActivity;
import no.nordicsemi.android.nrftoolbox.proximity.ProximityService;
public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMSBinder> implements PopupMenu.OnMenuItemClickListener {
private View mControlPanelStd;
private View mControlPanelAbort;
private ListView mRecordsListView;
private TextView mBatteryLevelView;
private CGMSRecordsAdapter mCgmsRecordsAdapter;
private CGMService.CGMSBinder mBinder;
@@ -65,6 +68,7 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
mRecordsListView = findViewById(R.id.list);
mControlPanelStd = findViewById(R.id.cgms_control_std);
mControlPanelAbort = findViewById(R.id.cgms_control_abort);
mBatteryLevelView = findViewById(R.id.battery);
findViewById(R.id.action_last).setOnClickListener(v -> {
clearRecords();
@@ -166,10 +170,15 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
});
}
public void onBatteryLevelChanged(final BluetoothDevice device, final int value) {
mBatteryLevelView.setText(getString(R.string.battery, value));
}
@Override
public void onDeviceDisconnected(final BluetoothDevice device) {
super.onDeviceDisconnected(device);
setOperationInProgress(false);
mBatteryLevelView.setText(R.string.not_available);
}
@Override
@@ -181,6 +190,7 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
@Override
protected void setDefaultUI() {
clearRecords();
mBatteryLevelView.setText(R.string.not_available);
}
@Override
@@ -217,6 +227,7 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
final BluetoothDevice device = intent.getParcelableExtra(ProximityService.EXTRA_DEVICE);
switch (action) {
case CGMService.BROADCAST_NEW_CGMS_VALUE: {
@@ -237,6 +248,11 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
// Update GUI
setOperationInProgress(true);
break;
case CGMService.BROADCAST_BATTERY_LEVEL:
final int batteryLevel = intent.getIntExtra(CGMService.EXTRA_BATTERY_LEVEL, 0);
// Update GUI
onBatteryLevelChanged(device, batteryLevel);
break;
case CGMService.OPERATION_FAILED:
// Update GUI
showToast(R.string.gls_operation_failed);
@@ -258,6 +274,7 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
intentFilter.addAction(CGMService.OPERATION_NOT_SUPPORTED);
intentFilter.addAction(CGMService.OPERATION_ABORTED);
intentFilter.addAction(CGMService.OPERATION_FAILED);
intentFilter.addAction(CGMService.BROADCAST_BATTERY_LEVEL);
return intentFilter;
}
}

View File

@@ -8,6 +8,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.SparseArray;
@@ -21,6 +22,10 @@ import no.nordicsemi.android.nrftoolbox.profile.BleProfileService;
public class CGMService extends BleProfileService implements CGMSManagerCallbacks {
private static final String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.cgms.ACTION_DISCONNECT";
public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL";
public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL";
public static final String BROADCAST_NEW_CGMS_VALUE = "no.nordicsemi.android.nrftoolbox.cgms.BROADCAST_NEW_CGMS_VALUE";
public static final String BROADCAST_DATA_SET_CLEAR = "no.nordicsemi.android.nrftoolbox.cgms.BROADCAST_DATA_SET_CLEAR";
public static final String OPERATION_STARTED = "no.nordicsemi.android.nrftoolbox.cgms.OPERATION_STARTED";
@@ -271,4 +276,12 @@ public class CGMService extends BleProfileService implements CGMSManagerCallback
public void onNumberOfRecordsRequested(final BluetoothDevice device, int value) {
showToast(getResources().getQuantityString(R.plurals.gls_progress, value, value));
}
@Override
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
final Intent broadcast = new Intent(BROADCAST_BATTERY_LEVEL);
broadcast.putExtra(EXTRA_DEVICE, device);
broadcast.putExtra(EXTRA_BATTERY_LEVEL, batteryLevel);
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
}
}