Skip to content

Commit

Permalink
Fix audioservice crashing when packagemanager fails
Browse files Browse the repository at this point in the history
  • Loading branch information
threethan committed Sep 3, 2023
1 parent f9409f2 commit 8dbed6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.bos.oculess"
minSdkVersion 24
targetSdkVersion 33
versionCode 13
versionName "1.5.0"
versionCode 14
versionName "1.5.1"
}

buildTypes {
Expand Down
31 changes: 24 additions & 7 deletions app/src/main/java/com/bos/oculess/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import android.provider.Settings.SettingNotFoundException
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import com.bos.oculess.util.AppOpsUtil
import java.lang.Exception

class AudioService : AccessibilityService() {
companion object {
private const val TAG = "OculessAudioService"
Expand Down Expand Up @@ -43,11 +45,9 @@ class AudioService : AccessibilityService() {
}
override fun onAccessibilityEvent(e: AccessibilityEvent?) {
if (e?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
Log.i(TAG, "Trigger: TYPE_WINDOW_STATE_CHANGED")
checkUpdateAppList()
audioFix()
} else if (e?.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
Log.i(TAG, "Trigger: TYPE_WINDOW_CONTENT_CHANGED")
audioFix()
}
}
Expand All @@ -58,7 +58,11 @@ class AudioService : AccessibilityService() {

private fun checkUpdateAppList() {
if (canUpdateAppList) {
updateAudioPackages()
try {
updateAudioPackages()
} catch (e: Exception) {
e.printStackTrace()
}
val handler: Handler = Handler()
canUpdateAppList = false
handler.postDelayed({
Expand All @@ -75,6 +79,7 @@ class AudioService : AccessibilityService() {
enableAudioPermission()
}
handler.postDelayed(r, 250)
handler.postDelayed(r, 750)
}
private fun updateAudioPackages() {
Log.i(TAG, "Start package scan...")
Expand All @@ -88,13 +93,23 @@ class AudioService : AccessibilityService() {
val packageInfoList = applicationContext.packageManager?.getInstalledPackages(0)
val packageNames = arrayListOf<String>()
for (packageInfo in packageInfoList!!) {
packageNames.add(packageInfo.packageName)
try {
packageNames.add(packageInfo.packageName)
} catch (e: Exception) {
Log.w(TAG, "Error while iterating packages")
e.printStackTrace()
}
}
// Remove system apps
val packageInfoListSystem = applicationContext.packageManager?.getInstalledPackages(
PackageManager.MATCH_SYSTEM_ONLY)
for (packageInfoSystem in packageInfoListSystem!!) {
packageNames.remove(packageInfoSystem.packageName)
try {
packageNames.remove(packageInfoSystem.packageName)
} catch (e: Exception) {
Log.w(TAG, "Error while iterating packages")
e.printStackTrace()
}
}
// Commit to app list
audioApps = packageNames.toTypedArray()
Expand All @@ -108,14 +123,16 @@ class AudioService : AccessibilityService() {
Log.i(TAG, "AudioApps is null!")
return
}
var first = true
for (app in audioApps!!) {
val info: ApplicationInfo?= applicationContext.packageManager?.getApplicationInfo(app, 0)
// https://cs.android.com/android/platform/superproject/+/master:frameworks/proto_logging/stats/enums/app/enums.proto;l=138?q=PLAY_AUDIO
try {
val info: ApplicationInfo?= applicationContext.packageManager?.getApplicationInfo(app, 0)
AppOpsUtil.allowOp(applicationContext, 27, info?.uid!!, app) // Record audio (Future-proofing)
AppOpsUtil.allowOp(applicationContext, 28, info?.uid!!, app) // Play audio
} catch (e: SecurityException) {
Log.w(TAG, "Audio service lacks permission to set appops. Is Oculess device owner?")
} catch (e: Exception) {
Log.w(TAG, "An exception occurred while setting permissions")
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ android.enableJetifier=true
kotlin.code.style=official
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
android.nonFinalResIds=false

# TetherFi
systemProp.http.proxyHost=192.168.49.1
systemProp.http.proxyPort=8282
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
systemProp.https.proxyHost=192.168.49.1
systemProp.https.proxyPort=8282
systemProp.https.proxyUser=userid
systemProp.https.proxyPassword=password
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost

0 comments on commit 8dbed6b

Please sign in to comment.