-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from enaboapps/iss203
Persist scan receiver state
- Loading branch information
Showing
11 changed files
with
179 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 51 additions & 24 deletions
75
app/src/main/java/com/enaboapps/switchify/service/SwitchifyAccessibilityService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,88 @@ | ||
package com.enaboapps.switchify.service | ||
|
||
import android.accessibilityservice.AccessibilityService | ||
import android.util.Log | ||
import android.view.KeyEvent | ||
import android.view.accessibility.AccessibilityEvent | ||
import com.enaboapps.switchify.preferences.PreferenceManager | ||
import com.enaboapps.switchify.service.gestures.GestureManager | ||
import com.enaboapps.switchify.service.nodes.NodeExaminer | ||
import com.enaboapps.switchify.service.scanning.ScanReceiver | ||
import com.enaboapps.switchify.service.scanning.ScanningManager | ||
import com.enaboapps.switchify.service.selection.AutoSelectionHandler | ||
import com.enaboapps.switchify.service.switches.SwitchListener | ||
import com.enaboapps.switchify.service.utils.KeyboardInfo | ||
|
||
/** | ||
* This is the main service class for the Switchify application. | ||
* It extends the AccessibilityService class to provide accessibility features. | ||
*/ | ||
class SwitchifyAccessibilityService : AccessibilityService() { | ||
|
||
private val TAG = "SwitchifyAccessibilityService" | ||
// ScanningManager instance for managing scanning operations | ||
private lateinit var scanningManager: ScanningManager | ||
|
||
private var scanningManager: ScanningManager? = null | ||
// SwitchListener instance for listening to switch events | ||
private lateinit var switchListener: SwitchListener | ||
|
||
private var switchListener: SwitchListener? = null | ||
/** | ||
* This function is called when the service is destroyed. | ||
* It shuts down the scanning manager. | ||
*/ | ||
override fun onDestroy() { | ||
super.onDestroy() | ||
scanningManager.shutdown() | ||
} | ||
|
||
/** | ||
* This method is called when an AccessibilityEvent is fired. | ||
* It updates the keyboard state and finds nodes in the active window. | ||
*/ | ||
override fun onAccessibilityEvent(event: AccessibilityEvent?) { | ||
Log.d(TAG, "onAccessibilityEvent: ${event?.eventType}") | ||
|
||
val rootNode = rootInActiveWindow | ||
if (rootNode != null) { | ||
rootInActiveWindow?.let { rootNode -> | ||
NodeExaminer.findNodes(rootNode, this) | ||
} | ||
|
||
KeyboardInfo.updateKeyboardState(windows) | ||
} | ||
|
||
override fun onInterrupt() { | ||
Log.d(TAG, "onInterrupt") | ||
} | ||
|
||
/** | ||
* This method is called when the service is interrupted. | ||
* Currently, it does nothing. | ||
*/ | ||
override fun onInterrupt() {} | ||
|
||
/** | ||
* This method is called when the service is connected. | ||
* It sets up the scanning manager, switch listener, gesture manager, and auto selection handler. | ||
* It also finds nodes in the active window and updates the keyboard state. | ||
*/ | ||
override fun onServiceConnected() { | ||
Log.d(TAG, "onServiceConnected") | ||
super.onServiceConnected() | ||
|
||
scanningManager = ScanningManager(this, this) | ||
ScanReceiver.preferenceManager = PreferenceManager(this) | ||
|
||
scanningManager?.setup() | ||
scanningManager = ScanningManager(this, this) | ||
scanningManager.setup() | ||
|
||
switchListener = SwitchListener(this, scanningManager!!) | ||
switchListener = SwitchListener(this, scanningManager) | ||
|
||
GestureManager.getInstance().setup(this) | ||
|
||
AutoSelectionHandler.init(this) | ||
} | ||
|
||
rootInActiveWindow?.let { rootNode -> | ||
NodeExaminer.findNodes(rootNode, this) | ||
} | ||
KeyboardInfo.updateKeyboardState(windows) | ||
} | ||
|
||
/** | ||
* This method is called when a key event is fired. | ||
* It handles switch press and release events. | ||
*/ | ||
override fun onKeyEvent(event: KeyEvent?): Boolean { | ||
if (event?.action == KeyEvent.ACTION_DOWN) { | ||
return switchListener?.onSwitchPressed(event.keyCode) ?: false | ||
} else if (event?.action == KeyEvent.ACTION_UP) { | ||
return switchListener?.onSwitchReleased(event.keyCode) ?: false | ||
return when (event?.action) { | ||
KeyEvent.ACTION_DOWN -> switchListener.onSwitchPressed(event.keyCode) | ||
KeyEvent.ACTION_UP -> switchListener.onSwitchReleased(event.keyCode) | ||
else -> true | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.