1 /*
2  * Copyright (C) 2017 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 package com.android.dialer.oem;
17 
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.provider.CallLog.Calls;
21 import android.support.annotation.VisibleForTesting;
22 import android.telephony.TelephonyManager;
23 import com.android.dialer.common.LogUtil;
24 import com.android.dialer.common.PackageUtils;
25 import com.android.dialer.configprovider.ConfigProviderComponent;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 
29 /** Util class for Motorola OEM devices. */
30 public class MotorolaUtils {
31 
32   private static final String CONFIG_HD_CODEC_BLINKING_ICON_WHEN_CONNECTING_CALL_ENABLED =
33       "hd_codec_blinking_icon_when_connecting_enabled";
34   private static final String CONFIG_HD_CODEC_SHOW_ICON_IN_NOTIFICATION_ENABLED =
35       "hd_codec_show_icon_in_notification_enabled";
36   private static final String CONFIG_WIFI_CALL_SHOW_ICON_IN_CALL_LOG_ENABLED =
37       "wifi_call_show_icon_in_call_log_enabled";
38 
39   @VisibleForTesting
40   static final String CONFIG_DISABLE_PHONE_NUMBER_FORMATTING = "disable_phone_number_formatting";
41 
42   // This is used to check if a Motorola device supports HD voice call feature, which comes from
43   // system feature setting.
44   private static final String HD_CALL_FEATRURE = "com.motorola.software.sprint.hd_call";
45   // This is used to check if a Motorola device supports WiFi call feature, by checking if a certain
46   // package is enabled.
47   @VisibleForTesting public static final String WIFI_CALL_PACKAGE_NAME = "com.motorola.sprintwfc";
48   // Thi is used to check if a Motorola device supports hidden menu feature.
49   @VisibleForTesting
50   static final String HIDDEN_MENU_FEATURE = "com.motorola.software.sprint.hidden_menu";
51 
52   private static Boolean disablePhoneNumberFormattingForTest = null;
53   private static boolean hasCheckedSprintWifiCall;
54   private static boolean supportSprintWifiCall;
55 
56   /**
57    * Returns true if SPN is specified and matched the current sim operator name. This is necessary
58    * since mcc310-mnc000 is not sufficient to identify Sprint network.
59    */
isSpnMatched(Context context)60   private static boolean isSpnMatched(Context context) {
61     try {
62       for (String spnResource :
63           context.getResources().getStringArray(R.array.motorola_enabled_spn)) {
64         if (spnResource.equalsIgnoreCase(
65             context.getSystemService(TelephonyManager.class).getSimOperatorName())) {
66           return true;
67         }
68       }
69       return false;
70     } catch (Resources.NotFoundException exception) {
71       // If SPN is not specified we consider as not necessary to enable/disable the feature.
72       return true;
73     }
74   }
75 
isSupportingHiddenMenu(Context context)76   static boolean isSupportingHiddenMenu(Context context) {
77     return context.getPackageManager().hasSystemFeature(HIDDEN_MENU_FEATURE)
78         && context.getResources().getBoolean(R.bool.motorola_hidden_menu_enabled);
79   }
80 
shouldBlinkHdIconWhenConnectingCall(Context context)81   public static boolean shouldBlinkHdIconWhenConnectingCall(Context context) {
82     return ConfigProviderComponent.get(context)
83             .getConfigProvider()
84             .getBoolean(CONFIG_HD_CODEC_BLINKING_ICON_WHEN_CONNECTING_CALL_ENABLED, true)
85         && isSupportingSprintHdCodec(context);
86   }
87 
shouldShowHdIconInNotification(Context context)88   public static boolean shouldShowHdIconInNotification(Context context) {
89     return ConfigProviderComponent.get(context)
90             .getConfigProvider()
91             .getBoolean(CONFIG_HD_CODEC_SHOW_ICON_IN_NOTIFICATION_ENABLED, true)
92         && isSupportingSprintHdCodec(context);
93   }
94 
shouldShowWifiIconInCallLog(Context context, int features)95   public static boolean shouldShowWifiIconInCallLog(Context context, int features) {
96     return ConfigProviderComponent.get(context)
97             .getConfigProvider()
98             .getBoolean(CONFIG_WIFI_CALL_SHOW_ICON_IN_CALL_LOG_ENABLED, true)
99         && (features & Calls.FEATURES_WIFI) == Calls.FEATURES_WIFI
100         && isSupportingSprintWifiCall(context);
101   }
102 
shouldDisablePhoneNumberFormatting(Context context)103   public static boolean shouldDisablePhoneNumberFormatting(Context context) {
104     if (disablePhoneNumberFormattingForTest != null) {
105       return disablePhoneNumberFormattingForTest;
106     }
107 
108     return ConfigProviderComponent.get(context)
109             .getConfigProvider()
110             .getBoolean(CONFIG_DISABLE_PHONE_NUMBER_FORMATTING, true)
111         && context.getResources().getBoolean(R.bool.motorola_disable_phone_number_formatting);
112   }
113 
114   /**
115    * Handle special char sequence entered in dialpad. This may launch special intent based on input.
116    *
117    * @param context context
118    * @param input input string
119    * @return true if the input is consumed and the intent is launched
120    */
handleSpecialCharSequence(Context context, String input)121   public static boolean handleSpecialCharSequence(Context context, String input) {
122     // TODO(a bug): Add check for Motorola devices.
123     return MotorolaHiddenMenuKeySequence.handleCharSequence(context, input);
124   }
125 
isWifiCallingAvailable(Context context)126   public static boolean isWifiCallingAvailable(Context context) {
127     if (!isSupportingSprintWifiCall(context)) {
128       return false;
129     }
130     TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
131     try {
132       Method method = TelephonyManager.class.getMethod("isWifiCallingAvailable");
133       boolean isWifiCallingAvailable = (boolean) method.invoke(telephonyManager);
134       LogUtil.d("MotorolaUtils.isWifiCallingAvailable", "%b", isWifiCallingAvailable);
135       return isWifiCallingAvailable;
136     } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
137       LogUtil.e("MotorolaUtils.isWifiCallingAvailable", "", e);
138     }
139     return false;
140   }
141 
isSupportingSprintHdCodec(Context context)142   private static boolean isSupportingSprintHdCodec(Context context) {
143     return isSpnMatched(context)
144         && context.getResources().getBoolean(R.bool.motorola_sprint_hd_codec)
145         && context.getPackageManager().hasSystemFeature(HD_CALL_FEATRURE);
146   }
147 
isSupportingSprintWifiCall(Context context)148   private static boolean isSupportingSprintWifiCall(Context context) {
149     if (!hasCheckedSprintWifiCall) {
150       supportSprintWifiCall = PackageUtils.isPackageEnabled(WIFI_CALL_PACKAGE_NAME, context);
151       hasCheckedSprintWifiCall = true;
152     }
153     return supportSprintWifiCall;
154   }
155 
156   @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setDisablePhoneNumberFormattingForTest(boolean disablePhoneNumberFormatting)157   public static void setDisablePhoneNumberFormattingForTest(boolean disablePhoneNumberFormatting) {
158     disablePhoneNumberFormattingForTest = disablePhoneNumberFormatting;
159   }
160 
161   @VisibleForTesting
resetForTest()162   public static void resetForTest() {
163     hasCheckedSprintWifiCall = false;
164     supportSprintWifiCall = false;
165   }
166 }
167