1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.sim.receivers 17 18 import android.app.job.JobInfo 19 import android.app.job.JobParameters 20 import android.app.job.JobScheduler 21 import android.app.job.JobService 22 import android.content.ComponentName 23 import android.content.Context 24 import android.util.Log 25 import com.android.settings.R 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.Dispatchers 28 import kotlinx.coroutines.Job 29 import kotlinx.coroutines.SupervisorJob 30 import kotlinx.coroutines.launch 31 32 /** A JobService work on SIM slot change. */ 33 class SimSlotChangeService : JobService() { 34 private var job: Job? = null 35 onStartJobnull36 override fun onStartJob(params: JobParameters): Boolean { 37 job = CoroutineScope(Dispatchers.Default + SupervisorJob()).launch { 38 try { 39 SimSlotChangeReceiver.runOnBackgroundThread(this@SimSlotChangeService) 40 } catch (exception: Throwable) { 41 Log.e(TAG, "Exception running job", exception) 42 } 43 jobFinished(params, false) 44 } 45 return true 46 } 47 onStopJobnull48 override fun onStopJob(params: JobParameters): Boolean { 49 job?.cancel() 50 return false 51 } 52 53 companion object { 54 private const val TAG = "SimSlotChangeService" 55 56 /** 57 * Schedules a service to work on SIM slot change. 58 * 59 * @param context is the caller context. 60 */ 61 @JvmStatic scheduleSimSlotChangenull62 fun scheduleSimSlotChange(context: Context) { 63 val component = ComponentName(context, SimSlotChangeService::class.java) 64 val jobScheduler = context.getSystemService(JobScheduler::class.java)!! 65 jobScheduler.schedule(JobInfo.Builder(R.integer.sim_slot_changed, component).build()) 66 } 67 } 68 }