From 58e504e2999ecb29584ead1856210d4526f05f80 Mon Sep 17 00:00:00 2001 From: Felix Konstantin Maurer Date: Sun, 7 Jan 2018 12:50:59 +0100 Subject: [PATCH] Improve code, add notification --- .../activities/SettingsActivity.java | 6 ++-- .../database/PeriodicExporter.java | 7 ++-- .../gadgetbridge/util/FileUtils.java | 16 ++++++++- .../freeyourgadget/gadgetbridge/util/GB.java | 33 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java index 7d499901e..16a10fed9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -57,6 +57,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActi import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM; @@ -351,15 +352,14 @@ public class SettingsActivity extends AbstractSettingsActivity { Cursor cursor = getContentResolver().query( uri, - new String[] { DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_SUMMARY }, + new String[] { DocumentsContract.Document.COLUMN_DISPLAY_NAME }, null, null, null, null ); if (cursor == null || ! cursor.moveToFirst()) { + LOG.warn("Unable to fetch information on URI " + uri.toString()); return; } String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); - String summary = cursor.getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_SUMMARY)); - LOG.info(displayName + " " + summary); findPreference("export_location").setSummary(displayName); boolean autoExportEnabled = GBApplication .getPrefs().getBoolean("auto_export_enabled", false); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/PeriodicExporter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/PeriodicExporter.java index 7ec4eb376..e8ef33f65 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/PeriodicExporter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/PeriodicExporter.java @@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory; import java.io.OutputStream; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.util.GB; /** * Created by maufl on 1/4/18. @@ -31,7 +33,7 @@ public class PeriodicExporter extends BroadcastReceiver { if (!autoExportEnabled) { return; } - int exportPeriod = autoExportPeriod * 1000;// * 60 * 60 * 1000; + int exportPeriod = autoExportPeriod * 60 * 60 * 1000; if (exportPeriod == 0) { return; } @@ -51,13 +53,14 @@ public class PeriodicExporter extends BroadcastReceiver { DBHelper helper = new DBHelper(context); String dst = GBApplication.getPrefs().getString("export_location", null); if (dst == null) { - LOG.info("Unable to export DB, export locatio not set"); + LOG.info("Unable to export DB, export location not set"); return; } Uri dstUri = Uri.parse(dst); OutputStream out = context.getContentResolver().openOutputStream(dstUri); helper.exportDB(dbHandler, out); } catch (Exception ex) { + GB.updateExportFailedNotification(context.getString(R.string.notif_export_failed_title), context); LOG.info("Exception while exporting DB: ", ex); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java index c9d1722db..891a0c35e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -85,6 +85,12 @@ public class FileUtils { } } + /** + * Copies the contents of the given file to the destination output stream. + * @param src the file from which to read. + * @param dst the output stream that is written to. Note: the caller has to close the output stream! + * @throws IOException + */ public static void copyFileToStream(File src, OutputStream dst) throws IOException { try (FileInputStream in = new FileInputStream(src)) { byte[] buf = new byte[4096]; @@ -111,6 +117,14 @@ public class FileUtils { } } + /** + * Copies the content of a file to an uri, + * which for example was retrieved using the storage access framework. + * @param context the application context. + * @param src the file from which the content should be copied. + * @param dst the destination uri. + * @throws IOException + */ public static void copyFileToURI(Context context, File src, Uri dst) throws IOException { OutputStream out = context.getContentResolver().openOutputStream(dst); if (out == null) { @@ -118,8 +132,8 @@ public class FileUtils { } try (OutputStream bufOut = new BufferedOutputStream(out)) { copyFileToStream(src, bufOut); - bufOut.close(); } + out.close(); } /** diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java index 47aedf82e..7837e3026 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/GB.java @@ -44,6 +44,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBEnvironment; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2; +import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; @@ -54,6 +55,7 @@ public class GB { public static final int NOTIFICATION_ID_INSTALL = 2; public static final int NOTIFICATION_ID_LOW_BATTERY = 3; public static final int NOTIFICATION_ID_TRANSFER = 4; + public static final int NOTIFICATION_ID_EXPORT_FAILED = 5; private static final Logger LOG = LoggerFactory.getLogger(GB.class); public static final int INFO = 1; @@ -420,6 +422,37 @@ public class GB { removeNotification(NOTIFICATION_ID_LOW_BATTERY, context); } + public static Notification createExportFailedNotification(String text, Context context) { + Intent notificationIntent = new Intent(context, SettingsActivity.class); + notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_CLEAR_TASK); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, + notificationIntent, 0); + + NotificationCompat.Builder nb = new NotificationCompat.Builder(context) + .setContentTitle(context.getString(R.string.notif_export_failed_title)) + .setContentText(text) + .setContentIntent(pendingIntent) + .setSmallIcon(R.drawable.ic_notification) + .setPriority(Notification.PRIORITY_HIGH) + .setOngoing(false); + + return nb.build(); + } + + public static void updateExportFailedNotification(String text, Context context) { + if (GBEnvironment.env().isLocalTest()) { + return; + } + Notification notification = createExportFailedNotification(text, context); + updateNotification(notification, NOTIFICATION_ID_EXPORT_FAILED, context); + } + + public static void removeExportFailedNotification(Context context) { + removeNotification(NOTIFICATION_ID_EXPORT_FAILED, context); + } + + public static void assertThat(boolean condition, String errorMessage) { if (!condition) { throw new AssertionError(errorMessage); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dbb455233..a4147f41f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -311,6 +311,7 @@ %1$s battery left: %2$s%% Last charge: %s \n Number of charges: %s + Export database failed! Please check your settings. Your sleep Sleep a week Sleep today, target: %1$s