Skip to content

Commit

Permalink
Merge pull request #198 from enaboapps/iss197
Browse files Browse the repository at this point in the history
Improve look and feel of service messages
  • Loading branch information
enaboapps authored Mar 7, 2024
2 parents b48e2ae + bf92f8d commit 33813b2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,95 @@ import android.os.Looper
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import com.enaboapps.switchify.R
import com.enaboapps.switchify.service.utils.ScreenUtils
import java.util.Timer

/**
* ServiceMessageHUD is responsible for displaying a message overlay on top of all other application windows.
* It supports two types of messages: disappearing and permanent.
*/
class ServiceMessageHUD {
private val TAG = "MessageManager"

companion object {
// Singleton instance to ensure only one instance of ServiceMessageHUD is used throughout the application.
val instance: ServiceMessageHUD by lazy {
ServiceMessageHUD()
}
}

// The application context
private var context: Context? = null

private var switchifyAccessibilityWindow: SwitchifyAccessibilityWindow = SwitchifyAccessibilityWindow.instance
// Reference to the accessibility service window manager
private var switchifyAccessibilityWindow: SwitchifyAccessibilityWindow =
SwitchifyAccessibilityWindow.instance

// The message to be displayed
private var message = ""

// The type of the currently shown message
private var shownMessageType: MessageType? = null

// The LinearLayout that serves as the container for the message view
private var messageView: LinearLayout? = null

// Handler to post tasks on the main thread
private val handler = Handler(Looper.getMainLooper())

/**
* Sets up the message HUD with the necessary context.
*
* @param context The application context.
*/
fun setup(context: Context) {
this.context = context
}

/**
* Enum to define the types of messages the HUD can show.
*/
enum class MessageType {
DISAPPEARING,
PERMANENT
}


/**
* Creates the message view with the specified configurations.
*/
private fun createMessageView() {
context?.let {
messageView = LinearLayout(it)
messageView?.orientation = LinearLayout.VERTICAL
messageView?.gravity = Gravity.CENTER
messageView?.setBackgroundColor(Color.parseColor("#CC000000"))
messageView?.setPadding(16, 16, 16, 16)
messageView?.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)

val messageTextView = TextView(it)
messageTextView.text = message
messageTextView.setTextColor(Color.WHITE)
messageTextView.textSize = 20f
context?.let { context ->
messageView = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
gravity = Gravity.CENTER
background = context.getDrawable(R.drawable.rounded_corners)
setPadding(16, 16, 16, 16)
layoutParams =
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
gravity = Gravity.CENTER_HORIZONTAL
}
}

val messageTextView = TextView(context).apply {
text = message
setTextColor(Color.WHITE)
textSize = 20f
gravity = Gravity.CENTER
}

messageView?.addView(messageTextView)
}
}


/**
* Displays the message on the screen.
*
* @param message The message text to be displayed.
* @param messageType The type of the message (DISAPPEARING or PERMANENT).
*/
fun showMessage(message: String, messageType: MessageType) {
if (message == this.message && messageType == this.shownMessageType) {
return
Expand All @@ -80,34 +114,34 @@ class ServiceMessageHUD {

createMessageView()

// Bottom of screen
val y = ScreenUtils.getHeight(context!!) - 300
val x = 100
val y = 150
val width = ScreenUtils.getWidth(context!!) - 200

handler.post {
if (messageView != null) {
messageView?.let {
switchifyAccessibilityWindow.addView(
messageView!!,
0,
it,
x,
y,
LinearLayout.LayoutParams.MATCH_PARENT,
width,
LinearLayout.LayoutParams.WRAP_CONTENT
)
}
}

if (messageType == MessageType.DISAPPEARING) {
// Remove the message after 5 seconds
handler.postDelayed({
hideMessage()
}, 5000)
handler.postDelayed({ hideMessage() }, 5000)
}
}


fun hideMessage() {
if (messageView != null) {
/**
* Hides and removes the currently displayed message from the screen.
*/
private fun hideMessage() {
messageView?.let { view ->
handler.post {
switchifyAccessibilityWindow.removeView(messageView!!)
switchifyAccessibilityWindow.removeView(view)
messageView = null
shownMessageType = null
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/rounded_corners.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- res/drawable/rounded_corners.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#CC000000" />
<corners android:radius="16dp" />
</shape>

0 comments on commit 33813b2

Please sign in to comment.