1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Improve code, add notification

This commit is contained in:
Felix Konstantin Maurer 2018-01-07 12:50:59 +01:00 committed by Carsten Pfeiffer
parent 254afafa3e
commit 58e504e299
5 changed files with 57 additions and 6 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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();
}
/**

View File

@ -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);

View File

@ -311,6 +311,7 @@
<string name="notif_battery_low_percent">%1$s battery left: %2$s%%</string>
<string name="notif_battery_low_bigtext_last_charge_time">Last charge: %s \n</string>
<string name="notif_battery_low_bigtext_number_of_charges">Number of charges: %s</string>
<string name="notif_export_failed_title">Export database failed! Please check your settings.</string>
<string name="sleepchart_your_sleep">Your sleep</string>
<string name="weeksleepchart_sleep_a_week">Sleep a week</string>
<string name="weeksleepchart_today_sleep_description">Sleep today, target: %1$s</string>