1 /*
2  * Copyright (C) 2013 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;
18 
19 import android.net.Uri;
20 import android.telecom.PhoneAccount;
21 import android.telephony.PhoneNumberUtils;
22 import com.android.dialer.common.LogUtil;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25 
26 /** Manages logging for the entire class. */
27 public class Log {
28 
d(String tag, String msg)29   public static void d(String tag, String msg) {
30     LogUtil.d(tag, msg);
31   }
32 
d(Object obj, String msg)33   public static void d(Object obj, String msg) {
34     LogUtil.d(getPrefix(obj), msg);
35   }
36 
d(Object obj, String str1, Object str2)37   public static void d(Object obj, String str1, Object str2) {
38     LogUtil.d(getPrefix(obj), str1 + str2);
39   }
40 
v(Object obj, String msg)41   public static void v(Object obj, String msg) {
42     LogUtil.v(getPrefix(obj), msg);
43   }
44 
v(Object obj, String str1, Object str2)45   public static void v(Object obj, String str1, Object str2) {
46     LogUtil.v(getPrefix(obj), str1 + str2);
47   }
48 
e(String tag, String msg, Exception e)49   public static void e(String tag, String msg, Exception e) {
50     LogUtil.e(tag, msg, e);
51   }
52 
e(String tag, String msg)53   public static void e(String tag, String msg) {
54     LogUtil.e(tag, msg);
55   }
56 
e(Object obj, String msg, Exception e)57   public static void e(Object obj, String msg, Exception e) {
58     LogUtil.e(getPrefix(obj), msg, e);
59   }
60 
e(Object obj, String msg)61   public static void e(Object obj, String msg) {
62     LogUtil.e(getPrefix(obj), msg);
63   }
64 
i(String tag, String msg)65   public static void i(String tag, String msg) {
66     LogUtil.i(tag, msg);
67   }
68 
i(Object obj, String msg)69   public static void i(Object obj, String msg) {
70     LogUtil.i(getPrefix(obj), msg);
71   }
72 
w(Object obj, String msg)73   public static void w(Object obj, String msg) {
74     LogUtil.w(getPrefix(obj), msg);
75   }
76 
piiHandle(Object pii)77   public static String piiHandle(Object pii) {
78     if (pii == null || LogUtil.isVerboseEnabled()) {
79       return String.valueOf(pii);
80     }
81 
82     if (pii instanceof Uri) {
83       Uri uri = (Uri) pii;
84 
85       // All Uri's which are not "tel" go through normal pii() method.
86       if (!PhoneAccount.SCHEME_TEL.equals(uri.getScheme())) {
87         return pii(pii);
88       } else {
89         pii = uri.getSchemeSpecificPart();
90       }
91     }
92 
93     String originalString = String.valueOf(pii);
94     StringBuilder stringBuilder = new StringBuilder(originalString.length());
95     for (char c : originalString.toCharArray()) {
96       if (PhoneNumberUtils.isDialable(c)) {
97         stringBuilder.append('*');
98       } else {
99         stringBuilder.append(c);
100       }
101     }
102     return stringBuilder.toString();
103   }
104 
105   /**
106    * Redact personally identifiable information for production users. If we are running in verbose
107    * mode, return the original string, otherwise return a SHA-1 hash of the input string.
108    */
pii(Object pii)109   public static String pii(Object pii) {
110     if (pii == null || LogUtil.isVerboseEnabled()) {
111       return String.valueOf(pii);
112     }
113     return "[" + secureHash(String.valueOf(pii).getBytes()) + "]";
114   }
115 
secureHash(byte[] input)116   private static String secureHash(byte[] input) {
117     MessageDigest messageDigest;
118     try {
119       messageDigest = MessageDigest.getInstance("SHA-1");
120     } catch (NoSuchAlgorithmException e) {
121       return null;
122     }
123     messageDigest.update(input);
124     byte[] result = messageDigest.digest();
125     return encodeHex(result);
126   }
127 
encodeHex(byte[] bytes)128   private static String encodeHex(byte[] bytes) {
129     StringBuffer hex = new StringBuffer(bytes.length * 2);
130 
131     for (int i = 0; i < bytes.length; i++) {
132       int byteIntValue = bytes[i] & 0xff;
133       if (byteIntValue < 0x10) {
134         hex.append("0");
135       }
136       hex.append(Integer.toString(byteIntValue, 16));
137     }
138 
139     return hex.toString();
140   }
141 
getPrefix(Object obj)142   private static String getPrefix(Object obj) {
143     return (obj == null ? "" : (obj.getClass().getSimpleName()));
144   }
145 }
146