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 package android.view.contentcapture; 17 18 import static android.view.contentcapture.ContentCaptureManager.DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL; 19 import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_DEBUG; 20 import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_OFF; 21 import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_VERBOSE; 22 23 import android.annotation.Nullable; 24 import android.os.Build; 25 import android.provider.DeviceConfig; 26 import android.util.ArraySet; 27 import android.util.Log; 28 import android.view.contentcapture.ContentCaptureManager.LoggingLevel; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Set; 33 34 /** 35 * Helper class for this package and server's. 36 * 37 * @hide 38 */ 39 public final class ContentCaptureHelper { 40 41 private static final String TAG = ContentCaptureHelper.class.getSimpleName(); 42 43 public static boolean sVerbose = false; 44 public static boolean sDebug = true; 45 46 /** 47 * Used to log text that could contain PII. 48 */ 49 @Nullable getSanitizedString(@ullable CharSequence text)50 public static String getSanitizedString(@Nullable CharSequence text) { 51 return text == null ? null : text.length() + "_chars"; 52 } 53 54 /** 55 * Gets the default logging level for the device. 56 */ 57 @LoggingLevel getDefaultLoggingLevel()58 public static int getDefaultLoggingLevel() { 59 return Build.IS_DEBUGGABLE ? LOGGING_LEVEL_DEBUG : LOGGING_LEVEL_OFF; 60 } 61 62 /** 63 * Sets the value of the static logging level constants based on device config. 64 */ setLoggingLevel()65 public static void setLoggingLevel() { 66 final int defaultLevel = getDefaultLoggingLevel(); 67 final int level = DeviceConfig.getInt(DeviceConfig.NAMESPACE_CONTENT_CAPTURE, 68 DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL, defaultLevel); 69 setLoggingLevel(level); 70 } 71 72 /** 73 * Sets the value of the static logging level constants based the given level. 74 */ setLoggingLevel(@oggingLevel int level)75 public static void setLoggingLevel(@LoggingLevel int level) { 76 Log.i(TAG, "Setting logging level to " + getLoggingLevelAsString(level)); 77 sVerbose = sDebug = false; 78 switch (level) { 79 case LOGGING_LEVEL_VERBOSE: 80 sVerbose = true; 81 // fall through 82 case LOGGING_LEVEL_DEBUG: 83 sDebug = true; 84 return; 85 case LOGGING_LEVEL_OFF: 86 // You log nothing, Jon Snow! 87 return; 88 default: 89 Log.w(TAG, "setLoggingLevel(): invalud level: " + level); 90 } 91 } 92 93 /** 94 * Gets a user-friendly value for a content capture logging level. 95 */ getLoggingLevelAsString(@oggingLevel int level)96 public static String getLoggingLevelAsString(@LoggingLevel int level) { 97 switch (level) { 98 case LOGGING_LEVEL_OFF: 99 return "OFF"; 100 case LOGGING_LEVEL_DEBUG: 101 return "DEBUG"; 102 case LOGGING_LEVEL_VERBOSE: 103 return "VERBOSE"; 104 default: 105 return "UNKNOWN-" + level; 106 } 107 } 108 109 /** 110 * Converts a set to a list. 111 */ 112 @Nullable toList(@ullable Set<T> set)113 public static <T> ArrayList<T> toList(@Nullable Set<T> set) { 114 return set == null ? null : new ArrayList<T>(set); 115 } 116 117 /** 118 * Converts a list to a set. 119 */ 120 @Nullable toSet(@ullable List<T> list)121 public static <T> ArraySet<T> toSet(@Nullable List<T> list) { 122 return list == null ? null : new ArraySet<T>(list); 123 } 124 ContentCaptureHelper()125 private ContentCaptureHelper() { 126 throw new UnsupportedOperationException("contains only static methods"); 127 } 128 } 129