Clean up ServiceStateStatus
And LifecycleCallback*, which is no longer used. Bug: 299068234 Test: m Settings Change-Id: I29ce5609db2a4b249477da1bcbb0502fa04902fa
This commit is contained in:
@@ -1,100 +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 androidx.annotation.MainThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleEventObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* A {@link androidx.lifecycle.LifecycleObserver} implementation of adapter over callback.
|
||||
*
|
||||
* Which including:
|
||||
* 1. Request to active callback when Lifecycle.State.STARTED
|
||||
* 2. Request to inactive callback when Lifecycle.State.STOPPED
|
||||
* 3. Close (no further resume) when Lifecycle.State.DESTROYED
|
||||
*/
|
||||
@VisibleForTesting
|
||||
abstract class LifecycleCallbackAdapter implements LifecycleEventObserver, AutoCloseable {
|
||||
private static final String TAG = "LifecycleCallbackAdapter";
|
||||
private AtomicReference<Lifecycle> mLifecycle = new AtomicReference<Lifecycle>();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lifecycle {@link Lifecycle} to monitor
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected LifecycleCallbackAdapter(@NonNull Lifecycle lifecycle) {
|
||||
mLifecycle.set(lifecycle);
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get {@link Lifecycle} under monitor.
|
||||
* @return {@link Lifecycle}. Return {@code null} when closed.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public Lifecycle getLifecycle() {
|
||||
return mLifecycle.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check current callback status.
|
||||
* @return true when callback is active.
|
||||
*/
|
||||
public abstract boolean isCallbackActive();
|
||||
|
||||
/**
|
||||
* Change callback status.
|
||||
* @param isActive true to active callback, otherwise inactive.
|
||||
*/
|
||||
public abstract void setCallbackActive(boolean isActive);
|
||||
|
||||
/**
|
||||
* Implementation of LifecycleEventObserver.
|
||||
*/
|
||||
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
|
||||
if (mLifecycle.get() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Lifecycle.State state = event.getTargetState();
|
||||
boolean expectCallbackActive = state.isAtLeast(Lifecycle.State.STARTED);
|
||||
if (expectCallbackActive != isCallbackActive()) {
|
||||
setCallbackActive(expectCallbackActive);
|
||||
}
|
||||
if (state == Lifecycle.State.DESTROYED) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of AutoCloseable.
|
||||
*/
|
||||
@MainThread
|
||||
public void close() {
|
||||
Lifecycle lifecycle = mLifecycle.getAndSet(null);
|
||||
if (lifecycle != null) {
|
||||
lifecycle.removeObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,132 +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 androidx.annotation.AnyThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
|
||||
import com.android.settingslib.utils.ThreadUtils;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A {@link LifecycleCallbackAdapter} which support carrying a result from any threads back to UI
|
||||
* thread through {@link #postResult(T)}.
|
||||
*
|
||||
* A {@link Consumer<T>} would be invoked from UI thread for further processing on the result.
|
||||
*
|
||||
* Note: Result not in STARTED or RESUMED stage will be discarded silently.
|
||||
* This is to align with the criteria set within
|
||||
* {@link LifecycleCallbackAdapter#onStateChanged()}.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public class LifecycleCallbackConverter<T> extends LifecycleCallbackAdapter {
|
||||
private static final String TAG = "LifecycleCallbackConverter";
|
||||
|
||||
private final Thread mUiThread;
|
||||
private final Consumer<T> mResultCallback;
|
||||
|
||||
/**
|
||||
* A record of number of active status change.
|
||||
* Even numbers (0, 2, 4, 6 ...) are inactive status.
|
||||
* Odd numbers (1, 3, 5, 7 ...) are active status.
|
||||
*/
|
||||
private final AtomicLong mNumberOfActiveStatusChange = new AtomicLong();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param lifecycle {@link Lifecycle} to monitor
|
||||
* @param resultCallback for further processing the result
|
||||
*/
|
||||
@VisibleForTesting
|
||||
@UiThread
|
||||
public LifecycleCallbackConverter(
|
||||
@NonNull Lifecycle lifecycle, @NonNull Consumer<T> resultCallback) {
|
||||
super(lifecycle);
|
||||
mUiThread = Thread.currentThread();
|
||||
mResultCallback = resultCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a result (from any thread) back to UI thread.
|
||||
*
|
||||
* @param result the object ready to be passed back to {@link Consumer<T>}.
|
||||
*/
|
||||
@AnyThread
|
||||
@VisibleForTesting
|
||||
public void postResult(T result) {
|
||||
/**
|
||||
* Since mNumberOfActiveStatusChange only increase, it is a concept of sequence number.
|
||||
* Carry it when sending data in between different threads allow to verify if the data
|
||||
* has arrived on time. And drop the data when expired.
|
||||
*/
|
||||
long currentNumberOfChange = mNumberOfActiveStatusChange.get();
|
||||
if (Thread.currentThread() == mUiThread) {
|
||||
dispatchExtResult(currentNumberOfChange, result); // Dispatch directly
|
||||
} else {
|
||||
postResultToUiThread(currentNumberOfChange, result);
|
||||
}
|
||||
}
|
||||
|
||||
@AnyThread
|
||||
protected void postResultToUiThread(long numberOfStatusChange, T result) {
|
||||
ThreadUtils.postOnMainThread(() -> dispatchExtResult(numberOfStatusChange, result));
|
||||
}
|
||||
|
||||
@UiThread
|
||||
protected void dispatchExtResult(long numberOfStatusChange, T result) {
|
||||
/**
|
||||
* For a postResult() sending in between different threads, not only create a latency
|
||||
* but also enqueued into main UI thread for dispatch.
|
||||
*
|
||||
* To align behavior within {@link LifecycleCallbackAdapter#onStateChanged()},
|
||||
* some checking on both numberOfStatusChange and {@link Lifecycle} status are required.
|
||||
*/
|
||||
if (isActiveStatus(numberOfStatusChange)
|
||||
&& (numberOfStatusChange == mNumberOfActiveStatusChange.get())
|
||||
&& getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
||||
mResultCallback.accept(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static final boolean isActiveStatus(long numberOfStatusChange) {
|
||||
return ((numberOfStatusChange & 1L) != 0L);
|
||||
}
|
||||
|
||||
/* Implementation of LifecycleCallbackAdapter */
|
||||
@UiThread
|
||||
public boolean isCallbackActive() {
|
||||
return isActiveStatus(mNumberOfActiveStatusChange.get());
|
||||
}
|
||||
|
||||
/* Implementation of LifecycleCallbackAdapter */
|
||||
@UiThread
|
||||
public void setCallbackActive(boolean updatedActiveStatus) {
|
||||
/**
|
||||
* Make sure only increase when active status got changed.
|
||||
* This is to implement the definition of mNumberOfActiveStatusChange.
|
||||
*/
|
||||
if (isCallbackActive() != updatedActiveStatus) {
|
||||
mNumberOfActiveStatusChange.getAndIncrement();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,104 +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.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Handler;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A {@link BroadcastReceiver} for {@link Intent}.
|
||||
*
|
||||
* This is {@link BroadcastReceiver} supported by {@link LifecycleCallbackConverter},
|
||||
* and only register when state is either START or RESUME.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public class LifecycleCallbackIntentReceiver extends LifecycleCallbackConverter<Intent> {
|
||||
private static final String TAG = "LifecycleCallbackIntentReceiver";
|
||||
|
||||
@VisibleForTesting
|
||||
protected final BroadcastReceiver mReceiver;
|
||||
|
||||
private final Runnable mRegisterCallback;
|
||||
private final Runnable mUnRegisterCallback;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lifecycle {@link Lifecycle} to monitor
|
||||
* @param context for this BroadcastReceiver
|
||||
* @param filter the IntentFilter for BroadcastReceiver
|
||||
* @param broadcastPermission for permission when listening
|
||||
* @param scheduler for running in background thread
|
||||
* @param resultCallback for the Intent from BroadcastReceiver
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public LifecycleCallbackIntentReceiver(@NonNull Lifecycle lifecycle,
|
||||
@NonNull Context context, @NonNull IntentFilter filter,
|
||||
String broadcastPermission, Handler scheduler,
|
||||
@NonNull Consumer<Intent> resultCallback) {
|
||||
super(lifecycle, resultCallback);
|
||||
|
||||
// BroadcastReceiver
|
||||
mReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (isInitialStickyBroadcast()) {
|
||||
return;
|
||||
}
|
||||
final String action = intent.getAction();
|
||||
if ((action == null) || (action.length() <= 0)) {
|
||||
return;
|
||||
}
|
||||
postResult(intent);
|
||||
}
|
||||
};
|
||||
|
||||
// Register operation
|
||||
mRegisterCallback = () -> {
|
||||
Intent initIntent = context.registerReceiver(mReceiver,
|
||||
filter, broadcastPermission, scheduler);
|
||||
if (initIntent != null) {
|
||||
postResult(initIntent);
|
||||
}
|
||||
};
|
||||
|
||||
// Un-Register operation
|
||||
mUnRegisterCallback = () -> {
|
||||
context.unregisterReceiver(mReceiver);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCallbackActive(boolean isActive) {
|
||||
super.setCallbackActive(isActive);
|
||||
Runnable op = (isActive) ? mRegisterCallback : mUnRegisterCallback;
|
||||
op.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
if (isCallbackActive()) {
|
||||
setCallbackActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 android.telephony.ServiceState;
|
||||
import android.telephony.TelephonyCallback;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A {@link LiveData} as a mapping of allowed network types reported from {@link TelephonyCallback}.
|
||||
* Only got update when Lifecycle.State is considered as STARTED or RESUMED.
|
||||
*
|
||||
* {@code null} when status unknown. Other values are {@link ServiceState}.
|
||||
*
|
||||
* @deprecated Please us {@link com.android.settings.network.telephony.ServiceStateFlowKt} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public class ServiceStateStatus extends LiveData<ServiceState> {
|
||||
private static final String TAG = "ServiceStateStatus";
|
||||
|
||||
@VisibleForTesting
|
||||
protected ServiceStateProducer mServiceStateProducer;
|
||||
|
||||
@VisibleForTesting
|
||||
protected LifecycleCallbackTelephonyAdapter mAdapter;
|
||||
|
||||
@VisibleForTesting
|
||||
protected Consumer<ServiceState> mLiveDataUpdater = status -> setValue(status);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lifecycle {@link Lifecycle} to monitor
|
||||
* @param telephonyManager {@link TelephonyManager} to interact with
|
||||
* @param executor {@link Executor} for receiving the notify from telephony framework.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public ServiceStateStatus(@NonNull Lifecycle lifecycle,
|
||||
@NonNull TelephonyManager telephonyManager, Executor executor) {
|
||||
super();
|
||||
|
||||
mServiceStateProducer = new ServiceStateProducer(this);
|
||||
|
||||
mAdapter = new LifecycleCallbackTelephonyAdapter<ServiceState>(lifecycle,
|
||||
telephonyManager, mServiceStateProducer, executor, mLiveDataUpdater) {
|
||||
@Override
|
||||
public void setCallbackActive(boolean isActive) {
|
||||
super.setCallbackActive(isActive);
|
||||
if (!isActive) {
|
||||
/**
|
||||
* Set to unknown status when no longer actively monitoring
|
||||
* {@link TelephonyCallback}.
|
||||
*/
|
||||
mLiveDataUpdater.accept(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of TelephonyCallback.
|
||||
*
|
||||
* Change of allowed network type will be forward to
|
||||
* {@link LifecycleCallbackTelephonyAdapter}.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected static class ServiceStateProducer extends TelephonyCallback
|
||||
implements TelephonyCallback.ServiceStateListener {
|
||||
private final ServiceStateStatus mStatus;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param status {@link ServiceStateStatus}
|
||||
*/
|
||||
public ServiceStateProducer(ServiceStateStatus status) {
|
||||
mStatus = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceStateChanged(ServiceState serviceState) {
|
||||
mStatus.mAdapter.postResult(serviceState);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,102 +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 androidx.lifecycle.LiveData;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A {@link LiveData} as a mapping of voice call state reported from {@link TelephonyCallback}.
|
||||
* Only got update when Lifecycle.State is considered as STARTED or RESUMED.
|
||||
*
|
||||
* {@code null} when status unknown. Other values are TelephonyManager#CALL_STATE_IDLE,
|
||||
* TelephonyManager#CALL_STATE_RINGING and TelephonyManager#CALL_STATE_OFFHOOK.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public class VoiceCallStatus extends LiveData<Integer> {
|
||||
private static final String TAG = "VoiceCallStatus";
|
||||
|
||||
@VisibleForTesting
|
||||
protected CallStateProducer mCallStateProducer;
|
||||
|
||||
@VisibleForTesting
|
||||
protected LifecycleCallbackTelephonyAdapter mAdapter;
|
||||
|
||||
@VisibleForTesting
|
||||
protected Consumer<Integer> mLiveDataUpdater = status -> setValue(status);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param lifecycle {@link Lifecycle} to monitor
|
||||
* @param telephonyManager {@link TelephonyManager} to interact with
|
||||
* @param executor {@link Executor} for receiving the notify from telephony framework.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public VoiceCallStatus(@NonNull Lifecycle lifecycle,
|
||||
@NonNull TelephonyManager telephonyManager, Executor executor) {
|
||||
super();
|
||||
|
||||
mCallStateProducer = new CallStateProducer(this);
|
||||
|
||||
mAdapter = new LifecycleCallbackTelephonyAdapter<Integer>(lifecycle,
|
||||
telephonyManager, mCallStateProducer, executor, mLiveDataUpdater) {
|
||||
@Override
|
||||
public void setCallbackActive(boolean isActive) {
|
||||
super.setCallbackActive(isActive);
|
||||
if (!isActive) {
|
||||
/**
|
||||
* Set to unknown status when no longer actively monitoring
|
||||
* {@link TelephonyCallback}.
|
||||
*/
|
||||
mLiveDataUpdater.accept(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of TelephonyCallback.
|
||||
*
|
||||
* Status of voice call will be forward to {@link LifecycleCallbackTelephonyAdapter}
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected static class CallStateProducer extends TelephonyCallback
|
||||
implements TelephonyCallback.CallStateListener {
|
||||
private final VoiceCallStatus mStatus;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param status {@link VoiceCallStatus}
|
||||
*/
|
||||
public CallStateProducer(VoiceCallStatus status) {
|
||||
mStatus = status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallStateChanged(int state) {
|
||||
mStatus.mAdapter.postResult(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class LifecycleCallbackAdapterTest implements LifecycleOwner {
|
||||
|
||||
private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this);
|
||||
|
||||
private TestObj mTarget;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mTarget = new TestObj(getLifecycle());
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifecycle_get_lifecycleToMonitor() {
|
||||
assertThat(mTarget.getLifecycle()).isEqualTo(mRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifecycle_stateChangeToStart_callbackActive() {
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
|
||||
assertThat(mTarget.getCallbackCount()).isEqualTo(0);
|
||||
assertThat(mTarget.isCallbackActive()).isEqualTo(Boolean.FALSE);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
assertThat(mTarget.getCallbackCount()).isEqualTo(1);
|
||||
assertThat(mTarget.isCallbackActive()).isEqualTo(Boolean.TRUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifecycle_stateChangeToStop_callbackInActive() {
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
|
||||
assertThat(mTarget.getCallbackCount()).isEqualTo(2);
|
||||
assertThat(mTarget.isCallbackActive()).isEqualTo(Boolean.FALSE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifecycle_stateChangeToDestroy_noFurtherActive() {
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
|
||||
|
||||
assertThat(mTarget.getCallbackCount()).isEqualTo(2);
|
||||
assertThat(mTarget.isCallbackActive()).isEqualTo(Boolean.FALSE);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
assertThat(mTarget.getCallbackCount()).isEqualTo(2);
|
||||
assertThat(mTarget.isCallbackActive()).isEqualTo(Boolean.FALSE);
|
||||
}
|
||||
|
||||
public static class TestObj extends LifecycleCallbackAdapter {
|
||||
boolean mIsActive;
|
||||
int mNumberOfCallback;
|
||||
|
||||
public TestObj(Lifecycle lifecycle) {
|
||||
super(lifecycle);
|
||||
}
|
||||
|
||||
public boolean isCallbackActive() {
|
||||
return mIsActive;
|
||||
}
|
||||
|
||||
public void setCallbackActive(boolean isActive) {
|
||||
mIsActive = isActive;
|
||||
mNumberOfCallback ++;
|
||||
}
|
||||
|
||||
protected int getCallbackCount() {
|
||||
return mNumberOfCallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,127 +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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
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 java.util.concurrent.Phaser;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class LifecycleCallbackConverterTest implements LifecycleOwner {
|
||||
|
||||
private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this);
|
||||
|
||||
private Object mTestData;
|
||||
private TestConsumer mConsumer;
|
||||
private LifecycleCallbackConverter<Object> mConverter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mTestData = new Object();
|
||||
mConsumer = new TestConsumer();
|
||||
}
|
||||
|
||||
private void initEnvPerTestCase() {
|
||||
mConverter = new LifecycleCallbackConverter<Object>(getLifecycle(), mConsumer);
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converter_dropResult_whenInActive() {
|
||||
initEnvPerTestCase();
|
||||
mConverter.postResult(mTestData);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converter_callbackResult_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
mConverter.postResult(mTestData);
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(1);
|
||||
assertThat(mConsumer.getData()).isEqualTo(mTestData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converter_dropResult_whenBackToInActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
|
||||
mConverter.postResult(mTestData);
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void converter_passResultToUiThread_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
final Phaser phaser = new Phaser(1);
|
||||
Thread executionThread = new Thread(() -> {
|
||||
mConverter.postResult(phaser);
|
||||
});
|
||||
executionThread.start();
|
||||
phaser.awaitAdvance(0);
|
||||
|
||||
assertThat(mConsumer.getData()).isEqualTo(phaser);
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
public static class TestConsumer implements Consumer<Object> {
|
||||
long mNumberOfCallback;
|
||||
AtomicReference<Object> mLatestData;
|
||||
|
||||
public TestConsumer() {
|
||||
mLatestData = new AtomicReference<Object>();
|
||||
}
|
||||
|
||||
public void accept(Object data) {
|
||||
mLatestData.set(data);
|
||||
mNumberOfCallback ++;
|
||||
if ((data != null) && (data instanceof Phaser)) {
|
||||
((Phaser)data).arrive();
|
||||
}
|
||||
}
|
||||
|
||||
protected long getCallbackCount() {
|
||||
return mNumberOfCallback;
|
||||
}
|
||||
|
||||
protected Object getData() {
|
||||
return mLatestData.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,190 +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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LifecycleRegistry;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class LifecycleCallbackIntentReceiverTest implements LifecycleOwner {
|
||||
|
||||
private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this);
|
||||
|
||||
private static final String TEST_SCHEDULER_HANDLER = "testScheduler";
|
||||
private static final String TEST_INTENT_ACTION = "testAction";
|
||||
private static final String TEST_INTENT_PERMISSION = "testPermission";
|
||||
|
||||
private Context mContext;
|
||||
private Intent mIntent;
|
||||
private IntentFilter mIntentFilter;
|
||||
private Handler mHandler;
|
||||
private TestConsumer mConsumer;
|
||||
|
||||
private TestObj mTarget;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
|
||||
mIntentFilter = new IntentFilter(TEST_INTENT_ACTION);
|
||||
mIntent = new Intent(TEST_INTENT_ACTION);
|
||||
|
||||
HandlerThread thread = new HandlerThread(TEST_SCHEDULER_HANDLER);
|
||||
thread.start();
|
||||
|
||||
mHandler = new Handler(thread.getLooper());
|
||||
mConsumer = new TestConsumer();
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
private void initEnvPerTestCase() {
|
||||
mTarget = new TestObj(getLifecycle(), mContext,
|
||||
mIntentFilter, TEST_INTENT_PERMISSION,
|
||||
mHandler, mConsumer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiver_register_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
|
||||
assertThat(mTarget.getCallbackActiveCount(true)
|
||||
+ mTarget.getCallbackActiveCount(false)).isEqualTo(0);
|
||||
|
||||
mTarget.mReceiver.onReceive(mContext, mIntent);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
assertThat(mTarget.getCallbackActiveCount(true)).isEqualTo(1);
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
|
||||
mTarget.mReceiver.onReceive(mContext, mIntent);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(1);
|
||||
assertThat(mConsumer.getData()).isEqualTo(mIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiver_unregister_whenInActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
|
||||
assertThat(mTarget.getCallbackActiveCount(false)).isEqualTo(1);
|
||||
|
||||
mTarget.mReceiver.onReceive(mContext, mIntent);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiver_register_whenReActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
assertThat(mTarget.getCallbackActiveCount(true)).isEqualTo(2);
|
||||
|
||||
mTarget.mReceiver.onReceive(mContext, mIntent);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(1);
|
||||
assertThat(mConsumer.getData()).isEqualTo(mIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiver_close_whenDestroy() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
|
||||
|
||||
assertThat(mTarget.getCallbackActiveCount(false)).isEqualTo(1);
|
||||
|
||||
mTarget.mReceiver.onReceive(mContext, mIntent);
|
||||
|
||||
assertThat(mConsumer.getCallbackCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
public static class TestConsumer implements Consumer<Intent> {
|
||||
long mNumberOfCallback;
|
||||
Intent mLatestData;
|
||||
|
||||
public TestConsumer() {}
|
||||
|
||||
public void accept(Intent data) {
|
||||
mLatestData = data;
|
||||
mNumberOfCallback ++;
|
||||
}
|
||||
|
||||
protected long getCallbackCount() {
|
||||
return mNumberOfCallback;
|
||||
}
|
||||
|
||||
protected Intent getData() {
|
||||
return mLatestData;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestObj extends LifecycleCallbackIntentReceiver {
|
||||
long mCallbackActiveCount;
|
||||
long mCallbackInActiveCount;
|
||||
|
||||
public TestObj(Lifecycle lifecycle, Context context, IntentFilter filter,
|
||||
String broadcastPermission, Handler scheduler, Consumer<Intent> resultCallback) {
|
||||
super(lifecycle, context, filter, broadcastPermission, scheduler, resultCallback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCallbackActive(boolean isActive) {
|
||||
if (isActive) {
|
||||
mCallbackActiveCount ++;
|
||||
} else {
|
||||
mCallbackInActiveCount ++;
|
||||
}
|
||||
super.setCallbackActive(isActive);
|
||||
}
|
||||
|
||||
protected long getCallbackActiveCount(boolean forActive) {
|
||||
return forActive ? mCallbackActiveCount : mCallbackInActiveCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,109 +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);
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
private void initEnvPerTestCase() {
|
||||
mAdapter = new LifecycleCallbackTelephonyAdapter<Object>(getLifecycle(), mTelMgr,
|
||||
mTestCallback, null, result -> mResult.set(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void telephonyCallback_register_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
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() {
|
||||
initEnvPerTestCase();
|
||||
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) {}
|
||||
}
|
||||
}
|
||||
@@ -1,99 +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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.telephony.ServiceState;
|
||||
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 ServiceStateStatusTest implements LifecycleOwner {
|
||||
|
||||
private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this);
|
||||
|
||||
@Mock
|
||||
private TelephonyManager mTelMgr;
|
||||
@Mock
|
||||
private ServiceState mTestData;
|
||||
|
||||
private AtomicReference<ServiceState> mStatusStorage;
|
||||
private ServiceStateStatus mServiceStateStatus;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mStatusStorage = new AtomicReference<ServiceState>();
|
||||
}
|
||||
|
||||
private void initEnvPerTestCase() {
|
||||
mServiceStateStatus = new ServiceStateStatus(getLifecycle(), mTelMgr, null) {
|
||||
@Override
|
||||
protected void setValue(ServiceState status) {
|
||||
mStatusStorage.set(status);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void telephonyCallback_updateStatus_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
|
||||
mServiceStateStatus.mServiceStateProducer.onServiceStateChanged(mTestData);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(null);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
mServiceStateStatus.mServiceStateProducer.onServiceStateChanged(mTestData);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(mTestData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void telephonyCallback_updateStatusToNull_whenInActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
mServiceStateStatus.mServiceStateProducer.onServiceStateChanged(mTestData);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(mTestData);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(null);
|
||||
}
|
||||
}
|
||||
@@ -1,100 +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 com.google.common.truth.Truth.assertThat;
|
||||
|
||||
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 VoiceCallStatusTest implements LifecycleOwner {
|
||||
|
||||
private final LifecycleRegistry mRegistry = LifecycleRegistry.createUnsafe(this);
|
||||
|
||||
@Mock
|
||||
private TelephonyManager mTelMgr;
|
||||
|
||||
private AtomicReference<Integer> mStatusStorage;
|
||||
private VoiceCallStatus mVoiceCallStatus;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mStatusStorage = new AtomicReference<Integer>();
|
||||
}
|
||||
|
||||
private void initEnvPerTestCase() {
|
||||
mVoiceCallStatus = new VoiceCallStatus(getLifecycle(), mTelMgr, null) {
|
||||
//ArchTaskExecutor.getMainThreadExecutor()) {
|
||||
@Override
|
||||
protected void setValue(Integer status) {
|
||||
mStatusStorage.set(status);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Lifecycle getLifecycle() {
|
||||
return mRegistry;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void telephonyCallback_updateStatus_whenActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
|
||||
mVoiceCallStatus.mCallStateProducer.onCallStateChanged(
|
||||
TelephonyManager.CALL_STATE_RINGING);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(null);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
mVoiceCallStatus.mCallStateProducer.onCallStateChanged(
|
||||
TelephonyManager.CALL_STATE_OFFHOOK);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(TelephonyManager.CALL_STATE_OFFHOOK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void telephonyCallback_updateStatusToNull_whenInActive() {
|
||||
initEnvPerTestCase();
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
|
||||
mVoiceCallStatus.mCallStateProducer.onCallStateChanged(
|
||||
TelephonyManager.CALL_STATE_OFFHOOK);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(TelephonyManager.CALL_STATE_OFFHOOK);
|
||||
|
||||
mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
|
||||
|
||||
assertThat(mStatusStorage.get()).isEqualTo(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user