From aeac0619dff98dbc1ca086da3178caf5a7d35034 Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Thu, 21 Apr 2022 11:04:44 +0800 Subject: [PATCH] [Settings] Code refactor for combining TelephonyCallback with Lifecycle This is a helper class which may support monitoring the TelephonyCallback under the condition of Lifecycle state STARTED or RESUMED. Bug: 229689535 Test: unit test Change-Id: I6d26bee604d9559e207e107b2f26583f700f8e0a (cherry picked from commit 7c5259efad7640fdd9b5d94110cb3073e8a9c1d2) --- .../LifecycleCallbackTelephonyAdapter.java | 72 ++++++++++++ ...LifecycleCallbackTelephonyAdapterTest.java | 105 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java create mode 100644 tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java diff --git a/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java b/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java new file mode 100644 index 00000000000..0fae4f27aaf --- /dev/null +++ b/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java @@ -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 extends LifecycleCallbackConverter { + 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 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(); + } +} diff --git a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java new file mode 100644 index 00000000000..be940f2fa95 --- /dev/null +++ b/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java @@ -0,0 +1,105 @@ +/* + * 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 static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import android.telephony.TelephonyCallback; +import android.telephony.TelephonyManager; + +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LifecycleRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.atomic.AtomicReference; + +@RunWith(AndroidJUnit4.class) +public class LifecycleCallbackTelephonyAdapterTest implements LifecycleOwner { + + private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this); + + @Mock + private TelephonyManager mTelMgr; + + private TestCallback mTestCallback; + private AtomicReference mResult; + private LifecycleCallbackTelephonyAdapter mAdapter; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mResult = new AtomicReference(); + mTestCallback = new TestCallback(); + + doNothing().when(mTelMgr).registerTelephonyCallback(null, mTestCallback); + doNothing().when(mTelMgr).unregisterTelephonyCallback(mTestCallback); + + mAdapter = new LifecycleCallbackTelephonyAdapter(getLifecycle(), mTelMgr, + mTestCallback, null, result -> mResult.set(result)); + } + + public Lifecycle getLifecycle() { + return mRegistry; + } + + @Test + public void telephonyCallback_register_whenActive() { + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + + verify(mTelMgr, never()).registerTelephonyCallback(anyObject(), anyObject()); + + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); + + verify(mTelMgr).registerTelephonyCallback(anyObject(), anyObject()); + } + + @Test + public void telephonyCallback_unregister_whenInActive() { + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + + verify(mTelMgr, never()).unregisterTelephonyCallback(anyObject()); + + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); + + verify(mTelMgr, never()).unregisterTelephonyCallback(anyObject()); + + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + + verify(mTelMgr).unregisterTelephonyCallback(anyObject()); + + mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + + verify(mTelMgr, times(1)).unregisterTelephonyCallback(anyObject()); + } + + protected static class TestCallback extends TelephonyCallback + implements TelephonyCallback.CallStateListener { + @Override + public void onCallStateChanged(int state) {} + } +}