blob: 6bc38290745775f3e0897f941b8c62a768471e1c [file] [log] [blame]
Felipe Lemebe002d82019-01-23 10:22:32 -08001/*
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 */
16package android.view.contentcapture;
17
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080018import static android.view.contentcapture.ContentCaptureManager.DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL;
19import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_DEBUG;
20import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_OFF;
21import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_VERBOSE;
22
Felipe Lemebe002d82019-01-23 10:22:32 -080023import android.annotation.Nullable;
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080024import android.os.Build;
25import android.provider.DeviceConfig;
26import android.util.Log;
27import android.view.contentcapture.ContentCaptureManager.LoggingLevel;
Felipe Lemebe002d82019-01-23 10:22:32 -080028
29/**
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080030 * Helper class for this package and server's.
31 *
32 * @hide
Felipe Lemebe002d82019-01-23 10:22:32 -080033 */
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080034public final class ContentCaptureHelper {
Felipe Lemebe002d82019-01-23 10:22:32 -080035
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080036 private static final String TAG = ContentCaptureHelper.class.getSimpleName();
37
38 public static boolean sVerbose = false;
39 public static boolean sDebug = true;
Felipe Lemebe002d82019-01-23 10:22:32 -080040
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 Lemed32d8f6f2019-02-15 10:25:33 -080049 /**
Felipe Leme326f15a2019-02-19 09:42:24 -080050 * 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 Lemed32d8f6f2019-02-15 10:25:33 -080058 * Sets the value of the static logging level constants based on device config.
59 */
60 public static void setLoggingLevel() {
Felipe Leme326f15a2019-02-19 09:42:24 -080061 final int defaultLevel = getDefaultLoggingLevel();
Stanislav Zholnina5343682019-03-07 16:43:01 +000062 final int level = DeviceConfig.getInt(DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
63 DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL, defaultLevel);
Felipe Leme326f15a2019-02-19 09:42:24 -080064 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 Lemed32d8f6f2019-02-15 10:25:33 -080071 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 Lemebe002d82019-01-23 10:22:32 -0800104 private ContentCaptureHelper() {
105 throw new UnsupportedOperationException("contains only static methods");
106 }
107}