From 0a9056747c738bb429c9d5a2824632d59e7713ee Mon Sep 17 00:00:00 2001 From: Steven Schoen Date: Tue, 3 Oct 2017 00:50:30 -0700 Subject: [PATCH] Fix download-related crash, use Timber for logging --- app/build.gradle | 5 +-- .../com/stevenschoen/putionew/Analytics.kt | 2 +- .../putionew/CrashlyticsTree.java | 31 +++++++++++++++++++ .../java/com/stevenschoen/putionew/Log.kt | 7 ----- .../stevenschoen/putionew/PutioApplication.kt | 7 +++++ .../putionew/files/DownloadFinishedService.kt | 14 ++++++--- .../putionew/files/FileDetailsLoader.kt | 6 ++-- app/src/main/res/layout/file_putio.xml | 2 +- build.gradle | 2 +- 9 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/stevenschoen/putionew/CrashlyticsTree.java delete mode 100644 app/src/main/java/com/stevenschoen/putionew/Log.kt diff --git a/app/build.gradle b/app/build.gradle index ebcc028a..9201f968 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,8 +26,8 @@ android { applicationId 'com.stevenschoen.putionew' minSdkVersion 19 targetSdkVersion 26 - versionCode 126 - versionName '4.2' + versionCode 128 + versionName '4.2.1' multiDexEnabled true } buildTypes { @@ -82,6 +82,7 @@ dependencies { compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; } + compile 'com.jakewharton.timber:timber:4.5.1' compile 'commons-io:commons-io:2.5' compile 'net.danlew:android.joda:2.9.9' compile 'io.reactivex.rxjava2:rxjava:2.1.4' diff --git a/app/src/main/java/com/stevenschoen/putionew/Analytics.kt b/app/src/main/java/com/stevenschoen/putionew/Analytics.kt index d646dfe9..61ba1bc6 100644 --- a/app/src/main/java/com/stevenschoen/putionew/Analytics.kt +++ b/app/src/main/java/com/stevenschoen/putionew/Analytics.kt @@ -86,7 +86,7 @@ class Analytics(context: Context) { private fun Bundle.addIdAndName(file: PutioFile) { putLong(FirebaseAnalytics.Param.ITEM_ID, file.id) - putString(FirebaseAnalytics.Param.ITEM_NAME, file.name) + putString(FirebaseAnalytics.Param.ITEM_NAME, file.name.take(100)) } private fun Bundle.logEvent(event: String) = putioApp.firebaseAnalytics.logEvent(event, this) diff --git a/app/src/main/java/com/stevenschoen/putionew/CrashlyticsTree.java b/app/src/main/java/com/stevenschoen/putionew/CrashlyticsTree.java new file mode 100644 index 00000000..59551d50 --- /dev/null +++ b/app/src/main/java/com/stevenschoen/putionew/CrashlyticsTree.java @@ -0,0 +1,31 @@ +package com.stevenschoen.putionew; + +import android.support.annotation.Nullable; +import android.util.Log; + +import com.crashlytics.android.Crashlytics; + +import timber.log.Timber; + +public class CrashlyticsTree extends Timber.Tree { + private static final String CRASHLYTICS_KEY_PRIORITY = "priority"; + private static final String CRASHLYTICS_KEY_TAG = "tag"; + private static final String CRASHLYTICS_KEY_MESSAGE = "message"; + + @Override + protected void log(int priority, @Nullable String tag, @Nullable String message, @Nullable Throwable t) { + if (priority == Log.VERBOSE || priority == Log.DEBUG || priority == Log.INFO) { + return; + } + + Crashlytics.setInt(CRASHLYTICS_KEY_PRIORITY, priority); + Crashlytics.setString(CRASHLYTICS_KEY_TAG, tag); + Crashlytics.setString(CRASHLYTICS_KEY_MESSAGE, message); + + if (t == null) { + Crashlytics.logException(new Exception(message)); + } else { + Crashlytics.logException(t); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/Log.kt b/app/src/main/java/com/stevenschoen/putionew/Log.kt deleted file mode 100644 index c5b2c5e7..00000000 --- a/app/src/main/java/com/stevenschoen/putionew/Log.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.stevenschoen.putionew - -import android.util.Log - -fun Any.log(message: String) { - Log.d("asdf", message) -} \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/PutioApplication.kt b/app/src/main/java/com/stevenschoen/putionew/PutioApplication.kt index aaf59901..6d88be3c 100644 --- a/app/src/main/java/com/stevenschoen/putionew/PutioApplication.kt +++ b/app/src/main/java/com/stevenschoen/putionew/PutioApplication.kt @@ -12,6 +12,7 @@ import com.stevenschoen.putionew.files.FileDownloadDatabase import com.stevenschoen.putionew.model.files.PutioFile import io.fabric.sdk.android.Fabric import net.danlew.android.joda.JodaTimeAndroid +import timber.log.Timber class PutioApplication : MultiDexApplication() { @@ -41,6 +42,12 @@ class PutioApplication : MultiDexApplication() { .build()) .build()) + if (BuildConfig.DEBUG) { + Timber.plant(Timber.DebugTree()) + } else { + Timber.plant(CrashlyticsTree()) + } + JodaTimeAndroid.init(this) if (Build.VERSION.SDK_INT >= 26) { diff --git a/app/src/main/java/com/stevenschoen/putionew/files/DownloadFinishedService.kt b/app/src/main/java/com/stevenschoen/putionew/files/DownloadFinishedService.kt index 8f0c4601..6576503f 100644 --- a/app/src/main/java/com/stevenschoen/putionew/files/DownloadFinishedService.kt +++ b/app/src/main/java/com/stevenschoen/putionew/files/DownloadFinishedService.kt @@ -9,6 +9,7 @@ import android.support.v4.app.JobIntentService import com.stevenschoen.putionew.PutioUtils import com.stevenschoen.putionew.analytics import com.stevenschoen.putionew.putioApp +import timber.log.Timber class DownloadFinishedService : JobIntentService() { @@ -32,10 +33,15 @@ class DownloadFinishedService : JobIntentService() { val downloadStatus = query.getInt(query.getColumnIndex(DownloadManager.COLUMN_STATUS)) if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL) { val fileDownloads = putioApp.fileDownloadDatabase.fileDownloadsDao() - fileDownloads.update(fileDownloads.getByDownloadIdSynchronous(downloadId)!!.apply { - status = FileDownload.Status.Downloaded - uri = downloadManager.getUriForDownloadedFile(downloadId).toString() - }) + val fileDownload = fileDownloads.getByDownloadIdSynchronous(downloadId) + if (fileDownload != null) { + fileDownloads.update(fileDownload.apply { + status = FileDownload.Status.Downloaded + uri = downloadManager.getUriForDownloadedFile(downloadId).toString() + }) + } else { + Timber.w("Got a finished download (ID $downloadId), but no matching FileDownload") + } putioApp.fileDownloadDatabase.fileDownloadsDao() .getByDownloadIdOnce(downloadId) diff --git a/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsLoader.kt b/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsLoader.kt index 6cbbb087..5a7f30af 100644 --- a/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsLoader.kt +++ b/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsLoader.kt @@ -7,9 +7,9 @@ import android.support.v4.app.LoaderManager import android.support.v4.content.Loader import com.stevenschoen.putionew.PutioBaseLoader import com.stevenschoen.putionew.getUniqueLoaderId -import com.stevenschoen.putionew.log import com.stevenschoen.putionew.model.files.PutioFile import com.stevenschoen.putionew.putioApp +import timber.log.Timber class FileDetailsLoader(context: Context, private val file: PutioFile) : PutioBaseLoader(context) { @@ -19,10 +19,10 @@ class FileDetailsLoader(context: Context, private val file: PutioFile) : PutioBa val isDownloaded = it.status == FileDownload.Status.Downloaded val isDownloading = it.status == FileDownload.Status.InProgress if ((isDownloaded || isDownloading) && !isFileDownloadedOrDownloading(context, it)) { - log("${file.name} appears to not be downloaded, marking NotDownloaded") + Timber.d("${file.name} appears to not be downloaded, marking NotDownloaded") markFileNotDownloaded(context, it) } else if (isDownloading && isFileDownloaded(context, it)) { - log("${file.name} appears to be downloaded, marking Downloaded") + Timber.d("${file.name} appears to be downloaded, marking Downloaded") markFileDownloaded(context, it) } /* diff --git a/app/src/main/res/layout/file_putio.xml b/app/src/main/res/layout/file_putio.xml index e3db5f00..14d6c15f 100644 --- a/app/src/main/res/layout/file_putio.xml +++ b/app/src/main/res/layout/file_putio.xml @@ -3,7 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="64dp" - android:background="?attr/listChoiceBackgroundIndicator" + android:background="?listChoiceBackgroundIndicator" android:focusable="true" android:gravity="center_vertical" android:orientation="horizontal" diff --git a/build.gradle b/build.gradle index ab7f43f4..7075c431 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.github.ben-manes:gradle-versions-plugin:0.15.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.google.gms:google-services:3.1.0' + classpath 'com.google.gms:google-services:3.1.1' } } allprojects {