blob: 6e84ff03d0f960d10e09bd42e41b76af869604ff [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
23import android.annotation.NonNull;
Felipe Lemebe002d82019-01-23 10:22:32 -080024import android.annotation.Nullable;
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080025import android.os.Build;
26import android.provider.DeviceConfig;
27import android.util.Log;
28import android.view.contentcapture.ContentCaptureManager.LoggingLevel;
Felipe Lemebe002d82019-01-23 10:22:32 -080029
30/**
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080031 * Helper class for this package and server's.
32 *
33 * @hide
Felipe Lemebe002d82019-01-23 10:22:32 -080034 */
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080035public final class ContentCaptureHelper {
Felipe Lemebe002d82019-01-23 10:22:32 -080036
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080037 private static final String TAG = ContentCaptureHelper.class.getSimpleName();
38
39 public static boolean sVerbose = false;
40 public static boolean sDebug = true;
Felipe Lemebe002d82019-01-23 10:22:32 -080041
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 Lemed32d8f6f2019-02-15 10:25:33 -080050 /**
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 Leme326f15a2019-02-19 09:42:24 -080066 * 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 Lemed32d8f6f2019-02-15 10:25:33 -080074 * Sets the value of the static logging level constants based on device config.
75 */
76 public static void setLoggingLevel() {
Felipe Leme326f15a2019-02-19 09:42:24 -080077 final int defaultLevel = getDefaultLoggingLevel();
Felipe Lemed32d8f6f2019-02-15 10:25:33 -080078 final int level = getIntDeviceConfigProperty(DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL,
79 defaultLevel);
Felipe Leme326f15a2019-02-19 09:42:24 -080080 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 Lemed32d8f6f2019-02-15 10:25:33 -080087 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 Lemebe002d82019-01-23 10:22:32 -0800120 private ContentCaptureHelper() {
121 throw new UnsupportedOperationException("contains only static methods");
122 }
123}