1 /*
2  * Copyright (C) 2016 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 
17 package android.view.autofill;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Set;
25 
26 /** @hide */
27 public final class Helper {
28 
29     // Debug-level flags are defined when service is bound.
30     public static boolean sDebug = false;
31     public static boolean sVerbose = false;
32 
33     /**
34      * Appends {@code value} to the {@code builder} redacting its contents.
35      */
appendRedacted(@onNull StringBuilder builder, @Nullable CharSequence value)36     public static void appendRedacted(@NonNull StringBuilder builder,
37             @Nullable CharSequence value) {
38         builder.append(getRedacted(value));
39     }
40 
41     /**
42      * Gets the redacted version of a value.
43      */
44     @NonNull
getRedacted(@ullable CharSequence value)45     public static String getRedacted(@Nullable CharSequence value) {
46         return (value == null) ? "null" : value.length() + "_chars";
47     }
48 
49     /**
50      * Appends {@code values} to the {@code builder} redacting its contents.
51      */
appendRedacted(@onNull StringBuilder builder, @Nullable String[] values)52     public static void appendRedacted(@NonNull StringBuilder builder, @Nullable String[] values) {
53         if (values == null) {
54             builder.append("N/A");
55             return;
56         }
57         builder.append("[");
58         for (String value : values) {
59             builder.append(" '");
60             appendRedacted(builder, value);
61             builder.append("'");
62         }
63         builder.append(" ]");
64     }
65 
66     /**
67      * Converts a collaction of {@link AutofillId AutofillIds} to an array.
68      *
69      * @param collection The collection.
70      * @return The array.
71      */
toArray(Collection<AutofillId> collection)72     public static @NonNull AutofillId[] toArray(Collection<AutofillId> collection) {
73         if (collection == null) {
74             return new AutofillId[0];
75         }
76         final AutofillId[] array = new AutofillId[collection.size()];
77         collection.toArray(array);
78         return array;
79     }
80 
81     /**
82      * Converts a Set to a List.
83      */
84     @Nullable
toList(@ullable Set<T> set)85     public static <T> ArrayList<T> toList(@Nullable Set<T> set) {
86         return set == null ? null : new ArrayList<T>(set);
87     }
88 
Helper()89     private Helper() {
90         throw new UnsupportedOperationException("contains static members only");
91     }
92 }
93