yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.telephony; |
| 18 | |
| 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | |
Philip P. Moltmann | 95a41b8 | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 22 | import android.os.Binder; |
yinxu | 5604943 | 2017-06-27 14:05:17 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.HandlerThread; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| 28 | import android.os.Messenger; |
yinxu | 5604943 | 2017-06-27 14:05:17 -0700 | [diff] [blame] | 29 | import android.os.Parcelable; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 31 | import android.util.SparseArray; |
Hall Liu | ee31373 | 2018-11-27 14:36:51 -0800 | [diff] [blame] | 32 | |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 33 | import com.android.internal.annotations.GuardedBy; |
Hall Liu | ee31373 | 2018-11-27 14:36:51 -0800 | [diff] [blame] | 34 | import com.android.internal.telephony.ITelephony; |
Meng Wang | 1dbea2f | 2020-01-30 12:25:08 -0800 | [diff] [blame] | 35 | import com.android.telephony.Rlog; |
Hall Liu | ee31373 | 2018-11-27 14:36:51 -0800 | [diff] [blame] | 36 | |
yinxu | 5604943 | 2017-06-27 14:05:17 -0700 | [diff] [blame] | 37 | import java.util.Arrays; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 38 | import java.util.List; |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 39 | import java.util.concurrent.Executor; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 40 | |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Manages the radio access network scan requests and callbacks. |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 43 | */ |
| 44 | public final class TelephonyScanManager { |
| 45 | |
| 46 | private static final String TAG = "TelephonyScanManager"; |
| 47 | |
| 48 | /** @hide */ |
yinxu | 5604943 | 2017-06-27 14:05:17 -0700 | [diff] [blame] | 49 | public static final String SCAN_RESULT_KEY = "scanResult"; |
| 50 | |
| 51 | /** @hide */ |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 52 | public static final int CALLBACK_SCAN_RESULTS = 1; |
| 53 | /** @hide */ |
| 54 | public static final int CALLBACK_SCAN_ERROR = 2; |
| 55 | /** @hide */ |
| 56 | public static final int CALLBACK_SCAN_COMPLETE = 3; |
Hall Liu | c9d7430 | 2019-02-28 15:29:19 -0800 | [diff] [blame] | 57 | /** @hide */ |
| 58 | public static final int CALLBACK_RESTRICTED_SCAN_RESULTS = 4; |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 59 | /** @hide */ |
| 60 | public static final int CALLBACK_TELEPHONY_DIED = 5; |
Hall Liu | c9d7430 | 2019-02-28 15:29:19 -0800 | [diff] [blame] | 61 | |
| 62 | /** @hide */ |
| 63 | public static final int INVALID_SCAN_ID = -1; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
yinxu | bbc3d36 | 2017-11-30 14:58:36 -0800 | [diff] [blame] | 66 | * The caller of |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 67 | * {@link |
| 68 | * TelephonyManager#requestNetworkScan(NetworkScanRequest, Executor, NetworkScanCallback)} |
| 69 | * should implement and provide this callback so that the scan results or errors can be |
| 70 | * returned. |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 71 | */ |
| 72 | public static abstract class NetworkScanCallback { |
| 73 | /** Returns the scan results to the user, this callback will be called multiple times. */ |
| 74 | public void onResults(List<CellInfo> results) {} |
| 75 | |
| 76 | /** |
| 77 | * Informs the user that the scan has stopped. |
| 78 | * |
| 79 | * This callback will be called when the scan is finished or cancelled by the user. |
| 80 | * The related NetworkScanRequest will be deleted after this callback. |
| 81 | */ |
| 82 | public void onComplete() {} |
| 83 | |
| 84 | /** |
| 85 | * Informs the user that there is some error about the scan. |
| 86 | * |
yinxu | 392b0f1 | 2017-11-08 16:10:16 -0800 | [diff] [blame] | 87 | * This callback will be called whenever there is any error about the scan, and the scan |
| 88 | * will be terminated. onComplete() will NOT be called. |
yinxu | bbc3d36 | 2017-11-30 14:58:36 -0800 | [diff] [blame] | 89 | * |
| 90 | * @param error Error code when the scan is failed, as defined in {@link NetworkScan}. |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 91 | */ |
yinxu | bbc3d36 | 2017-11-30 14:58:36 -0800 | [diff] [blame] | 92 | public void onError(@NetworkScan.ScanErrorCode int error) {} |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private static class NetworkScanInfo { |
| 96 | private final NetworkScanRequest mRequest; |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 97 | private final Executor mExecutor; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 98 | private final NetworkScanCallback mCallback; |
| 99 | |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 100 | NetworkScanInfo( |
| 101 | NetworkScanRequest request, Executor executor, NetworkScanCallback callback) { |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 102 | mRequest = request; |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 103 | mExecutor = executor; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 104 | mCallback = callback; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private final Looper mLooper; |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 109 | private final Handler mHandler; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 110 | private final Messenger mMessenger; |
Hall Liu | 418e779 | 2019-10-03 11:48:31 -0700 | [diff] [blame] | 111 | private final SparseArray<NetworkScanInfo> mScanInfo = new SparseArray<NetworkScanInfo>(); |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 112 | private final Binder.DeathRecipient mDeathRecipient; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 113 | |
| 114 | public TelephonyScanManager() { |
| 115 | HandlerThread thread = new HandlerThread(TAG); |
| 116 | thread.start(); |
| 117 | mLooper = thread.getLooper(); |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 118 | mHandler = new Handler(mLooper) { |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void handleMessage(Message message) { |
| 121 | checkNotNull(message, "message cannot be null"); |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 122 | if (message.what == CALLBACK_TELEPHONY_DIED) { |
| 123 | // If there are no objects in mScanInfo then binder death will simply return. |
| 124 | synchronized (mScanInfo) { |
| 125 | for (int i = 0; i < mScanInfo.size(); i++) { |
| 126 | NetworkScanInfo nsi = mScanInfo.valueAt(i); |
| 127 | // At this point we go into panic mode and ignore errors that would |
| 128 | // normally stop the show in order to try and clean up as gracefully |
| 129 | // as possible. |
| 130 | if (nsi == null) continue; // shouldn't be possible |
| 131 | Executor e = nsi.mExecutor; |
| 132 | NetworkScanCallback cb = nsi.mCallback; |
| 133 | if (e == null || cb == null) continue; |
| 134 | try { |
| 135 | e.execute( |
| 136 | () -> cb.onError(NetworkScan.ERROR_MODEM_UNAVAILABLE)); |
| 137 | } catch (java.util.concurrent.RejectedExecutionException ignore) { |
| 138 | // ignore so that we can continue |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | mScanInfo.clear(); |
| 143 | } |
| 144 | return; |
| 145 | } |
| 146 | |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 147 | NetworkScanInfo nsi; |
| 148 | synchronized (mScanInfo) { |
| 149 | nsi = mScanInfo.get(message.arg2); |
| 150 | } |
| 151 | if (nsi == null) { |
| 152 | throw new RuntimeException( |
| 153 | "Failed to find NetworkScanInfo with id " + message.arg2); |
| 154 | } |
| 155 | NetworkScanCallback callback = nsi.mCallback; |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 156 | Executor executor = nsi.mExecutor; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 157 | if (callback == null) { |
| 158 | throw new RuntimeException( |
| 159 | "Failed to find NetworkScanCallback with id " + message.arg2); |
| 160 | } |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 161 | if (executor == null) { |
| 162 | throw new RuntimeException( |
| 163 | "Failed to find Executor with id " + message.arg2); |
| 164 | } |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 165 | |
| 166 | switch (message.what) { |
Hall Liu | c9d7430 | 2019-02-28 15:29:19 -0800 | [diff] [blame] | 167 | case CALLBACK_RESTRICTED_SCAN_RESULTS: |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 168 | case CALLBACK_SCAN_RESULTS: |
| 169 | try { |
yinxu | 5604943 | 2017-06-27 14:05:17 -0700 | [diff] [blame] | 170 | final Bundle b = message.getData(); |
| 171 | final Parcelable[] parcelables = b.getParcelableArray(SCAN_RESULT_KEY); |
| 172 | CellInfo[] ci = new CellInfo[parcelables.length]; |
| 173 | for (int i = 0; i < parcelables.length; i++) { |
| 174 | ci[i] = (CellInfo) parcelables[i]; |
| 175 | } |
Hall Liu | c9d7430 | 2019-02-28 15:29:19 -0800 | [diff] [blame] | 176 | executor.execute(() -> { |
yinxu | 2e8d5ed | 2018-03-15 11:39:15 -0700 | [diff] [blame] | 177 | Rlog.d(TAG, "onResults: " + ci.toString()); |
Hall Liu | c9d7430 | 2019-02-28 15:29:19 -0800 | [diff] [blame] | 178 | callback.onResults(Arrays.asList(ci)); |
yinxu | 2e8d5ed | 2018-03-15 11:39:15 -0700 | [diff] [blame] | 179 | }); |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 180 | } catch (Exception e) { |
| 181 | Rlog.e(TAG, "Exception in networkscan callback onResults", e); |
| 182 | } |
| 183 | break; |
| 184 | case CALLBACK_SCAN_ERROR: |
| 185 | try { |
yinxu | 17dbd5b | 2018-03-12 14:07:49 -0700 | [diff] [blame] | 186 | final int errorCode = message.arg1; |
yinxu | 2e8d5ed | 2018-03-15 11:39:15 -0700 | [diff] [blame] | 187 | executor.execute(() -> { |
| 188 | Rlog.d(TAG, "onError: " + errorCode); |
| 189 | callback.onError(errorCode); |
| 190 | }); |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 191 | synchronized (mScanInfo) { |
| 192 | mScanInfo.remove(message.arg2); |
| 193 | } |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 194 | } catch (Exception e) { |
| 195 | Rlog.e(TAG, "Exception in networkscan callback onError", e); |
| 196 | } |
| 197 | break; |
| 198 | case CALLBACK_SCAN_COMPLETE: |
| 199 | try { |
yinxu | 2e8d5ed | 2018-03-15 11:39:15 -0700 | [diff] [blame] | 200 | executor.execute(() -> { |
| 201 | Rlog.d(TAG, "onComplete"); |
| 202 | callback.onComplete(); |
| 203 | }); |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 204 | synchronized (mScanInfo) { |
| 205 | mScanInfo.remove(message.arg2); |
| 206 | } |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 207 | } catch (Exception e) { |
| 208 | Rlog.e(TAG, "Exception in networkscan callback onComplete", e); |
| 209 | } |
| 210 | break; |
| 211 | default: |
| 212 | Rlog.e(TAG, "Unhandled message " + Integer.toHexString(message.what)); |
| 213 | break; |
| 214 | } |
| 215 | } |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 216 | }; |
| 217 | mMessenger = new Messenger(mHandler); |
| 218 | mDeathRecipient = new Binder.DeathRecipient() { |
| 219 | @Override |
| 220 | public void binderDied() { |
| 221 | mHandler.obtainMessage(CALLBACK_TELEPHONY_DIED).sendToTarget(); |
| 222 | } |
| 223 | }; |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Request a network scan. |
| 228 | * |
| 229 | * This method is asynchronous, so the network scan results will be returned by callback. |
| 230 | * The returned NetworkScan will contain a callback method which can be used to stop the scan. |
| 231 | * |
| 232 | * <p> |
| 233 | * Requires Permission: |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 234 | * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 235 | * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE} |
| 236 | * Or the calling app has carrier privileges. @see #hasCarrierPrivileges |
| 237 | * |
| 238 | * @param request Contains all the RAT with bands/channels that need to be scanned. |
| 239 | * @param callback Returns network scan results or errors. |
Philip P. Moltmann | 95a41b8 | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 240 | * @param callingPackage The package name of the caller |
| 241 | * @param callingFeatureId The feature id inside of the calling package |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 242 | * @return A NetworkScan obj which contains a callback which can stop the scan. |
| 243 | * @hide |
| 244 | */ |
| 245 | public NetworkScan requestNetworkScan(int subId, |
Hall Liu | ee31373 | 2018-11-27 14:36:51 -0800 | [diff] [blame] | 246 | NetworkScanRequest request, Executor executor, NetworkScanCallback callback, |
Philip P. Moltmann | 95a41b8 | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 247 | String callingPackage, @Nullable String callingFeatureId) { |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 248 | try { |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 249 | final ITelephony telephony = getITelephony(); |
| 250 | if (telephony == null) return null; |
| 251 | |
| 252 | int scanId = telephony.requestNetworkScan( |
| 253 | subId, request, mMessenger, new Binder(), callingPackage, |
| 254 | callingFeatureId); |
| 255 | if (scanId == INVALID_SCAN_ID) { |
| 256 | Rlog.e(TAG, "Failed to initiate network scan"); |
| 257 | return null; |
| 258 | } |
| 259 | synchronized (mScanInfo) { |
| 260 | // We link to death whenever a scan is started to ensure that we are linked |
| 261 | // at the point that phone process death might matter. |
| 262 | // We never unlink because: |
| 263 | // - Duplicate links to death with the same callback do not result in |
| 264 | // extraneous callbacks (the tracking de-dupes). |
| 265 | // - Receiving binderDeath() when no scans are active is a no-op. |
| 266 | telephony.asBinder().linkToDeath(mDeathRecipient, 0); |
| 267 | saveScanInfo(scanId, request, executor, callback); |
| 268 | return new NetworkScan(scanId, subId); |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 269 | } |
| 270 | } catch (RemoteException ex) { |
| 271 | Rlog.e(TAG, "requestNetworkScan RemoteException", ex); |
| 272 | } catch (NullPointerException ex) { |
| 273 | Rlog.e(TAG, "requestNetworkScan NPE", ex); |
| 274 | } |
| 275 | return null; |
| 276 | } |
| 277 | |
Nathan Harold | 5a65328 | 2020-05-15 15:25:47 -0700 | [diff] [blame] | 278 | @GuardedBy("mScanInfo") |
yinxu | 1abe9b1 | 2018-02-28 15:43:42 -0800 | [diff] [blame] | 279 | private void saveScanInfo( |
| 280 | int id, NetworkScanRequest request, Executor executor, NetworkScanCallback callback) { |
Hall Liu | 418e779 | 2019-10-03 11:48:31 -0700 | [diff] [blame] | 281 | mScanInfo.put(id, new NetworkScanInfo(request, executor, callback)); |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private ITelephony getITelephony() { |
| 285 | return ITelephony.Stub.asInterface( |
Peter Wang | d9eebca | 2019-12-30 16:14:01 -0800 | [diff] [blame] | 286 | TelephonyFrameworkInitializer |
| 287 | .getTelephonyServiceManager() |
| 288 | .getTelephonyServiceRegisterer() |
| 289 | .get()); |
yinxu | fdfb6f4 | 2017-04-12 16:09:54 -0700 | [diff] [blame] | 290 | } |
| 291 | } |