1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 02:25:50 +01:00

Add Intent API to trigger activity sync and DB export

This commit is contained in:
José Rebelo 2022-10-29 00:28:45 +01:00 committed by Gitea
parent 6d35ca3ec0
commit 4a0e67cb30
5 changed files with 135 additions and 6 deletions

View File

@ -42,6 +42,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class PeriodicExporter extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(PeriodicExporter.class);
public static final String ACTION_DATABASE_EXPORT_SUCCESS = "nodomain.freeyourgadget.gadgetbridge.action.DATABASE_EXPORT_SUCCESS";
public static final String ACTION_DATABASE_EXPORT_FAIL = "nodomain.freeyourgadget.gadgetbridge.action.DATABASE_EXPORT_FAIL";
public static void enablePeriodicExport(Context context) {
Prefs prefs = GBApplication.getPrefs();
GBApplication gbApp = GBApplication.app();
@ -101,7 +104,8 @@ public class PeriodicExporter extends BroadcastReceiver {
DBHelper helper = new DBHelper(localContext);
String dst = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null);
if (dst == null) {
LOG.info("Unable to export DB, export location not set");
LOG.warn("Unable to export DB, export location not set");
broadcastSuccess(false);
return;
}
Uri dstUri = Uri.parse(dst);
@ -110,12 +114,29 @@ public class PeriodicExporter extends BroadcastReceiver {
GBApplication gbApp = GBApplication.app();
gbApp.setLastAutoExportTimestamp(System.currentTimeMillis());
}
broadcastSuccess(true);
LOG.info("DB export completed");
} catch (Exception ex) {
GB.updateExportFailedNotification(localContext.getString(R.string.notif_export_failed_title), localContext);
LOG.info("Exception while exporting DB: ", ex);
broadcastSuccess(false);
}
}
private void broadcastSuccess(final boolean success) {
if (!GBApplication.getPrefs().getBoolean("intent_api_broadcast_export", false)) {
return;
}
LOG.info("Broadcasting database export success={}", success);
final String action = success ? ACTION_DATABASE_EXPORT_SUCCESS : ACTION_DATABASE_EXPORT_FAIL;
final Intent exportedNotifyIntent = new Intent(action);
localContext.sendBroadcast(exportedNotifyIntent);
}
@Override
protected void onPostExecute(Object o) {
}

View File

@ -0,0 +1,78 @@
/* Copyright (C) 2022 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter;
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class IntentApiReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(IntentApiReceiver.class);
public static final String COMMAND_ACTIVITY_SYNC = "nodomain.freeyourgadget.gadgetbridge.command.ACTIVITY_SYNC";
public static final String COMMAND_TRIGGER_EXPORT = "nodomain.freeyourgadget.gadgetbridge.command.TRIGGER_EXPORT";
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction() == null) {
LOG.warn("Action is null");
return;
}
final Prefs prefs = GBApplication.getPrefs();
switch (intent.getAction()) {
case COMMAND_ACTIVITY_SYNC:
if (!prefs.getBoolean("intent_api_allow_activity_sync", false)) {
LOG.warn("Intent API activity sync trigger not allowed");
return;
}
LOG.info("Triggering activity sync");
GBApplication.deviceService().onFetchRecordedData(RecordedDataTypes.TYPE_ACTIVITY);
break;
case COMMAND_TRIGGER_EXPORT:
if (!prefs.getBoolean("intent_api_allow_trigger_export", false)) {
LOG.warn("Intent API export trigger not allowed");
return;
}
LOG.info("Triggering export");
final Intent exportIntent = new Intent(context, PeriodicExporter.class);
context.sendBroadcast(exportIntent);
break;
}
}
public IntentFilter buildFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(COMMAND_ACTIVITY_SYNC);
intentFilter.addAction(COMMAND_TRIGGER_EXPORT);
return intentFilter;
}
}

View File

@ -61,6 +61,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.CMWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.DeviceSettingsReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.GenericWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.IntentApiReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.LineageOsWeatherReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.OmniJawsObserver;
@ -328,6 +329,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private GenericWeatherReceiver mGenericWeatherReceiver = null;
private OmniJawsObserver mOmniJawsObserver = null;
private final DeviceSettingsReceiver deviceSettingsReceiver = new DeviceSettingsReceiver();
private final IntentApiReceiver intentApiReceiver = new IntentApiReceiver();
private final String[] mMusicActions = {
"com.android.music.metachanged",
@ -496,6 +498,8 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
final IntentFilter deviceSettingsIntentFilter = new IntentFilter();
deviceSettingsIntentFilter.addAction(DeviceSettingsReceiver.COMMAND);
registerReceiver(deviceSettingsReceiver, deviceSettingsIntentFilter);
registerReceiver(intentApiReceiver, intentApiReceiver.buildFilter());
}
private DeviceSupportFactory getDeviceSupportFactory() {
@ -1295,6 +1299,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
unregisterReceiver(bluetoothCommandReceiver);
unregisterReceiver(deviceSettingsReceiver);
unregisterReceiver(intentApiReceiver);
}
@Override

View File

@ -271,6 +271,7 @@
<string name="pref_title_canned_messages_dismisscall">Call Dismissal</string>
<string name="pref_title_canned_messages_set">Update on device</string>
<string name="pref_header_development">Developer options</string>
<string name="pref_header_intent_api">Intent API</string>
<string name="pref_header_authentication">Authentication</string>
<string name="pref_title_development_miaddr">Mi Band address</string>
<string name="pref_title_pebble_settings">Pebble settings</string>
@ -1973,6 +1974,12 @@
<string name="devicetype_flipper_zero">Flipper zero</string>
<string name="activity_prefs_allow_bluetooth_intent_api">Bluetooth Intent API</string>
<string name="activity_prefs_summary_allow_bluetooth_intent_api">Allow controlling Bluetooth connection via Intent API</string>
<string name="intent_api_allow_activity_sync_title">Allow activity sync trigger</string>
<string name="intent_api_allow_activity_sync_summary">Allow triggering activity sync via Intent API</string>
<string name="intent_api_allow_trigger_export_title">Allow database export</string>
<string name="intent_api_allow_trigger_export_summary">Allow triggering database export via Intent API</string>
<string name="intent_api_broadcast_export_title">Broadcast on database export</string>
<string name="intent_api_broadcast_export_summary">Broadcast an intent when database export finishes</string>
<string name="devicetype_super_cars">Shell Racing</string>
<string name="supercars_turbo_speed_label">Turbo Speed</string>
<string name="supercars_lights_label">Lights</string>

View File

@ -324,16 +324,15 @@
<PreferenceCategory
android:title="@string/pref_header_auto_export">
<Preference
android:key="auto_export_location"
android:title="@string/pref_title_auto_export_location"
android:summary="%s" />
<CheckBoxPreference
android:layout="@layout/preference_checkbox"
android:defaultValue="false"
android:key="auto_export_enabled"
android:title="@string/pref_title_auto_export_enabled" />
<Preference
android:dependency="auto_export_enabled"
android:key="auto_export_location"
android:title="@string/pref_title_auto_export_location"
android:summary="%s" />
<EditTextPreference
android:dependency="auto_export_enabled"
android:inputType="number"
@ -385,11 +384,30 @@
<Preference
android:key="pref_discovery_pairing"
android:title="@string/activity_prefs_discovery_pairing" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_intent_api"
android:title="@string/pref_header_intent_api">
<CheckBoxPreference
android:key="prefs_key_allow_bluetooth_intent_api"
android:title="@string/activity_prefs_allow_bluetooth_intent_api"
android:summary="@string/activity_prefs_summary_allow_bluetooth_intent_api" />
<CheckBoxPreference
android:key="intent_api_allow_activity_sync"
android:title="@string/intent_api_allow_activity_sync_title"
android:summary="@string/intent_api_allow_activity_sync_summary" />
<CheckBoxPreference
android:key="intent_api_allow_trigger_export"
android:title="@string/intent_api_allow_trigger_export_title"
android:summary="@string/intent_api_allow_trigger_export_summary" />
<CheckBoxPreference
android:key="intent_api_broadcast_export"
android:title="@string/intent_api_broadcast_export_title"
android:summary="@string/intent_api_broadcast_export_summary" />
</PreferenceCategory>
</PreferenceScreen>