mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Convert DB export to WorkManager
This commit is contained in:
@@ -243,6 +243,7 @@ dependencies {
|
||||
implementation "androidx.activity:activity-ktx:1.10.1"
|
||||
implementation "androidx.fragment:fragment-ktx:1.8.8"
|
||||
implementation "androidx.viewpager2:viewpager2:1.1.0"
|
||||
implementation "androidx.work:work-runtime-ktx:2.10.3"
|
||||
|
||||
// Not latest version because of https://github.com/material-components/material-components-android/issues/3924
|
||||
implementation "com.google.android.material:material:1.12.0"
|
||||
|
||||
@@ -605,10 +605,6 @@
|
||||
<action android:name="nodomain.freeyourgadget.gadgetbridge.callcontrol" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".database.PeriodicExporter"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
<!--
|
||||
forcing the DebugActivity to portrait mode avoids crashes with the progress
|
||||
dialog when changing orientation
|
||||
|
||||
+1
-1
@@ -232,10 +232,10 @@ public class DataManagementActivity extends AbstractGBActivity {
|
||||
testExportDBButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
sendBroadcast(new Intent(context, PeriodicExporter.class));
|
||||
GB.toast(context,
|
||||
context.getString(R.string.activity_DB_test_export_message),
|
||||
Toast.LENGTH_SHORT, GB.INFO);
|
||||
PeriodicExporter.trigger();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.database
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication
|
||||
import nodomain.freeyourgadget.gadgetbridge.R
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import androidx.core.net.toUri
|
||||
|
||||
class DatabaseExportWorker(
|
||||
private val mContext: Context,
|
||||
workerParams: WorkerParameters
|
||||
) : Worker(mContext, workerParams) {
|
||||
override fun doWork(): Result {
|
||||
val dst = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null)
|
||||
|
||||
LOG.info("Starting DB export, dst={}", dst)
|
||||
|
||||
if (dst == null) {
|
||||
LOG.warn("Unable to export DB, export location not set")
|
||||
broadcastSuccess(false)
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
try {
|
||||
GBApplication.acquireDB().use { dbHandler ->
|
||||
val helper = DBHelper(mContext)
|
||||
mContext.contentResolver.openOutputStream(dst.toUri()).use { out ->
|
||||
helper.exportDB(dbHandler, out)
|
||||
}
|
||||
}
|
||||
|
||||
GBApplication.app().lastAutoExportTimestamp = System.currentTimeMillis()
|
||||
} catch (e: Exception) {
|
||||
GB.updateExportFailedNotification(mContext.getString(R.string.notif_export_failed_title), mContext)
|
||||
LOG.error("Exception while exporting DB", e)
|
||||
broadcastSuccess(false)
|
||||
return Result.failure()
|
||||
}
|
||||
|
||||
LOG.info("DB export completed")
|
||||
|
||||
broadcastSuccess(true)
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun broadcastSuccess(success: Boolean) {
|
||||
if (!GBApplication.getPrefs().getBoolean("intent_api_broadcast_export", false)) {
|
||||
return
|
||||
}
|
||||
|
||||
LOG.info("Broadcasting database export success={}", success)
|
||||
|
||||
val action: String = if (success) ACTION_DATABASE_EXPORT_SUCCESS else ACTION_DATABASE_EXPORT_FAIL
|
||||
val exportedNotifyIntent = Intent(action)
|
||||
mContext.sendBroadcast(exportedNotifyIntent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(DatabaseExportWorker::class.java)
|
||||
|
||||
const val ACTION_DATABASE_EXPORT_SUCCESS: String =
|
||||
"nodomain.freeyourgadget.gadgetbridge.action.DATABASE_EXPORT_SUCCESS"
|
||||
const val ACTION_DATABASE_EXPORT_FAIL: String =
|
||||
"nodomain.freeyourgadget.gadgetbridge.action.DATABASE_EXPORT_FAIL"
|
||||
}
|
||||
}
|
||||
-149
@@ -1,149 +0,0 @@
|
||||
/* Copyright (C) 2018-2024 Carsten Pfeiffer, Felix Konstantin Maurer,
|
||||
Ganblejs, José Rebelo, Petr Vaněk
|
||||
|
||||
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 <https://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.database;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
/**
|
||||
* Created by maufl on 1/4/18.
|
||||
*/
|
||||
|
||||
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();
|
||||
long autoExportScheduled = gbApp.getAutoExportScheduledTimestamp();
|
||||
boolean autoExportEnabled = prefs.getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false);
|
||||
Integer autoExportInterval = prefs.getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
||||
scheduleAlarm(context, autoExportInterval, autoExportEnabled && autoExportScheduled == 0);
|
||||
}
|
||||
|
||||
public static void scheduleAlarm(Context context, Integer autoExportInterval, boolean autoExportEnabled) {
|
||||
Intent i = new Intent(context, PeriodicExporter.class);
|
||||
i.setPackage(BuildConfig.APPLICATION_ID);
|
||||
PendingIntent pi = PendingIntentUtils.getBroadcast(context, 0, i, 0, false);
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
am.cancel(pi);
|
||||
if (!autoExportEnabled) {
|
||||
LOG.info("Not scheduling periodic export, either already scheduled or not enabled");
|
||||
return;
|
||||
}
|
||||
int exportPeriod = autoExportInterval * 60 * 60 * 1000;
|
||||
if (exportPeriod == 0) {
|
||||
LOG.info("Not scheduling periodic export, interval set to 0");
|
||||
return;
|
||||
}
|
||||
LOG.info("Scheduling periodic export");
|
||||
GBApplication gbApp = GBApplication.app();
|
||||
gbApp.setAutoExportScheduledTimestamp(System.currentTimeMillis() + exportPeriod);
|
||||
am.setInexactRepeating(
|
||||
AlarmManager.ELAPSED_REALTIME,
|
||||
SystemClock.elapsedRealtime() + exportPeriod,
|
||||
exportPeriod,
|
||||
pi
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
LOG.info("Received command to export DB");
|
||||
createRefreshTask("Export database", context).execute();
|
||||
}
|
||||
|
||||
protected RefreshTask createRefreshTask(String task, Context context) {
|
||||
return new RefreshTask(task, context);
|
||||
}
|
||||
|
||||
public class RefreshTask extends DBAccess {
|
||||
Context localContext;
|
||||
|
||||
public RefreshTask(String task, Context context) {
|
||||
super(task, context);
|
||||
localContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInBackground(DBHandler handler) {
|
||||
LOG.info("Exporting DB in a background thread");
|
||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||
DBHelper helper = new DBHelper(localContext);
|
||||
String dst = GBApplication.getPrefs().getString(GBPrefs.AUTO_EXPORT_LOCATION, null);
|
||||
if (dst == null) {
|
||||
LOG.warn("Unable to export DB, export location not set");
|
||||
broadcastSuccess(false);
|
||||
return;
|
||||
}
|
||||
Uri dstUri = Uri.parse(dst);
|
||||
try (OutputStream out = localContext.getContentResolver().openOutputStream(dstUri)) {
|
||||
helper.exportDB(dbHandler, out);
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/* Copyright (C) 2018-2024 Carsten Pfeiffer, Felix Konstantin Maurer,
|
||||
Ganblejs, José Rebelo, Petr Vaněk
|
||||
|
||||
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 <https://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.OneTimeWorkRequest
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object PeriodicExporter {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(PeriodicExporter::class.java)
|
||||
|
||||
private const val TAG = "exporter_db"
|
||||
|
||||
@JvmStatic
|
||||
fun enablePeriodicExport(context: Context) {
|
||||
val prefs: Prefs = GBApplication.getPrefs()
|
||||
val autoExportScheduled = GBApplication.app().autoExportScheduledTimestamp
|
||||
val autoExportEnabled = prefs.getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false)
|
||||
val autoExportInterval = prefs.getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0)
|
||||
scheduleAlarm(context, autoExportInterval, autoExportEnabled && autoExportScheduled == 0L)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun scheduleAlarm(
|
||||
context: Context,
|
||||
autoExportInterval: Int,
|
||||
autoExportEnabled: Boolean
|
||||
) {
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
workManager.cancelAllWorkByTag(TAG)
|
||||
|
||||
if (!autoExportEnabled) {
|
||||
LOG.info("Not scheduling periodic export, either already scheduled or not enabled")
|
||||
return
|
||||
}
|
||||
|
||||
if (autoExportInterval == 0) {
|
||||
LOG.info("Not scheduling periodic export, interval set to 0")
|
||||
return
|
||||
}
|
||||
|
||||
LOG.info("Scheduling periodic export for {}h in the future", autoExportInterval)
|
||||
|
||||
val exportPeriodMillis = autoExportInterval * 60 * 60 * 1000
|
||||
GBApplication.app().autoExportScheduledTimestamp = System.currentTimeMillis() + exportPeriodMillis
|
||||
|
||||
val exportRequest = PeriodicWorkRequestBuilder<DatabaseExportWorker>(
|
||||
autoExportInterval.toLong(),
|
||||
TimeUnit.HOURS
|
||||
).addTag(TAG).build()
|
||||
|
||||
workManager.enqueue(exportRequest)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun trigger() {
|
||||
val workManager = WorkManager.getInstance(GBApplication.getContext())
|
||||
val exportRequest = OneTimeWorkRequest.Builder(DatabaseExportWorker::class.java)
|
||||
.addTag(TAG)
|
||||
.build()
|
||||
workManager.enqueue(exportRequest)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user