Home | History | Annotate | Download | only in telephony
      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.content.Context;
     20 import android.os.RemoteException;
     21 import android.os.ServiceManager;
     22 import android.annotation.IntDef;
     23 import android.util.Log;
     24 
     25 import com.android.internal.telephony.ITelephony;
     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      */
    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 (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
    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      */
    143     public NetworkScan(int scanId, int subId) {
    144         mScanId = scanId;
    145         mSubId = subId;
    146     }
    147 
    148     private ITelephony getITelephony() {
    149         return ITelephony.Stub.asInterface(
    150             ServiceManager.getService(Context.TELEPHONY_SERVICE));
    151     }
    152 }
    153