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 |
| index fc26a81d7e4ceb0fe83e2bbdb84d217d52d5be46..03968f9517b4b55ff41754f7a6927ab0e6efeb39 100644 |
| --- a/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java |
| +++ b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java |
| @@ -4,153 +4,100 @@ |
| package org.chromium.net; |
| -import android.Manifest; |
| import android.annotation.TargetApi; |
| 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.PhoneStateListener; |
| +import android.telephony.SignalStrength; |
| import android.telephony.TelephonyManager; |
| +import org.chromium.base.ThreadUtils; |
| import org.chromium.base.annotations.CalledByNative; |
| import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.base.annotations.SuppressFBWarnings; |
| -import java.util.Iterator; |
| -import java.util.List; |
| +import javax.annotation.concurrent.GuardedBy; |
| /** |
| - * This class interacts with the CellInfo API provided by Android. This class is thread safe. |
| + * This class provides the cellular signal strength using the APIs provided by Android. This class |
| + * is thread safe. |
| */ |
| -@JNINamespace("net::android::cellular_signal_strength") |
| +@JNINamespace("net::android") |
| public class AndroidCellularSignalStrength { |
| - /** |
| - * @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. |
| - */ |
| - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| - @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)); |
| - } |
| + private final Context mContext; |
| - /** |
| - * @return the signal strength level (between 0 and 4, both inclusive) for the currently |
| - * registered cellular network with lower value indicating lower signal strength. Returns |
| - * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength level is |
| - * unavailable or if there are multiple cellular radios on the device. |
| - */ |
| - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| - @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)); |
| - } |
| + private final Object mLock = new Object(); |
| - /** |
| - * Returns true if the API for quering the signal strength is available. |
| - * {@link android.telephony#CellInfoWcdma} is only available on API Level |
| - * {@link Build.VERSION_CODES#JELLY_BEAN_MR2} and higher. Also verifies that appropriate |
| - * permissions are already available. This ensures that on Android M and higher, Chromium will |
| - * not request run-time permission from the user when querying for cellular signal strength. |
| - * TODO(tbansal): Consider using {@link TelephonyManager#getNeighboringCellInfo} |
| - * for earlier versions of Android. |
| - */ |
| - private static boolean isAPIAvailable(Context context) { |
| - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false; |
| - |
| - try { |
| - return context.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION, |
| - Process.myPid(), Process.myUid()) |
| - == PackageManager.PERMISSION_GRANTED; |
| - } catch (Exception ignored) { |
| - // Work around certain platforms where this method sometimes throws a runtime exception. |
| - // See crbug.com/663360. |
| - } |
| - return false; |
| - } |
| + @GuardedBy("mLock") |
| + private int mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SUPPORTED; |
| + |
| + @SuppressFBWarnings("URF_UNREAD_FIELD") |
| + private CellStateListener mCellStateListener; |
| /** |
| - * 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 {@code null}. |
| + * This class listens to the changes in the cellular signal strength level and updates {@link |
| + * mSignalLevel}. |
| */ |
| - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| - private static List<CellInfo> getRegisteredCellInfo(Context context) { |
| - if (!isAPIAvailable(context)) { |
| - return null; |
| + private class CellStateListener extends PhoneStateListener { |
| + CellStateListener() { |
| + ThreadUtils.assertOnUiThread(); |
| + TelephonyManager telephonyManager = |
| + (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| + |
| + if (telephonyManager.getSimState() != TelephonyManager.SIM_STATE_READY) return; |
|
pauljensen
2017/04/13 11:36:30
Do these calls require any permissions?
tbansal1
2017/06/07 21:39:20
No permissions required.
|
| + |
| + telephonyManager.listen(this, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); |
| } |
| - TelephonyManager telephonyManager = |
| - (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| - if (telephonyManager == null) { |
| - return null; |
| + @Override |
| + @TargetApi(Build.VERSION_CODES.M) |
| + public void onSignalStrengthsChanged(SignalStrength signalStrength) { |
| + synchronized (mLock) { |
| + mSignalLevel = signalStrength.getLevel(); |
| + } |
| } |
| + } |
| + |
| + @CalledByNative |
| + private static AndroidCellularSignalStrength create(Context context) { |
| + return new AndroidCellularSignalStrength(context); |
| + } |
| + |
| + private AndroidCellularSignalStrength(Context context) { |
| + mContext = context; |
| - List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); |
| - if (cellInfos == null) { |
| - return null; |
| + if (!isAPIAvailable(context)) { |
| + return; |
| } |
| - Iterator<CellInfo> iter = cellInfos.iterator(); |
| - while (iter.hasNext()) { |
| - if (!iter.next().isRegistered()) { |
| - iter.remove(); |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + mCellStateListener = new CellStateListener(); |
| } |
| - } |
| - return cellInfos; |
| + }); |
| } |
| /** |
| - * @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link |
| - * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength is unavailable. |
| + * @return the signal strength level (between 0 and 4, both inclusive) for the currently |
| + * registered cellular network with lower value indicating lower signal strength. Returns |
| + * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength level is |
| + * unavailable. |
| */ |
| - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| - 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(); |
| + @TargetApi(Build.VERSION_CODES.M) |
| + @CalledByNative |
| + public int getSignalStrengthLevel() { |
| + synchronized (mLock) { |
| + return mSignalLevel; |
| } |
| - 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. |
| + * Returns true if the API for quering the signal strength is available. |
| + * {@link android.telephony.SignalStrength#getLevel} is only available on API Level |
|
pauljensen
2017/04/13 11:36:30
extra spaces after @link
tbansal1
2017/06/07 21:39:20
Done.
|
| + * {@link Build.VERSION_CODES#M} and higher. |
| */ |
| - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
| - 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; |
| + private static boolean isAPIAvailable(Context context) { |
| + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; |
| } |
| } |