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