diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt index c457c698..350dd232 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt @@ -31,6 +31,7 @@ import com.dp.logcatapp.fragments.logcatlive.dialogs.* import com.dp.logcatapp.fragments.shared.dialogs.CopyToClipboardDialogFragment import com.dp.logcatapp.fragments.shared.dialogs.FilterExclusionDialogFragment import com.dp.logcatapp.services.LogcatService +import com.dp.logcatapp.services.getService import com.dp.logcatapp.util.* import com.dp.logcatapp.views.IndeterminateProgressSnackBar import com.dp.logger.Logger @@ -458,7 +459,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen val logcat = it.logcat if (recording) { Snackbar.make(view!!, getString(R.string.started_recording), - Snackbar.LENGTH_SHORT) + Snackbar.LENGTH_SHORT) .show() logcat.startRecording() } else { @@ -572,7 +573,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen override fun onServiceConnected(name: ComponentName, service: IBinder) { Logger.debug(LogcatLiveFragment::class, "onServiceConnected") - logcatService = (service as LogcatService.LocalBinder).getLogcatService() + logcatService = service.getService() val logcat = logcatService!!.logcat logcat.pause() // resume on updateFilters callback diff --git a/app/src/main/java/com/dp/logcatapp/services/BaseService.kt b/app/src/main/java/com/dp/logcatapp/services/BaseService.kt index f2e3d91d..624a931c 100644 --- a/app/src/main/java/com/dp/logcatapp/services/BaseService.kt +++ b/app/src/main/java/com/dp/logcatapp/services/BaseService.kt @@ -1,11 +1,16 @@ package com.dp.logcatapp.services -import android.app.Service +import android.content.Intent import android.content.SharedPreferences +import android.os.Binder +import android.os.IBinder +import androidx.lifecycle.LifecycleService import com.dp.logcatapp.util.getDefaultSharedPreferences import com.dp.logcatapp.util.setTheme -abstract class BaseService : Service(), SharedPreferences.OnSharedPreferenceChangeListener { +abstract class BaseService : LifecycleService(), SharedPreferences.OnSharedPreferenceChangeListener { + private val localBinder = LocalBinder() + override fun onCreate() { setTheme() super.onCreate() @@ -21,6 +26,18 @@ abstract class BaseService : Service(), SharedPreferences.OnSharedPreferenceChan super.onDestroy() } + override fun onBind(intent: Intent): IBinder? { + super.onBind(intent) + return localBinder + } + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { } -} \ No newline at end of file + + inner class LocalBinder : Binder() { + @Suppress("UNCHECKED_CAST") + fun getService() = this@BaseService as T + } +} + +inline fun IBinder.getService() = (this as BaseService.LocalBinder).getService() \ No newline at end of file diff --git a/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt b/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt index 29f24aa2..04c0f3f4 100644 --- a/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt +++ b/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt @@ -9,7 +9,6 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.graphics.BitmapFactory -import android.os.Binder import android.os.Build import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat @@ -29,7 +28,6 @@ class LogcatService : BaseService() { private const val NOTIFICATION_ID = 1 } - private val localBinder = LocalBinder() lateinit var logcat: Logcat private set var restartedLogcat = false @@ -60,6 +58,7 @@ class LogcatService : BaseService() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + super.onStartCommand(intent, flags, startId) startForeground(NOTIFICATION_ID, createNotification(recording)) return START_STICKY } @@ -82,7 +81,7 @@ class LogcatService : BaseService() { PendingIntent.FLAG_UPDATE_CURRENT) val exitAction = NotificationCompat.Action.Builder(R.drawable.ic_clear_white_18dp, - getString(R.string.exit), exitPendingIntent) + getString(R.string.exit), exitPendingIntent) .build() val builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL) @@ -105,7 +104,7 @@ class LogcatService : BaseService() { val stopRecordingPendingIntent = PendingIntent.getActivity(this, 2, stopRecordingIntent, PendingIntent.FLAG_UPDATE_CURRENT) val stopRecordingAction = NotificationCompat.Action.Builder(R.drawable.ic_stop_white_18dp, - getString(R.string.stop_recording), stopRecordingPendingIntent) + getString(R.string.stop_recording), stopRecordingPendingIntent) .build() builder.addAction(stopRecordingAction) @@ -134,8 +133,6 @@ class LogcatService : BaseService() { nm.deleteNotificationChannel(NOTIFICATION_CHANNEL) } - override fun onBind(intent: Intent?) = localBinder - override fun onDestroy() { super.onDestroy() logcat.close() @@ -169,8 +166,7 @@ class LogcatService : BaseService() { } private fun handleBufferUpdate(sharedPreferences: SharedPreferences, key: String) { - val bufferValues = sharedPreferences.getStringSet(key, - PreferenceKeys.Logcat.Default.BUFFERS)!! + val bufferValues = sharedPreferences.getStringSet(key, PreferenceKeys.Logcat.Default.BUFFERS)!! val buffers = Logcat.AVAILABLE_BUFFERS showToast(getString(R.string.restarting_logcat)) @@ -196,8 +192,4 @@ class LogcatService : BaseService() { logcat.logcatBuffers = bufferValues.map { e -> buffers[e.toInt()].toLowerCase() }.toSet() logcat.start() } - - inner class LocalBinder : Binder() { - fun getLogcatService() = this@LogcatService - } } \ No newline at end of file diff --git a/app/src/main/java/com/dp/logcatapp/util/PreferenceKeys.kt b/app/src/main/java/com/dp/logcatapp/util/PreferenceKeys.kt index 79a6c860..e97269b1 100644 --- a/app/src/main/java/com/dp/logcatapp/util/PreferenceKeys.kt +++ b/app/src/main/java/com/dp/logcatapp/util/PreferenceKeys.kt @@ -1,5 +1,9 @@ package com.dp.logcatapp.util +import com.dp.logcat.Logcat.Companion.AVAILABLE_BUFFERS +import com.dp.logcat.Logcat.Companion.DEFAULT_BUFFERS +import com.dp.logcat.Logcat.Companion.INITIAL_LOG_CAPACITY + object PreferenceKeys { const val MAIN_PREF_SCREEN = "pref_key_main_screen" @@ -40,13 +44,12 @@ object PreferenceKeys { object Default { const val POLL_INTERVAL = "250" val BUFFERS: Set = getDefaultBufferValues() - const val MAX_LOGS = com.dp.logcat.Logcat.INITIAL_LOG_CAPACITY.toString() + const val MAX_LOGS = INITIAL_LOG_CAPACITY.toString() const val SAVE_LOCATION = "" private fun getDefaultBufferValues(): Set { val bufferValues = mutableSetOf() - com.dp.logcat.Logcat.DEFAULT_BUFFERS - .map { com.dp.logcat.Logcat.AVAILABLE_BUFFERS.indexOf(it) } + DEFAULT_BUFFERS.map { AVAILABLE_BUFFERS.indexOf(it) } .filter { it != -1 } .forEach { bufferValues += it.toString() } return bufferValues diff --git a/app/src/main/java/com/dp/logcatapp/util/ServiceBinder.kt b/app/src/main/java/com/dp/logcatapp/util/ServiceBinder.kt index 17fa46be..5acbd544 100644 --- a/app/src/main/java/com/dp/logcatapp/util/ServiceBinder.kt +++ b/app/src/main/java/com/dp/logcatapp/util/ServiceBinder.kt @@ -7,22 +7,22 @@ import com.dp.logger.Logger import java.io.Closeable class ServiceBinder(private val mClass: Class<*>, - private var mServiceConnection: ServiceConnection?) : Closeable { + private val mServiceConnection: ServiceConnection) : Closeable { var isBound: Boolean = false private set + private var closed: Boolean = false + fun bind(context: Context) { - checkNotNull(mServiceConnection) { "This ServiceBinder has already been closed." } + check(!closed) { "This ServiceBinder has already been closed." } - context.bindService(Intent(context, mClass), mServiceConnection!!, Context.BIND_ABOVE_CLIENT) + context.bindService(Intent(context, mClass), mServiceConnection, Context.BIND_ABOVE_CLIENT) isBound = true } fun unbind(context: Context) { if (isBound) { - if (mServiceConnection != null) { - context.unbindService(mServiceConnection!!) - } + context.unbindService(mServiceConnection) isBound = false } else { Logger.warning(ServiceBinder::class, "service is not bound!") @@ -30,6 +30,6 @@ class ServiceBinder(private val mClass: Class<*>, } override fun close() { - mServiceConnection = null + closed = true } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index ed85e6c2..ed41baf9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.61' + ext.kotlin_version = '1.3.70' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.android.tools.build:gradle:3.6.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong @@ -24,7 +24,7 @@ allprojects { ext { core_ktx_version = "1.2.0" - fragment_ktx_version = "1.2.1" + fragment_ktx_version = "1.2.2" appcompat_version = "1.1.0" material_version = "1.1.0" preference_version = "1.1.0" @@ -34,7 +34,7 @@ ext { lifecycle_extensions_version = "2.2.0" lifecycle_viewmodel_ktx_version = "2.2.0" constraint_layout_version = "1.1.3" - room_version = "2.2.3" + room_version = "2.2.4" kotlin_coroutines_core_version = "1.2.2" kotlin_coroutines_android_version = "1.2.2" documentfile_version = "1.0.1" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index cf092398..3d81090c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sat Aug 24 22:51:39 PDT 2019 +#Mon Feb 24 18:20:59 PST 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip