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