Skip to content

Commit c6d7066

Browse files
committed
refactor: replace broadcast receiver with content provider for hook status communication
- Remove HookBroadcastReceiver and its broadcast-based communication - Add HookStatusProvider as a content provider for secure cross-process communication - Add HookNotifier utility class to handle content provider interactions - Update Constants to reflect new method names (SET_HOOKED, INCREMENT_LAUNCH_COUNT) - Update AndroidManifest.xml to register the content provider instead of broadcast receiver - Update VolumeKeyControlModuleHandlers to use HookNotifier instead of direct broadcasts
1 parent 689cffa commit c6d7066

6 files changed

Lines changed: 90 additions & 67 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@
2727
</intent-filter>
2828
</activity>
2929

30-
<receiver
31-
android:name="ru.hepolise.volumekeytrackcontrol.receiver.HookBroadcastReceiver"
32-
android:exported="false">
33-
<intent-filter>
34-
<action android:name="${applicationId}.HOOK_UPDATE" />
35-
</intent-filter>
36-
</receiver>
30+
<provider
31+
android:name="ru.hepolise.volumekeytrackcontrol.provider.HookStatusProvider"
32+
android:authorities="${applicationId}.hookstatusprovider"
33+
android:exported="true"
34+
tools:ignore="ExportedContentProvider" />
3735

3836
<!-- metadata -->
3937
<meta-data

app/src/main/java/ru/hepolise/volumekeytrackcontrol/module/VolumeKeyControlModuleHandlers.kt

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ru.hepolise.volumekeytrackcontrol.module
22

33
import android.content.BroadcastReceiver
4-
import android.content.ComponentName
54
import android.content.Context
65
import android.content.Intent
76
import android.content.IntentFilter
@@ -18,17 +17,15 @@ import android.view.KeyEvent
1817
import de.robv.android.xposed.XC_MethodHook
1918
import de.robv.android.xposed.XC_MethodHook.MethodHookParam
2019
import de.robv.android.xposed.XposedHelpers
20+
import ru.hepolise.volumekeytrackcontrol.module.util.HookNotifier
2121
import ru.hepolise.volumekeytrackcontrol.module.util.LogHelper
22-
import ru.hepolise.volumekeytrackcontrol.receiver.HookBroadcastReceiver
23-
import ru.hepolise.volumekeytrackcontrol.util.Constants
2422
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil
2523
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getAppFilterType
2624
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getApps
2725
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
2826
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.isSwapButtons
2927
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
3028
import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration
31-
import ru.hepolise.volumekeytrackcontrolmodule.BuildConfig
3229

3330

3431
object VolumeKeyControlModuleHandlers {
@@ -85,9 +82,7 @@ object VolumeKeyControlModuleHandlers {
8582
val filter = IntentFilter(Intent.ACTION_USER_UNLOCKED)
8683
context.registerReceiver(object : BroadcastReceiver() {
8784
override fun onReceive(ctx: Context, intent: Intent) {
88-
context.sendBroadcast {
89-
putExtra(Constants.HOOKED, true)
90-
}
85+
HookNotifier.notifyHooked(context)
9186
ctx.unregisterReceiver(this)
9287
}
9388
}, filter)
@@ -255,21 +250,6 @@ object VolumeKeyControlModuleHandlers {
255250
}
256251
}
257252

258-
private fun Context.sendBroadcast(block: Intent.() -> Unit) {
259-
val modulePackage = BuildConfig.APPLICATION_ID
260-
261-
val intent = Intent().apply {
262-
component = ComponentName(
263-
modulePackage,
264-
HookBroadcastReceiver::class.java.name
265-
)
266-
action = Constants.HOOK_UPDATE
267-
block()
268-
setPackage(modulePackage)
269-
}
270-
sendBroadcast(intent)
271-
}
272-
273253
private fun MethodHookParam.delay(event: MediaEvent) {
274254
val handler = getHandler()
275255
handler.postDelayed(
@@ -311,9 +291,7 @@ object VolumeKeyControlModuleHandlers {
311291
log("Sending ${this::class.simpleName}")
312292
isLongPress = true
313293
sendMediaButtonEventAndTriggerVibration(this)
314-
context.sendBroadcast {
315-
putExtra(Constants.INCREMENT_LAUNCH_COUNT, true)
316-
}
294+
HookNotifier.incrementLaunchCount(context)
317295
}
318296

319297
object PlayPause : MediaEvent("mVolumeBothLongPress") {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.hepolise.volumekeytrackcontrol.module.util
2+
3+
import android.content.Context
4+
import androidx.core.net.toUri
5+
import ru.hepolise.volumekeytrackcontrol.util.Constants
6+
import ru.hepolise.volumekeytrackcontrolmodule.BuildConfig
7+
8+
object HookNotifier {
9+
10+
private val authorityUri = "content://${BuildConfig.APPLICATION_ID}.hookstatusprovider".toUri()
11+
12+
fun notifyHooked(context: Context) {
13+
runCatching {
14+
context.contentResolver.call(authorityUri, Constants.SET_HOOKED, null, null)
15+
}
16+
}
17+
18+
fun incrementLaunchCount(context: Context) {
19+
runCatching {
20+
context.contentResolver.call(authorityUri, Constants.INCREMENT_LAUNCH_COUNT, null, null)
21+
}
22+
}
23+
24+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ru.hepolise.volumekeytrackcontrol.provider
2+
3+
import android.content.ContentProvider
4+
import android.content.ContentValues
5+
import android.content.Context
6+
import android.database.Cursor
7+
import android.net.Uri
8+
import android.os.Bundle
9+
import androidx.core.content.edit
10+
import ru.hepolise.volumekeytrackcontrol.util.Constants
11+
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.HOOK_PREFS_NAME
12+
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LAST_INIT_HOOK_TIME
13+
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LAUNCHED_COUNT
14+
import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLaunchedCount
15+
16+
class HookStatusProvider : ContentProvider() {
17+
18+
override fun onCreate(): Boolean = true
19+
20+
override fun call(method: String, arg: String?, extras: Bundle?): Bundle? {
21+
val context = context ?: return null
22+
val prefs = context.getSharedPreferences(HOOK_PREFS_NAME, Context.MODE_PRIVATE)
23+
24+
prefs.edit {
25+
when (method) {
26+
Constants.SET_HOOKED -> {
27+
putLong(LAST_INIT_HOOK_TIME, System.currentTimeMillis())
28+
}
29+
30+
Constants.INCREMENT_LAUNCH_COUNT -> {
31+
val current = prefs.getLaunchedCount()
32+
putInt(LAUNCHED_COUNT, current + 1)
33+
}
34+
}
35+
}
36+
return null
37+
}
38+
39+
override fun query(
40+
uri: Uri,
41+
projection: Array<out String>?,
42+
selection: String?,
43+
selectionArgs: Array<out String>?,
44+
sortOrder: String?
45+
): Cursor? = null
46+
47+
override fun getType(uri: Uri): String? = null
48+
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
49+
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
50+
override fun update(
51+
uri: Uri,
52+
values: ContentValues?,
53+
selection: String?,
54+
selectionArgs: Array<out String>?
55+
): Int = 0
56+
}

app/src/main/java/ru/hepolise/volumekeytrackcontrol/receiver/HookBroadcastReceiver.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package ru.hepolise.volumekeytrackcontrol.util
22

3-
import ru.hepolise.volumekeytrackcontrolmodule.BuildConfig
4-
53
object Constants {
64
const val GITHUB_URL = "https://github.com/Hepolise/VolumeKeyTrackControlModule"
75
const val GITHUB_NEW_ISSUE_URL =
86
"https://github.com/Hepolise/VolumeKeyTrackControlModule/issues/new/choose"
97
const val LSPOSED_GITHUB_URL =
108
"https://github.com/JingMatrix/LSPosed/actions/workflows/core.yml"
119

12-
const val HOOK_UPDATE = BuildConfig.APPLICATION_ID + ".HOOK_UPDATE"
13-
const val HOOKED = "hooked"
14-
const val INCREMENT_LAUNCH_COUNT = "incrementLaunchCount"
10+
const val SET_HOOKED = "set_hooked"
11+
const val INCREMENT_LAUNCH_COUNT = "increment_launch_count"
1512
}

0 commit comments

Comments
 (0)