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 android.annotation.IntDef; 20 import android.os.RemoteException; 21 22 import com.android.internal.telephony.ITelephony; 23 import com.android.telephony.Rlog; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 28 /** 29 * The caller of 30 * {@link TelephonyManager#requestNetworkScan(NetworkScanRequest, Executor, NetworkScanCallback)} 31 * will receive an instance of {@link NetworkScan}, which contains a callback method 32 * {@link #stopScan()} for stopping the in-progress scan. 33 */ 34 public class NetworkScan { 35 36 private static final String TAG = "NetworkScan"; 37 38 // Below errors are mapped from RadioError which is returned from RIL. We will consolidate 39 // RadioErrors during the mapping if those RadioErrors mean no difference to the users. 40 41 /** 42 * Defines acceptable values of scan error code. 43 * @hide 44 */ 45 @Retention(RetentionPolicy.SOURCE) 46 @IntDef({ERROR_MODEM_ERROR, ERROR_INVALID_SCAN, ERROR_MODEM_UNAVAILABLE, ERROR_UNSUPPORTED, 47 ERROR_RADIO_INTERFACE_ERROR, ERROR_INVALID_SCANID, ERROR_INTERRUPTED}) 48 public @interface ScanErrorCode {} 49 50 /** 51 * The RIL has successfully performed the network scan. 52 */ 53 public static final int SUCCESS = 0; // RadioError:NONE 54 55 /** 56 * The scan has failed due to some modem errors. 57 */ 58 public static final int ERROR_MODEM_ERROR = 1; // RadioError:RADIO_NOT_AVAILABLE 59 // RadioError:NO_MEMORY 60 // RadioError:INTERNAL_ERR 61 // RadioError:MODEM_ERR 62 // RadioError:OPERATION_NOT_ALLOWED 63 64 /** 65 * The parameters of the scan is invalid. 66 */ 67 public static final int ERROR_INVALID_SCAN = 2; // RadioError:INVALID_ARGUMENTS 68 69 /** 70 * The modem can not perform the scan because it is doing something else. 71 */ 72 public static final int ERROR_MODEM_UNAVAILABLE = 3; // RadioError:DEVICE_IN_USE 73 74 /** 75 * The modem does not support the request scan. 76 */ 77 public static final int ERROR_UNSUPPORTED = 4; // RadioError:REQUEST_NOT_SUPPORTED 78 79 80 // Below errors are generated at the Telephony. 81 82 /** 83 * The RIL returns nothing or exceptions. 84 */ 85 public static final int ERROR_RADIO_INTERFACE_ERROR = 10000; 86 87 /** 88 * The scan ID is invalid. The user is either trying to stop a scan which does not exist 89 * or started by others. 90 */ 91 public static final int ERROR_INVALID_SCANID = 10001; 92 93 /** 94 * The scan has been interrupted by another scan with higher priority. 95 */ 96 public static final int ERROR_INTERRUPTED = 10002; 97 98 private final int mScanId; 99 private final int mSubId; 100 101 /** 102 * Stops the network scan 103 * 104 * Use this method to stop an ongoing scan. When user requests a new scan, a {@link NetworkScan} 105 * object will be returned, and the user can stop the scan by calling this method. 106 */ stopScan()107 public void stopScan() { 108 ITelephony telephony = getITelephony(); 109 if (telephony == null) { 110 Rlog.e(TAG, "Failed to get the ITelephony instance."); 111 } 112 try { 113 telephony.stopNetworkScan(mSubId, mScanId); 114 } catch (IllegalArgumentException ex) { 115 Rlog.d(TAG, "stopNetworkScan - no active scan for ScanID=" + mScanId); 116 } catch (RemoteException ex) { 117 Rlog.e(TAG, "stopNetworkScan RemoteException", ex); 118 } catch (RuntimeException ex) { 119 Rlog.e(TAG, "stopNetworkScan RuntimeException", ex); 120 } 121 } 122 123 /** 124 * @deprecated Use {@link #stopScan()} 125 * @removed 126 */ 127 @Deprecated stop()128 public void stop() throws RemoteException { 129 try { 130 stopScan(); 131 } catch (RuntimeException ex) { 132 throw new RemoteException("Failed to stop the network scan with id " + mScanId); 133 } 134 } 135 136 /** 137 * Creates a new NetworkScan with scanId 138 * 139 * @param scanId The id of the scan 140 * @param subId the id of the subscription 141 * @hide 142 */ NetworkScan(int scanId, int subId)143 public NetworkScan(int scanId, int subId) { 144 mScanId = scanId; 145 mSubId = subId; 146 } 147 getITelephony()148 private ITelephony getITelephony() { 149 return ITelephony.Stub.asInterface( 150 TelephonyFrameworkInitializer 151 .getTelephonyServiceManager() 152 .getTelephonyServiceRegisterer() 153 .get()); 154 } 155 } 156