summaryrefslogtreecommitdiff
path: root/android/telephony/NetworkScan.java
blob: a27721261daded01f8252954b5a52e19afda69f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.telephony;

import android.content.Context;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.annotation.IntDef;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * The caller of
 * {@link TelephonyManager#requestNetworkScan(NetworkScanRequest, NetworkScanCallback)}
 * will receive an instance of {@link NetworkScan}, which contains a callback method
 * {@link #stop()} for stopping the in-progress scan.
 */
public class NetworkScan {

    private static final String TAG = "NetworkScan";

    // Below errors are mapped from RadioError which is returned from RIL. We will consolidate
    // RadioErrors during the mapping if those RadioErrors mean no difference to the users.

    /**
     * Defines acceptable values of scan error code.
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({ERROR_MODEM_ERROR, ERROR_INVALID_SCAN, ERROR_MODEM_UNAVAILABLE, ERROR_UNSUPPORTED,
            ERROR_RADIO_INTERFACE_ERROR, ERROR_INVALID_SCANID, ERROR_INTERRUPTED})
    public @interface ScanErrorCode {}

    /**
     * The RIL has successfully performed the network scan.
     */
    public static final int SUCCESS = 0;                    // RadioError:NONE

    /**
     * The scan has failed due to some modem errors.
     */
    public static final int ERROR_MODEM_ERROR = 1;          // RadioError:RADIO_NOT_AVAILABLE
                                                            // RadioError:NO_MEMORY
                                                            // RadioError:INTERNAL_ERR
                                                            // RadioError:MODEM_ERR
                                                            // RadioError:OPERATION_NOT_ALLOWED

    /**
     * The parameters of the scan is invalid.
     */
    public static final int ERROR_INVALID_SCAN = 2;         // RadioError:INVALID_ARGUMENTS

    /**
     * The modem can not perform the scan because it is doing something else.
     */
    public static final int ERROR_MODEM_UNAVAILABLE = 3;    // RadioError:DEVICE_IN_USE

    /**
     * The modem does not support the request scan.
     */
    public static final int ERROR_UNSUPPORTED = 4;          // RadioError:REQUEST_NOT_SUPPORTED


    // Below errors are generated at the Telephony.

    /**
     * The RIL returns nothing or exceptions.
     */
    public static final int ERROR_RADIO_INTERFACE_ERROR = 10000;

    /**
     * The scan ID is invalid. The user is either trying to stop a scan which does not exist
     * or started by others.
     */
    public static final int ERROR_INVALID_SCANID = 10001;

    /**
     * The scan has been interrupted by another scan with higher priority.
     */
    public static final int ERROR_INTERRUPTED = 10002;

    private final int mScanId;
    private final int mSubId;

    /**
     * Stops the network scan
     *
     * Use this method to stop an ongoing scan. When user requests a new scan, a {@link NetworkScan}
     * object will be returned, and the user can stop the scan by calling this method.
     */
    public void stop() throws RemoteException {
        try {
            ITelephony telephony = getITelephony();
            if (telephony != null) {
                telephony.stopNetworkScan(mSubId, mScanId);
            } else {
                throw new RemoteException("Failed to get the ITelephony instance.");
            }
        } catch (RemoteException ex) {
            Rlog.e(TAG, "stopNetworkScan  RemoteException", ex);
            throw new RemoteException("Failed to stop the network scan with id " + mScanId);
        }
    }

    /**
     * Creates a new NetworkScan with scanId
     *
     * @param scanId The id of the scan
     * @param subId the id of the subscription
     * @hide
     */
    public NetworkScan(int scanId, int subId) {
        mScanId = scanId;
        mSubId = subId;
    }

    private ITelephony getITelephony() {
        return ITelephony.Stub.asInterface(
            ServiceManager.getService(Context.TELEPHONY_SERVICE));
    }
}