| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.Manifest; | 7 import android.Manifest; |
| 8 import android.annotation.TargetApi; | 8 import android.annotation.TargetApi; |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
| 11 import android.os.Build; | 11 import android.os.Build; |
| 12 import android.os.Process; | 12 import android.os.Process; |
| 13 import android.telephony.CellInfo; | 13 import android.telephony.CellInfo; |
| 14 import android.telephony.CellInfoCdma; | 14 import android.telephony.CellInfoCdma; |
| 15 import android.telephony.CellInfoGsm; | 15 import android.telephony.CellInfoGsm; |
| 16 import android.telephony.CellInfoLte; | 16 import android.telephony.CellInfoLte; |
| 17 import android.telephony.CellInfoWcdma; | 17 import android.telephony.CellInfoWcdma; |
| 18 import android.telephony.TelephonyManager; | 18 import android.telephony.TelephonyManager; |
| 19 | 19 |
| 20 import org.chromium.base.ContextUtils; |
| 20 import org.chromium.base.annotations.CalledByNative; | 21 import org.chromium.base.annotations.CalledByNative; |
| 21 import org.chromium.base.annotations.JNINamespace; | 22 import org.chromium.base.annotations.JNINamespace; |
| 22 | 23 |
| 23 import java.util.Iterator; | 24 import java.util.Iterator; |
| 24 import java.util.List; | 25 import java.util.List; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * This class interacts with the CellInfo API provided by Android. This class is
thread safe. | 28 * This class interacts with the CellInfo API provided by Android. This class is
thread safe. |
| 28 */ | 29 */ |
| 29 @JNINamespace("net::android::cellular_signal_strength") | 30 @JNINamespace("net::android::cellular_signal_strength") |
| 30 public class AndroidCellularSignalStrength { | 31 public class AndroidCellularSignalStrength { |
| 31 /** | 32 /** |
| 32 * @return Signal strength (in dbM) for the currently registered cellular ne
twork. Returns | 33 * @return Signal strength (in dbM) for the currently registered cellular ne
twork. Returns |
| 33 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str
ength is | 34 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str
ength is |
| 34 * unavailable or if there are multiple cellular radios on the device. | 35 * unavailable or if there are multiple cellular radios on the device. |
| 35 */ | 36 */ |
| 36 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 37 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 37 @CalledByNative | 38 @CalledByNative |
| 38 public static int getSignalStrengthDbm(Context context) { | 39 public static int getSignalStrengthDbm() { |
| 39 List<CellInfo> cellInfos = getRegisteredCellInfo(context); | 40 List<CellInfo> cellInfos = getRegisteredCellInfo(); |
| 40 return cellInfos == null || cellInfos.size() != 1 | 41 return cellInfos == null || cellInfos.size() != 1 |
| 41 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | 42 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED |
| 42 : getSignalStrengthDbm(cellInfos.get(0)); | 43 : getSignalStrengthDbm(cellInfos.get(0)); |
| 43 } | 44 } |
| 44 | 45 |
| 45 /** | 46 /** |
| 46 * @return the signal strength level (between 0 and 4, both inclusive) for t
he currently | 47 * @return the signal strength level (between 0 and 4, both inclusive) for t
he currently |
| 47 * registered cellular network with lower value indicating lower signal stre
ngth. Returns | 48 * registered cellular network with lower value indicating lower signal stre
ngth. Returns |
| 48 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str
ength level is | 49 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str
ength level is |
| 49 * unavailable or if there are multiple cellular radios on the device. | 50 * unavailable or if there are multiple cellular radios on the device. |
| 50 */ | 51 */ |
| 51 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 52 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 52 @CalledByNative | 53 @CalledByNative |
| 53 public static int getSignalStrengthLevel(Context context) { | 54 public static int getSignalStrengthLevel() { |
| 54 List<CellInfo> cellInfos = getRegisteredCellInfo(context); | 55 List<CellInfo> cellInfos = getRegisteredCellInfo(); |
| 55 return cellInfos == null || cellInfos.size() != 1 | 56 return cellInfos == null || cellInfos.size() != 1 |
| 56 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | 57 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED |
| 57 : getSignalStrengthLevel(cellInfos.get(0)); | 58 : getSignalStrengthLevel(cellInfos.get(0)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 /** | 61 /** |
| 61 * Returns true if the API for quering the signal strength is available. | 62 * Returns true if the API for quering the signal strength is available. |
| 62 * {@link android.telephony#CellInfoWcdma} is only available on API Level | 63 * {@link android.telephony#CellInfoWcdma} is only available on API Level |
| 63 * {@link Build.VERSION_CODES#JELLY_BEAN_MR2} and higher. Also verifies that
appropriate | 64 * {@link Build.VERSION_CODES#JELLY_BEAN_MR2} and higher. Also verifies that
appropriate |
| 64 * permissions are already available. This ensures that on Android M and hig
her, Chromium will | 65 * permissions are already available. This ensures that on Android M and hig
her, Chromium will |
| 65 * not request run-time permission from the user when querying for cellular
signal strength. | 66 * not request run-time permission from the user when querying for cellular
signal strength. |
| 66 * TODO(tbansal): Consider using {@link TelephonyManager#getNeighboringCellI
nfo} | 67 * TODO(tbansal): Consider using {@link TelephonyManager#getNeighboringCellI
nfo} |
| 67 * for earlier versions of Android. | 68 * for earlier versions of Android. |
| 68 */ | 69 */ |
| 69 private static boolean isAPIAvailable(Context context) { | 70 private static boolean isAPIAvailable() { |
| 70 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return f
alse; | 71 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return f
alse; |
| 71 | 72 |
| 72 try { | 73 try { |
| 73 return context.checkPermission(Manifest.permission.ACCESS_COARSE_LOC
ATION, | 74 return ContextUtils.getApplicationContext().checkPermission( |
| 74 Process.myPid(), Process.myUid()) | 75 Manifest.permission.ACCESS_COARSE_LOCATION, Process.m
yPid(), |
| 76 Process.myUid()) |
| 75 == PackageManager.PERMISSION_GRANTED; | 77 == PackageManager.PERMISSION_GRANTED; |
| 76 } catch (Exception ignored) { | 78 } catch (Exception ignored) { |
| 77 // Work around certain platforms where this method sometimes throws
a runtime exception. | 79 // Work around certain platforms where this method sometimes throws
a runtime exception. |
| 78 // See crbug.com/663360. | 80 // See crbug.com/663360. |
| 79 } | 81 } |
| 80 return false; | 82 return false; |
| 81 } | 83 } |
| 82 | 84 |
| 83 /** | 85 /** |
| 84 * Returns all observed cell information from all radios on the device inclu
ding the primary | 86 * Returns all observed cell information from all radios on the device inclu
ding the primary |
| 85 * and neighboring cells. Returns only the information of cells that are reg
istered to a | 87 * and neighboring cells. Returns only the information of cells that are reg
istered to a |
| 86 * mobile network. May return {@code null}. | 88 * mobile network. May return {@code null}. |
| 87 */ | 89 */ |
| 88 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 90 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| 89 private static List<CellInfo> getRegisteredCellInfo(Context context) { | 91 private static List<CellInfo> getRegisteredCellInfo() { |
| 90 if (!isAPIAvailable(context)) { | 92 if (!isAPIAvailable()) { |
| 91 return null; | 93 return null; |
| 92 } | 94 } |
| 93 | 95 |
| 94 TelephonyManager telephonyManager = | 96 TelephonyManager telephonyManager = |
| 95 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SE
RVICE); | 97 (TelephonyManager) ContextUtils.getApplicationContext().getSyste
mService( |
| 98 Context.TELEPHONY_SERVICE); |
| 96 if (telephonyManager == null) { | 99 if (telephonyManager == null) { |
| 97 return null; | 100 return null; |
| 98 } | 101 } |
| 99 | 102 |
| 100 List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); | 103 List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); |
| 101 if (cellInfos == null) { | 104 if (cellInfos == null) { |
| 102 return null; | 105 return null; |
| 103 } | 106 } |
| 104 | 107 |
| 105 Iterator<CellInfo> iter = cellInfos.iterator(); | 108 Iterator<CellInfo> iter = cellInfos.iterator(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 150 } |
| 148 if (cellInfo instanceof CellInfoLte) { | 151 if (cellInfo instanceof CellInfoLte) { |
| 149 return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); | 152 return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); |
| 150 } | 153 } |
| 151 if (cellInfo instanceof CellInfoWcdma) { | 154 if (cellInfo instanceof CellInfoWcdma) { |
| 152 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel()
; | 155 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel()
; |
| 153 } | 156 } |
| 154 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | 157 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; |
| 155 } | 158 } |
| 156 } | 159 } |
| OLD | NEW |