Skip to content

Commit

Permalink
Merge pull request #31 from TorryDo/release/v0.5.2
Browse files Browse the repository at this point in the history
Release/v0.5.2
  • Loading branch information
TorryDo committed Apr 16, 2023
2 parents 599b231 + 9b97362 commit bc01c9c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ internal fun View.getXYPointOnScreen(): Point {
return Point(arr[0], arr[1])
}

internal val Int.toDp: Int get() = (this / getSystem().displayMetrics.density).toInt()
internal val Int.toPx: Int get() = (this * getSystem().displayMetrics.density).toInt()
internal fun Int.toDp(): Int = (this / getSystem().displayMetrics.density).toInt()
internal fun Int.toPx(): Int = (this * getSystem().displayMetrics.density).toInt()

inline fun View.afterMeasured(crossinline afterMeasuredWork: () -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.graphics.Bitmap
import android.graphics.Point
import android.util.Size
import android.view.View
import androidx.annotation.Discouraged
import androidx.annotation.DrawableRes
import androidx.annotation.StyleRes
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -90,8 +89,8 @@ internal constructor(
}

internal fun tryShowCloseBubbleAndBackground() {
bottomBackground?.show()
closeBubbleView?.show()
bottomBackground?.show()
}

internal fun tryRemoveCloseBubbleAndBackground() {
Expand Down Expand Up @@ -201,7 +200,6 @@ internal constructor(

// config
internal var startPoint = Point(0, 0)
internal var elevation = 0
internal var opacity = 1f
internal var isCloseBubbleEnabled = true
internal var isAnimateToEdgeEnabled = true
Expand All @@ -228,7 +226,7 @@ internal constructor(
*
* @param dp distance between bubble and close-bubble
* */
fun closablePerimeter(dp: Int): Builder {
fun distanceToClose(dp: Int): Builder {
this.closablePerimeterDp = dp
return this
}
Expand All @@ -242,7 +240,7 @@ internal constructor(
}

/**
* @param enabled animate bubble to the left/right side of the screen
* @param enabled animate the bubble to the left/right side of the screen when finger is released, true by default
* */
fun enableAnimateToEdge(enabled: Boolean): Builder {
isAnimateToEdgeEnabled = enabled
Expand Down Expand Up @@ -275,7 +273,7 @@ internal constructor(
* set drawable to bubble width given width and height in dp
* */
fun bubble(@DrawableRes drawable: Int, widthDp: Int, heightDp: Int): Builder {
bubbleSizePx = Size(widthDp.toPx, heightDp.toPx)
bubbleSizePx = Size(widthDp.toPx(), heightDp.toPx())
return bubble(drawable)
}

Expand All @@ -291,7 +289,7 @@ internal constructor(
* set bitmap to bubble width given width and height in dp
* */
fun bubble(bitmap: Bitmap, widthDp: Int, heightDp: Int): Builder {
bubbleSizePx = Size(widthDp.toPx, heightDp.toPx)
bubbleSizePx = Size(widthDp.toPx(), heightDp.toPx())
return bubble(bitmap)
}

Expand Down Expand Up @@ -321,7 +319,7 @@ internal constructor(
* set drawable to close-bubble with given width and height in dp
* */
fun closeBubble(@DrawableRes drawable: Int, widthDp: Int, heightDp: Int): Builder {
this.closeBubbleSizePx = Size(widthDp.toPx, heightDp.toPx)
this.closeBubbleSizePx = Size(widthDp.toPx(), heightDp.toPx())
return closeBubble(drawable)
}

Expand Down Expand Up @@ -380,29 +378,37 @@ internal constructor(
}

/**
* examples: x=0, y=0 show bubble on the top-left corner of the screen.
* examples: x=0, y=0 show the bubble on the top-left corner of the screen.
*
* you can set x/y as negative value, but the bubble will be outside the screen.
* you can set x/y as a negative values, but the bubble will be outside the screen.
*
* @param x 0 ... screenWidth (px).
* @param y 0 ... screenHeight (px).
* @param x 0 ... screenWidth (dp).
* @param y 0 ... screenHeight (dp).
* */
fun startLocation(x: Int, y: Int): Builder {
startPoint.x = x
startPoint.y = y
startPoint.x = x.toPx()
startPoint.y = y.toPx()
return this
}

// not exposed to the outside packages because of being developed
internal fun elevation(dp: Int): Builder {
elevation = dp
/**
* examples: x=0, y=0 show the bubble on the top-left corner of the screen.
*
* you can set x/y as negative values, but the bubble will be outside the screen.
*
* @param x 0 ... screenWidth (px).
* @param y 0 ... screenHeight (px).
* */
fun startLocationPx(x: Int, y: Int): Builder {
startPoint.x = x
startPoint.y = y
return this
}

/**
* - 0.0f: invisible
* - 0.0f < x < 1.0f: view with opacity
* - 1.0f: completely visible
* - 1.0f: fully visible
* */
fun opacity(opacity: Float): Builder {
this.opacity = opacity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.Discouraged
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.PRIORITY_MIN
import androidx.core.app.NotificationManagerCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch


abstract class FloatingBubbleService : Service() {
Expand Down Expand Up @@ -61,14 +54,14 @@ abstract class FloatingBubbleService : Service() {

when (currentRoute) {
Route.Empty -> {
initViewsAndNotification()
startWithNotification()
}
Route.Bubble -> {
initViewsAndNotification()
startWithNotification()
showBubbles()
}
Route.ExpandableView -> {
initViewsAndNotification()
startWithNotification()
showExpandableView()
}
}
Expand All @@ -84,29 +77,18 @@ abstract class FloatingBubbleService : Service() {
* init view instances and notification
* */
@Throws(PermissionDeniedException::class)
private fun initViewsAndNotification() {
private fun startWithNotification() {

if (!isDrawOverlaysPermissionGranted()) {
throw PermissionDeniedException()
}

initViewInstances()

if (isHigherThanAndroid8()) {
showForegroundNotification()
}

}

private fun initViewInstances() {
floatingBubble = setupBubble(customFloatingBubbleAction)
.addServiceInteractor(customFloatingBubbleServiceInteractor)
.build()

expandableView = setupExpandableView(customExpandableViewListener)
?.build()
}

// region Public Methods -----------------------------------------------------------------------

/**
Expand All @@ -115,6 +97,11 @@ abstract class FloatingBubbleService : Service() {
fun currentRoute() = currentRoute

fun showBubbles() {
if (floatingBubble == null) {
floatingBubble = setupBubble(customFloatingBubbleAction)
.addServiceInteractor(customFloatingBubbleServiceInteractor)
.build()
}
floatingBubble!!.showIcon()
currentRoute = Route.Bubble
}
Expand All @@ -133,13 +120,19 @@ abstract class FloatingBubbleService : Service() {
@Throws(NotImplementedError::class)
fun showExpandableView(): Boolean {
if (expandableView == null) {
throw NotImplementedError("you DID NOT override expandable view")

expandableView = setupExpandableView(customExpandableViewListener)
?.build()

if (expandableView == null) {
throw NotImplementedError("you DID NOT override expandable view")
}
}
try {
expandableView!!.show()
currentRoute = Route.ExpandableView
} catch (e: Exception) {
Log.e("<>", "showExpandableView: ", e)
// Log.e("<>", "showExpandableView: ", e)
return false
}

Expand All @@ -151,16 +144,10 @@ abstract class FloatingBubbleService : Service() {
}

/**
* remove all views or init notification if first-time call
* remove all views
* */
fun removeAllViews() {

if (isNotificationInitialized.not()) {
initViewsAndNotification()
isNotificationInitialized = true
return
}

removeExpandableView()
removeBubbles()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package com.torrydo.floatingbubbleview
import android.annotation.SuppressLint
import android.graphics.Point
import android.graphics.PointF
import android.util.Log
import android.view.GestureDetector
import android.view.*
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.WindowManager
import com.torrydo.floatingbubbleview.databinding.BubbleBinding

internal class FloatingBubbleView(
Expand Down Expand Up @@ -41,7 +37,7 @@ internal class FloatingBubbleView(
}

halfIconWidthPx = width / 2
halfIconHeightPx = height /2
halfIconHeightPx = height / 2

setupLayoutParams()
setupBubbleProperties()
Expand Down Expand Up @@ -181,7 +177,7 @@ internal class FloatingBubbleView(
builder.listener?.onDown(motionEvent.rawX, motionEvent.rawY)
}

fun onActionMove(motionEvent: MotionEvent){
fun onActionMove(motionEvent: MotionEvent) {
builder.listener?.onMove(motionEvent.rawX, motionEvent.rawY)
}

Expand All @@ -191,19 +187,27 @@ internal class FloatingBubbleView(

// listen actions --------------------------------------------------------------------------

val gestureDetector = GestureDetector(builder.context, SingleTapConfirm())
val gestureDetector = GestureDetector(builder.context, object : SimpleOnGestureListener() {

// override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
// builder.listener?.onClick()
// return super.onSingleTapConfirmed(e)
// }

override fun onSingleTapUp(e: MotionEvent): Boolean {
builder.listener?.onClick()
return super.onSingleTapUp(e)
}

})

binding.bubbleView.apply {

afterMeasured { updateGestureExclusion(builder.context) }

setOnTouchListener { _, motionEvent ->

// detect onTouch event first. If event is consumed, return@setOnTouch...
if (gestureDetector.onTouchEvent(motionEvent)) {
builder.listener?.onClick()
return@setOnTouchListener true
}
gestureDetector.onTouchEvent(motionEvent)

when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> onActionDown(motionEvent)
Expand All @@ -213,12 +217,7 @@ internal class FloatingBubbleView(

return@setOnTouchListener true
}
}
}

private class SingleTapConfirm : SimpleOnGestureListener() {
override fun onSingleTapUp(event: MotionEvent): Boolean {
return true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class FloatingCloseBubbleView(
centerCloseBubbleX = baseX + halfWidthPx
centerCloseBubbleY = baseY + halfHeightPx

closablePerimeterPx = builder.closablePerimeterDp.toPx
closablePerimeterPx = builder.closablePerimeterDp.toPx()

setupLayoutParams()
setupCloseBubbleProperties()
Expand Down Expand Up @@ -121,7 +121,7 @@ internal class FloatingCloseBubbleView(
return distanceRatio
}

fun distanceRatioFromLocationToClosableArea(x: Float, y: Float): Float{
fun distanceRatioFromLocationToClosableArea(x: Float, y: Float): Float {
val distanceToLocation = MathHelper.distance(
x1 = centerCloseBubbleX.toDouble(),
y1 = centerCloseBubbleY.toDouble(),
Expand All @@ -136,6 +136,7 @@ internal class FloatingCloseBubbleView(
return distanceRatio

}

fun animateCloseIconByBubble(x: Int, y: Int) {

val distanceRatio = distanceRatioFromBubbleToClosableArea(x, y)
Expand Down
Loading

0 comments on commit bc01c9c

Please sign in to comment.