aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/internal/telephony/domainselection/DomainSelectionResolver.java
blob: 65ac8b3ca7cf80b511e7e49253ae27b74bd5dcd8 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2022 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 com.android.internal.telephony.domainselection;

import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;

import static com.android.internal.telephony.RIL.RADIO_HAL_VERSION_2_1;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.telephony.DomainSelectionService;
import android.util.IndentingPrintWriter;
import android.util.LocalLog;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;

import java.io.FileDescriptor;
import java.io.PrintWriter;

/**
 * This class is an entry point to provide whether the AOSP domain selection is supported or not,
 * and bind the {@link DomainSelectionController} with the given {@link DomainSelectionService} to
 * provide a specific {@link DomainSelectionConnection} object for communicating with each domain
 * selector.
 */
public class DomainSelectionResolver {
    private static final String TAG = DomainSelectionResolver.class.getSimpleName();
    private static DomainSelectionResolver sInstance = null;

    /**
     * Creates the DomainSelectionResolver singleton instance.
     *
     * @param context The context of the application.
     * @param deviceConfigEnabled The flag to indicate whether or not the device supports
     *                            the domain selection service or not.
     */
    public static void make(Context context, boolean deviceConfigEnabled) {
        if (sInstance == null) {
            sInstance = new DomainSelectionResolver(context, deviceConfigEnabled);
        }
    }

    /**
     * Returns the singleton instance of DomainSelectionResolver.
     *
     * @return A {@link DomainSelectionResolver} instance.
     */
    public static DomainSelectionResolver getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException("DomainSelectionResolver is not ready!");
        }
        return sInstance;
    }

    /**
     * Sets a {@link DomainSelectionResolver} for injecting mock DomainSelectionResolver.
     *
     * @param resolver A {@link DomainSelectionResolver} instance to test.
     */
    @VisibleForTesting
    public static void setDomainSelectionResolver(DomainSelectionResolver resolver) {
        sInstance = resolver;
    }

    /**
     * Testing interface for injecting mock DomainSelectionController.
     */
    @VisibleForTesting
    public interface DomainSelectionControllerFactory {
        /**
         * Returns a {@link DomainSelectionController} created using the specified
         * context and {@link DomainSelectionService} instance.
         */
        DomainSelectionController create(@NonNull Context context,
                @NonNull DomainSelectionService service);
    }

    private DomainSelectionControllerFactory mDomainSelectionControllerFactory =
            new DomainSelectionControllerFactory() {
        @Override
        public DomainSelectionController create(@NonNull Context context,
                @NonNull DomainSelectionService service) {
            return new DomainSelectionController(context, service);
        }
    };

    // Persistent Logging
    private final LocalLog mEventLog = new LocalLog(10);
    private final Context mContext;
    // The flag to indicate whether the device supports the domain selection service or not.
    private final boolean mDeviceConfigEnabled;
    // DomainSelectionController, which are bound to DomainSelectionService.
    private DomainSelectionController mController;

    public DomainSelectionResolver(Context context, boolean deviceConfigEnabled) {
        mContext = context;
        mDeviceConfigEnabled = deviceConfigEnabled;
        logi("DomainSelectionResolver created: device-config=" + deviceConfigEnabled);
    }

    /**
     * Checks if the device supports the domain selection service to route the call / SMS /
     * supplementary services to the appropriate domain.
     * This checks the device-config and Radio HAL version for supporting the domain selection.
     * The domain selection requires the Radio HAL version greater than or equal to 2.1.
     *
     * @return {@code true} if the domain selection is supported on the device,
     *         {@code false} otherwise.
     */
    public boolean isDomainSelectionSupported() {
        return mDeviceConfigEnabled && PhoneFactory.getDefaultPhone()
                .getHalVersion(HAL_SERVICE_NETWORK).greaterOrEqual(RADIO_HAL_VERSION_2_1);
    }

    /**
     * Returns a {@link DomainSelectionConnection} instance.
     *
     * @param phone The Phone instance for witch this request is.
     * @param selectorType Indicates the selector type requested.
     * @param isEmergency Indicates whether this is for emergency service.
     * @throws IllegalStateException If the {@link DomainSelectionController} is not created
     *         because {@link #initialize} method is not called even if the domain selection is
     *         supported.
     * @return A {@link DomainSelectionConnection} instance if the device supports
     *         AOSP domain selection and IMS is available or {@code null} otherwise.
     */
    public @Nullable DomainSelectionConnection getDomainSelectionConnection(Phone phone,
            @DomainSelectionService.SelectorType int selectorType, boolean isEmergency) {
        if (mController == null) {
            // If the caller calls this method without checking whether the domain selection
            // is supported or not, this exception will be thrown.
            throw new IllegalStateException("DomainSelection is not supported!");
        }

        if (phone == null || phone.getImsPhone() == null
                || (!(isEmergency && selectorType == DomainSelectionService.SELECTOR_TYPE_CALLING)
                        && !phone.isImsAvailable())) {
            // In case of emergency calls, to recover the temporary failure in IMS service
            // connection, DomainSelection shall be started even when IMS isn't available.
            // DomainSelector will keep finding next available transport.
            // For other telephony services, if the binder of ImsService is not available,
            // CS domain will be used.
            return null;
        }

        return mController.getDomainSelectionConnection(phone, selectorType, isEmergency);
    }

    /** Sets a factory interface for creating {@link DomainSelectionController} instance. */
    @VisibleForTesting
    public void setDomainSelectionControllerFactory(DomainSelectionControllerFactory factory) {
        mDomainSelectionControllerFactory = factory;
    }

    /**
     * Needs to be called after the constructor to create a {@link DomainSelectionController} that
     * is bound to the given {@link DomainSelectionService}.
     *
     * @param service A {@link DomainSelectionService} to be bound.
     */
    public void initialize(@NonNull DomainSelectionService service) {
        logi("Initialize.");
        mController = mDomainSelectionControllerFactory.create(mContext, service);
    }

    /**
     * Dumps this instance into a readable format for dumpsys usage.
     */
    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");
        ipw.println("Resolver:");
        ipw.increaseIndent();
        ipw.println("Event Log:");
        ipw.increaseIndent();
        mEventLog.dump(ipw);
        ipw.decreaseIndent();
        ipw.decreaseIndent();

        ipw.println("Controller:");
        ipw.increaseIndent();
        DomainSelectionController controller = mController;
        if (controller == null) {
            ipw.println("no active controller");
        } else {
            controller.dump(ipw);
        }
        ipw.decreaseIndent();
    }

    private void logi(String s) {
        Log.i(TAG, s);
        mEventLog.log(s);
    }
}