From 394cec3c77b140c7b0f05f3dd4a5c7c1cc53b524 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 8 May 2023 20:31:56 +0800 Subject: [PATCH] Fix ANR of SimSlotChangeService The JobService.onStartJob() is run on main thread, should return in time instead of wait for job complete, otherwise will cause ANR. Not wait the job finish, and call jobFinished() when finished to fix this issue. Fix: 277031144 Test: Manual check with log that onStartJob() is returned in time Test: Manual check with log that job is not run in main thread Change-Id: I1e83363ca87a29fb9424100bf99b3f2d212fd9d0 --- .../sim/receivers/SimSlotChangeService.java | 77 ------------------- .../sim/receivers/SimSlotChangeService.kt | 68 ++++++++++++++++ 2 files changed, 68 insertions(+), 77 deletions(-) delete mode 100644 src/com/android/settings/sim/receivers/SimSlotChangeService.java create mode 100644 src/com/android/settings/sim/receivers/SimSlotChangeService.kt diff --git a/src/com/android/settings/sim/receivers/SimSlotChangeService.java b/src/com/android/settings/sim/receivers/SimSlotChangeService.java deleted file mode 100644 index deaecaf679e..00000000000 --- a/src/com/android/settings/sim/receivers/SimSlotChangeService.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.sim.receivers; - -import android.app.job.JobInfo; -import android.app.job.JobParameters; -import android.app.job.JobScheduler; -import android.app.job.JobService; -import android.content.ComponentName; -import android.content.Context; -import android.os.Handler; -import android.os.HandlerThread; -import android.util.Log; - -import com.android.settings.R; -import com.android.settingslib.utils.ThreadUtils; - -import java.util.concurrent.Phaser; - -/** A JobService work on SIM slot change. */ -public class SimSlotChangeService extends JobService { - - private static final String TAG = "SimSlotChangeService"; - - /** - * Schedules a service to work on SIM slot change. - * - * @param context is the caller context. - */ - public static void scheduleSimSlotChange(Context context) { - Context appContext = context.getApplicationContext(); - JobScheduler jobScheduler = appContext.getSystemService(JobScheduler.class); - ComponentName component = new ComponentName(appContext, SimSlotChangeService.class); - - jobScheduler.schedule( - new JobInfo.Builder(R.integer.sim_slot_changed, component).build()); - } - - @Override - public boolean onStartJob(JobParameters params) { - - HandlerThread thread = new HandlerThread(TAG); - thread.start(); - final Phaser blocker = new Phaser(1); - Handler handler = new Handler(thread.getLooper()); - handler.post(() -> { - try { - SimSlotChangeReceiver.runOnBackgroundThread(this); - } catch (Throwable exception) { - Log.e(TAG, "Exception running job", exception); - } - blocker.arrive(); - }); - blocker.awaitAdvance(0); - thread.quit(); - return false; - } - - @Override - public boolean onStopJob(JobParameters params) { - return false; - } -} diff --git a/src/com/android/settings/sim/receivers/SimSlotChangeService.kt b/src/com/android/settings/sim/receivers/SimSlotChangeService.kt new file mode 100644 index 00000000000..7ecb4830af4 --- /dev/null +++ b/src/com/android/settings/sim/receivers/SimSlotChangeService.kt @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.sim.receivers + +import android.app.job.JobInfo +import android.app.job.JobParameters +import android.app.job.JobScheduler +import android.app.job.JobService +import android.content.ComponentName +import android.content.Context +import android.util.Log +import com.android.settings.R +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch + +/** A JobService work on SIM slot change. */ +class SimSlotChangeService : JobService() { + private var job: Job? = null + + override fun onStartJob(params: JobParameters): Boolean { + job = CoroutineScope(Dispatchers.Default + SupervisorJob()).launch { + try { + SimSlotChangeReceiver.runOnBackgroundThread(this@SimSlotChangeService) + } catch (exception: Throwable) { + Log.e(TAG, "Exception running job", exception) + } + jobFinished(params, false) + } + return true + } + + override fun onStopJob(params: JobParameters): Boolean { + job?.cancel() + return false + } + + companion object { + private const val TAG = "SimSlotChangeService" + + /** + * Schedules a service to work on SIM slot change. + * + * @param context is the caller context. + */ + @JvmStatic + fun scheduleSimSlotChange(context: Context) { + val component = ComponentName(context, SimSlotChangeService::class.java) + val jobScheduler = context.getSystemService(JobScheduler::class.java)!! + jobScheduler.schedule(JobInfo.Builder(R.integer.sim_slot_changed, component).build()) + } + } +} \ No newline at end of file