Merge "Revert "[Settings] Code refactor for combining TelephonyCallback with Lifecycle"" into tm-dev am: c28169adb9
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17950674 Change-Id: I08aee9acbacc7d48ddb69c00f56d8111e1e34393 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -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<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();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -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<Object> mResult;
|
|
||||||
private LifecycleCallbackTelephonyAdapter<Object> mAdapter;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
|
|
||||||
mResult = new AtomicReference<Object>();
|
|
||||||
mTestCallback = new TestCallback();
|
|
||||||
|
|
||||||
doNothing().when(mTelMgr).registerTelephonyCallback(null, mTestCallback);
|
|
||||||
doNothing().when(mTelMgr).unregisterTelephonyCallback(mTestCallback);
|
|
||||||
|
|
||||||
mAdapter = new LifecycleCallbackTelephonyAdapter<Object>(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) {}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user