1 /*
2  * Copyright (C) 2018 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 androidx.car.utils;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 import android.text.InputFilter;
24 import android.widget.TextView;
25 
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 
30 import androidx.annotation.RestrictTo;
31 
32 /**
33  * Utility class that helps {@code View}s comply with {@link CarUxRestrictions}.
34  *
35  * @hide
36  */
37 @RestrictTo(LIBRARY_GROUP)
38 public class CarUxRestrictionsUtils {
39 
CarUxRestrictionsUtils()40     private CarUxRestrictionsUtils() {};
41 
42     private static InputFilter sStringLengthFilter;
43 
44     /**
45      * Updates a {@code TextView} to comply with active UX restrictions.
46      *
47      * <p>Adds/removes a {@link android.text.InputFilter.LengthFilter} that truncates the text
48      * in {@code TextView}.
49      *
50      * @param carUxRestrictions current Car UX restrictions.
51      * @param tv TextView to be updated to comply with UX restrictions.
52      */
comply(Context context, CarUxRestrictions carUxRestrictions, TextView tv)53     public static void comply(Context context, CarUxRestrictions carUxRestrictions, TextView tv) {
54         if (sStringLengthFilter == null) {
55             int lengthLimit = carUxRestrictions.getMaxRestrictedStringLength();
56             sStringLengthFilter = new InputFilter.LengthFilter(lengthLimit);
57         }
58 
59         int activeUxr = carUxRestrictions.getActiveRestrictions();
60 
61         // Note the list returned by Arrays.asList does not support structural modification.
62         List<InputFilter> filters = Arrays.asList(tv.getFilters());
63         if ((activeUxr & CarUxRestrictions.UX_RESTRICTIONS_LIMIT_STRING_LENGTH) != 0) {
64             // Check whether length filter exists to avoid unnecessary array operations.
65             if (!filters.contains(sStringLengthFilter)) {
66                 ArrayList<InputFilter> updatedFilters = new ArrayList<>(filters);
67                 updatedFilters.add(sStringLengthFilter);
68                 tv.setFilters(updatedFilters.toArray(new InputFilter[updatedFilters.size()]));
69             }
70         } else if (filters.contains(sStringLengthFilter)) {
71             ArrayList<InputFilter> updatedFilters = new ArrayList<>(filters);
72             updatedFilters.remove(sStringLengthFilter);
73             tv.setFilters(updatedFilters.toArray(new InputFilter[updatedFilters.size()]));
74         }
75     }
76 }
77 
78