Merge "[Settings] Code refactor for combining TelephonyCallback with Lifecycle" into tm-dev am: 3fc05a917e am: 6483721f20

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17903597

Change-Id: I1ee76a888432e9d153ecd50cfbf6e7197ef5a8df
Ignore-AOSP-First: this is an automerge
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Bonian Chen
2022-04-26 11:35:12 +00:00
committed by Automerger Merge Worker
2 changed files with 177 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
/*
* 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.network.helper;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyManager;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* A {@link LifecycleCallbackConverter} for supporting the register/unregister work for
* {@link TelephonyCallback}.
*/
@VisibleForTesting
public class LifecycleCallbackTelephonyAdapter<T> extends LifecycleCallbackConverter<T> {
private static final String TAG = "LifecycleCallbackTelephony";
private final Runnable mRegisterCallback;
private final Runnable mUnRegisterCallback;
/**
* Constructor
* @param lifecycle {@link Lifecycle} to monitor
* @param telephonyManager {@link TelephonyManager} to interact with
* @param telephonyCallback {@link TelephonyCallback}
* @param executor {@link Executor} for receiving the notify from telephony framework.
* @param resultCallback for the result from {@link TelephonyCallback}
*/
@VisibleForTesting
public LifecycleCallbackTelephonyAdapter(@NonNull Lifecycle lifecycle,
@NonNull TelephonyManager telephonyManager,
@NonNull TelephonyCallback telephonyCallback,
Executor executor, @NonNull Consumer<T> resultCallback) {
super(lifecycle, resultCallback);
// Register operation
mRegisterCallback = () -> {
telephonyManager.registerTelephonyCallback(executor, telephonyCallback);
};
// Un-Register operation
mUnRegisterCallback = () -> {
telephonyManager.unregisterTelephonyCallback(telephonyCallback);
};
}
@Override
public void setCallbackActive(boolean isActive) {
super.setCallbackActive(isActive);
Runnable op = (isActive) ? mRegisterCallback : mUnRegisterCallback;
op.run();
}
}