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