Skip to content

Commit

Permalink
Update bunch of deps, and some code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Mar 21, 2020
1 parent 3b02cbe commit 6d2cf4e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/com/dp/logcatapp/services/BaseService.kt
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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) {
}
}

inner class LocalBinder : Binder() {
@Suppress("UNCHECKED_CAST")
fun <T : BaseService> getService() = this@BaseService as T
}
}

inline fun <reified T : BaseService> IBinder.getService() = (this as BaseService.LocalBinder).getService<T>()
16 changes: 4 additions & 12 deletions app/src/main/java/com/dp/logcatapp/services/LogcatService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -134,8 +133,6 @@ class LogcatService : BaseService() {
nm.deleteNotificationChannel(NOTIFICATION_CHANNEL)
}

override fun onBind(intent: Intent?) = localBinder

override fun onDestroy() {
super.onDestroy()
logcat.close()
Expand Down Expand Up @@ -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))
Expand All @@ -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
}
}
9 changes: 6 additions & 3 deletions app/src/main/java/com/dp/logcatapp/util/PreferenceKeys.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -40,13 +44,12 @@ object PreferenceKeys {
object Default {
const val POLL_INTERVAL = "250"
val BUFFERS: Set<String> = 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<String> {
val bufferValues = mutableSetOf<String>()
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
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/dp/logcatapp/util/ServiceBinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ 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!")
}
}

override fun close() {
mServiceConnection = null
closed = true
}
}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6d2cf4e

Please sign in to comment.