Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 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 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 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.NonNull; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 25 | import android.os.Build; |
| 26 | import android.provider.DeviceConfig; |
| 27 | import android.util.Log; |
| 28 | import android.view.contentcapture.ContentCaptureManager.LoggingLevel; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 31 | * Helper class for this package and server's. |
| 32 | * |
| 33 | * @hide |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 34 | */ |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 35 | public final class ContentCaptureHelper { |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 36 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 37 | private static final String TAG = ContentCaptureHelper.class.getSimpleName(); |
| 38 | |
| 39 | public static boolean sVerbose = false; |
| 40 | public static boolean sDebug = true; |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Used to log text that could contain PII. |
| 44 | */ |
| 45 | @Nullable |
| 46 | public static String getSanitizedString(@Nullable CharSequence text) { |
| 47 | return text == null ? null : text.length() + "_chars"; |
| 48 | } |
| 49 | |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Gets the value of a device config property from the Content Capture namespace. |
| 52 | */ |
| 53 | public static int getIntDeviceConfigProperty(@NonNull String key, int defaultValue) { |
| 54 | final String value = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_CONTENT_CAPTURE, key); |
| 55 | if (value == null) return defaultValue; |
| 56 | |
| 57 | try { |
| 58 | return Integer.parseInt(value); |
| 59 | } catch (Exception e) { |
| 60 | Log.w(TAG, "error parsing value (" + value + ") of property " + key + ": " + e); |
| 61 | return defaultValue; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 66 | * Gets the default logging level for the device. |
| 67 | */ |
| 68 | @LoggingLevel |
| 69 | public static int getDefaultLoggingLevel() { |
| 70 | return Build.IS_DEBUGGABLE ? LOGGING_LEVEL_DEBUG : LOGGING_LEVEL_OFF; |
| 71 | } |
| 72 | |
| 73 | /** |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 74 | * Sets the value of the static logging level constants based on device config. |
| 75 | */ |
| 76 | public static void setLoggingLevel() { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 77 | final int defaultLevel = getDefaultLoggingLevel(); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 78 | final int level = getIntDeviceConfigProperty(DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL, |
| 79 | defaultLevel); |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 80 | setLoggingLevel(level); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sets the value of the static logging level constants based the given level. |
| 85 | */ |
| 86 | public static void setLoggingLevel(@LoggingLevel int level) { |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 87 | Log.i(TAG, "Setting logging level to " + getLoggingLevelAsString(level)); |
| 88 | sVerbose = sDebug = false; |
| 89 | switch (level) { |
| 90 | case LOGGING_LEVEL_VERBOSE: |
| 91 | sVerbose = true; |
| 92 | // fall through |
| 93 | case LOGGING_LEVEL_DEBUG: |
| 94 | sDebug = true; |
| 95 | return; |
| 96 | case LOGGING_LEVEL_OFF: |
| 97 | // You log nothing, Jon Snow! |
| 98 | return; |
| 99 | default: |
| 100 | Log.w(TAG, "setLoggingLevel(): invalud level: " + level); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Gets a user-friendly value for a content capture logging level. |
| 106 | */ |
| 107 | public static String getLoggingLevelAsString(@LoggingLevel int level) { |
| 108 | switch (level) { |
| 109 | case LOGGING_LEVEL_OFF: |
| 110 | return "OFF"; |
| 111 | case LOGGING_LEVEL_DEBUG: |
| 112 | return "DEBUG"; |
| 113 | case LOGGING_LEVEL_VERBOSE: |
| 114 | return "VERBOSE"; |
| 115 | default: |
| 116 | return "UNKNOWN-" + level; |
| 117 | } |
| 118 | } |
| 119 | |
Felipe Leme | be002d8 | 2019-01-23 10:22:32 -0800 | [diff] [blame] | 120 | private ContentCaptureHelper() { |
| 121 | throw new UnsupportedOperationException("contains only static methods"); |
| 122 | } |
| 123 | } |