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