From 51867c5f3f4839437038a68a6da687528378d01d Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Sun, 20 Mar 2016 11:38:27 +0100 Subject: [PATCH] Notification for external file logging * Show notificatin when external file logging is active * Use column name constants for ServiceDB access --- .../main/java/at/bitfire/davdroid/App.java | 40 +++++++++++++++++-- .../java/at/bitfire/davdroid/Constants.java | 4 +- .../CalendarsSyncAdapterService.java | 11 ++--- .../ContactsSyncAdapterService.java | 2 +- .../syncadapter/TasksSyncAdapterService.java | 4 +- .../main/res/drawable/ic_sd_storage_light.xml | 17 ++++++++ app/src/main/res/values/strings.xml | 7 ++++ 7 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/drawable/ic_sd_storage_light.xml diff --git a/app/src/main/java/at/bitfire/davdroid/App.java b/app/src/main/java/at/bitfire/davdroid/App.java index ec370b60..a4abf7e1 100644 --- a/app/src/main/java/at/bitfire/davdroid/App.java +++ b/app/src/main/java/at/bitfire/davdroid/App.java @@ -9,8 +9,20 @@ package at.bitfire.davdroid; import android.accounts.Account; +import android.app.ActivityManager; import android.app.Application; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.ContentResolver; +import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.drawable.BitmapDrawable; +import android.net.Uri; +import android.os.*; +import android.os.Process; +import android.support.v4.app.ActivityManagerCompat; +import android.support.v7.app.NotificationCompat; import android.util.Log; import org.apache.commons.lang3.time.DateFormatUtils; @@ -98,23 +110,43 @@ public class App extends Application implements SharedPreferences.OnSharedPrefer // add logcat handler log.addHandler(LogcatHandler.INSTANCE); + NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // log to external file according to preferences if (logToFile) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + builder .setSmallIcon(R.drawable.ic_sd_storage_light) + .setLargeIcon(((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap()) + .setContentTitle(getString(R.string.logging_davdroid_file_logging)) + .setLocalOnly(true); + File dir = getExternalFilesDir(null); if (dir != null) try { String pattern = new File(dir, "davdroid%u-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd-HHmmss") + ".txt").toString(); - log.info("Logging to external file: " + pattern); FileHandler fileHandler = new FileHandler(pattern); fileHandler.setFormatter(PlainTextFormatter.DEFAULT); log.addHandler(fileHandler); + builder .setContentText(dir.getPath()) + .setSubText(getString(R.string.logging_to_external_storage_warning)) + .setCategory(NotificationCompat.CATEGORY_STATUS) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(getString(R.string.logging_to_external_storage, dir.getPath()))) + .setOngoing(true); + } catch (IOException e) { - log.log(Level.SEVERE, "Can't create external log file", e); + log.log(Level.SEVERE, "Couldn't create external log file", e); + + builder .setContentText(getString(R.string.logging_couldnt_create_file, e.getLocalizedMessage())) + .setCategory(NotificationCompat.CATEGORY_ERROR); } else - log.severe("No external media found, can't create external log file"); - } + builder.setContentText(getString(R.string.logging_no_external_storage)); + + nm.notify(Constants.NOTIFICATION_EXTERNAL_FILE_LOGGING, builder.build()); + } else + nm.cancel(Constants.NOTIFICATION_EXTERNAL_FILE_LOGGING); } } diff --git a/app/src/main/java/at/bitfire/davdroid/Constants.java b/app/src/main/java/at/bitfire/davdroid/Constants.java index f8095344..8e36efbf 100644 --- a/app/src/main/java/at/bitfire/davdroid/Constants.java +++ b/app/src/main/java/at/bitfire/davdroid/Constants.java @@ -16,8 +16,8 @@ public class Constants { // notification IDs public final static int - NOTIFICATION_ANDROID_VERSION_UPDATED = 0, - NOTIFICATION_ACCOUNT_SETTINGS_UPDATED = 1, + NOTIFICATION_ACCOUNT_SETTINGS_UPDATED = 0, + NOTIFICATION_EXTERNAL_FILE_LOGGING = 1, NOTIFICATION_CONTACTS_SYNC = 10, NOTIFICATION_CALENDAR_SYNC = 11, NOTIFICATION_TASK_SYNC = 12; diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarsSyncAdapterService.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarsSyncAdapterService.java index b6887fe1..cbef37ba 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarsSyncAdapterService.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/CalendarsSyncAdapterService.java @@ -28,8 +28,9 @@ import java.util.logging.Level; import at.bitfire.davdroid.App; import at.bitfire.davdroid.model.CollectionInfo; -import at.bitfire.davdroid.model.ServiceDB; +import at.bitfire.davdroid.model.ServiceDB.Collections; import at.bitfire.davdroid.model.ServiceDB.OpenHelper; +import at.bitfire.davdroid.model.ServiceDB.Services; import at.bitfire.davdroid.resource.LocalCalendar; import at.bitfire.ical4android.CalendarStorageException; import lombok.Cleanup; @@ -117,16 +118,16 @@ public class CalendarsSyncAdapterService extends Service { } long getService(Account account) { - @Cleanup Cursor c = db.query(ServiceDB.Services._TABLE, new String[]{ServiceDB.Services.ID}, - ServiceDB.Services.ACCOUNT_NAME + "=? AND " + ServiceDB.Services.SERVICE + "=?", new String[]{account.name, ServiceDB.Services.SERVICE_CALDAV}, null, null, null); + @Cleanup Cursor c = db.query(Services._TABLE, new String[]{ Services.ID }, + Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[]{account.name, Services.SERVICE_CALDAV}, null, null, null); c.moveToNext(); return c.getLong(0); } private Map remoteCalendars(long service) { Map collections = new LinkedHashMap<>(); - @Cleanup Cursor cursor = db.query(ServiceDB.Collections._TABLE, ServiceDB.Collections._COLUMNS, - ServiceDB.Collections.SERVICE_ID + "=? AND " + ServiceDB.Collections.SUPPORTS_VEVENT + "!=0 AND selected", + @Cleanup Cursor cursor = db.query(Collections._TABLE, Collections._COLUMNS, + Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VEVENT + "!=0 AND " + Collections.SYNC, new String[] { String.valueOf(service) }, null, null, null); while (cursor.moveToNext()) { ContentValues values = new ContentValues(); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncAdapterService.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncAdapterService.java index 538b866e..940e3ebf 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncAdapterService.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/ContactsSyncAdapterService.java @@ -94,7 +94,7 @@ public class ContactsSyncAdapterService extends Service { @Nullable private CollectionInfo remoteAddressBook(long service) { @Cleanup Cursor c = db.query(Collections._TABLE, Collections._COLUMNS, - Collections.SERVICE_ID + "=? AND selected", new String[] { String.valueOf(service) }, null, null, null); + Collections.SERVICE_ID + "=? AND " + Collections.SYNC, new String[] { String.valueOf(service) }, null, null, null); if (c.moveToNext()) { ContentValues values = new ContentValues(); DatabaseUtils.cursorRowToContentValues(c, values); diff --git a/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncAdapterService.java b/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncAdapterService.java index f29eb05b..8a102e7c 100644 --- a/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncAdapterService.java +++ b/app/src/main/java/at/bitfire/davdroid/syncadapter/TasksSyncAdapterService.java @@ -125,7 +125,7 @@ public class TasksSyncAdapterService extends Service { long getService(Account account) { @Cleanup Cursor c = db.query(Services._TABLE, new String[]{ Services.ID }, - Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[]{ account.name, Services.SERVICE_CALDAV }, null, null, null); + Services.ACCOUNT_NAME + "=? AND " + Services.SERVICE + "=?", new String[] { account.name, Services.SERVICE_CALDAV }, null, null, null); c.moveToNext(); return c.getLong(0); } @@ -133,7 +133,7 @@ public class TasksSyncAdapterService extends Service { private Map remoteTaskLists(long service) { Map collections = new LinkedHashMap<>(); @Cleanup Cursor cursor = db.query(Collections._TABLE, Collections._COLUMNS, - Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VTODO + "!=0 AND selected", + Collections.SERVICE_ID + "=? AND " + Collections.SUPPORTS_VTODO + "!=0 AND " + Collections.SYNC, new String[] { String.valueOf(service) }, null, null, null); while (cursor.moveToNext()) { ContentValues values = new ContentValues(); diff --git a/app/src/main/res/drawable/ic_sd_storage_light.xml b/app/src/main/res/drawable/ic_sd_storage_light.xml new file mode 100644 index 00000000..4532fd05 --- /dev/null +++ b/app/src/main/res/drawable/ic_sd_storage_light.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ca9b6adf..52ab524e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -36,6 +36,13 @@ able to synchronize task lists. You may have to re-install DAVdroid and then add your accounts again after installing OpenTasks. Install OpenTasks + + DAVdroid file logging + Logging to external storage: %s + Delete logs as soon as possible! + Couldn\'t create external log file: %s + External storage not found + Open navigation drawer Close navigation drawer