Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 94a8a518 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Avoid crash caused by SettingsPreferenceService

Also add log to identify which PreferenceMetadata has problem.

Fix: 418876153
Flag: com.android.settingslib.flags.settings_catalyst
Test: manual
Change-Id: I6979e1f02589f215b803b0436e6de5229bd9a72d
parent b0dac768
Loading
Loading
Loading
Loading
+15 −10
Original line number Diff line number Diff line
@@ -319,6 +319,7 @@ private constructor(
        metadata: PreferenceMetadata,
        isRoot: Boolean,
    ) =
        try {
            metadata
                .toProto(context, callingPid, callingUid, screenMetadata, isRoot, request.flags)
                .also {
@@ -331,6 +332,10 @@ private constructor(
                        }
                    }
                }
        } catch (e: RuntimeException) {
            Log.e(TAG, "Fail to convert $screenMetadata $metadata", e)
            throw e
        }

    private suspend fun String?.toActionTarget(extras: Bundle?): ActionTarget? {
        if (this.isNullOrEmpty()) return null
+50 −31
Original line number Diff line number Diff line
@@ -43,13 +43,13 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.BAKLAVA)
class SettingsPreferenceService(
class SettingsPreferenceService
@JvmOverloads
constructor(
    private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO),
    metricsLogger: PreferenceRemoteOpMetricsLogger?,
    metricsLogger: PreferenceRemoteOpMetricsLogger? = null,
) : android.service.settings.preferences.SettingsPreferenceService() {

    constructor() : this(CoroutineScope(SupervisorJob() + Dispatchers.IO), null)

    private val getApiHandler: PreferenceGetterApiHandler
    private val setApiHandler: PreferenceSetterApiHandler
    private val graphApi: GetPreferenceGraphApiHandler
@@ -81,6 +81,7 @@ class SettingsPreferenceService(
        val callingUid = Binder.getCallingUid()
        Log.i(TAG, "GetAllPreferenceMetadata pid=$callingPid uid=$callingUid")
        scope.launch {
            try {
                val graphProto =
                    graphApi.invoke(
                        application,
@@ -91,6 +92,10 @@ class SettingsPreferenceService(
                val result =
                    transformCatalystGetMetadataResponse(this@SettingsPreferenceService, graphProto)
                callback.onResult(result)
            } catch (e: RuntimeException) {
                Log.e(TAG, "GetAllPreferenceMetadata pid=$callingPid uid=$callingUid", e)
                callback.onError(e)
            }
        }
    }

@@ -103,15 +108,24 @@ class SettingsPreferenceService(
        val callingUid = Binder.getCallingUid()
        Log.i(TAG, "GetPreferenceValue pid=$callingPid uid=$callingUid")
        scope.launch {
            try {
                val apiRequest = transformFrameworkGetValueRequest(request)
                val response = getApiHandler.invoke(application, callingPid, callingUid, apiRequest)
                val result =
                transformCatalystGetValueResponse(this@SettingsPreferenceService, request, response)
                    transformCatalystGetValueResponse(
                        this@SettingsPreferenceService,
                        request,
                        response,
                    )
                if (result == null) {
                    callback.onError(IllegalStateException("No response"))
                } else {
                    callback.onResult(result)
                }
            } catch (e: RuntimeException) {
                Log.e(TAG, "GetPreferenceValue pid=$callingPid uid=$callingUid", e)
                callback.onError(e)
            }
        }
    }

@@ -124,16 +138,21 @@ class SettingsPreferenceService(
        val callingUid = Binder.getCallingUid()
        Log.i(TAG, "SetPreferenceValue pid=$callingPid uid=$callingUid")
        scope.launch {
            try {
                val apiRequest = transformFrameworkSetValueRequest(request)
                if (apiRequest == null) {
                    callback.onResult(
                        SetValueResult.Builder(SetValueResult.RESULT_INVALID_REQUEST).build()
                    )
                } else {
                val response = setApiHandler.invoke(application, callingPid, callingUid, apiRequest)

                    val response =
                        setApiHandler.invoke(application, callingPid, callingUid, apiRequest)
                    callback.onResult(transformCatalystSetValueResponse(response))
                }
            } catch (e: RuntimeException) {
                Log.e(TAG, "SetPreferenceValue pid=$callingPid uid=$callingUid", e)
                callback.onError(e)
            }
        }
    }