mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-09 03:37:03 +01:00
Reduce stutters on device changes / data fetch
This commit is contained in:
parent
7d9f8704c9
commit
2db88c63db
@ -93,10 +93,12 @@ public class DashboardFragment extends Fragment {
|
||||
String action = intent.getAction();
|
||||
if (action == null) return;
|
||||
switch (action) {
|
||||
case GBDevice.ACTION_DEVICE_CHANGED:
|
||||
GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
case GBApplication.ACTION_NEW_DATA:
|
||||
final GBDevice dev = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
if (dev != null && !dev.isBusy()) {
|
||||
refresh();
|
||||
if (dashboardData.showAllDevices || dashboardData.showDeviceList.contains(dev.getAddress())) {
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ACTION_CONFIG_CHANGE:
|
||||
@ -140,6 +142,7 @@ public class DashboardFragment extends Fragment {
|
||||
|
||||
IntentFilter filterLocal = new IntentFilter();
|
||||
filterLocal.addAction(GBDevice.ACTION_DEVICE_CHANGED);
|
||||
filterLocal.addAction(GBApplication.ACTION_NEW_DATA);
|
||||
filterLocal.addAction(ACTION_CONFIG_CHANGE);
|
||||
LocalBroadcastManager.getInstance(requireContext()).registerReceiver(mReceiver, filterLocal);
|
||||
|
||||
|
@ -75,9 +75,17 @@ public class DevicesFragment extends Fragment {
|
||||
switch (Objects.requireNonNull(action)) {
|
||||
case DeviceManager.ACTION_DEVICES_CHANGED:
|
||||
case GBApplication.ACTION_NEW_DATA:
|
||||
createRefreshTask("get activity data", requireContext()).execute();
|
||||
mGBDeviceAdapter.rebuildFolders();
|
||||
refreshPairedDevices();
|
||||
final GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
||||
if (action.equals(GBApplication.ACTION_NEW_DATA)) {
|
||||
createRefreshTask("get activity data", requireContext(), device).execute();
|
||||
}
|
||||
if (device != null) {
|
||||
// Refresh only this device
|
||||
refreshSingleDevice(device);
|
||||
} else {
|
||||
refreshPairedDevices();
|
||||
}
|
||||
|
||||
break;
|
||||
case DeviceService.ACTION_REALTIME_SAMPLES:
|
||||
handleRealtimeSample(intent.getSerializableExtra(DeviceService.EXTRA_REALTIME_SAMPLE));
|
||||
@ -117,7 +125,7 @@ public class DevicesFragment extends Fragment {
|
||||
@Override
|
||||
public void run() {
|
||||
if (getContext() != null) {
|
||||
createRefreshTask("get activity data", getContext()).execute();
|
||||
createRefreshTask("get activity data", getContext(), null).execute();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -216,36 +224,56 @@ public class DevicesFragment extends Fragment {
|
||||
|
||||
public void refreshPairedDevices() {
|
||||
if (mGBDeviceAdapter != null) {
|
||||
mGBDeviceAdapter.notifyDataSetChanged();
|
||||
mGBDeviceAdapter.rebuildFolders();
|
||||
mGBDeviceAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public RefreshTask createRefreshTask(String task, Context context) {
|
||||
return new RefreshTask(task, context);
|
||||
public void refreshSingleDevice(final GBDevice device) {
|
||||
if (mGBDeviceAdapter != null) {
|
||||
mGBDeviceAdapter.refreshSingleDevice(device);
|
||||
}
|
||||
}
|
||||
|
||||
public RefreshTask createRefreshTask(String task, Context context, GBDevice device) {
|
||||
return new RefreshTask(task, context, device);
|
||||
}
|
||||
|
||||
public class RefreshTask extends DBAccess {
|
||||
public RefreshTask(String task, Context context) {
|
||||
private final GBDevice device;
|
||||
|
||||
public RefreshTask(final String task, final Context context, final GBDevice device) {
|
||||
super(task, context);
|
||||
this.device = device;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInBackground(DBHandler db) {
|
||||
for (GBDevice gbDevice : deviceList) {
|
||||
final DeviceCoordinator coordinator = gbDevice.getDeviceCoordinator();
|
||||
boolean showActivityCard = GBApplication.getDevicePrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD, true);
|
||||
if (coordinator.supportsActivityTracking() && showActivityCard) {
|
||||
long[] stepsAndSleepData = getSteps(gbDevice, db);
|
||||
deviceActivityHashMap.put(gbDevice.getAddress(), stepsAndSleepData);
|
||||
protected void doInBackground(final DBHandler db) {
|
||||
if (device != null) {
|
||||
updateDevice(db, device);
|
||||
} else {
|
||||
for (GBDevice gbDevice : deviceList) {
|
||||
updateDevice(db, gbDevice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Object o) {
|
||||
refreshPairedDevices();
|
||||
private void updateDevice(final DBHandler db, final GBDevice gbDevice) {
|
||||
final DeviceCoordinator coordinator = gbDevice.getDeviceCoordinator();
|
||||
final boolean showActivityCard = GBApplication.getDevicePrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREFS_ACTIVITY_IN_DEVICE_CARD, true);
|
||||
if (coordinator.supportsActivityTracking() && showActivityCard) {
|
||||
final long[] stepsAndSleepData = getSteps(gbDevice, db);
|
||||
deviceActivityHashMap.put(gbDevice.getAddress(), stepsAndSleepData);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final Object o) {
|
||||
if (device != null) {
|
||||
refreshSingleDevice(device);
|
||||
} else {
|
||||
refreshPairedDevices();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ package nodomain.freeyourgadget.gadgetbridge.adapter;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
@ -143,7 +144,7 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
||||
private String expandedFolderName = "";
|
||||
private ViewGroup parent;
|
||||
private HashMap<String, long[]> deviceActivityMap = new HashMap();
|
||||
private StableIdGenerator idGenerator = new StableIdGenerator();
|
||||
private final StableIdGenerator idGenerator = new StableIdGenerator();
|
||||
|
||||
public GBDeviceAdapterv2(Context context, List<GBDevice> deviceList, HashMap<String,long[]> deviceMap) {
|
||||
super(new GBDeviceDiffUtil());
|
||||
@ -153,10 +154,22 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
||||
this.deviceActivityMap = deviceMap;
|
||||
}
|
||||
|
||||
public void rebuildFolders(){
|
||||
public void rebuildFolders() {
|
||||
this.devicesListWithFolders = enrichDeviceListWithFolder(deviceList);
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
public final void refreshSingleDevice(final GBDevice device) {
|
||||
final int i = devicesListWithFolders.indexOf(device);
|
||||
if (i > 0) {
|
||||
notifyItemChanged(i);
|
||||
} else {
|
||||
// Somehow the device was not on the list - rebuild everything
|
||||
rebuildFolders();
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private List<GBDevice> enrichDeviceListWithFolder(List<GBDevice> deviceList) {
|
||||
final Map<String, List<GBDevice>> devicesPerFolder = new LinkedHashMap<>();
|
||||
final List<GBDevice> enrichedList = new ArrayList<>();
|
||||
@ -221,10 +234,10 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
|
||||
holder.container.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(expandedFolderName.equals(folder.getName())){
|
||||
if (expandedFolderName.equals(folder.getName())){
|
||||
// collapse open folder
|
||||
expandedFolderName = "";
|
||||
}else {
|
||||
} else {
|
||||
expandedFolderName = folder.getName();
|
||||
}
|
||||
rebuildFolders();
|
||||
|
@ -297,7 +297,7 @@ public final class BtLEQueue {
|
||||
|
||||
private void setDeviceConnectionState(final State newState) {
|
||||
new Handler(Looper.getMainLooper()).post(() -> {
|
||||
LOG.debug("new device connection state: " + newState);
|
||||
LOG.debug("new device connection state: {}", newState);
|
||||
mGbDevice.setState(newState);
|
||||
mGbDevice.sendDeviceUpdateIntent(mContext, GBDevice.DeviceUpdateSubject.CONNECTION_STATE);
|
||||
});
|
||||
|
@ -668,6 +668,7 @@ class BangleJSActivityTrack {
|
||||
resetPacketCount();
|
||||
device.unsetBusyTask();
|
||||
device.sendDeviceUpdateIntent(context);
|
||||
GB.signalActivityDataFinish(device);
|
||||
GB.updateTransferNotification(null, "", false, 100, context);
|
||||
GB.toast(context.getString(R.string.activity_detail_end_label) + " : " + context.getString(R.string.busy_task_fetch_sports_details), Toast.LENGTH_SHORT, GB.INFO);
|
||||
}
|
||||
|
@ -495,8 +495,8 @@ public class CmfActivitySync {
|
||||
|
||||
activitiesWithGps.clear();
|
||||
|
||||
GB.signalActivityDataFinish();
|
||||
getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
}
|
||||
|
||||
|
@ -572,10 +572,10 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
private void fetchRecordedDataFinished() {
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
LOG.info("Sync finished!");
|
||||
getDevice().unsetBusyTask();
|
||||
getDevice().sendDeviceUpdateIntent(getContext());
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
|
||||
private void fetchHistoryActivity() {
|
||||
|
@ -365,7 +365,7 @@ public class FitProDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
public void indicateFinishedFetchingOperation() {
|
||||
//LOG.debug("download finish announced");
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
unsetBusy();
|
||||
}
|
||||
|
||||
@ -1354,7 +1354,7 @@ public class FitProDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
addGBActivitySample(sample);
|
||||
broadcastSample(sample);
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
|
||||
public void handleDayTotalsData(int steps, int distance, int calories) {
|
||||
|
@ -541,8 +541,8 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
||||
LOG.debug("No pending files to process");
|
||||
// No downloaded fit files to process
|
||||
if (gbDevice.isBusy() && isBusyFetching) {
|
||||
GB.signalActivityDataFinish();
|
||||
getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
getDevice().sendDeviceUpdateIntent(getContext());
|
||||
}
|
||||
@ -570,8 +570,8 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
GB.signalActivityDataFinish();
|
||||
getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
getDevice().sendDeviceUpdateIntent(getContext());
|
||||
isBusyFetching = false;
|
||||
@ -903,7 +903,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
||||
public void onFinish() {
|
||||
parsingFitFilesFromStorage = false;
|
||||
GB.updateTransferNotification("", "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ public abstract class AbstractFetchOperation extends AbstractHuamiOperation {
|
||||
LOG.debug("All operations finished");
|
||||
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
operationFinished();
|
||||
unsetBusy();
|
||||
}
|
||||
|
@ -810,7 +810,7 @@ public class HuaweiSupportProvider {
|
||||
|
||||
// Properly update the device card
|
||||
gbDevice.sendDeviceUpdateIntent(GBApplication.getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
|
||||
public void setProtocolVersion(byte protocolVersion) {
|
||||
@ -1289,7 +1289,7 @@ public class HuaweiSupportProvider {
|
||||
gbDevice.unsetBusyTask();
|
||||
gbDevice.sendDeviceUpdateIntent(context);
|
||||
}
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
|
||||
public void onReset(int flags) {
|
||||
@ -2118,7 +2118,7 @@ public class HuaweiSupportProvider {
|
||||
for (HuaweiTruSleepParser.TruSleepStatus status : results)
|
||||
addSleepActivity(status.startTime, status.endTime, (byte) 0x06, (byte) 0x0a);
|
||||
// This should only be called once per sync - also if we start downloading more sleep data
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
// Unsetting busy is done at the end of all downloads
|
||||
} // "sleep_data.bin" later as well
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ public class FetchActivityOperation extends AbstractMiBand1Operation {
|
||||
activityStruct = null;
|
||||
operationFinished();
|
||||
unsetBusy();
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -514,7 +514,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||
GB.updateTransferNotification(null,"", false, 100, getContext());
|
||||
if (getDevice().isBusy()) {
|
||||
getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
@ -70,7 +70,7 @@ class AppMessageHandlerMisfit extends AppMessageHandler {
|
||||
LOG.info("incoming data start");
|
||||
break;
|
||||
case KEY_INCOMING_DATA_END:
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
LOG.info("incoming data end");
|
||||
break;
|
||||
case KEY_INCOMING_DATA:
|
||||
|
@ -109,7 +109,7 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
for (Pair<Integer, Object> pair : pairs) {
|
||||
if (Objects.equals(pair.first, keyTransmit)) {
|
||||
ctrl_message |= CTRL_TRANSMIT_DONE;
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
} else if (pair.first.equals(keyGoneoff)) {
|
||||
alarm_gone_off = (int) pair.second;
|
||||
LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
|
||||
|
@ -2320,7 +2320,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
||||
devEvtsDataLogging = new GBDeviceEvent[]{dataLogging, null};
|
||||
}
|
||||
if (datalogSession.uuid.equals(UUID_ZERO) && (datalogSession.tag == 81 || datalogSession.tag == 83 || datalogSession.tag == 84)) {
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
mDatalogSessions.remove(id);
|
||||
}
|
||||
|
@ -1240,7 +1240,7 @@ public class PineTimeJFSupport extends AbstractBTLEDeviceSupport implements DfuL
|
||||
provider.addGBActivitySample(sample);
|
||||
}
|
||||
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
|
||||
} catch (Exception ex) {
|
||||
GB.toast(getContext(), "Error saving samples: " + ex.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
|
||||
|
@ -565,7 +565,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
|
||||
queueWrite(new FileDeleteRequest(getHandle()));
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDeviceSupport().getDevice());
|
||||
LOG.debug("Synchronized activity data");
|
||||
} catch (Exception ex) {
|
||||
GB.toast(getContext(), "Error saving steps data: " + ex.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
|
||||
|
@ -1251,7 +1251,7 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter {
|
||||
}
|
||||
queueWrite(new FileDeleteRequest(fileHandle));
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDeviceSupport().getDevice());
|
||||
LOG.debug("Synchronized activity data");
|
||||
} catch (Exception ex) {
|
||||
GB.toast(getContext(), "Error saving steps data: " + ex.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
|
||||
|
@ -468,7 +468,7 @@ public class MisfitWatchAdapter extends WatchAdapter {
|
||||
getDeviceSupport().getDevice().unsetBusyTask();
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDeviceSupport().getDevice());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -543,7 +543,7 @@ public class TLW64Support extends AbstractBTLEDeviceSupport {
|
||||
if (getDevice().isBusy()) {
|
||||
getDevice().unsetBusyTask();
|
||||
getDevice().sendDeviceUpdateIntent(getContext());
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
@ -146,8 +146,8 @@ public class XiaomiActivityFileFetcher {
|
||||
if (fileId == null) {
|
||||
LOG.debug("Nothing more to fetch");
|
||||
isFetching = false;
|
||||
GB.signalActivityDataFinish();
|
||||
mHealthService.getSupport().getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish(mHealthService.getSupport().getDevice());
|
||||
GB.updateTransferNotification(null, "", false, 100, mHealthService.getSupport().getContext());
|
||||
mHealthService.getSupport().getDevice().sendDeviceUpdateIntent(mHealthService.getSupport().getContext());
|
||||
return;
|
||||
|
@ -1033,6 +1033,8 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
getHeartRateData();
|
||||
} else if (availableSleepData > 0) {
|
||||
getSleepData();
|
||||
} else {
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1179,6 +1181,8 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
getHeartRateData();
|
||||
} else if (availableSleepData > 0) {
|
||||
getSleepData();
|
||||
} else {
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1217,7 +1221,7 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
GB.updateTransferNotification(null, "", false, 100, getContext());
|
||||
if (getDevice().isBusy()) {
|
||||
getDevice().unsetBusyTask();
|
||||
GB.signalActivityDataFinish();
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
if (!prefs.getBoolean(ZeTimeConstants.PREF_ZETIME_DONT_DEL_ACTDATA, false)) {
|
||||
deleteSleepData();
|
||||
@ -1283,6 +1287,8 @@ public class ZeTimeDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
}
|
||||
if (availableSleepData > 0) {
|
||||
getSleepData();
|
||||
} else {
|
||||
GB.signalActivityDataFinish(getDevice());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -677,8 +677,10 @@ public class GB {
|
||||
}
|
||||
}
|
||||
|
||||
public static void signalActivityDataFinish() {
|
||||
public static void signalActivityDataFinish(final GBDevice device) {
|
||||
final Intent intent = new Intent(GBApplication.ACTION_NEW_DATA);
|
||||
intent.putExtra(GBDevice.EXTRA_DEVICE, device);
|
||||
|
||||
LocalBroadcastManager.getInstance(GBApplication.getContext()).sendBroadcast(intent);
|
||||
|
||||
if (!GBApplication.getPrefs().getBoolean("intent_api_broadcast_activity_sync", false)) {
|
||||
|
Loading…
Reference in New Issue
Block a user