Skip to content

Commit

Permalink
Add BannerAction
Browse files Browse the repository at this point in the history
  • Loading branch information
uncn committed Dec 23, 2019
1 parent a6bd69f commit 8555a31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Library/src/main/java/com/sunzn/banner/library/Banner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ class Banner<T> @JvmOverloads constructor(context: Context, attrs: AttributeSet,
private val mReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (Intent.ACTION_USER_PRESENT == action) {
setPlaying(true)
} else if (Intent.ACTION_SCREEN_OFF == action) {
setPlaying(false)
} else if (Intent.ACTION_SCREEN_ON == action) {
setPlaying(true)
when {
Intent.ACTION_USER_PRESENT == action -> {
setPlaying(true)
}
Intent.ACTION_SCREEN_ON == action -> {
setPlaying(true)
}
Intent.ACTION_SCREEN_OFF == action -> {
setPlaying(false)
}
BannerAction.ACTION_LOOP == action -> {
setPlaying(true)
}
BannerAction.ACTION_STOP == action -> {
setPlaying(false)
}
}
}
}
Expand Down Expand Up @@ -171,6 +181,8 @@ class Banner<T> @JvmOverloads constructor(context: Context, attrs: AttributeSet,
mFilter.addAction(Intent.ACTION_SCREEN_ON)
mFilter.addAction(Intent.ACTION_SCREEN_OFF)
mFilter.addAction(Intent.ACTION_USER_PRESENT)
mFilter.addAction(BannerAction.ACTION_LOOP)
mFilter.addAction(BannerAction.ACTION_STOP)

PagerSnapHelper().attachToRecyclerView(mRecyclerView)
mBannerAdapter = BannerAdapter()
Expand Down Expand Up @@ -387,7 +399,7 @@ class Banner<T> @JvmOverloads constructor(context: Context, attrs: AttributeSet,
}
}

fun playBanner() {
private fun playBanner() {
if (!isPlaying && mBannerAdapter!!.itemCount > 1) {
isPlaying = true
mHandler.removeCallbacks(mBannerTask)
Expand All @@ -396,7 +408,7 @@ class Banner<T> @JvmOverloads constructor(context: Context, attrs: AttributeSet,
}
}

fun stopBanner() {
private fun stopBanner() {
isPlaying = false
mHandler.removeCallbacks(mBannerTask)
Log.e(TAG, "Stop Banner")
Expand Down Expand Up @@ -432,4 +444,8 @@ class Banner<T> @JvmOverloads constructor(context: Context, attrs: AttributeSet,
}
}

override fun onDestroy(owner: LifecycleOwner) {
owner.lifecycle.removeObserver(this)
}

}
10 changes: 10 additions & 0 deletions Library/src/main/java/com/sunzn/banner/library/BannerAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sunzn.banner.library

class BannerAction {

companion object {
const val ACTION_LOOP = "banner.action.LOOP"
const val ACTION_STOP = "banner.action.STOP"
}

}

0 comments on commit 8555a31

Please sign in to comment.