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.content.Context;
20 import android.telecom.TelecomManager;
21 import android.telephony.AccessNetworkConstants;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.ims.ImsException;
24 import android.telephony.ims.feature.MmTelFeature;
25 import android.telephony.ims.stub.ImsRegistrationImplBase;
26 import android.util.Log;
27 
28 import androidx.annotation.VisibleForTesting;
29 
30 /**
31  * Controller class for querying Volte status
32  */
33 public class VolteQueryImsState extends ImsQueryController {
34 
35     private static final String LOG_TAG = "VolteQueryImsState";
36 
37     private Context mContext;
38     private int mSubId;
39 
40     /**
41      * Constructor
42      *
43      * @param context {@link Context}
44      * @param subId subscription's id
45      */
VolteQueryImsState(Context context, int subId)46     public VolteQueryImsState(Context context, int subId) {
47         super(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
48                 ImsRegistrationImplBase.REGISTRATION_TECH_LTE,
49                 AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
50         mContext = context;
51         mSubId = subId;
52     }
53 
54     /**
55      * Implementation of ImsQueryController#isEnabledByUser(int subId)
56      */
57     @VisibleForTesting
isEnabledByUser(int subId)58     boolean isEnabledByUser(int subId) {
59         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
60             return false;
61         }
62         return (new ImsQueryEnhanced4gLteModeUserSetting(subId)).query();
63     }
64 
65     /**
66      * Check whether VoLTE has been provisioned or not on this subscription
67      *
68      * @return true when VoLTE has been enabled, otherwise false
69      */
isVoLteProvisioned()70     public boolean isVoLteProvisioned() {
71         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
72             return false;
73         }
74         if (!isProvisionedOnDevice(mSubId)) {
75             return false;
76         }
77         try {
78             return isEnabledByPlatform(mSubId);
79         } catch (InterruptedException | IllegalArgumentException | ImsException exception) {
80             Log.w(LOG_TAG, "fail to get VoLte supporting status. subId=" + mSubId, exception);
81         }
82         return false;
83     }
84 
85     /**
86      * Check whether VoLTE can be perform or not on this subscription
87      *
88      * @return true when VoLTE can be performed, otherwise false
89      */
isReadyToVoLte()90     public boolean isReadyToVoLte() {
91         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
92             return false;
93         }
94         if (!isVoLteProvisioned()) {
95             return false;
96         }
97         try {
98             return isServiceStateReady(mSubId);
99         } catch (InterruptedException | IllegalArgumentException | ImsException exception) {
100             Log.w(LOG_TAG, "fail to get VoLte service status. subId=" + mSubId, exception);
101         }
102         return false;
103     }
104 
105     /**
106      * Get allowance status for user to alter configuration
107      *
108      * @return true when changing configuration by user is allowed.
109      */
isAllowUserControl()110     public boolean isAllowUserControl() {
111         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
112             return false;
113         }
114 
115         return ((!isTtyEnabled(mContext))
116                 || (isTtyOnVolteEnabled(mSubId)));
117     }
118 
119     @VisibleForTesting
isTtyEnabled(Context context)120     boolean isTtyEnabled(Context context) {
121         final TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
122         return (telecomManager.getCurrentTtyMode() != TelecomManager.TTY_MODE_OFF);
123     }
124 
125     /**
126      * Get user's configuration
127      *
128      * @return true when user's configuration is ON otherwise false.
129      */
isEnabledByUser()130     public boolean isEnabledByUser() {
131         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
132             return false;
133         }
134         return isEnabledByUser(mSubId);
135     }
136 }
137