2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2018-2020 Carsten Pfeiffer, Felix Konstantin Maurer
|
2018-02-26 14:27:32 +01:00
|
|
|
|
|
|
|
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/>. */
|
2018-01-04 15:13:06 +01:00
|
|
|
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;
|
2022-10-09 14:53:04 +02:00
|
|
|
import android.os.Build;
|
2018-01-04 15:13:06 +01:00
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2018-01-07 12:50:59 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2018-01-08 11:27:28 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
2022-10-09 14:53:04 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
|
2018-01-08 12:07:39 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2018-01-04 15:13:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by maufl on 1/4/18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class PeriodicExporter extends BroadcastReceiver {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(PeriodicExporter.class);
|
|
|
|
|
2022-10-29 01:28:45 +02:00
|
|
|
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";
|
|
|
|
|
2018-01-08 12:07:39 +01:00
|
|
|
public static void enablePeriodicExport(Context context) {
|
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
2022-02-27 18:43:53 +01:00
|
|
|
GBApplication gbApp = GBApplication.app();
|
|
|
|
long autoExportScheduled = gbApp.getAutoExportScheduledTimestamp();
|
2018-01-08 12:07:39 +01:00
|
|
|
boolean autoExportEnabled = prefs.getBoolean(GBPrefs.AUTO_EXPORT_ENABLED, false);
|
|
|
|
Integer autoExportInterval = prefs.getInt(GBPrefs.AUTO_EXPORT_INTERVAL, 0);
|
2022-02-27 18:43:53 +01:00
|
|
|
scheduleAlarm(context, autoExportInterval, autoExportEnabled && autoExportScheduled == 0);
|
2018-01-08 12:07:39 +01:00
|
|
|
}
|
2018-01-04 15:13:06 +01:00
|
|
|
|
2022-02-27 18:43:53 +01:00
|
|
|
public static void scheduleAlarm(Context context, Integer autoExportInterval, boolean autoExportEnabled) {
|
2018-01-04 15:13:06 +01:00
|
|
|
Intent i = new Intent(context, PeriodicExporter.class);
|
2022-10-09 14:53:04 +02:00
|
|
|
PendingIntent pi = PendingIntentUtils.getBroadcast(context, 0, i, 0, false);
|
2018-01-04 15:13:06 +01:00
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
am.cancel(pi);
|
|
|
|
if (!autoExportEnabled) {
|
2022-02-27 18:43:53 +01:00
|
|
|
LOG.info("Not scheduling periodic export, either already scheduled or not enabled");
|
2018-01-04 15:13:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-08 12:07:39 +01:00
|
|
|
int exportPeriod = autoExportInterval * 60 * 60 * 1000;
|
2018-01-04 15:13:06 +01:00
|
|
|
if (exportPeriod == 0) {
|
2022-02-27 18:43:53 +01:00
|
|
|
LOG.info("Not scheduling periodic export, interval set to 0");
|
2018-01-04 15:13:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-02-27 18:43:53 +01:00
|
|
|
LOG.info("Scheduling periodic export");
|
|
|
|
GBApplication gbApp = GBApplication.app();
|
|
|
|
gbApp.setAutoExportScheduledTimestamp(System.currentTimeMillis() + exportPeriod);
|
2018-01-04 15:13:06 +01:00
|
|
|
am.setInexactRepeating(
|
|
|
|
AlarmManager.ELAPSED_REALTIME,
|
|
|
|
SystemClock.elapsedRealtime() + exportPeriod,
|
|
|
|
exportPeriod,
|
|
|
|
pi
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2022-02-27 18:43:53 +01:00
|
|
|
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) {
|
2022-10-29 01:28:45 +02:00
|
|
|
LOG.warn("Unable to export DB, export location not set");
|
|
|
|
broadcastSuccess(false);
|
2022-02-27 18:43:53 +01:00
|
|
|
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());
|
|
|
|
}
|
2022-10-29 01:28:45 +02:00
|
|
|
|
|
|
|
broadcastSuccess(true);
|
|
|
|
|
|
|
|
LOG.info("DB export completed");
|
2022-02-27 18:43:53 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
GB.updateExportFailedNotification(localContext.getString(R.string.notif_export_failed_title), localContext);
|
|
|
|
LOG.info("Exception while exporting DB: ", ex);
|
2022-10-29 01:28:45 +02:00
|
|
|
broadcastSuccess(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void broadcastSuccess(final boolean success) {
|
|
|
|
if (!GBApplication.getPrefs().getBoolean("intent_api_broadcast_export", false)) {
|
|
|
|
return;
|
2018-02-19 21:41:24 +01:00
|
|
|
}
|
2022-10-29 01:28:45 +02:00
|
|
|
|
|
|
|
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);
|
2022-02-27 18:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Object o) {
|
2018-01-04 15:13:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|