Skip to content

Commit

Permalink
Prevent null pointer exception if weakreference not yet created
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Nov 10, 2017
1 parent 396647d commit 1f9651d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ If you want to track time the app has been paused:
//ideally in your onResume
val time = Lifeline.timeSpentOutsideApp()
```
If you want to track when the app is backgrounded:
```kotlin
Lifeline.registerOnBackgroundedListener {
Toast.makeText(this, "On backgrounded", Toast.LENGTH_SHORT)
.show()
}
```
If you want to track when the app is foregrounded:
```kotlin
Lifeline.registerOnForegroundedListener {
Toast.makeText(this, "On foregrounded", Toast.LENGTH_SHORT)
.show()
}
```
Make sure to unregister these when appropriate

License
--------
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/commit451/lifeline/sample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class App : Application() {
super.onCreate()
Lifeline.init(this)
Lifeline.registerOnBackgroundedListener {
Toast.makeText(this@App, "On backgrounded", Toast.LENGTH_SHORT)
Toast.makeText(this, "On backgrounded", Toast.LENGTH_SHORT)
.show()
}
Lifeline.registerOnForegroundedListener {
Toast.makeText(this@App, "On foregrounded", Toast.LENGTH_SHORT)
Toast.makeText(this, "On foregrounded", Toast.LENGTH_SHORT)
.show()
}
}
Expand Down
7 changes: 1 addition & 6 deletions lifeline/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commit451.lifeline">

<application />

</manifest>
<manifest package="com.commit451.lifeline"/>
59 changes: 34 additions & 25 deletions lifeline/src/main/java/com/commit451/lifeline/Lifeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@ import android.content.res.Configuration
import android.os.Bundle

import java.lang.ref.WeakReference
import java.util.ArrayList

/**
* Keeps a pulse on your application
*/
object Lifeline {

private var lifecycleHandler: TrackedLifecycleCallbacks? = null
private val onBackgroundedListeners = ArrayList<OnBackgroundedListener>()
private val onForegroundedListeners = ArrayList<OnForegroundedListener>()
private val onBackgroundedListeners = mutableListOf<OnBackgroundedListener>()
private val onForegroundedListeners = mutableListOf<OnForegroundedListener>()

/**
* Hooks your Application up to this Lifeline
*
* @param application application
*/
fun init(application: Application) {
lifecycleHandler = TrackedLifecycleCallbacks()
application.registerComponentCallbacks(BackgroundComponentCallbacks2({
for (listener in onBackgroundedListeners) {
listener.invoke()
}
}))
application.registerActivityLifecycleCallbacks(lifecycleHandler)
}

/**
* Check if the app is currently in the foreground
Expand Down Expand Up @@ -56,9 +70,7 @@ object Lifeline {
*/
fun currentVisibleActivity(): Activity? {
val lifecycleHandler = lifecycleHandler()
return if (lifecycleHandler.currentVisibleActivityRef != null) {
lifecycleHandler.currentVisibleActivityRef!!.get()
} else null
return lifecycleHandler.currentVisibleActivityRef?.get()
}

/**
Expand All @@ -69,9 +81,7 @@ object Lifeline {
*/
fun currentCreatedActivity(): Activity? {
val lifecycleHandler = lifecycleHandler()
return if (lifecycleHandler.currentCreatedActivityRef != null) {
lifecycleHandler.currentCreatedActivityRef!!.get()
} else null
return lifecycleHandler.currentCreatedActivityRef?.get()
}

/**
Expand All @@ -81,38 +91,34 @@ object Lifeline {
* @return the latest started activity or null if none exists
*/
fun currentStartedActivity(): Activity? {
return if (lifecycleHandler!!.currentStartedActivityRef != null) {
lifecycleHandler!!.currentStartedActivityRef!!.get()
} else null
val lifecycleHandler = lifecycleHandler()
return lifecycleHandler.currentStartedActivityRef?.get()
}

/**
* Hooks your Application up to this Lifeline
*
* @param application application
* Register for a callback when the app is backgrounded
*/
fun init(application: Application) {
lifecycleHandler = TrackedLifecycleCallbacks()
application.registerComponentCallbacks(BackgroundComponentCallbacks2({
for (listener in onBackgroundedListeners) {
listener.invoke()
}
}))
application.registerActivityLifecycleCallbacks(lifecycleHandler)
}

fun registerOnBackgroundedListener(listener: OnBackgroundedListener) {
onBackgroundedListeners.add(listener)
}

/**
* Unregister the callback for when the app is backgrounded
*/
fun unregisterOnBackgroundedListener(listener: OnBackgroundedListener) {
onBackgroundedListeners.remove(listener)
}

/**
* Register for a callback when the app is foregrounded
*/
fun registerOnForegroundedListener(listener: OnForegroundedListener) {
onForegroundedListeners.add(listener)
}

/**
* Unregister the callback for when the app is foregrounded
*/
fun unregisterOnForegroundedListener(listener: OnForegroundedListener) {
onForegroundedListeners.remove(listener)
}
Expand All @@ -121,6 +127,9 @@ object Lifeline {
return lifecycleHandler ?: throw IllegalStateException("You need to call init() before accessing Lifeline")
}

/**
* What a great class name
*/
private class BackgroundComponentCallbacks2(private val onBackgrounded: () -> Unit) : ComponentCallbacks2 {

override fun onTrimMemory(level: Int) {
Expand Down

0 comments on commit 1f9651d

Please sign in to comment.