| Index: net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java
|
| diff --git a/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6dfafc759b7abab61156a54287848cd878dd750c
|
| --- /dev/null
|
| +++ b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java
|
| @@ -0,0 +1,151 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.net;
|
| +
|
| +import android.annotation.SuppressLint;
|
| +import android.content.Context;
|
| +import android.os.Build;
|
| +import android.telephony.CellInfo;
|
| +import android.telephony.CellInfoCdma;
|
| +import android.telephony.CellInfoGsm;
|
| +import android.telephony.CellInfoLte;
|
| +import android.telephony.CellInfoWcdma;
|
| +import android.telephony.CellSignalStrengthCdma;
|
| +import android.telephony.CellSignalStrengthGsm;
|
| +import android.telephony.CellSignalStrengthLte;
|
| +import android.telephony.CellSignalStrengthWcdma;
|
| +import android.telephony.TelephonyManager;
|
| +
|
| +import org.chromium.base.annotations.CalledByNative;
|
| +import org.chromium.base.annotations.JNINamespace;
|
| +
|
| +import java.util.List;
|
| +
|
| +/**
|
| + * This class interacts with the cellular signal strength API provided by Android.
|
| + */
|
| +@JNINamespace("net::android::cellular_signal_strength")
|
| +public class AndroidCellularSignalStrength {
|
| + /**
|
| + * True if the Android API to query the cellular signal strength is available.
|
| + * getAllCellInfo is only available on API Level JELLY_BEAN_MR1 and higher.
|
| + */
|
| + private static final boolean sAPIAvailable =
|
| + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
|
| +
|
| + private AndroidCellularSignalStrength() {}
|
| +
|
| + /**
|
| + * @return RSSI (in dbM) for the currently registered cellular network. Returns {@link
|
| + * CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the RSSI is unavailable.
|
| + */
|
| + @SuppressWarnings("unused")
|
| + @SuppressLint("NewApi")
|
| + @CalledByNative
|
| + private static int getRssiDbm(Context context) {
|
| + List<CellInfo> cellInfos = getCellInfo(context);
|
| + if (cellInfos == null) {
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +
|
| + for (CellInfo cellInfo : cellInfos) {
|
| + if (cellInfo.isRegistered()) {
|
| + return getRssiDbm(cellInfo);
|
| + }
|
| + }
|
| +
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +
|
| + /**
|
| + * @return the signal level (between 0 and 4, both inclusive) for the currently registered
|
| + * cellular network. Returns {@link CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the
|
| + * signal level is unavailable.
|
| + */
|
| + @SuppressWarnings("unused")
|
| + @SuppressLint("NewApi")
|
| + @CalledByNative
|
| + private static int getSignalLevel(Context context) {
|
| + List<CellInfo> cellInfos = getCellInfo(context);
|
| + if (cellInfos == null) {
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +
|
| + for (CellInfo cellInfo : cellInfos) {
|
| + if (cellInfo.isRegistered()) {
|
| + return getSignalLevel(cellInfo);
|
| + }
|
| + }
|
| +
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +
|
| + /**
|
| + * Returns all observed cell information from all radios on the device including the primary and
|
| + * neighboring cells. May return null.
|
| + */
|
| + @SuppressLint("NewApi")
|
| + private static List<CellInfo> getCellInfo(Context context) {
|
| + if (!sAPIAvailable) {
|
| + return null;
|
| + }
|
| +
|
| + TelephonyManager telephonyManager =
|
| + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
| + if (telephonyManager == null) return null;
|
| +
|
| + return telephonyManager.getAllCellInfo();
|
| + }
|
| +
|
| + /**
|
| + * @return RSSI (in dbM) from {@link cellInfo}. Returns {@link
|
| + * CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the RSSI is
|
| + * unavailable.
|
| + */
|
| + @SuppressLint("NewApi")
|
| + private static int getRssiDbm(CellInfo cellInfo) {
|
| + if (cellInfo instanceof CellInfoCdma) {
|
| + CellSignalStrengthCdma signalStrength =
|
| + ((CellInfoCdma) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getDbm();
|
| + } else if (cellInfo instanceof CellInfoGsm) {
|
| + CellSignalStrengthGsm signalStrength = ((CellInfoGsm) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getDbm();
|
| + } else if (cellInfo instanceof CellInfoLte) {
|
| + CellSignalStrengthLte signalStrength = ((CellInfoLte) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getDbm();
|
| + } else if (cellInfo instanceof CellInfoWcdma) {
|
| + CellSignalStrengthWcdma signalStrength =
|
| + ((CellInfoWcdma) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getDbm();
|
| + }
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +
|
| + /**
|
| + * @return the signal level from {@link cellInfo}. Returns {@link
|
| + * CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the signal
|
| + * level is unavailable.
|
| + */
|
| + @SuppressLint("NewApi")
|
| + private static int getSignalLevel(CellInfo cellInfo) {
|
| + if (cellInfo instanceof CellInfoCdma) {
|
| + CellSignalStrengthCdma signalStrength =
|
| + ((CellInfoCdma) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getLevel();
|
| + } else if (cellInfo instanceof CellInfoGsm) {
|
| + CellSignalStrengthGsm signalStrength = ((CellInfoGsm) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getLevel();
|
| + } else if (cellInfo instanceof CellInfoLte) {
|
| + CellSignalStrengthLte signalStrength = ((CellInfoLte) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getLevel();
|
| + } else if (cellInfo instanceof CellInfoWcdma) {
|
| + CellSignalStrengthWcdma signalStrength =
|
| + ((CellInfoWcdma) cellInfo).getCellSignalStrength();
|
| + return signalStrength.getLevel();
|
| + }
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
|
| + }
|
| +}
|
|
|