From 3bddc056d803de7ded2541a9b165c9219196bd7d Mon Sep 17 00:00:00 2001 From: Bonian Chen Date: Wed, 27 Apr 2022 00:03:20 +0000 Subject: [PATCH] Revert "[Settings] Code refactor for combining TelephonyCallback with Lifecycle" This reverts commit aeac0619dff98dbc1ca086da3178caf5a7d35034. Reason for revert: merge failure in other branches Change-Id: I601e674be41fc5d4c97bd8db54ab1982377455f1 --- .../LifecycleCallbackTelephonyAdapter.java | 72 ------------ ...LifecycleCallbackTelephonyAdapterTest.java | 105 ------------------ 2 files changed, 177 deletions(-) delete mode 100644 src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java delete 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 deleted file mode 100644 index 0fae4f27aaf..00000000000 --- a/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapter.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 deleted file mode 100644 index be940f2fa95..00000000000 --- a/tests/unit/src/com/android/settings/network/helper/LifecycleCallbackTelephonyAdapterTest.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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) {} - } -}