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 17 package com.android.settings.network.helper; 18 19 import android.telephony.TelephonyCallback; 20 import android.telephony.TelephonyManager; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.VisibleForTesting; 24 import androidx.lifecycle.Lifecycle; 25 26 import java.util.concurrent.Executor; 27 import java.util.function.Consumer; 28 29 /** 30 * A {@link LifecycleCallbackConverter} for supporting the register/unregister work for 31 * {@link TelephonyCallback}. 32 */ 33 @VisibleForTesting 34 public class LifecycleCallbackTelephonyAdapter<T> extends LifecycleCallbackConverter<T> { 35 private static final String TAG = "LifecycleCallbackTelephony"; 36 37 private final Runnable mRegisterCallback; 38 private final Runnable mUnRegisterCallback; 39 40 /** 41 * Constructor 42 * @param lifecycle {@link Lifecycle} to monitor 43 * @param telephonyManager {@link TelephonyManager} to interact with 44 * @param telephonyCallback {@link TelephonyCallback} 45 * @param executor {@link Executor} for receiving the notify from telephony framework. 46 * @param resultCallback for the result from {@link TelephonyCallback} 47 */ 48 @VisibleForTesting LifecycleCallbackTelephonyAdapter(@onNull Lifecycle lifecycle, @NonNull TelephonyManager telephonyManager, @NonNull TelephonyCallback telephonyCallback, Executor executor, @NonNull Consumer<T> resultCallback)49 public LifecycleCallbackTelephonyAdapter(@NonNull Lifecycle lifecycle, 50 @NonNull TelephonyManager telephonyManager, 51 @NonNull TelephonyCallback telephonyCallback, 52 Executor executor, @NonNull Consumer<T> resultCallback) { 53 super(lifecycle, resultCallback); 54 55 // Register operation 56 mRegisterCallback = () -> { 57 telephonyManager.registerTelephonyCallback(executor, telephonyCallback); 58 }; 59 60 // Un-Register operation 61 mUnRegisterCallback = () -> { 62 telephonyManager.unregisterTelephonyCallback(telephonyCallback); 63 }; 64 } 65 66 @Override setCallbackActive(boolean isActive)67 public void setCallbackActive(boolean isActive) { 68 super.setCallbackActive(isActive); 69 Runnable op = (isActive) ? mRegisterCallback : mUnRegisterCallback; 70 op.run(); 71 } 72 } 73