Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7e52888 authored by Bonian Chen's avatar Bonian Chen Committed by Android (Google) Code Review
Browse files

Merge "[Settings] Manual network select without aggregate"

parents 54746678 ef1481f5
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -19,9 +19,6 @@
    android:key="mobile_choose_network_pref_screen"
    android:title="@string/choose_network_title">

    <PreferenceCategory
        android:key="connected_network_operator_preference"/>

    <PreferenceCategory
        android:key="network_operators_preference"/>

+68 −81
Original line number Diff line number Diff line
@@ -17,23 +17,26 @@
package com.android.settings.network.telephony;

import android.telephony.CellIdentity;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityNr;
import android.telephony.CellIdentityTdscdma;
import android.telephony.CellIdentityWcdma;
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.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.util.Log;

import com.android.internal.telephony.OperatorInfo;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
@@ -46,86 +49,56 @@ public final class CellInfoUtil {
    private CellInfoUtil() {
    }

    /**
     * Wrap a CellIdentity into a CellInfo.
     */
    public static CellInfo wrapCellInfoWithCellIdentity(CellIdentity cellIdentity) {
        if (cellIdentity instanceof CellIdentityLte) {
            CellInfoLte cellInfo = new CellInfoLte();
            cellInfo.setCellIdentity((CellIdentityLte) cellIdentity);
            return cellInfo;
        } else if (cellIdentity instanceof CellIdentityCdma) {
            CellInfoCdma cellInfo = new CellInfoCdma();
            cellInfo.setCellIdentity((CellIdentityCdma) cellIdentity);
            return cellInfo;
        }  else if (cellIdentity instanceof CellIdentityWcdma) {
            CellInfoWcdma cellInfo = new CellInfoWcdma();
            cellInfo.setCellIdentity((CellIdentityWcdma) cellIdentity);
            return cellInfo;
        } else if (cellIdentity instanceof CellIdentityGsm) {
            CellInfoGsm cellInfo = new CellInfoGsm();
            cellInfo.setCellIdentity((CellIdentityGsm) cellIdentity);
            return cellInfo;
        } else {
            Log.e(TAG, "Invalid CellInfo type");
            return null;
        }
    }

    /**
     * Returns the title of the network obtained in the manual search.
     *
     * @param cellInfo contains the information of the network.
     * @param cellId contains the information of the network.
     * @param networkMccMnc contains the MCCMNC string of the network
     * @return Long Name if not null/empty, otherwise Short Name if not null/empty,
     * else MCCMNC string.
     */
    public static String getNetworkTitle(CellInfo cellInfo) {
        OperatorInfo oi = getOperatorInfoFromCellInfo(cellInfo);

        if (!TextUtils.isEmpty(oi.getOperatorAlphaLong())) {
            return oi.getOperatorAlphaLong();
        } else if (!TextUtils.isEmpty(oi.getOperatorAlphaShort())) {
            return oi.getOperatorAlphaShort();
        } else {
            BidiFormatter bidiFormatter = BidiFormatter.getInstance();
            return bidiFormatter.unicodeWrap(oi.getOperatorNumeric(), TextDirectionHeuristics.LTR);
    public static String getNetworkTitle(CellIdentity cellId, String networkMccMnc) {
        if (cellId != null) {
            String title = Objects.toString(cellId.getOperatorAlphaLong(), "");
            if (TextUtils.isEmpty(title)) {
                title = Objects.toString(cellId.getOperatorAlphaShort(), "");
            }
            if (!TextUtils.isEmpty(title)) {
                return title;
            }
        }
        if (TextUtils.isEmpty(networkMccMnc)) {
            return "";
        }
        final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
        return bidiFormatter.unicodeWrap(networkMccMnc, TextDirectionHeuristics.LTR);
    }

    /**
     * Wrap a cell info into an operator info.
     * Returns the CellIdentity from CellInfo
     *
     * @param cellInfo contains the information of the network.
     * @return CellIdentity within CellInfo
     */
    public static OperatorInfo getOperatorInfoFromCellInfo(CellInfo cellInfo) {
        OperatorInfo oi;
        if (cellInfo instanceof CellInfoLte) {
            CellInfoLte lte = (CellInfoLte) cellInfo;
            oi = new OperatorInfo(
                    (String) lte.getCellIdentity().getOperatorAlphaLong(),
                    (String) lte.getCellIdentity().getOperatorAlphaShort(),
                    lte.getCellIdentity().getMobileNetworkOperator());
        } else if (cellInfo instanceof CellInfoWcdma) {
            CellInfoWcdma wcdma = (CellInfoWcdma) cellInfo;
            oi = new OperatorInfo(
                    (String) wcdma.getCellIdentity().getOperatorAlphaLong(),
                    (String) wcdma.getCellIdentity().getOperatorAlphaShort(),
                    wcdma.getCellIdentity().getMobileNetworkOperator());
        } else if (cellInfo instanceof CellInfoGsm) {
            CellInfoGsm gsm = (CellInfoGsm) cellInfo;
            oi = new OperatorInfo(
                    (String) gsm.getCellIdentity().getOperatorAlphaLong(),
                    (String) gsm.getCellIdentity().getOperatorAlphaShort(),
                    gsm.getCellIdentity().getMobileNetworkOperator());
    public static CellIdentity getCellIdentity(CellInfo cellInfo) {
        if (cellInfo == null) {
            return null;
        }
        CellIdentity cellId = null;
        if (cellInfo instanceof CellInfoGsm) {
            cellId = ((CellInfoGsm) cellInfo).getCellIdentity();
        } else if (cellInfo instanceof CellInfoCdma) {
            CellInfoCdma cdma = (CellInfoCdma) cellInfo;
            oi = new OperatorInfo(
                    (String) cdma.getCellIdentity().getOperatorAlphaLong(),
                    (String) cdma.getCellIdentity().getOperatorAlphaShort(),
                    "" /* operator numeric */);
        } else {
            Log.e(TAG, "Invalid CellInfo type");
            oi = new OperatorInfo("", "", "");
            cellId = ((CellInfoCdma) cellInfo).getCellIdentity();
        } else if (cellInfo instanceof CellInfoWcdma) {
            cellId = ((CellInfoWcdma) cellInfo).getCellIdentity();
        } else if (cellInfo instanceof CellInfoTdscdma) {
            cellId = ((CellInfoTdscdma) cellInfo).getCellIdentity();
        } else if (cellInfo instanceof CellInfoLte) {
            cellId = ((CellInfoLte) cellInfo).getCellIdentity();
        } else if (cellInfo instanceof CellInfoNr) {
            cellId = ((CellInfoNr) cellInfo).getCellIdentity();
        }
        return oi;
        return cellId;
    }

    /**
@@ -135,14 +108,14 @@ public final class CellInfoUtil {
     * we only want to wrap the operator info and PLMN to a CellInfo object.
     */
    public static CellInfo convertOperatorInfoToCellInfo(OperatorInfo operatorInfo) {
        String operatorNumeric = operatorInfo.getOperatorNumeric();
        final String operatorNumeric = operatorInfo.getOperatorNumeric();
        String mcc = null;
        String mnc = null;
        if (operatorNumeric != null && operatorNumeric.matches("^[0-9]{5,6}$")) {
            mcc = operatorNumeric.substring(0, 3);
            mnc = operatorNumeric.substring(3);
        }
        CellIdentityGsm cig = new CellIdentityGsm(
        final CellIdentityGsm cig = new CellIdentityGsm(
                Integer.MAX_VALUE /* lac */,
                Integer.MAX_VALUE /* cid */,
                Integer.MAX_VALUE /* arfcn */,
@@ -152,17 +125,11 @@ public final class CellInfoUtil {
                operatorInfo.getOperatorAlphaLong(),
                operatorInfo.getOperatorAlphaShort());

        CellInfoGsm ci = new CellInfoGsm();
        final CellInfoGsm ci = new CellInfoGsm();
        ci.setCellIdentity(cig);
        return ci;
    }

    /** Checks whether the network operator is forbidden. */
    public static boolean isForbidden(CellInfo cellInfo, List<String> forbiddenPlmns) {
        String plmn = CellInfoUtil.getOperatorInfoFromCellInfo(cellInfo).getOperatorNumeric();
        return forbiddenPlmns != null && forbiddenPlmns.contains(plmn);
    }

    /** Convert a list of cellInfos to readable string without sensitive info. */
    public static String cellInfoListToString(List<CellInfo> cellInfos) {
        return cellInfos.stream()
@@ -172,11 +139,31 @@ public final class CellInfoUtil {

    /** Convert {@code cellInfo} to a readable string without sensitive info. */
    public static String cellInfoToString(CellInfo cellInfo) {
        String cellType = cellInfo.getClass().getSimpleName();
        CellIdentity cid = cellInfo.getCellIdentity();
        final String cellType = cellInfo.getClass().getSimpleName();
        final CellIdentity cid = getCellIdentity(cellInfo);
        String mcc = null;
        String mnc = null;
        if (cid != null) {
            if (cid instanceof CellIdentityGsm) {
                mcc = ((CellIdentityGsm) cid).getMccString();
                mnc = ((CellIdentityGsm) cid).getMncString();
            } else if (cid instanceof CellIdentityWcdma) {
                mcc = ((CellIdentityWcdma) cid).getMccString();
                mnc = ((CellIdentityWcdma) cid).getMncString();
            } else if (cid instanceof CellIdentityTdscdma) {
                mcc = ((CellIdentityTdscdma) cid).getMccString();
                mnc = ((CellIdentityTdscdma) cid).getMncString();
            } else if (cid instanceof CellIdentityLte) {
                mcc = ((CellIdentityLte) cid).getMccString();
                mnc = ((CellIdentityLte) cid).getMncString();
            } else if (cid instanceof CellIdentityNr) {
                mcc = ((CellIdentityNr) cid).getMccString();
                mnc = ((CellIdentityNr) cid).getMncString();
            }
        }
        return String.format(
                "{CellType = %s, isRegistered = %b, mcc = %s, mnc = %s, alphaL = %s, alphaS = %s}",
                cellType, cellInfo.isRegistered(), cid.getMccString(), cid.getMncString(),
                cellType, cellInfo.isRegistered(), mcc, mnc,
                cid.getOperatorAlphaLong(), cid.getOperatorAlphaShort());
    }
}
+133 −27
Original line number Diff line number Diff line
@@ -19,16 +19,28 @@ package com.android.settings.network.telephony;
import static android.telephony.SignalStrength.NUM_SIGNAL_STRENGTH_BINS;

import android.content.Context;
import android.telephony.CellIdentity;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityNr;
import android.telephony.CellIdentityTdscdma;
import android.telephony.CellIdentityWcdma;
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.preference.Preference;

import com.android.settings.R;
import com.android.settings.Utils;

import java.util.List;
import java.util.Objects;

/**
 * A Preference represents a network operator in the NetworkSelectSetting fragment.
@@ -41,38 +53,74 @@ public class NetworkOperatorPreference extends Preference {
    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(
            CellInfo cellinfo, Context context, List<String> forbiddenPlmns, boolean show4GForLTE) {
    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);
        mCellInfo = null;
        mCellId = connectedCellId;
        refresh();
    }

    private NetworkOperatorPreference(
            Context context, List<String> forbiddenPlmns, boolean show4GForLTE) {
        super(context);
        mCellInfo = cellinfo;
        mForbiddenPlmns = forbiddenPlmns;
        mShow4GForLTE = show4GForLTE;
        mUseNewApi = context.getResources().getBoolean(
                com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
    }

    /**
     * Change cell information
     */
    public void updateCell(CellInfo cellinfo) {
        mCellInfo = cellinfo;
        mCellId = CellInfoUtil.getCellIdentity(cellinfo);
        refresh();
    }

    public CellInfo getCellInfo() {
        return mCellInfo;
    /**
     * Compare cell within preference
     */
    public boolean isSameCell(CellInfo cellinfo) {
        if (cellinfo == null) {
            return false;
        }
        return mCellId.equals(CellInfoUtil.getCellIdentity(cellinfo));
    }

    /**
     * Refresh the NetworkOperatorPreference by updating the title and the icon.
     */
    public void refresh() {
        if (DBG) Log.d(TAG, "refresh the network: " + CellInfoUtil.getNetworkTitle(mCellInfo));
        String networkTitle = CellInfoUtil.getNetworkTitle(mCellInfo);
        if (CellInfoUtil.isForbidden(mCellInfo, mForbiddenPlmns)) {
            networkTitle += " " + getContext().getResources().getString(R.string.forbidden_network);
        String networkTitle = getOperatorName();

        if ((mForbiddenPlmns != null) && mForbiddenPlmns.contains(getOperatorNumeric())) {
            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;
        }
        setTitle(networkTitle);

        final CellSignalStrength signalStrength = mCellInfo.getCellSignalStrength();
        final CellSignalStrength signalStrength = getCellSignalStrength(mCellInfo);
        final int level = signalStrength != null ? signalStrength.getLevel() : LEVEL_NONE;
        if (DBG) Log.d(TAG, "refresh level: " + String.valueOf(level));
        if (mLevel != level) {
@@ -88,29 +136,87 @@ public class NetworkOperatorPreference extends Preference {
        updateIcon(level);
    }

    /**
     * Operator numeric of this cell
     */
    public String getOperatorNumeric() {
        final CellIdentity cellId = mCellId;
        if (cellId == null) {
            return null;
        }
        if (cellId instanceof CellIdentityGsm) {
            return ((CellIdentityGsm) cellId).getMobileNetworkOperator();
        }
        if (cellId instanceof CellIdentityWcdma) {
            return ((CellIdentityWcdma) cellId).getMobileNetworkOperator();
        }
        if (cellId instanceof CellIdentityTdscdma) {
            return ((CellIdentityTdscdma) cellId).getMobileNetworkOperator();
        }
        if (cellId instanceof CellIdentityLte) {
            return ((CellIdentityLte) cellId).getMobileNetworkOperator();
        }
        if (cellId instanceof CellIdentityNr) {
            final String mcc = ((CellIdentityNr) cellId).getMccString();
            if (mcc == null) {
                return null;
            }
            return mcc.concat(((CellIdentityNr) cellId).getMncString());
        }
        return null;
    }

    /**
     * Operator name of this cell
     */
    public String getOperatorName() {
        return CellInfoUtil.getNetworkTitle(mCellId, getOperatorNumeric());
    }

    private int getIconIdForCell(CellInfo ci) {
        final int type = ci.getCellIdentity().getType();
        switch (type) {
            case CellInfo.TYPE_GSM:
        if (ci instanceof CellInfoGsm) {
            return R.drawable.signal_strength_g;
            case CellInfo.TYPE_WCDMA: // fall through
            case CellInfo.TYPE_TDSCDMA:
        }
        if (ci instanceof CellInfoCdma) {
            return R.drawable.signal_strength_1x;
        }
        if ((ci instanceof CellInfoWcdma) || (ci instanceof CellInfoTdscdma)) {
            return R.drawable.signal_strength_3g;
            case CellInfo.TYPE_LTE:
        }
        if (ci instanceof CellInfoLte) {
            return mShow4GForLTE
                    ? R.drawable.ic_signal_strength_4g : R.drawable.signal_strength_lte;
            case CellInfo.TYPE_CDMA:
                return R.drawable.signal_strength_1x;
            default:
        }
        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 void updateIcon(int level) {
        if (!mUseNewApi || level < 0 || level >= NUM_SIGNAL_STRENGTH_BINS) {
            return;
        }
        Context context = getContext();
        final Context context = getContext();
        setIcon(MobileNetworkUtils.getSignalStrengthIcon(context, level, NUM_SIGNAL_STRENGTH_BINS,
                getIconIdForCell(mCellInfo), false));
    }
+183 −129

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public class NetworkOperatorPreferenceTest {
        when(mResources.getBoolean(com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI))
                .thenReturn(false);
        final NetworkOperatorPreference preference = spy(
                new NetworkOperatorPreference(mCellInfo, mContext,
                new NetworkOperatorPreference(mContext, mCellInfo,
                        new ArrayList<>() /* forbiddenPlmns */, false /* show4GForLTE */));

        preference.setIcon(LEVEL);
Loading