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