1 /* 2 * Copyright (C) 2023 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.systemui.statusbar.pipeline.mobile.util 18 19 import android.annotation.SuppressLint 20 import android.content.Context 21 import android.telephony.SubscriptionInfo 22 import android.telephony.SubscriptionManager 23 import com.android.systemui.dagger.qualifiers.Application 24 import com.android.systemui.dagger.qualifiers.Background 25 import javax.inject.Inject 26 import kotlinx.coroutines.CoroutineDispatcher 27 import kotlinx.coroutines.withContext 28 29 interface SubscriptionManagerProxy { getDefaultDataSubscriptionIdnull30 fun getDefaultDataSubscriptionId(): Int 31 fun isValidSubscriptionId(subId: Int): Boolean 32 suspend fun getActiveSubscriptionInfo(subId: Int): SubscriptionInfo? 33 } 34 35 /** Injectable proxy class for [SubscriptionManager]'s static methods */ 36 class SubscriptionManagerProxyImpl 37 @Inject 38 constructor( 39 @Application private val applicationContext: Context, 40 @Background private val backgroundDispatcher: CoroutineDispatcher, 41 private val subscriptionManager: SubscriptionManager, 42 ) : SubscriptionManagerProxy { 43 /** The system default data subscription id, or INVALID_SUBSCRIPTION_ID on error */ 44 override fun getDefaultDataSubscriptionId() = SubscriptionManager.getDefaultDataSubscriptionId() 45 46 override fun isValidSubscriptionId(subId: Int): Boolean { 47 return SubscriptionManager.isValidSubscriptionId(subId) 48 } 49 50 @SuppressLint("MissingPermission") 51 override suspend fun getActiveSubscriptionInfo(subId: Int): SubscriptionInfo? { 52 return withContext(backgroundDispatcher) { 53 subscriptionManager.getActiveSubscriptionInfo(subId) 54 } 55 } 56 } 57