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 Wifi calling status
32  */
33 public class WifiCallingQueryImsState extends ImsQueryController  {
34 
35     private static final String LOG_TAG = "WifiCallingQueryImsState";
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      */
WifiCallingQueryImsState(Context context, int subId)46     public WifiCallingQueryImsState(Context context, int subId) {
47         super(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
48                 ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN,
49                 AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
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 ImsQueryWfcUserSetting(subId)).query();
63     }
64 
65     /**
66      * Check whether Wifi Calling is a supported feature on this subscription
67      *
68      * @return true when Wifi Calling is a supported feature, otherwise false
69      */
isWifiCallingSupported()70     public boolean isWifiCallingSupported() {
71         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
72             return false;
73         }
74         try {
75             return isEnabledByPlatform(mSubId);
76         } catch (InterruptedException | IllegalArgumentException | ImsException exception) {
77             Log.w(LOG_TAG, "fail to get WFC supporting status. subId=" + mSubId, exception);
78         }
79         return false;
80     }
81 
82     /**
83      * Check whether Wifi Calling has been provisioned or not on this subscription
84      *
85      * @return true when Wifi Calling has been enabled, otherwise false
86      */
isWifiCallingProvisioned()87     public boolean isWifiCallingProvisioned() {
88         return isWifiCallingSupported() && isProvisionedOnDevice(mSubId);
89     }
90 
91     /**
92      * Check whether Wifi Calling can be perform or not on this subscription
93      *
94      * @return true when Wifi Calling can be performed, otherwise false
95      */
isReadyToWifiCalling()96     public boolean isReadyToWifiCalling() {
97         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
98             return false;
99         }
100         if (!isWifiCallingProvisioned()) {
101             return false;
102         }
103         try {
104             return isServiceStateReady(mSubId);
105         } catch (InterruptedException | IllegalArgumentException | ImsException exception) {
106             Log.w(LOG_TAG, "fail to get WFC service status. subId=" + mSubId, exception);
107         }
108         return false;
109     }
110 
111     /**
112      * Get allowance status for user to alter configuration
113      *
114      * @return true when changing configuration by user is allowed.
115      */
isAllowUserControl()116     public boolean isAllowUserControl() {
117         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
118             return false;
119         }
120 
121         return ((!isTtyEnabled(mContext))
122                 || (isTtyOnVolteEnabled(mSubId)));
123     }
124 
125     @VisibleForTesting
isTtyEnabled(Context context)126     boolean isTtyEnabled(Context context) {
127         final TelecomManager telecomManager = context.getSystemService(TelecomManager.class);
128         return (telecomManager.getCurrentTtyMode() != TelecomManager.TTY_MODE_OFF);
129     }
130 
131     /**
132      * Get user's configuration
133      *
134      * @return true when user's configuration is ON otherwise false.
135      */
isEnabledByUser()136     public boolean isEnabledByUser() {
137         if (!SubscriptionManager.isValidSubscriptionId(mSubId)) {
138             return false;
139         }
140         return isEnabledByUser(mSubId);
141     }
142 }
143