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..8e062e2e8bc26b431996670315ec7c3f2da56439 |
| --- /dev/null |
| +++ b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java |
| @@ -0,0 +1,161 @@ |
| +// 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.Manifest; |
| +import android.annotation.SuppressLint; |
| +import android.content.Context; |
| +import android.content.pm.PackageManager; |
| +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.ContextUtils; |
| +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. |
|
bengr
2016/06/10 20:57:37
You mean specifically the SignalStrength API, righ
tbansal1
2016/06/16 00:58:12
Done.
|
| + */ |
| +@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. |
| + * For Android M and higher, verify that appropriate permissions are already |
| + * available. This ensures that Chromium would not request run-time |
| + * permission from the user when querying for cellular signal strength. |
|
pauljensen
2016/06/10 13:52:15
You could probably get this info from TelephonyMan
tbansal1
2016/06/16 00:58:12
Added a TODO.
|
| + */ |
| + private static final boolean sAPIAvailable = |
| + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 |
| + && (Build.VERSION.SDK_INT < Build.VERSION_CODES.M |
| + || ContextUtils.getApplicationContext().checkSelfPermission( |
|
pauljensen
2016/06/10 13:52:15
I don't know a whole lot about Java static initial
tbansal1
2016/06/16 00:58:10
Java static code is initialized in the order of wh
|
| + Manifest.permission.ACCESS_COARSE_LOCATION) |
| + == PackageManager.PERMISSION_GRANTED); |
| + |
| + private AndroidCellularSignalStrength() {} |
|
bengr
2016/06/10 20:57:37
Are all methods private because this should only b
tbansal1
2016/06/16 00:58:10
Changed two of them to public since it is okay to
|
| + |
| + /** |
| + * @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) { |
|
bengr
2016/06/10 20:57:37
Maybe rename this as getSignalStrengthDbm
tbansal1
2016/06/16 00:58:08
Done.
|
| + List<CellInfo> cellInfos = getCellInfo(context); |
| + if (cellInfos == null) { |
|
bengr
2016/06/10 20:57:37
If getCellInfo returned an empty list instead of n
tbansal1
2016/06/16 00:58:11
Done.
|
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; |
| + } |
| + |
| + for (CellInfo cellInfo : cellInfos) { |
| + if (cellInfo.isRegistered()) { |
| + return getRssiDbm(cellInfo); |
|
bengr
2016/06/10 20:57:37
I don't understand. Is at most one cellInfo regist
tbansal1
2016/06/16 00:58:09
There could be multiple radios, and in that case t
bengr
2016/06/20 20:07:26
If there are multiple radios, which cellinfo do yo
tbansal1
2016/06/20 21:32:03
I return the first one, which depends on what Andr
bengr
2016/06/22 16:54:07
I don't like this interface because the rssi (or s
|
| + } |
| + } |
| + |
| + 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) { |
|
bengr
2016/06/10 20:57:37
Naming suggestion: getSignalStrengthLevel
tbansal1
2016/06/16 00:58:09
Done.
|
| + List<CellInfo> cellInfos = getCellInfo(context); |
| + if (cellInfos == null) { |
|
bengr
2016/06/10 20:57:37
See comment on line 59.
tbansal1
2016/06/16 00:58:10
Done.
|
| + 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(); |
|
pauljensen
2016/06/10 13:52:15
can we get rid of these signalStrength variables?
tbansal1
2016/06/16 00:58:10
Done.
|
| + } else if (cellInfo instanceof CellInfoGsm) { |
|
pauljensen
2016/06/10 13:52:15
all these "else"'s aren't needed because you retur
tbansal1
2016/06/16 00:58:12
Done.
|
| + 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 with lower value indicating lower signal strength. |
| + */ |
| + @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; |
| + } |
| +} |