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.telephony.PhoneStateListener; |
13 import android.telephony.CellInfo; | 11 import android.telephony.SignalStrength; |
14 import android.telephony.CellInfoCdma; | |
15 import android.telephony.CellInfoGsm; | |
16 import android.telephony.CellInfoLte; | |
17 import android.telephony.CellInfoWcdma; | |
18 import android.telephony.TelephonyManager; | 12 import android.telephony.TelephonyManager; |
19 | 13 |
14 import org.chromium.base.ThreadUtils; | |
20 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
21 import org.chromium.base.annotations.JNINamespace; | 16 import org.chromium.base.annotations.JNINamespace; |
17 import org.chromium.base.annotations.SuppressFBWarnings; | |
22 | 18 |
23 import java.util.Iterator; | 19 import javax.annotation.concurrent.GuardedBy; |
24 import java.util.List; | |
25 | 20 |
26 /** | 21 /** |
27 * This class interacts with the CellInfo API provided by Android. This class is thread safe. | 22 * This class provides the cellular signal strength using the APIs provided by A ndroid. This class |
23 * is thread safe. | |
28 */ | 24 */ |
29 @JNINamespace("net::android::cellular_signal_strength") | 25 @JNINamespace("net::android") |
30 public class AndroidCellularSignalStrength { | 26 public class AndroidCellularSignalStrength { |
27 private final Context mContext; | |
28 | |
29 private final Object mLock = new Object(); | |
30 | |
31 @GuardedBy("mLock") | |
32 private int mSignalLevel = CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
33 | |
34 @SuppressFBWarnings("URF_UNREAD_FIELD") | |
35 private CellStateListener mCellStateListener; | |
36 | |
31 /** | 37 /** |
32 * @return Signal strength (in dbM) for the currently registered cellular ne twork. Returns | 38 * This class listens to the changes in the cellular signal strength level a nd updates {@link |
33 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str ength is | 39 * mSignalLevel}. |
34 * unavailable or if there are multiple cellular radios on the device. | |
35 */ | 40 */ |
36 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 41 private class CellStateListener extends PhoneStateListener { |
42 CellStateListener() { | |
43 ThreadUtils.assertOnUiThread(); | |
44 TelephonyManager telephonyManager = | |
45 (TelephonyManager) mContext.getSystemService(Context.TELEPHO NY_SERVICE); | |
46 | |
47 if (telephonyManager.getSimState() != TelephonyManager.SIM_STATE_REA DY) return; | |
pauljensen
2017/04/13 11:36:30
Do these calls require any permissions?
tbansal1
2017/06/07 21:39:20
No permissions required.
| |
48 | |
49 telephonyManager.listen(this, PhoneStateListener.LISTEN_SIGNAL_STREN GTHS); | |
50 } | |
51 | |
52 @Override | |
53 @TargetApi(Build.VERSION_CODES.M) | |
54 public void onSignalStrengthsChanged(SignalStrength signalStrength) { | |
55 synchronized (mLock) { | |
56 mSignalLevel = signalStrength.getLevel(); | |
57 } | |
58 } | |
59 } | |
60 | |
37 @CalledByNative | 61 @CalledByNative |
38 public static int getSignalStrengthDbm(Context context) { | 62 private static AndroidCellularSignalStrength create(Context context) { |
39 List<CellInfo> cellInfos = getRegisteredCellInfo(context); | 63 return new AndroidCellularSignalStrength(context); |
40 return cellInfos == null || cellInfos.size() != 1 | 64 } |
41 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | 65 |
42 : getSignalStrengthDbm(cellInfos.get(0)); | 66 private AndroidCellularSignalStrength(Context context) { |
67 mContext = context; | |
68 | |
69 if (!isAPIAvailable(context)) { | |
70 return; | |
71 } | |
72 | |
73 ThreadUtils.runOnUiThread(new Runnable() { | |
74 @Override | |
75 public void run() { | |
76 mCellStateListener = new CellStateListener(); | |
77 } | |
78 }); | |
43 } | 79 } |
44 | 80 |
45 /** | 81 /** |
46 * @return the signal strength level (between 0 and 4, both inclusive) for t he currently | 82 * @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 | 83 * 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 | 84 * {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal str ength level is |
49 * unavailable or if there are multiple cellular radios on the device. | 85 * unavailable. |
50 */ | 86 */ |
51 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | 87 @TargetApi(Build.VERSION_CODES.M) |
52 @CalledByNative | 88 @CalledByNative |
53 public static int getSignalStrengthLevel(Context context) { | 89 public int getSignalStrengthLevel() { |
54 List<CellInfo> cellInfos = getRegisteredCellInfo(context); | 90 synchronized (mLock) { |
55 return cellInfos == null || cellInfos.size() != 1 | 91 return mSignalLevel; |
56 ? CellularSignalStrengthError.ERROR_NOT_SUPPORTED | 92 } |
57 : getSignalStrengthLevel(cellInfos.get(0)); | |
58 } | 93 } |
59 | 94 |
60 /** | 95 /** |
61 * Returns true if the API for quering the signal strength is available. | 96 * Returns true if the API for quering the signal strength is available. |
62 * {@link android.telephony#CellInfoWcdma} is only available on API Level | 97 * {@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.
| |
63 * {@link Build.VERSION_CODES#JELLY_BEAN_MR2} and higher. Also verifies that appropriate | 98 * {@link Build.VERSION_CODES#M} and higher. |
64 * permissions are already available. This ensures that on Android M and hig her, Chromium will | 99 */ |
65 * not request run-time permission from the user when querying for cellular signal strength. | |
66 * TODO(tbansal): Consider using {@link TelephonyManager#getNeighboringCellI nfo} | |
67 * for earlier versions of Android. | |
68 */ | |
69 private static boolean isAPIAvailable(Context context) { | 100 private static boolean isAPIAvailable(Context context) { |
70 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return f alse; | 101 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; |
71 | |
72 try { | |
73 return context.checkPermission(Manifest.permission.ACCESS_COARSE_LOC ATION, | |
74 Process.myPid(), Process.myUid()) | |
75 == PackageManager.PERMISSION_GRANTED; | |
76 } catch (Exception ignored) { | |
77 // Work around certain platforms where this method sometimes throws a runtime exception. | |
78 // See crbug.com/663360. | |
79 } | |
80 return false; | |
81 } | |
82 | |
83 /** | |
84 * 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 | |
86 * mobile network. May return {@code null}. | |
87 */ | |
88 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
89 private static List<CellInfo> getRegisteredCellInfo(Context context) { | |
90 if (!isAPIAvailable(context)) { | |
91 return null; | |
92 } | |
93 | |
94 TelephonyManager telephonyManager = | |
95 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SE RVICE); | |
96 if (telephonyManager == null) { | |
97 return null; | |
98 } | |
99 | |
100 List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); | |
101 if (cellInfos == null) { | |
102 return null; | |
103 } | |
104 | |
105 Iterator<CellInfo> iter = cellInfos.iterator(); | |
106 while (iter.hasNext()) { | |
107 if (!iter.next().isRegistered()) { | |
108 iter.remove(); | |
109 } | |
110 } | |
111 return cellInfos; | |
112 } | |
113 | |
114 /** | |
115 * @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link | |
116 * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength i s unavailable. | |
117 */ | |
118 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
119 private static int getSignalStrengthDbm(CellInfo cellInfo) { | |
120 if (cellInfo instanceof CellInfoCdma) { | |
121 return ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm(); | |
122 } | |
123 if (cellInfo instanceof CellInfoGsm) { | |
124 return ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm(); | |
125 } | |
126 if (cellInfo instanceof CellInfoLte) { | |
127 return ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm(); | |
128 } | |
129 if (cellInfo instanceof CellInfoWcdma) { | |
130 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm(); | |
131 } | |
132 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
133 } | |
134 | |
135 /** | |
136 * @return the signal level from {@link cellInfo}. Returns {@link | |
137 * CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal | |
138 * level is unavailable with lower value indicating lower signal strength. | |
139 */ | |
140 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) | |
141 private static int getSignalStrengthLevel(CellInfo cellInfo) { | |
142 if (cellInfo instanceof CellInfoCdma) { | |
143 return ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel(); | |
144 } | |
145 if (cellInfo instanceof CellInfoGsm) { | |
146 return ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel(); | |
147 } | |
148 if (cellInfo instanceof CellInfoLte) { | |
149 return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel(); | |
150 } | |
151 if (cellInfo instanceof CellInfoWcdma) { | |
152 return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel() ; | |
153 } | |
154 return CellularSignalStrengthError.ERROR_NOT_SUPPORTED; | |
155 } | 102 } |
156 } | 103 } |
OLD | NEW |