Skip to content

Commit

Permalink
Added encapsulated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
khirr committed Jun 16, 2017
1 parent 46ccd4b commit 4e74c7a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "net.khirr.android.foreground.example"
minSdkVersion 15
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void onCreate() {
// Initialize
Foreground.Companion.init(this);
// Add listener
Foreground.Companion.getInstance().addListener(this);
Foreground.Companion.addListener(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void onClick(View v) {
}
});

//Foreground.Companion.getInstance().isForeground();
//Foreground.Companion.getInstance().isBackground();
//Foreground.Companion.isForeground();
//Foreground.Companion.isBackground();
}
}
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 15
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
Expand Down
26 changes: 21 additions & 5 deletions library/src/main/java/net/khirr/library/foreground/Foreground.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,45 @@ class Foreground : Application.ActivityLifecycleCallbacks {
}

companion object {
val instance = Foreground()
private val instance = Foreground()

fun init(application: Application) {
application.registerActivityLifecycleCallbacks(instance)
}

fun addListener(listener: Listener) {
instance.addListener(listener)
}

fun removeListener(listener: Listener) {
instance.removeListener(listener)
}

fun isForeground(): Boolean {
return instance.isForeground()
}

fun isBackground(): Boolean {
return instance.isBackground()
}
}

val listeners: ArrayList<Listener> = ArrayList()
var numStarted = 0

fun addListener(listener: Listener) {
private fun addListener(listener: Listener) {
listeners.add(listener)
}

fun removeListener(listener: Listener) {
private fun removeListener(listener: Listener) {
listeners.remove(listener)
}

fun isForeground(): Boolean {
private fun isForeground(): Boolean {
return (numStarted > 0)
}

fun isBackground(): Boolean {
private fun isBackground(): Boolean {
return (numStarted == 0)
}

Expand Down

0 comments on commit 4e74c7a

Please sign in to comment.