@@ -8,6 +8,7 @@ import android.os.Binder
88import android.os.IBinder
99import android.os.Parcel
1010import android.util.Log
11+ import dev.lexip.hecate.analytics.AnalyticsLogger
1112import rikka.shizuku.Shizuku
1213
1314object ShizukuManager {
@@ -45,19 +46,26 @@ object ShizukuManager {
4546
4647 fun isBinderReady (): Boolean = binderReady
4748
48- fun hasPermission (): Boolean {
49+ fun hasPermission (context : Context ): Boolean {
4950 if (Shizuku .isPreV11()) return false
5051 if (! binderReady) return false
5152
5253 return try {
5354 Shizuku .checkSelfPermission() == PackageManager .PERMISSION_GRANTED
5455 } catch (t: Throwable ) {
5556 Log .w(TAG , " Failed to check Shizuku permission" , t)
57+ AnalyticsLogger .logUnexpectedShizukuError(
58+ context = context,
59+ operation = " check_permission" ,
60+ stage = " check_self_permission" ,
61+ throwable = t,
62+ binderReady = binderReady
63+ )
5664 false
5765 }
5866 }
5967
60- fun requestPermission (@Suppress( " UNUSED_PARAMETER " ) context : Context ) {
68+ fun requestPermission (context : Context ) {
6169 if (Shizuku .isPreV11()) {
6270 Log .w(TAG , " Ignoring Shizuku.requestPermission on pre-v11" )
6371 return
@@ -82,10 +90,10 @@ object ShizukuManager {
8290 fun buildGrantWriteSecureSettingsCommand (packageName : String ): String =
8391 " pm grant $packageName android.permission.WRITE_SECURE_SETTINGS"
8492
85- fun executeGrantViaShizuku (packageName : String ): GrantResult {
93+ fun executeGrantViaShizuku (context : Context , packageName : String ): GrantResult {
8694 if (Shizuku .isPreV11()) return GrantResult .ServiceNotRunning
8795 if (! binderReady) return GrantResult .ServiceNotRunning
88- if (! hasPermission()) return GrantResult .NotAuthorized
96+ if (! hasPermission(context )) return GrantResult .NotAuthorized
8997
9098 val cmd = buildGrantWriteSecureSettingsCommand(packageName)
9199
@@ -94,7 +102,13 @@ object ShizukuManager {
94102 var result: GrantResult = GrantResult .Unexpected (IllegalStateException (" No result" ))
95103
96104 val args = createGrantServiceArgs()
97- val connection = createGrantServiceConnection(args, cmd, monitor) { grantResult ->
105+ val connection = createGrantServiceConnection(
106+ context,
107+ args,
108+ cmd,
109+ monitor,
110+ packageName
111+ ) { grantResult ->
98112 result = grantResult
99113 }
100114
@@ -104,6 +118,14 @@ object ShizukuManager {
104118 result
105119 } catch (t: Throwable ) {
106120 Log .e(TAG , " Grant via Shizuku failed" , t)
121+ AnalyticsLogger .logUnexpectedShizukuError(
122+ context = context,
123+ operation = " grant_write_secure_settings" ,
124+ stage = " execute_grant_via_shizuku" ,
125+ throwable = t,
126+ binderReady = binderReady,
127+ packageName = packageName
128+ )
107129 GrantResult .Unexpected (t)
108130 }
109131 }
@@ -118,9 +140,11 @@ object ShizukuManager {
118140 }
119141
120142 private fun createGrantServiceConnection (
143+ context : Context ,
121144 args : Shizuku .UserServiceArgs ,
122145 cmd : String ,
123146 monitor : Object ,
147+ packageName : String ,
124148 onResult : (GrantResult ) -> Unit
125149 ): ServiceConnection {
126150 return object : ServiceConnection {
@@ -137,13 +161,31 @@ object ShizukuManager {
137161 } catch (t: Throwable ) {
138162 when (t) {
139163 is SecurityException -> GrantResult .NotAuthorized
140- else -> GrantResult .Unexpected (t)
164+ else -> {
165+ AnalyticsLogger .logUnexpectedShizukuError(
166+ context = context,
167+ operation = " grant_write_secure_settings" ,
168+ stage = " on_service_connected_execute" ,
169+ throwable = t,
170+ binderReady = binderReady,
171+ packageName = packageName
172+ )
173+ GrantResult .Unexpected (t)
174+ }
141175 }
142176 } finally {
143177 try {
144178 Shizuku .unbindUserService(args, this , true )
145179 } catch (t: Throwable ) {
146180 Log .w(TAG , " Error while unbinding Shizuku user service" , t)
181+ AnalyticsLogger .logUnexpectedShizukuError(
182+ context = context,
183+ operation = " grant_write_secure_settings" ,
184+ stage = " unbind_user_service" ,
185+ throwable = t,
186+ binderReady = binderReady,
187+ packageName = packageName
188+ )
147189 }
148190 synchronized(monitor) {
149191 monitor.notifyAll()
0 commit comments