1 /* 2 * Copyright (C) 2024 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 17 package com.android.settings.sim 18 19 import android.app.job.JobInfo 20 import android.app.job.JobParameters 21 import android.app.job.JobScheduler 22 import android.app.job.JobService 23 import android.content.ComponentName 24 import android.content.Context 25 import android.content.Intent 26 import android.util.Log 27 import com.android.settings.R 28 import kotlinx.coroutines.CoroutineScope 29 import kotlinx.coroutines.Dispatchers 30 import kotlinx.coroutines.Job 31 import kotlinx.coroutines.SupervisorJob 32 import kotlinx.coroutines.launch 33 34 /** A JobService work on primary subscription list changed. */ 35 class PrimarySubscriptionListChangedService : JobService() { 36 private var job: Job? = null 37 onStartJobnull38 override fun onStartJob(params: JobParameters): Boolean { 39 job = CoroutineScope(Dispatchers.Default + SupervisorJob()).launch { 40 try { 41 val intent = Intent() 42 intent.putExtras(params.transientExtras) 43 SimSelectNotification.onPrimarySubscriptionListChanged( 44 this@PrimarySubscriptionListChangedService, 45 intent 46 ) 47 } catch (exception: Throwable) { 48 Log.e(TAG, "Exception running job", exception) 49 } 50 jobFinished(params, false) 51 } 52 return true 53 } 54 onStopJobnull55 override fun onStopJob(params: JobParameters): Boolean { 56 job?.cancel() 57 return false 58 } 59 60 companion object { 61 private const val TAG = "PrimarySubscriptionListChangedService" 62 63 /** 64 * Schedules a service to work on primary subscription changed. 65 * 66 * @param context is the caller context. 67 */ 68 @JvmStatic scheduleJobnull69 fun scheduleJob(context: Context, intent: Intent) { 70 val component = 71 ComponentName(context, PrimarySubscriptionListChangedService::class.java) 72 val jobScheduler = context.getSystemService(JobScheduler::class.java)!! 73 74 val jobInfoBuilder = 75 JobInfo.Builder(R.integer.primary_subscription_list_changed, component) 76 intent.extras?.let { 77 jobInfoBuilder.setTransientExtras(it) 78 } 79 jobScheduler.schedule(jobInfoBuilder.build()) 80 } 81 } 82 }