Skip to content

Instantly share code, notes, and snippets.

@jerryOkafor
Last active July 28, 2018 14:43
Show Gist options
  • Save jerryOkafor/2dbc2106a2ec66e1d2aaa0fab70d6013 to your computer and use it in GitHub Desktop.
Save jerryOkafor/2dbc2106a2ec66e1d2aaa0fab70d6013 to your computer and use it in GitHub Desktop.
package velasolutions.velabank.services
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import org.greenrobot.eventbus.EventBus
import timber.log.Timber
import velasolutions.velabank.R
import velasolutions.velabank.events.VelaBankEvent
import velasolutions.velabank.persistence.Session
class VelaBankAccessibilityService : AccessibilityService() {
private var overlay: View? = null
private lateinit var windowManager: WindowManager
private var overlayDrawn: Boolean = false
override fun onCreate() {
super.onCreate()
windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
Timber.d("On Event: $event")
//get the text for subsequent events, which are events of interest
val ussdResponse = event?.text.toString()
closePopupDialog()
if (event == null) {
return
}
if ((event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
|| event.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) &&
(!event.packageName.contains("com.android.phone") ||
!event.packageName.contains("com.android.phone"))) {
return
}
val className = event.className
if (className.contains("ProgressDialog")) {
//The first event that has "ProgressDialog" as class name has been fired.
//attach the window overlay at this point
addOverlay()
}
if (className.contains("FrameLayout")) {
removeOverlay()
}
//check if the second event is fired.
if (className.contains("AlertDialog")) {
removeOverlay()
val velaBankEvent = VelaBankEvent(ussdResponse, Session.getInstance().getMenuServiceCode())
EventBus.getDefault().post(velaBankEvent)
}
}
private fun closePopupDialog() {
performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
}
override fun onInterrupt() {
Timber.d("onServiceInterrupted")
}
public override fun onServiceConnected() {
super.onServiceConnected()
Timber.d("onServiceConnected")
val info = AccessibilityServiceInfo()
info.flags = AccessibilityServiceInfo.DEFAULT
info.packageNames = arrayOf("com.android.phone", "com.android.app")
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED or AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC
serviceInfo = info
}
private fun addOverlay() {
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
},
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE)
params.gravity = Gravity.CENTER
overlay = LayoutInflater.from(this).inflate(R.layout.ussd_alert_overlay, null)
windowManager.addView(overlay, params)
overlayDrawn = true
}
private fun removeOverlay() {
if (overlayDrawn && overlay != null) {
try {
windowManager.removeView(overlay)
overlayDrawn = false
} catch (e: IllegalArgumentException) {
e.printStackTrace()
}
}
}
companion object {
const val TAG = "AccessibilityService"
fun getTAG(): String {
return TAG
}
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="@dimen/fab_margin"
app:cardElevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="48dp" />
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment