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..ac4df038617e59cc978047d5882b3a2344fb9f0b |
| --- /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.Manifest; |
| +import android.annotation.SuppressLint; |
| +import android.content.Context; |
| +import android.content.pm.PackageManager; |
| +import android.os.Build; |
| +import android.os.Process; |
| +import android.telephony.CellInfo; |
| +import android.telephony.CellInfoCdma; |
| +import android.telephony.CellInfoGsm; |
| +import android.telephony.CellInfoLte; |
| +import android.telephony.CellInfoWcdma; |
| +import android.telephony.TelephonyManager; |
| + |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * This class interacts with the CellInfo 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. |
| + * Also verify that appropriate permissions are already available. This |
| + * ensures that on Android M and higher, Chromium would not request run-time |
| + * permission from the user when querying for cellular signal strength. |
| + * TODO(tbansal): Consider using TelephonyManager.getNeighboringCellInfo() |
| + * for earlier versions of Android. |
| + */ |
| + private static final boolean sAPIAvailable = |
| + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 |
| + && ContextUtils.getApplicationContext() != null |
|
pauljensen
2016/06/24 11:53:14
The null-check here indicates a silent failure con
tbansal1
2016/06/27 19:01:17
Done.
|
| + && ContextUtils.getApplicationContext().checkPermission( |
| + Manifest.permission.ACCESS_COARSE_LOCATION, Process.myPid(), Process.myUid()) |
| + == PackageManager.PERMISSION_GRANTED; |
| + |
| + /** |
| + * @return Signal strength (in dbM) for the currently registered cellular network. Returns |
| + * {@link CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the signal strength is |
| + * unavailable or if there are multiple cellular radios on the device. |
| + */ |
| + @SuppressLint("NewApi") |
| + @CalledByNative |
| + public static int getSignalStrengthDbm(Context context) { |
| + List<CellInfo> cellInfos = getRegisteredCellInfo(context); |
| + return cellInfos == null || cellInfos.size() != 1 |
| + ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED |
| + : getSignalStrengthDbm(cellInfos.get(0)); |
| + } |
| + |
| + /** |
| + * @return the signal strength level (between 0 and 4, both inclusive) for the currently |
| + * registered cellular network. Returns {@link CellularSignalStrengthError.ERROR_NOT_SUPPORTED} |
| + * if the signal strength level is unavailable or if there are multiple cellular radios on the |
| + * device. |
| + */ |
| + @SuppressWarnings("unused") |
| + @SuppressLint("NewApi") |
| + @CalledByNative |
| + public static int getSignalStrengthLevel(Context context) { |
| + List<CellInfo> cellInfos = getRegisteredCellInfo(context); |
| + return cellInfos == null || cellInfos.size() != 1 |
| + ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED |
| + : getSignalStrengthLevel(cellInfos.get(0)); |
| + } |
| + |
| + /** |
| + * Returns all observed cell information from all radios on the device including the primary |
| + * and neighboring cells. Returns only the information of cells that are registered to a |
| + * mobile network. May return null. |
| + */ |
| + @SuppressLint("NewApi") |
| + private static List<CellInfo> getRegisteredCellInfo(Context context) { |
| + if (!sAPIAvailable) { |
| + return null; |
| + } |
| + |
| + TelephonyManager telephonyManager = |
| + (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| + if (telephonyManager == null) return null; |
|
pauljensen
2016/06/24 11:53:14
I think Chrome style is not to put if-statement cl
tbansal1
2016/06/27 19:01:17
Done.
|
| + |
| + List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); |
| + if (cellInfos == null) return null; |
| + |
| + List<CellInfo> registeredCellInfos = new ArrayList<CellInfo>(); |
| + for (CellInfo cellInfo : cellInfos) { |
| + if (cellInfo.isRegistered()) { |
| + registeredCellInfos.add(cellInfo); |
| + } |
| + } |
| + return registeredCellInfos; |
| + } |
| + |
| + /** |
| + * @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link |
| + * CellularSignalStrengthError.ERROR_NOT_SUPPORTED} if the signal strength is unavailable. |
| + */ |
| + @SuppressLint("NewApi") |
| + private static int getSignalStrengthDbm(CellInfo cellInfo) { |
| + if (cellInfo instanceof CellInfoCdma) { |
| + return ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm(); |
| + } |
| + if (cellInfo instanceof CellInfoGsm) { |
| + return ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm(); |
| + } |
| + if (cellInfo instanceof CellInfoLte) { |
| + return ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm(); |
| + } |
| + if (cellInfo instanceof CellInfoWcdma) { |
| + return ((CellInfoWcdma) cellInfo).getCellSignalStrength().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 getSignalStrengthLevel(CellInfo cellInfo) { |
| + if (cellInfo instanceof CellInfoCdma) { |
| + return ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel(); |
| + } |
| + if (cellInfo instanceof CellInfoGsm) { |
| + return ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel(); |
| + } |
| + if (cellInfo instanceof CellInfoLte) { |
| + return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); |
| + } |
| + if (cellInfo instanceof CellInfoWcdma) { |
| + return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel(); |
| + } |
| + return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; |
| + } |
| +} |