1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-05 01:37:03 +01:00

Widget: some cleanups

- use LocalBroadcastManager when broadcasting new data event
- use constants for actions everywhere and move them
This commit is contained in:
Andreas Shimokawa 2019-09-01 22:09:09 +02:00
parent 957d441859
commit 8eb494ab85
3 changed files with 15 additions and 13 deletions

View File

@ -112,6 +112,7 @@ public class GBApplication extends Application {
public static final String ACTION_QUIT public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit";
public static final String ACTION_LANGUAGE_CHANGE = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.language_change"; public static final String ACTION_LANGUAGE_CHANGE = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.language_change";
public static final String ACTION_NEW_DATA = "nodomain.freeyourgadget.gadgetbridge.action.new_data";
private static GBApplication app; private static GBApplication app;

View File

@ -42,15 +42,14 @@ import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DailyTotals; import nodomain.freeyourgadget.gadgetbridge.model.DailyTotals;
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class Widget extends AppWidgetProvider { public class Widget extends AppWidgetProvider {
public static final String WIDGET_CLICK = "nodomain.freeyourgadget.gadgetbridge.WidgetClick"; public static final String WIDGET_CLICK = "nodomain.freeyourgadget.gadgetbridge.WidgetClick";
public static final String NEW_DATA_ACTION = "nodomain.freeyourgadget.gadgetbridge.NewDataTrigger";
public static final String APPWIDGET_DELETED = "nodomain.freeyourgadget.gadgetbridge.APPWIDGET_DELETED"; public static final String APPWIDGET_DELETED = "nodomain.freeyourgadget.gadgetbridge.APPWIDGET_DELETED";
public static final String ACTION_DEVICE_CHANGED = "nodomain.freeyourgadget.gadgetbridge.gbdevice.action.device_changed";
private static final Logger LOG = LoggerFactory.getLogger(Widget.class); private static final Logger LOG = LoggerFactory.getLogger(Widget.class);
static BroadcastReceiver broadcastReceiver = null; static BroadcastReceiver broadcastReceiver = null;
@ -181,9 +180,9 @@ public class Widget extends AppWidgetProvider {
@Override @Override
public void onEnabled(Context context) { public void onEnabled(Context context) {
if (this.broadcastReceiver == null) { if (broadcastReceiver == null) {
LOG.debug("gbwidget BROADCAST receiver initialized."); LOG.debug("gbwidget BROADCAST receiver initialized.");
this.broadcastReceiver = new BroadcastReceiver() { broadcastReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
LOG.debug("gbwidget BROADCAST, action" + intent.getAction()); LOG.debug("gbwidget BROADCAST, action" + intent.getAction());
@ -191,18 +190,18 @@ public class Widget extends AppWidgetProvider {
} }
}; };
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(NEW_DATA_ACTION); intentFilter.addAction(GBApplication.ACTION_NEW_DATA);
intentFilter.addAction(ACTION_DEVICE_CHANGED); intentFilter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(context).registerReceiver(this.broadcastReceiver, intentFilter); LocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, intentFilter);
} }
} }
@Override @Override
public void onDisabled(Context context) { public void onDisabled(Context context) {
if (this.broadcastReceiver != null) { if (broadcastReceiver != null) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this.broadcastReceiver); AndroidUtils.safeUnregisterBroadcastReceiver(context,broadcastReceiver);
this.broadcastReceiver=null; broadcastReceiver = null;
} }
} }
@ -212,7 +211,7 @@ public class Widget extends AppWidgetProvider {
LOG.debug("gbwidget LOCAL onReceive, action: " + intent.getAction()); LOG.debug("gbwidget LOCAL onReceive, action: " + intent.getAction());
//this handles widget re-connection after apk updates //this handles widget re-connection after apk updates
if (WIDGET_CLICK.equals(intent.getAction())) { if (WIDGET_CLICK.equals(intent.getAction())) {
if (this.broadcastReceiver == null) { if (broadcastReceiver == null) {
onEnabled(context); onEnabled(context);
} }
refreshData(); refreshData();

View File

@ -25,6 +25,7 @@ import android.widget.Toast;
import androidx.annotation.CallSuper; import androidx.annotation.CallSuper;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -127,8 +128,9 @@ public abstract class AbstractFetchOperation extends AbstractHuamiOperation {
GB.updateTransferNotification(null, "", false, 100, getContext()); GB.updateTransferNotification(null, "", false, 100, getContext());
operationFinished(); operationFinished();
unsetBusy(); unsetBusy();
Intent intent = new Intent("nodomain.freeyourgadget.gadgetbridge.NewDataTrigger");
getContext().sendBroadcast(intent); Intent intent = new Intent(GBApplication.ACTION_NEW_DATA);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
} }
/** /**