Merge "Migrate NetworkOperatorPreference to Kotlin" into main

This commit is contained in:
Chaohui Wang
2024-01-04 09:00:05 +00:00
committed by Android (Google) Code Review
4 changed files with 171 additions and 256 deletions

View File

@@ -1,234 +0,0 @@
/*
* Copyright (C) 2018 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.telephony;
import static android.telephony.SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
import static com.android.settings.network.telephony.CellInfoUtil.getOperatorNumeric;
import android.content.Context;
import android.telephony.AccessNetworkConstants.AccessNetworkType;
import android.telephony.CellIdentity;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoNr;
import android.telephony.CellInfoTdscdma;
import android.telephony.CellInfoWcdma;
import android.telephony.CellSignalStrength;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import com.android.internal.telephony.OperatorInfo;
import com.android.settings.R;
import java.util.List;
import java.util.Objects;
/**
* A Preference represents a network operator in the NetworkSelectSetting fragment.
*/
public class NetworkOperatorPreference extends Preference {
private static final String TAG = "NetworkOperatorPref";
private static final boolean DBG = false;
private static final int LEVEL_NONE = -1;
private CellInfo mCellInfo;
private CellIdentity mCellId;
private List<String> mForbiddenPlmns;
private int mLevel = LEVEL_NONE;
private boolean mShow4GForLTE;
private boolean mUseNewApi;
public NetworkOperatorPreference(Context context, CellInfo cellinfo,
List<String> forbiddenPlmns, boolean show4GForLTE) {
this(context, forbiddenPlmns, show4GForLTE);
updateCell(cellinfo);
}
public NetworkOperatorPreference(Context context, CellIdentity connectedCellId,
List<String> forbiddenPlmns, boolean show4GForLTE) {
this(context, forbiddenPlmns, show4GForLTE);
updateCell(null, connectedCellId);
}
private NetworkOperatorPreference(
Context context, List<String> forbiddenPlmns, boolean show4GForLTE) {
super(context);
mForbiddenPlmns = forbiddenPlmns;
mShow4GForLTE = show4GForLTE;
mUseNewApi = context.getResources().getBoolean(
com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
}
/**
* Change cell information
*/
public void updateCell(CellInfo cellinfo) {
updateCell(cellinfo, cellinfo.getCellIdentity());
}
@VisibleForTesting
protected void updateCell(CellInfo cellinfo, CellIdentity cellId) {
mCellInfo = cellinfo;
mCellId = cellId;
refresh();
}
/**
* Compare cell within preference
*/
public boolean isSameCell(CellInfo cellinfo) {
if (cellinfo == null) {
return false;
}
return mCellId.equals(cellinfo.getCellIdentity());
}
/**
* Return true when this preference is for forbidden network
*/
public boolean isForbiddenNetwork() {
return ((mForbiddenPlmns != null) && mForbiddenPlmns.contains(getOperatorNumeric(mCellId)));
}
/**
* Refresh the NetworkOperatorPreference by updating the title and the icon.
*/
public void refresh() {
String networkTitle = getOperatorName();
if (isForbiddenNetwork()) {
if (DBG) Log.d(TAG, "refresh forbidden network: " + networkTitle);
networkTitle += " "
+ getContext().getResources().getString(R.string.forbidden_network);
} else {
if (DBG) Log.d(TAG, "refresh the network: " + networkTitle);
}
setTitle(Objects.toString(networkTitle, ""));
if (mCellInfo == null) {
return;
}
final CellSignalStrength signalStrength = getCellSignalStrength(mCellInfo);
final int level = signalStrength != null ? signalStrength.getLevel() : LEVEL_NONE;
if (DBG) Log.d(TAG, "refresh level: " + String.valueOf(level));
mLevel = level;
updateIcon(mLevel);
}
/**
* Update the icon according to the input signal strength level.
*/
public void setIcon(int level) {
updateIcon(level);
}
/**
* Operator name of this cell
*/
@Nullable
public String getOperatorName() {
return CellInfoUtil.getNetworkTitle(mCellId);
}
/**
* Operator info of this cell
*/
public OperatorInfo getOperatorInfo() {
return new OperatorInfo(Objects.toString(mCellId.getOperatorAlphaLong(), ""),
Objects.toString(mCellId.getOperatorAlphaShort(), ""),
getOperatorNumeric(mCellId), getAccessNetworkTypeFromCellInfo(mCellInfo));
}
private int getIconIdForCell(CellInfo ci) {
if (ci instanceof CellInfoGsm) {
return R.drawable.signal_strength_g;
}
if (ci instanceof CellInfoCdma) {
return R.drawable.signal_strength_1x;
}
if ((ci instanceof CellInfoWcdma) || (ci instanceof CellInfoTdscdma)) {
return R.drawable.signal_strength_3g;
}
if (ci instanceof CellInfoLte) {
return mShow4GForLTE
? R.drawable.ic_signal_strength_4g : R.drawable.signal_strength_lte;
}
if (ci instanceof CellInfoNr) {
return R.drawable.signal_strength_5g;
}
return MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON;
}
private CellSignalStrength getCellSignalStrength(CellInfo ci) {
if (ci instanceof CellInfoGsm) {
return ((CellInfoGsm) ci).getCellSignalStrength();
}
if (ci instanceof CellInfoCdma) {
return ((CellInfoCdma) ci).getCellSignalStrength();
}
if (ci instanceof CellInfoWcdma) {
return ((CellInfoWcdma) ci).getCellSignalStrength();
}
if (ci instanceof CellInfoTdscdma) {
return ((CellInfoTdscdma) ci).getCellSignalStrength();
}
if (ci instanceof CellInfoLte) {
return ((CellInfoLte) ci).getCellSignalStrength();
}
if (ci instanceof CellInfoNr) {
return ((CellInfoNr) ci).getCellSignalStrength();
}
return null;
}
private int getAccessNetworkTypeFromCellInfo(CellInfo ci) {
if (ci instanceof CellInfoGsm) {
return AccessNetworkType.GERAN;
}
if (ci instanceof CellInfoCdma) {
return AccessNetworkType.CDMA2000;
}
if ((ci instanceof CellInfoWcdma) || (ci instanceof CellInfoTdscdma)) {
return AccessNetworkType.UTRAN;
}
if (ci instanceof CellInfoLte) {
return AccessNetworkType.EUTRAN;
}
if (ci instanceof CellInfoNr) {
return AccessNetworkType.NGRAN;
}
return AccessNetworkType.UNKNOWN;
}
private void updateIcon(int level) {
if (!mUseNewApi || level < 0 || level >= NUM_SIGNAL_STRENGTH_BINS) {
return;
}
final Context context = getContext();
setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS,
getIconIdForCell(mCellInfo), false, false));
}
}

View File

@@ -0,0 +1,150 @@
/*
* Copyright (C) 2024 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.telephony
import android.content.Context
import android.telephony.AccessNetworkConstants.AccessNetworkType
import android.telephony.CellIdentity
import android.telephony.CellInfo
import android.telephony.CellInfoCdma
import android.telephony.CellInfoGsm
import android.telephony.CellInfoLte
import android.telephony.CellInfoNr
import android.telephony.CellInfoTdscdma
import android.telephony.CellInfoWcdma
import android.telephony.SignalStrength
import android.util.Log
import androidx.annotation.OpenForTesting
import androidx.preference.Preference
import com.android.internal.telephony.OperatorInfo
import com.android.settings.R
import com.android.settings.network.telephony.CellInfoUtil.getNetworkTitle
import com.android.settings.network.telephony.CellInfoUtil.getOperatorNumeric
import java.util.Objects
/**
* A Preference represents a network operator in the NetworkSelectSetting fragment.
*/
@OpenForTesting
open class NetworkOperatorPreference(
context: Context,
private val forbiddenPlmns: List<String>,
private val show4GForLTE: Boolean,
) : Preference(context) {
private var cellInfo: CellInfo? = null
private var cellId: CellIdentity? = null
private val useNewApi = context.resources.getBoolean(
com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI
)
/**
* Change cell information
*/
@JvmOverloads
fun updateCell(cellInfo: CellInfo?, cellId: CellIdentity? = cellInfo?.cellIdentity) {
this.cellInfo = cellInfo
this.cellId = cellId
refresh()
}
/**
* Compare cell within preference
*/
fun isSameCell(cellInfo: CellInfo): Boolean = cellInfo.cellIdentity == cellId
/**
* Return true when this preference is for forbidden network
*/
fun isForbiddenNetwork(): Boolean = cellId?.getOperatorNumeric() in forbiddenPlmns
/**
* Refresh the NetworkOperatorPreference by updating the title and the icon.
*/
fun refresh() {
var networkTitle = cellId?.getNetworkTitle() ?: return
if (isForbiddenNetwork()) {
if (DBG) Log.d(TAG, "refresh forbidden network: $networkTitle")
networkTitle += " ${context.getString(R.string.forbidden_network)}"
} else {
if (DBG) Log.d(TAG, "refresh the network: $networkTitle")
}
title = networkTitle
val level = (cellInfo ?: return).cellSignalStrength.level
if (DBG) Log.d(TAG, "refresh level: $level")
setIcon(level)
}
/**
* Update the icon according to the input signal strength level.
*/
override fun setIcon(level: Int) {
if (!useNewApi || level < 0 || level >= SignalStrength.NUM_SIGNAL_STRENGTH_BINS) {
return
}
icon = MobileNetworkUtils.getSignalStrengthIcon(
context,
level,
SignalStrength.NUM_SIGNAL_STRENGTH_BINS,
getIconIdForCell(),
false,
false,
)
}
/**
* Operator name of this cell
*/
fun getOperatorName(): String? = cellId?.getNetworkTitle()
/**
* Operator info of this cell
*/
fun getOperatorInfo() = OperatorInfo(
Objects.toString(cellId?.operatorAlphaLong, ""),
Objects.toString(cellId?.operatorAlphaShort, ""),
cellId?.getOperatorNumeric(),
getAccessNetworkTypeFromCellInfo(),
)
private fun getIconIdForCell(): Int = when (cellInfo) {
is CellInfoGsm -> R.drawable.signal_strength_g
is CellInfoCdma -> R.drawable.signal_strength_1x
is CellInfoWcdma, is CellInfoTdscdma -> R.drawable.signal_strength_3g
is CellInfoLte -> {
if (show4GForLTE) R.drawable.ic_signal_strength_4g
else R.drawable.signal_strength_lte
}
is CellInfoNr -> R.drawable.signal_strength_5g
else -> MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON
}
private fun getAccessNetworkTypeFromCellInfo(): Int = when (cellInfo) {
is CellInfoGsm -> AccessNetworkType.GERAN
is CellInfoCdma -> AccessNetworkType.CDMA2000
is CellInfoWcdma, is CellInfoTdscdma -> AccessNetworkType.UTRAN
is CellInfoLte -> AccessNetworkType.EUTRAN
is CellInfoNr -> AccessNetworkType.NGRAN
else -> AccessNetworkType.UNKNOWN
}
companion object {
private const val TAG = "NetworkOperatorPref"
private const val DBG = false
}
}

View File

@@ -38,6 +38,7 @@ import android.util.Log;
import android.view.View;
import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
@@ -422,17 +423,14 @@ public class NetworkSelectSettings extends DashboardFragment {
mCellInfoList = doAggregation(results);
Log.d(TAG, "CellInfoList: " + CellInfoUtil.cellInfoListToString(mCellInfoList));
if (mCellInfoList != null && mCellInfoList.size() != 0) {
final NetworkOperatorPreference connectedPref =
updateAllPreferenceCategory();
final NetworkOperatorPreference connectedPref = updateAllPreferenceCategory();
if (connectedPref != null) {
// update selected preference instance into connected preference
if (mSelectedPreference != null) {
mSelectedPreference = connectedPref;
}
} else if (!isPreferenceScreenEnabled()) {
if (connectedPref == null) {
mSelectedPreference.setSummary(R.string.network_connecting);
}
mSelectedPreference.setSummary(R.string.network_connecting);
}
enablePreferenceScreen(true);
} else if (isPreferenceScreenEnabled()) {
@@ -445,8 +443,13 @@ public class NetworkSelectSettings extends DashboardFragment {
@Keep
@VisibleForTesting
protected NetworkOperatorPreference createNetworkOperatorPreference(CellInfo cellInfo) {
return new NetworkOperatorPreference(getPrefContext(),
cellInfo, mForbiddenPlmns, mShow4GForLTE);
if (mForbiddenPlmns == null) {
updateForbiddenPlmns();
}
NetworkOperatorPreference preference =
new NetworkOperatorPreference(getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
preference.updateCell(cellInfo);
return preference;
}
/**
@@ -454,6 +457,7 @@ public class NetworkSelectSettings extends DashboardFragment {
*
* @return preference which shows connected
*/
@Nullable
private NetworkOperatorPreference updateAllPreferenceCategory() {
int numberOfPreferences = mPreferenceCategory.getPreferenceCount();
@@ -547,7 +551,8 @@ public class NetworkSelectSettings extends DashboardFragment {
continue;
}
final NetworkOperatorPreference pref = new NetworkOperatorPreference(
getPrefContext(), cellIdentity, mForbiddenPlmns, mShow4GForLTE);
getPrefContext(), mForbiddenPlmns, mShow4GForLTE);
pref.updateCell(null, cellIdentity);
if (pref.isForbiddenNetwork()) {
continue;
}

View File

@@ -18,12 +18,12 @@ package com.android.settings.network.telephony;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.CellIdentity;
@@ -32,6 +32,7 @@ import android.telephony.CellIdentityLte;
import android.telephony.CellInfo;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrength;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.TelephonyManager;
@@ -39,7 +40,6 @@ import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
@@ -68,10 +68,6 @@ public class NetworkSelectSettingsTest {
@Mock
public MetricsFeatureProvider mMetricsFeatureProvider;
@Mock
public NetworkOperatorPreference mNetworkOperatorPreference1;
@Mock
public NetworkOperatorPreference mNetworkOperatorPreference2;
@Mock
private CellInfo mCellInfo1;
@Mock
private CellIdentity mCellId1;
@@ -80,7 +76,6 @@ public class NetworkSelectSettingsTest {
@Mock
private CellIdentity mCellId2;
private PreferenceScreen mPreferenceScreen;
@Mock
public PreferenceManager mPreferenceManager;
@@ -88,7 +83,6 @@ public class NetworkSelectSettingsTest {
public PreferenceCategory mPreferenceCategory;
public boolean mIsAggregationEnabled = true;
private Bundle mInitArguments;
private TargetClass mNetworkSelectSettings;
@Before
@@ -103,8 +97,10 @@ public class NetworkSelectSettingsTest {
mPreferenceCategory = spy(new PreferenceCategory(mContext));
doReturn(mPreferenceManager).when(mPreferenceCategory).getPreferenceManager();
doReturn(mCellId1).when(mCellInfo1).getCellIdentity();
doReturn(mock(CellSignalStrength.class)).when(mCellInfo1).getCellSignalStrength();
doReturn(CARRIER_NAME1).when(mCellId1).getOperatorAlphaLong();
doReturn(mCellId2).when(mCellInfo2).getCellIdentity();
doReturn(mock(CellSignalStrength.class)).when(mCellInfo2).getCellSignalStrength();
doReturn(CARRIER_NAME2).when(mCellId2).getOperatorAlphaLong();
mIsAggregationEnabled = true;
mNetworkSelectSettings = spy(new TargetClass(this));
@@ -116,8 +112,8 @@ public class NetworkSelectSettingsTest {
doReturn(TelephonyManager.DATA_CONNECTED).when(mTelephonyManager).getDataState();
}
public class TargetClass extends NetworkSelectSettings {
private NetworkSelectSettingsTest mTestEnv;
public static class TargetClass extends NetworkSelectSettings {
private final NetworkSelectSettingsTest mTestEnv;
private boolean mIsPreferenceScreenEnabled;
public TargetClass(NetworkSelectSettingsTest env) {
@@ -284,8 +280,7 @@ public class NetworkSelectSettingsTest {
private CellInfoLte createLteCellInfo(boolean registered, int cellId, String mcc, String mnc,
String plmnName) {
CellIdentityLte cil = new CellIdentityLte(
cellId, 5, 200, 2000, new int[]{1, 2}, 10000, new String(mcc),
new String(mnc), new String(plmnName), new String(plmnName),
cellId, 5, 200, 2000, new int[]{1, 2}, 10000, mcc, mnc, plmnName, plmnName,
Collections.emptyList(), null);
CellSignalStrengthLte cssl = new CellSignalStrengthLte(15, 16, 17, 18, 19, 20);
@@ -299,8 +294,7 @@ public class NetworkSelectSettingsTest {
private CellInfoGsm createGsmCellInfo(boolean registered, int cellId, String mcc, String mnc,
String plmnName) {
CellIdentityGsm cig = new CellIdentityGsm(1, cellId, 40, 5, new String(mcc),
new String(mnc), new String(plmnName), new String(plmnName),
CellIdentityGsm cig = new CellIdentityGsm(1, cellId, 40, 5, mcc, mnc, plmnName, plmnName,
Collections.emptyList());
CellSignalStrengthGsm cssg = new CellSignalStrengthGsm(5, 6, 7);
CellInfoGsm cellInfoGsm = new CellInfoGsm();