1 /*
2  * Copyright (C) 2016 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.incallui.incall.impl;
18 
19 import android.support.v4.util.ArrayMap;
20 import android.telephony.TelephonyManager;
21 import com.android.incallui.incall.impl.MappedButtonConfig.MappingInfo;
22 import com.android.incallui.incall.protocol.InCallButtonIds;
23 import java.util.Map;
24 
25 /**
26  * Creates {@link ButtonChooser} objects, based on the current network and phone type.
27  */
28 class ButtonChooserFactory {
29 
30   /**
31    * Creates the appropriate {@link ButtonChooser} based on the given information.
32    *
33    * @param voiceNetworkType the result of a call to {@link TelephonyManager#getVoiceNetworkType()}.
34    * @param isWiFi {@code true} if the call is made over WiFi, {@code false} otherwise.
35    * @param phoneType the result of a call to {@link TelephonyManager#getPhoneType()}.
36    * @return the ButtonChooser.
37    */
newButtonChooser( int voiceNetworkType, boolean isWiFi, int phoneType)38   public static ButtonChooser newButtonChooser(
39       int voiceNetworkType, boolean isWiFi, int phoneType) {
40     if (voiceNetworkType == TelephonyManager.NETWORK_TYPE_LTE || isWiFi) {
41       return newImsAndWiFiButtonChooser();
42     }
43 
44     if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) {
45       return newCdmaButtonChooser();
46     }
47 
48     if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {
49       return newGsmButtonChooser();
50     }
51 
52     return newImsAndWiFiButtonChooser();
53   }
54 
newImsAndWiFiButtonChooser()55   private static ButtonChooser newImsAndWiFiButtonChooser() {
56     Map<Integer, MappingInfo> mapping = createCommonMapping();
57     mapping.put(
58         InCallButtonIds.BUTTON_MANAGE_VOICE_CONFERENCE,
59         MappingInfo.builder(4).setSlotOrder(0).build());
60     // RTT call is only supported on IMS and WiFi.
61     mapping.put(
62         InCallButtonIds.BUTTON_UPGRADE_TO_RTT, MappingInfo.builder(3).setSlotOrder(0).build());
63     mapping.put(
64         InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, MappingInfo.builder(4).setSlotOrder(10).build());
65     mapping.put(
66         InCallButtonIds.BUTTON_SWITCH_TO_SECONDARY, MappingInfo.builder(5).setSlotOrder(0).build());
67     mapping.put(InCallButtonIds.BUTTON_HOLD, MappingInfo.builder(5).setSlotOrder(10).build());
68 
69     return new ButtonChooser(new MappedButtonConfig(mapping));
70   }
71 
newCdmaButtonChooser()72   private static ButtonChooser newCdmaButtonChooser() {
73     Map<Integer, MappingInfo> mapping = createCommonMapping();
74     mapping.put(
75         InCallButtonIds.BUTTON_MANAGE_VOICE_CONFERENCE,
76         MappingInfo.builder(4).setSlotOrder(0).build());
77     mapping.put(
78         InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, MappingInfo.builder(4).setSlotOrder(10).build());
79     mapping.put(InCallButtonIds.BUTTON_SWAP, MappingInfo.builder(5).setSlotOrder(0).build());
80     // For multi-sim devices, the first sim's phoneType is used so hold button might be not
81     // available for CDMA + GSM devices calling with GSM sim. Adding hold button as low priority
82     // here to let telecom control whether it should be shown.
83     mapping.put(InCallButtonIds.BUTTON_HOLD, MappingInfo.builder(5).setSlotOrder(5).build());
84     mapping.put(
85         InCallButtonIds.BUTTON_SWITCH_TO_SECONDARY,
86         MappingInfo.builder(5)
87             .setSlotOrder(Integer.MAX_VALUE)
88             .setMutuallyExclusiveButton(InCallButtonIds.BUTTON_SWAP)
89             .build());
90 
91     return new ButtonChooser(new MappedButtonConfig(mapping));
92   }
93 
newGsmButtonChooser()94   private static ButtonChooser newGsmButtonChooser() {
95     Map<Integer, MappingInfo> mapping = createCommonMapping();
96     mapping.put(
97         InCallButtonIds.BUTTON_SWITCH_TO_SECONDARY, MappingInfo.builder(4).setSlotOrder(0).build());
98     mapping.put(
99         InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, MappingInfo.builder(4).setSlotOrder(10).build());
100 
101     /*
102      * Unlike the other configurations, MANAGE_VOICE_CONFERENCE shares a spot with HOLD for GSM.
103      * On GSM, pressing hold while there's a background call just swaps to the background call. It
104      * doesn't make sense to show both SWITCH_TO_SECONDARY and HOLD when they do the same thing, so
105      * we show MANAGE_VOICE_CONFERENCE instead. Previously MANAGE_VOICE_CONFERENCE would not show.
106      */
107     mapping.put(
108         InCallButtonIds.BUTTON_MANAGE_VOICE_CONFERENCE,
109         MappingInfo.builder(5).setSlotOrder(0).build());
110     mapping.put(InCallButtonIds.BUTTON_HOLD, MappingInfo.builder(5).setSlotOrder(5).build());
111 
112     return new ButtonChooser(new MappedButtonConfig(mapping));
113   }
114 
createCommonMapping()115   private static Map<Integer, MappingInfo> createCommonMapping() {
116     Map<Integer, MappingInfo> mapping = new ArrayMap<>();
117     mapping.put(InCallButtonIds.BUTTON_MUTE, MappingInfo.builder(0).build());
118     mapping.put(InCallButtonIds.BUTTON_DIALPAD, MappingInfo.builder(1).build());
119     mapping.put(InCallButtonIds.BUTTON_AUDIO, MappingInfo.builder(2).build());
120     mapping.put(InCallButtonIds.BUTTON_MERGE, MappingInfo.builder(3).setSlotOrder(5).build());
121     mapping.put(InCallButtonIds.BUTTON_ADD_CALL, MappingInfo.builder(3).build());
122     mapping.put(InCallButtonIds.BUTTON_SWAP_SIM, MappingInfo.builder(4).build());
123     return mapping;
124   }
125 }
126