1 /*
2  * Copyright (C) 2020 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 com.android.settings.network.ims;
18 
19 import android.telephony.AccessNetworkConstants;
20 import android.telephony.SubscriptionManager;
21 import android.telephony.ims.ImsException;
22 import android.telephony.ims.ImsMmTelManager;
23 import android.telephony.ims.feature.ImsFeature;
24 import android.telephony.ims.feature.MmTelFeature;
25 import android.telephony.ims.stub.ImsRegistrationImplBase;
26 
27 import androidx.annotation.VisibleForTesting;
28 
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 
32 /**
33  * Controller class for querying IMS status
34  */
35 abstract class ImsQueryController {
36 
37     private static final long TIMEOUT_MILLIS = 2000;
38 
39     private volatile int mCapability;
40     private volatile int mTech;
41     private volatile int mTransportType;
42 
43     /**
44      * Constructor for query IMS status
45      *
46      * @param capability {@link MmTelFeature.MmTelCapabilities#MmTelCapability}
47      * @param tech {@link ImsRegistrationImplBase#ImsRegistrationTech}
48      * @param transportType {@link AccessNetworkConstants#TransportType}
49      */
ImsQueryController( @mTelFeature.MmTelCapabilities.MmTelCapability int capability, @ImsRegistrationImplBase.ImsRegistrationTech int tech, @AccessNetworkConstants.TransportType int transportType)50     ImsQueryController(
51             @MmTelFeature.MmTelCapabilities.MmTelCapability int capability,
52             @ImsRegistrationImplBase.ImsRegistrationTech int tech,
53             @AccessNetworkConstants.TransportType int transportType) {
54         mCapability = capability;
55         mTech = tech;
56         mTransportType = transportType;
57     }
58 
isEnabledByUser(int subId)59     abstract boolean isEnabledByUser(int subId);
60 
61     @VisibleForTesting
isTtyOnVolteEnabled(int subId)62     boolean isTtyOnVolteEnabled(int subId) {
63         return (new ImsQueryTtyOnVolteStat(subId)).query();
64     }
65 
66     @VisibleForTesting
isEnabledByPlatform(int subId)67     boolean isEnabledByPlatform(int subId) throws InterruptedException, ImsException,
68             IllegalArgumentException {
69         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
70             return false;
71         }
72 
73         final ImsMmTelManager imsMmTelManager = ImsMmTelManager.createForSubscriptionId(subId);
74         // TODO: have a shared thread pool instead of create ExecutorService
75         //       everytime to improve performance.
76         final ExecutorService executor = Executors.newSingleThreadExecutor();
77         final BooleanConsumer booleanResult = new BooleanConsumer();
78         imsMmTelManager.isSupported(mCapability, mTransportType, executor, booleanResult);
79         // get() will be blocked until end of execution(isSupported()) within thread(executor)
80         // or timeout after TIMEOUT_MILLIS milliseconds
81         return booleanResult.get(TIMEOUT_MILLIS);
82     }
83 
84     @VisibleForTesting
isProvisionedOnDevice(int subId)85     boolean isProvisionedOnDevice(int subId) {
86         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
87             return false;
88         }
89         return (new ImsQueryProvisioningStat(subId, mCapability, mTech)).query();
90     }
91 
92     @VisibleForTesting
isServiceStateReady(int subId)93     boolean isServiceStateReady(int subId) throws InterruptedException, ImsException,
94             IllegalArgumentException {
95         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
96             return false;
97         }
98 
99         final ImsMmTelManager imsMmTelManager = ImsMmTelManager.createForSubscriptionId(subId);
100         // TODO: have a shared thread pool instead of create ExecutorService
101         //       everytime to improve performance.
102         final ExecutorService executor = Executors.newSingleThreadExecutor();
103         final IntegerConsumer intResult = new IntegerConsumer();
104         imsMmTelManager.getFeatureState(executor, intResult);
105         return (intResult.get(TIMEOUT_MILLIS) == ImsFeature.STATE_READY);
106     }
107 }
108