1 /*
2  * Copyright (C) 2019 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.googlecode.android_scripting.facade.telephony;
18 
19 import android.telephony.AccessNetworkConstants;
20 import android.telephony.ims.ImsException;
21 import android.telephony.ims.ImsMmTelManager;
22 import android.telephony.ims.feature.MmTelFeature;
23 
24 import com.googlecode.android_scripting.Log;
25 import com.googlecode.android_scripting.facade.FacadeManager;
26 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
27 import com.googlecode.android_scripting.rpc.Rpc;
28 import com.googlecode.android_scripting.rpc.RpcParameter;
29 
30 import java.util.concurrent.Executor;
31 import java.util.concurrent.LinkedBlockingQueue;
32 
33 /**
34  * Exposes ImsMmManager functionality
35  */
36 public class ImsMmTelManagerFacade extends RpcReceiver {
37 
38     /**
39      * Exposes ImsMmTelManager functionality
40      */
ImsMmTelManagerFacade(FacadeManager manager)41     public ImsMmTelManagerFacade(FacadeManager manager) {
42         super(manager);
43     }
44 
45     /**
46      * Get whether Advanced Calling is enabled for a subId
47      *
48      * @param subId The subscription ID of the sim you want to check
49      */
50     @Rpc(description = "Return True if Enhanced 4g Lte mode is enabled.")
imsMmTelIsAdvancedCallingEnabled( @pcParametername = "subId") Integer subId)51     public boolean imsMmTelIsAdvancedCallingEnabled(
52                         @RpcParameter(name = "subId") Integer subId) {
53         return ImsMmTelManager.createForSubscriptionId(subId).isAdvancedCallingSettingEnabled();
54     }
55 
56     /**
57      * Set Advanced Calling for a subId
58      *
59      * @param subId The subscription ID of the sim you want to check
60      * @param isEnabled Whether the sim should have Enhanced 4g Lte on or off
61      */
62     @Rpc(description = "Set Enhanced 4g Lte mode")
imsMmTelSetAdvancedCallingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)63     public void imsMmTelSetAdvancedCallingEnabled(
64                         @RpcParameter(name = "subId") Integer subId,
65                         @RpcParameter(name = "isEnabled") Boolean isEnabled) {
66         ImsMmTelManager.createForSubscriptionId(subId).setAdvancedCallingSettingEnabled(isEnabled);
67     }
68 
69     /**
70      * Get whether VoWiFi Roaming setting is enabled for a subId
71      *
72      * @param subId The subscription ID of the sim you want to check
73      */
74     @Rpc(description = "Return True if VoWiFi Roaming is enabled.")
imsMmTelIsVoWiFiRoamingSettingEnabled( @pcParametername = "subId") Integer subId)75     public boolean imsMmTelIsVoWiFiRoamingSettingEnabled(
76                         @RpcParameter(name = "subId") Integer subId) {
77         return ImsMmTelManager.createForSubscriptionId(subId).isVoWiFiRoamingSettingEnabled();
78     }
79 
80     /**
81      * Set VoWiFi Roaming setting for a subId
82      *
83      * @param subId The subscription ID of the sim you want to check
84      * @param isEnabled Whether the sim should have VoWiFi Roaming on or off
85      */
86     @Rpc(description = "Set VoWiFi Roaming setting")
imsMmTelSetVoWiFiRoamingSettingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)87     public void imsMmTelSetVoWiFiRoamingSettingEnabled(
88                         @RpcParameter(name = "subId") Integer subId,
89                         @RpcParameter(name = "isEnabled") Boolean isEnabled) {
90         ImsMmTelManager.createForSubscriptionId(subId).setVoWiFiRoamingSettingEnabled(isEnabled);
91     }
92 
93     /**
94      * Get whether VoWiFi setting is enabled for a subId
95      *
96      * @param subId The subscription ID of the sim you want to check
97      */
98     @Rpc(description = "Return True if VoWiFi is enabled.")
imsMmTelIsVoWiFiSettingEnabled(@pcParametername = "subId") Integer subId)99     public boolean imsMmTelIsVoWiFiSettingEnabled(@RpcParameter(name = "subId") Integer subId) {
100         return ImsMmTelManager.createForSubscriptionId(subId).isVoWiFiSettingEnabled();
101     }
102 
103     /**
104      * Set VoWiFi setting for a subId
105      *
106      * @param subId The subscription ID of the sim you want to check
107      * @param isEnabled Whether the sim should have VoWiFi on or off
108      */
109     @Rpc(description = "Set VoWiFi setting")
imsMmTelSetVoWiFiSettingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)110     public void imsMmTelSetVoWiFiSettingEnabled(
111                         @RpcParameter(name = "subId") Integer subId,
112                         @RpcParameter(name = "isEnabled") Boolean isEnabled) {
113         ImsMmTelManager.createForSubscriptionId(subId).setVoWiFiSettingEnabled(isEnabled);
114     }
115 
116     /**
117      * Get whether Voice over Cross Sim is enabled for a subId
118      *
119      * @param subId The subscription ID of the sim you want to check
120      */
121     @Rpc(description = "Return True if Cross Sim Calling is enabled.")
imsMmTelIsCrossSimCallingEnabled( @pcParametername = "subId") Integer subId)122     public boolean imsMmTelIsCrossSimCallingEnabled(
123                         @RpcParameter(name = "subId") Integer subId) {
124         try {
125             return ImsMmTelManager.createForSubscriptionId(subId).isCrossSimCallingEnabled();
126         } catch (ImsException e) {
127             Log.d("ImsException " + e);
128             return false;
129         }
130     }
131 
132     /**
133      * Set Voice over Cross Sim Calling for a subId
134      *
135      * @param subId The subscription ID of the sim you want to check
136      * @param isEnabled Whether the sim should have Cross Sim Calling on or off
137      */
138     @Rpc(description = "Set Voice over Cross Sim Calling setting")
imsMmTelSetCrossSimCallingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)139     public void imsMmTelSetCrossSimCallingEnabled(
140                         @RpcParameter(name = "subId") Integer subId,
141                         @RpcParameter(name = "isEnabled") Boolean isEnabled) {
142         try {
143             ImsMmTelManager.createForSubscriptionId(subId)
144                 .setCrossSimCallingEnabled(isEnabled);
145         } catch (ImsException e) {
146             Log.d("ImsException " + e);
147         }
148     }
149 
150     /**
151      * Get whether Video Telephony setting is enabled for a subId
152      *
153      * @param subId The subscription ID of the sim you want to check
154      */
155     @Rpc(description = "Return True if VT is enabled.")
imsMmTelIsVtSettingEnabled( @pcParametername = "subId") Integer subId)156     public boolean imsMmTelIsVtSettingEnabled(
157                         @RpcParameter(name = "subId") Integer subId) {
158         return ImsMmTelManager.createForSubscriptionId(subId).isVtSettingEnabled();
159     }
160 
161     /**
162      * Set Video Telephony setting for a subId
163      *
164      * @param subId The subscription ID of the sim you want to check
165      * @param isEnabled Whether the sim should have VT on or off
166      */
167     @Rpc(description = "Set VT setting")
imsMmTelSetVtSettingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)168     public void imsMmTelSetVtSettingEnabled(
169                         @RpcParameter(name = "subId") Integer subId,
170                         @RpcParameter(name = "isEnabled") Boolean isEnabled) {
171         ImsMmTelManager.createForSubscriptionId(subId).setVtSettingEnabled(isEnabled);
172     }
173 
174     /**
175      * Get current VoWiFi Mode Pref for a subId
176      *
177      * @param subId The subscription ID of the sim you want to check
178      */
179     @Rpc(description = "Return Preferred WFC Mode if Enabled.")
imsMmTelGetVoWiFiModeSetting( @pcParametername = "subId") Integer subId)180     public String imsMmTelGetVoWiFiModeSetting(
181                         @RpcParameter(name = "subId") Integer subId) {
182         return TelephonyUtils.getWfcModeString(
183             ImsMmTelManager.createForSubscriptionId(subId).getVoWiFiModeSetting());
184     }
185 
186     /**
187      * Set VoWiFi Mode Pref for a subId
188      *
189      * @param subId The subscription ID of the sim you want to check
190      * @mode mode pref can be one of the following
191      * - WIFI_ONLY
192      * - WIFI_PREFERRED
193      * - CELLULAR_PREFERRED
194      */
195     @Rpc(description = "Set the Preferred WFC Mode")
imsMmTelSetVoWiFiModeSetting( @pcParametername = "subId") Integer subId, @RpcParameter(name = "mode") String mode)196     public void imsMmTelSetVoWiFiModeSetting(
197                         @RpcParameter(name = "subId") Integer subId,
198                         @RpcParameter(name = "mode") String mode)
199             throws IllegalArgumentException {
200 
201         int mode_val;
202 
203         switch (mode.toUpperCase()) {
204             case TelephonyConstants.WFC_MODE_WIFI_ONLY:
205                 mode_val = ImsMmTelManager.WIFI_MODE_WIFI_ONLY;
206                 break;
207             case TelephonyConstants.WFC_MODE_CELLULAR_PREFERRED:
208                 mode_val = ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED;
209                 break;
210             case TelephonyConstants.WFC_MODE_WIFI_PREFERRED:
211                 mode_val = ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED;
212                 break;
213             default:
214                 throw new IllegalArgumentException("Invalid WfcMode");
215         }
216 
217         ImsMmTelManager.createForSubscriptionId(subId).setVoWiFiModeSetting(mode_val);
218         return;
219     }
220 
221     /**
222      * Check MmTel capability is supported by the carrier
223      *
224      * @param subId The subscription ID of the sim you want to check
225      * @param capability includes voice, video, sms
226      * @param transportType includes wlan, wwan
227      */
228     @Rpc(description = "Return Preferred WFC Mode if Enabled.")
imsMmTelIsSupported( @pcParametername = "subId") Integer subId, @RpcParameter(name = "capability") String capability, @RpcParameter(name = "transportType") String transportType)229     public Boolean imsMmTelIsSupported(
230                         @RpcParameter(name = "subId") Integer subId,
231                         @RpcParameter(name = "capability") String capability,
232                         @RpcParameter(name = "transportType") String transportType)
233             throws IllegalArgumentException {
234 
235         int capability_val;
236         int transport_val;
237 
238         LinkedBlockingQueue<Boolean> resultQueue = new LinkedBlockingQueue<>(1);
239 
240         Executor executor = new Executor() {
241             public void execute(Runnable r) {
242                 Log.d("Running MmTel Executor");
243                 r.run();
244                 }
245             };
246 
247         switch (capability.toUpperCase()) {
248             case TelephonyConstants.CAPABILITY_TYPE_VOICE:
249                 capability_val = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE;
250                 break;
251             case TelephonyConstants.CAPABILITY_TYPE_VIDEO:
252                 capability_val = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO;
253                 break;
254             case TelephonyConstants.CAPABILITY_TYPE_UT:
255                 capability_val = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT;
256                 break;
257             case TelephonyConstants.CAPABILITY_TYPE_SMS:
258                 capability_val = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_SMS;
259                 break;
260             default:
261                 throw new IllegalArgumentException("Invalid Capability");
262         }
263 
264         switch (transportType.toUpperCase()) {
265             case TelephonyConstants.TRANSPORT_TYPE_INVALID:
266                 transport_val = AccessNetworkConstants.TRANSPORT_TYPE_INVALID;
267                 break;
268             case TelephonyConstants.TRANSPORT_TYPE_WWAN:
269                 transport_val = AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
270                 break;
271             case TelephonyConstants.TRANSPORT_TYPE_WLAN:
272                 transport_val = AccessNetworkConstants.TRANSPORT_TYPE_WLAN;
273                 break;
274             default:
275                 throw new IllegalArgumentException("Invalid transportType");
276         }
277 
278         try {
279             ImsMmTelManager.createForSubscriptionId(subId)
280                 .isSupported(capability_val, transport_val, executor, resultQueue::offer);
281         } catch (ImsException e) {
282             Log.d("ImsException " + e);
283             return false;
284         }
285         return true;
286     }
287 
288     @Override
shutdown()289     public void shutdown() {
290 
291     }
292 }
293