Chromium Code Reviews| 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..650e02b7c98c44e57437035d111bede0eaac7552 |
| --- /dev/null |
| +++ b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java |
| @@ -0,0 +1,147 @@ |
| +// 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 boolean sAPIAvailable = |
|
pauljensen
2016/04/22 11:54:13
final
tbansal1
2016/04/28 20:12:03
Done.
|
| + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| + |
| + private AndroidCellularSignalStrength() {} |
| + |
| + /** |
| + * @return RSSI (in dbM) for the currently registered cellular network. Returns {@link |
| + * invalidValue} if the RSSI is unavailable. |
| + */ |
| + @SuppressWarnings("unused") |
| + @SuppressLint("NewApi") |
| + @CalledByNative |
| + private static long getRssiDbm(Context context, long invalidValue) { |
| + if (!sAPIAvailable) { |
|
pauljensen
2016/04/22 11:54:12
how about moving lines 48-60 into a function? it
tbansal1
2016/04/28 20:12:02
Done.
|
| + return invalidValue; |
| + } |
| + |
| + TelephonyManager telephonyManager = |
| + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| + if (telephonyManager == null) return invalidValue; |
| + |
| + List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); |
| + if (cellInfos == null) { |
| + return invalidValue; |
| + } |
| + |
| + for (CellInfo cellInfo : cellInfos) { |
| + if (cellInfo.isRegistered()) { |
| + return getRssiDbm(cellInfo, invalidValue); |
| + } |
| + } |
| + |
| + return invalidValue; |
| + } |
| + |
| + /** |
| + * @return the signal level (between 0 and 4, both inclusive) for the currently registered |
| + * cellular network. Returns {@link invalidValue} if the signal level is unavailable. |
| + */ |
| + @SuppressWarnings("unused") |
| + @SuppressLint("NewApi") |
| + @CalledByNative |
| + private static long getSignalLevel(Context context, long invalidValue) { |
|
pauljensen
2016/04/22 11:54:12
why not return an int?
tbansal1
2016/04/28 20:12:02
Done.
|
| + if (!sAPIAvailable) { |
| + return invalidValue; |
| + } |
| + |
| + TelephonyManager telephonyManager = |
| + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| + if (telephonyManager == null) return invalidValue; |
| + |
| + List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); |
| + if (cellInfos == null) { |
| + return invalidValue; |
| + } |
| + |
| + for (CellInfo cellInfo : cellInfos) { |
| + if (cellInfo.isRegistered()) { |
| + return getSignalLevel(cellInfo, invalidValue); |
| + } |
| + } |
| + |
| + return invalidValue; |
| + } |
| + |
| + /** |
| + * @return RSSI (in dbM) from {@link cellInfo}. Returns {@link invalidValue} if the RSSI is |
| + * unavailable. |
| + */ |
| + @SuppressLint("NewApi") |
| + private static long getRssiDbm(CellInfo cellInfo, long invalidValue) { |
|
pauljensen
2016/04/22 11:54:12
how about returning an int? and you could remove
tbansal1
2016/04/28 20:12:02
Done.
|
| + 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 invalidValue; |
| + } |
| + |
| + /** |
| + * @return the signal level from {@link cellInfo}. Returns {@link invalidValue} if the signal |
| + * level is unavailable. |
| + */ |
| + @SuppressLint("NewApi") |
| + private static long getSignalLevel(CellInfo cellInfo, long invalidValue) { |
|
pauljensen
2016/04/22 11:54:13
why not an int? I think all the getLevel function
pauljensen
2016/04/22 11:54:13
instead of invalidValue, you could pre-define some
tbansal1
2016/04/28 20:12:02
Done.
tbansal1
2016/04/28 20:12:03
Done.
|
| + 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 invalidValue; |
| + } |
| +} |