File: ContentCaptureHelper.java

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (128 lines) | stat: -rw-r--r-- 4,155 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.view.contentcapture;

import static android.view.contentcapture.ContentCaptureManager.DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL;
import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_DEBUG;
import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_OFF;
import static android.view.contentcapture.ContentCaptureManager.LOGGING_LEVEL_VERBOSE;

import android.annotation.Nullable;
import android.os.Build;
import android.provider.DeviceConfig;
import android.util.ArraySet;
import android.util.Log;
import android.view.contentcapture.ContentCaptureManager.LoggingLevel;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Helper class for this package and server's.
 *
 * @hide
 */
public final class ContentCaptureHelper {

    private static final String TAG = ContentCaptureHelper.class.getSimpleName();

    public static boolean sVerbose = false;
    public static boolean sDebug = true;

    /**
     * Used to log text that could contain PII.
     */
    @Nullable
    public static String getSanitizedString(@Nullable CharSequence text) {
        return text == null ? null : text.length() + "_chars";
    }

    /**
     * Gets the default logging level for the device.
     */
    @LoggingLevel
    public static int getDefaultLoggingLevel() {
        return Build.IS_DEBUGGABLE ? LOGGING_LEVEL_DEBUG : LOGGING_LEVEL_OFF;
    }

    /**
     * Sets the value of the static logging level constants based on device config.
     */
    public static void setLoggingLevel() {
        final int defaultLevel = getDefaultLoggingLevel();
        final int level = DeviceConfig.getInt(DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
                DEVICE_CONFIG_PROPERTY_LOGGING_LEVEL, defaultLevel);
        setLoggingLevel(level);
    }

    /**
     * Sets the value of the static logging level constants based the given level.
     */
    public static void setLoggingLevel(@LoggingLevel int level) {
        Log.i(TAG, "Setting logging level to " + getLoggingLevelAsString(level));
        sVerbose = sDebug = false;
        switch (level) {
            case LOGGING_LEVEL_VERBOSE:
                sVerbose = true;
                // fall through
            case LOGGING_LEVEL_DEBUG:
                sDebug = true;
                return;
            case LOGGING_LEVEL_OFF:
                // You log nothing, Jon Snow!
                return;
            default:
                Log.w(TAG, "setLoggingLevel(): invalud level: " + level);
        }
    }

    /**
     * Gets a user-friendly value for a content capture logging level.
     */
    public static String getLoggingLevelAsString(@LoggingLevel int level) {
        switch (level) {
            case LOGGING_LEVEL_OFF:
                return "OFF";
            case LOGGING_LEVEL_DEBUG:
                return "DEBUG";
            case LOGGING_LEVEL_VERBOSE:
                return "VERBOSE";
            default:
                return "UNKNOWN-" + level;
        }
    }

    /**
     * Converts a set to a list.
     */
    @Nullable
    public static <T> ArrayList<T> toList(@Nullable Set<T> set) {
        return set == null ? null : new ArrayList<T>(set);
    }

    /**
     * Converts a list to a set.
     */
    @Nullable
    public static <T> ArraySet<T> toSet(@Nullable List<T> list) {
        return list == null ? null : new ArraySet<T>(list);
    }

    private ContentCaptureHelper() {
        throw new UnsupportedOperationException("contains only static methods");
    }
}