Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Unified Diff: net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java

Issue 2763853002: Use Android callback API to obtain cellular signal strength (Closed)
Patch Set: comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/android/cellular_signal_strength_unittest.cc ('k') | net/nqe/network_quality_estimator.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 06a0a9f0c114ef7d42584b8cd86803130460ae7a..c5f50d0e6b04458b8b680dc33771658388613a59 100644
--- a/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java
+++ b/net/android/java/src/org/chromium/net/AndroidCellularSignalStrength.java
@@ -4,156 +4,111 @@
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.os.Handler;
+import android.os.HandlerThread;
+import android.telephony.PhoneStateListener;
+import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
+import org.chromium.base.ApplicationState;
+import org.chromium.base.ApplicationStatus;
import org.chromium.base.ContextUtils;
+import org.chromium.base.ThreadUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
-import java.util.Iterator;
-import java.util.List;
-
/**
- * 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() {
- List<CellInfo> cellInfos = getRegisteredCellInfo();
- return cellInfos == null || cellInfos.size() != 1
- ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED
- : getSignalStrengthDbm(cellInfos.get(0));
- }
+ // {@link mSignalLevel} is set to volatile since may be accessed across threads.
+ private volatile int mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
+
+ private static final AndroidCellularSignalStrength sInstance =
+ new AndroidCellularSignalStrength();
/**
- * @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.
+ * This class listens to the changes in the cellular signal strength level and updates {@link
+ * mSignalLevel}. {@link CellStateListener} registers as a signal strength observer only if the
+ * application has running activities.
*/
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
- @CalledByNative
- public static int getSignalStrengthLevel() {
- List<CellInfo> cellInfos = getRegisteredCellInfo();
- return cellInfos == null || cellInfos.size() != 1
- ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED
- : getSignalStrengthLevel(cellInfos.get(0));
- }
+ private class CellStateListener
+ extends PhoneStateListener implements ApplicationStatus.ApplicationStateListener {
+ private final TelephonyManager mTelephonyManager;
- /**
- * 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() {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false;
-
- try {
- return ContextUtils.getApplicationContext().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.
+ CellStateListener() {
+ ThreadUtils.assertOnBackgroundThread();
+
+ mTelephonyManager =
+ (TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
+ Context.TELEPHONY_SERVICE);
+
+ if (mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_READY) return;
+
+ ApplicationStatus.registerApplicationStateListener(this);
+ onApplicationStateChange(ApplicationStatus.getStateForApplication());
}
- return false;
- }
- /**
- * 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}.
- */
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
- private static List<CellInfo> getRegisteredCellInfo() {
- if (!isAPIAvailable()) {
- return null;
+ private void register() {
+ mTelephonyManager.listen(this, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
- TelephonyManager telephonyManager =
- (TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
- Context.TELEPHONY_SERVICE);
- if (telephonyManager == null) {
- return null;
+ private void unregister() {
+ mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
+ mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
}
- List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
- if (cellInfos == null) {
- return null;
+ @Override
+ @TargetApi(Build.VERSION_CODES.M)
+ public void onSignalStrengthsChanged(SignalStrength signalStrength) {
+ if (ApplicationStatus.getStateForApplication()
+ != ApplicationState.HAS_RUNNING_ACTIVITIES) {
+ return;
+ }
+ mSignalLevel = signalStrength.getLevel();
}
- Iterator<CellInfo> iter = cellInfos.iterator();
- while (iter.hasNext()) {
- if (!iter.next().isRegistered()) {
- iter.remove();
+ // ApplicationStatus.ApplicationStateListener
+ @Override
+ public void onApplicationStateChange(int newState) {
+ if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
+ register();
+ } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
+ unregister();
}
}
- return cellInfos;
}
- /**
- * @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link
- * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength 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();
- }
- return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
+ private AndroidCellularSignalStrength() {
+ // {@link android.telephony.SignalStrength#getLevel} is only available on API Level
+ // {@link Build.VERSION_CODES#M} and higher.
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
+
+ HandlerThread handlerThread = new HandlerThread("AndroidCellularSignalStrength");
+ handlerThread.start();
+
+ new Handler(handlerThread.getLooper()).post(new Runnable() {
+ @Override
+ public void run() {
+ new CellStateListener();
+ }
+ });
}
/**
- * @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.
+ * @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 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;
+ @TargetApi(Build.VERSION_CODES.M)
+ @CalledByNative
+ private static int getSignalStrengthLevel() {
+ return sInstance.mSignalLevel;
}
}
« no previous file with comments | « net/android/cellular_signal_strength_unittest.cc ('k') | net/nqe/network_quality_estimator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698