Chromium Code Reviews| 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; | |
| 8 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 9 import android.content.Context; | 8 import android.content.Context; |
| 10 import android.content.pm.PackageManager; | |
| 11 import android.os.Build; | 9 import android.os.Build; |
| 12 import android.os.Process; | 10 import android.os.Handler; |
| 13 import android.telephony.CellInfo; | 11 import android.os.HandlerThread; |
| 14 import android.telephony.CellInfoCdma; | 12 import android.telephony.PhoneStateListener; |
| 15 import android.telephony.CellInfoGsm; | 13 import android.telephony.SignalStrength; |
| 16 import android.telephony.CellInfoLte; | |
| 17 import android.telephony.CellInfoWcdma; | |
| 18 import android.telephony.TelephonyManager; | 14 import android.telephony.TelephonyManager; |
| 19 | 15 |
| 16 import org.chromium.base.ApplicationState; | |
| 17 import org.chromium.base.ApplicationStatus; | |
| 20 import org.chromium.base.ContextUtils; | 18 import org.chromium.base.ContextUtils; |
| 19 import org.chromium.base.ThreadUtils; | |
| 21 import org.chromium.base.annotations.CalledByNative; | 20 import org.chromium.base.annotations.CalledByNative; |
| 22 import org.chromium.base.annotations.JNINamespace; | 21 import org.chromium.base.annotations.JNINamespace; |
| 23 | 22 import org.chromium.base.annotations.SuppressFBWarnings; |
| 24 import java.util.Iterator; | |
| 25 import java.util.List; | |
| 26 | 23 |
| 27 /** | 24 /** |
| 28 * This class interacts with the CellInfo API provided by Android. This class is thread safe. | 25 * This class provides the cellular signal strength using the APIs provided by A ndroid. This class |
| 26 * is thread safe. | |
| 29 */ | 27 */ |
| 30 @JNINamespace("net::android::cellular_signal_strength") | 28 @JNINamespace("net::android") |
| 31 public class AndroidCellularSignalStrength { | 29 public class AndroidCellularSignalStrength { |
| 30 // {@link mSignalLevel} is set to volatile since may be accessed across thre ads. | |
| 31 private volatile int mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SU PPORTED; | |
| 32 | |
| 33 @SuppressFBWarnings("URF_UNREAD_FIELD") | |
| 34 private CellStateListener mCellStateListener; | |
|
pauljensen
2017/06/23 14:31:25
unused, remove
tbansal1
2017/06/26 16:08:00
umm, this is in use.
pauljensen
2017/06/26 16:24:22
Where is the use?
tbansal1
2017/06/26 16:28:09
mCellStateListener is used in Line 100 below in th
pauljensen
2017/06/26 16:29:37
There is a dead write, but no use of it.
tbansal1
2017/06/26 16:49:28
Thanks. Fixed.
| |
| 35 | |
| 36 private static final AndroidCellularSignalStrength sInstance = | |
| 37 new AndroidCellularSignalStrength(); | |
|
pauljensen
2017/06/23 14:31:25
When is this created? I think statics are initial
tbansal1
2017/06/26 16:08:00
This is created when the static object is first re
| |
| 38 | |
| 32 /** | 39 /** |
| 33 * @return Signal strength (in dbM) for the currently registered cellular ne twork. Returns | 40 * This class listens to the changes in the cellular signal strength level a nd updates {@link |
| 34 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str ength is | 41 * mSignalLevel}. {@link CellStateListener} registers as a signal strength o bserver only if the |
| 35 * unavailable or if there are multiple cellular radios on the device. | 42 * application has running activities. |
| 36 */ | 43 */ |
| 37 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 44 private class CellStateListener |
| 38 @CalledByNative | 45 extends PhoneStateListener implements ApplicationStatus.ApplicationS tateListener { |
| 39 public static int getSignalStrengthDbm() { | 46 private final TelephonyManager mTelephonyManager; |
| 40 List<CellInfo> cellInfos = getRegisteredCellInfo(); | 47 |
| 41 return cellInfos == null || cellInfos.size() != 1 | 48 CellStateListener() { |
| 42 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | 49 ThreadUtils.assertOnBackgroundThread(); |
| 43 : getSignalStrengthDbm(cellInfos.get(0)); | 50 |
| 51 mTelephonyManager = | |
| 52 (TelephonyManager) ContextUtils.getApplicationContext().getS ystemService( | |
| 53 Context.TELEPHONY_SERVICE); | |
| 54 | |
| 55 if (mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_RE ADY) return; | |
| 56 | |
| 57 ApplicationStatus.registerApplicationStateListener(this); | |
| 58 onApplicationStateChange(ApplicationStatus.getStateForApplication()) ; | |
| 59 } | |
| 60 | |
| 61 private void register() { | |
| 62 mTelephonyManager.listen(this, PhoneStateListener.LISTEN_SIGNAL_STRE NGTHS); | |
| 63 } | |
| 64 | |
| 65 private void unregister() { | |
| 66 mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
|
pauljensen
2017/06/23 14:31:25
This is racy. An onSignalStrengthsChanged() event
tbansal1
2017/06/26 16:08:00
Done.
| |
| 67 mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 @TargetApi(Build.VERSION_CODES.M) | |
| 72 public void onSignalStrengthsChanged(SignalStrength signalStrength) { | |
| 73 mSignalLevel = signalStrength.getLevel(); | |
| 74 } | |
| 75 | |
| 76 // ApplicationStatus.ApplicationStateListener | |
| 77 @Override | |
| 78 public void onApplicationStateChange(int newState) { | |
| 79 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { | |
| 80 register(); | |
| 81 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { | |
| 82 unregister(); | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 private AndroidCellularSignalStrength() { | |
| 88 if (!isAPIAvailable(ContextUtils.getApplicationContext())) return; | |
| 89 | |
| 90 HandlerThread handlerThread = new HandlerThread("AndroidCellularSignalSt rength"); | |
| 91 handlerThread.start(); | |
| 92 | |
| 93 new Handler(handlerThread.getLooper()).post(new Runnable() { | |
| 94 @Override | |
| 95 public void run() { | |
| 96 mCellStateListener = new CellStateListener(); | |
| 97 } | |
| 98 }); | |
| 44 } | 99 } |
| 45 | 100 |
| 46 /** | 101 /** |
| 47 * @return the signal strength level (between 0 and 4, both inclusive) for t he currently | 102 * @return the signal strength level (between 0 and 4, both inclusive) for t he currently |
| 48 * registered cellular network with lower value indicating lower signal stre ngth. Returns | 103 * registered cellular network with lower value indicating lower signal stre ngth. Returns |
| 49 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str ength level is | 104 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str ength level is |
| 50 * unavailable or if there are multiple cellular radios on the device. | 105 * unavailable. |
| 51 */ | 106 */ |
| 52 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 107 @TargetApi(Build.VERSION_CODES.M) |
| 53 @CalledByNative | 108 @CalledByNative |
| 54 public static int getSignalStrengthLevel() { | 109 private static int getSignalStrengthLevel() { |
| 55 List<CellInfo> cellInfos = getRegisteredCellInfo(); | 110 return sInstance.mSignalLevel; |
| 56 return cellInfos == null || cellInfos.size() != 1 | |
| 57 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | |
| 58 : getSignalStrengthLevel(cellInfos.get(0)); | |
| 59 } | 111 } |
| 60 | 112 |
| 61 /** | 113 /** |
| 62 * Returns true if the API for quering the signal strength is available. | 114 * Returns true if the API for quering the signal strength is available. |
| 63 * {@link android.telephony#CellInfoWcdma} is only available on API Level | 115 * {@link android.telephony.SignalStrength#getLevel} is only available on AP I Level |
| 64 * {@link Build.VERSION_CODES#JELLY_BEAN_MR2} and higher. Also verifies that appropriate | 116 * {@link Build.VERSION_CODES#M} and higher. |
| 65 * permissions are already available. This ensures that on Android M and hig her, Chromium will | |
| 66 * not request run-time permission from the user when querying for cellular signal strength. | |
| 67 * TODO(tbansal): Consider using {@link TelephonyManager#getNeighboringCellI nfo} | |
| 68 * for earlier versions of Android. | |
| 69 */ | |
| 70 private static boolean isAPIAvailable() { | |
| 71 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return f alse; | |
| 72 | |
| 73 try { | |
| 74 return ContextUtils.getApplicationContext().checkPermission( | |
| 75 Manifest.permission.ACCESS_COARSE_LOCATION, Process.m yPid(), | |
| 76 Process.myUid()) | |
| 77 == PackageManager.PERMISSION_GRANTED; | |
| 78 } catch (Exception ignored) { | |
| 79 // Work around certain platforms where this method sometimes throws a runtime exception. | |
| 80 // See crbug.com/663360. | |
| 81 } | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Returns all observed cell information from all radios on the device inclu ding the primary | |
| 87 * and neighboring cells. Returns only the information of cells that are reg istered to a | |
| 88 * mobile network. May return {@code null}. | |
| 89 */ | 117 */ |
| 90 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 118 private static boolean isAPIAvailable(Context context) { |
| 91 private static List<CellInfo> getRegisteredCellInfo() { | 119 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; |
| 92 if (!isAPIAvailable()) { | |
| 93 return null; | |
| 94 } | |
| 95 | |
| 96 TelephonyManager telephonyManager = | |
| 97 (TelephonyManager) ContextUtils.getApplicationContext().getSyste mService( | |
| 98 Context.TELEPHONY_SERVICE); | |
| 99 if (telephonyManager == null) { | |
| 100 return null; | |
| 101 } | |
| 102 | |
| 103 List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); | |
| 104 if (cellInfos == null) { | |
| 105 return null; | |
| 106 } | |
| 107 | |
| 108 Iterator<CellInfo> iter = cellInfos.iterator(); | |
| 109 while (iter.hasNext()) { | |
| 110 if (!iter.next().isRegistered()) { | |
| 111 iter.remove(); | |
| 112 } | |
| 113 } | |
| 114 return cellInfos; | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link | |
| 119 * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength i s unavailable. | |
| 120 */ | |
| 121 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
| 122 private static int getSignalStrengthDbm(CellInfo cellInfo) { | |
| 123 if (cellInfo instanceof CellInfoCdma) { | |
| 124 return ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm(); | |
| 125 } | |
| 126 if (cellInfo instanceof CellInfoGsm) { | |
| 127 return ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm(); | |
| 128 } | |
| 129 if (cellInfo instanceof CellInfoLte) { | |
| 130 return ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm(); | |
| 131 } | |
| 132 if (cellInfo instanceof CellInfoWcdma) { | |
| 133 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm(); | |
| 134 } | |
| 135 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * @return the signal level from {@link cellInfo}. Returns {@link | |
| 140 * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal | |
| 141 * level is unavailable with lower value indicating lower signal strength. | |
| 142 */ | |
| 143 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
| 144 private static int getSignalStrengthLevel(CellInfo cellInfo) { | |
| 145 if (cellInfo instanceof CellInfoCdma) { | |
| 146 return ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel(); | |
| 147 } | |
| 148 if (cellInfo instanceof CellInfoGsm) { | |
| 149 return ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel(); | |
| 150 } | |
| 151 if (cellInfo instanceof CellInfoLte) { | |
| 152 return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); | |
| 153 } | |
| 154 if (cellInfo instanceof CellInfoWcdma) { | |
| 155 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel() ; | |
| 156 } | |
| 157 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
| 158 } | 120 } |
| 159 } | 121 } |
| OLD | NEW |