1 /*
2  * Copyright (C) 2022 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 com.android.safetycenter;
18 
19 import android.safetycenter.SafetySourceData;
20 import android.safetycenter.config.SafetySource;
21 import android.util.Log;
22 
23 import com.android.safetycenter.UserProfileGroup.ProfileType;
24 
25 /**
26  * A helper class to facilitate working with {@link SafetySource} objects.
27  *
28  * @hide
29  */
30 public final class SafetySources {
31 
32     private static final String TAG = "SafetySources";
33 
34     /**
35      * Returns whether a {@link SafetySource} is external, i.e. if {@link SafetySourceData} can be
36      * provided for it.
37      */
isExternal(SafetySource safetySource)38     public static boolean isExternal(SafetySource safetySource) {
39         int safetySourceType = safetySource.getType();
40         switch (safetySourceType) {
41             case SafetySource.SAFETY_SOURCE_TYPE_STATIC:
42                 return false;
43             case SafetySource.SAFETY_SOURCE_TYPE_DYNAMIC:
44             case SafetySource.SAFETY_SOURCE_TYPE_ISSUE_ONLY:
45                 return true;
46         }
47         Log.w(TAG, "Unexpected safety source type: " + safetySourceType);
48         return false;
49     }
50 
51     /** Returns whether a {@link SafetySource} supports managed profiles. */
supportsManagedProfiles(SafetySource safetySource)52     public static boolean supportsManagedProfiles(SafetySource safetySource) {
53         return supportsAllProfiles(safetySource);
54     }
55 
56     /**
57      * Returns whether a {@link SafetySource} supports the profile of the given type
58      * {@code profileType}.
59      */
supportsProfileType( SafetySource safetySource, @ProfileType int profileType)60     public static boolean supportsProfileType(
61             SafetySource safetySource, @ProfileType int profileType) {
62         if (UserProfileGroup.PROFILE_TYPE_PRIMARY == profileType) {
63             return true;
64         }
65         return supportsAllProfiles(safetySource);
66     }
67 
68     /** Returns whether a {@link SafetySource} supports all profiles. */
supportsAllProfiles(SafetySource safetySource)69     private static boolean supportsAllProfiles(SafetySource safetySource) {
70         int safetySourceProfile = safetySource.getProfile();
71         switch (safetySourceProfile) {
72             case SafetySource.PROFILE_PRIMARY:
73             case SafetySource.PROFILE_NONE:
74                 return false;
75             case SafetySource.PROFILE_ALL:
76                 return true;
77         }
78         Log.w(TAG, "Unexpected safety source profile: " + safetySourceProfile);
79         return false;
80     }
81 
82     /** Returns whether a {@link SafetySource} default entry should be hidden in the UI. */
isDefaultEntryHidden(SafetySource safetySource)83     static boolean isDefaultEntryHidden(SafetySource safetySource) {
84         int safetySourceType = safetySource.getType();
85         switch (safetySourceType) {
86             case SafetySource.SAFETY_SOURCE_TYPE_STATIC:
87             case SafetySource.SAFETY_SOURCE_TYPE_ISSUE_ONLY:
88                 return false;
89             case SafetySource.SAFETY_SOURCE_TYPE_DYNAMIC:
90                 return safetySource.getInitialDisplayState()
91                         == SafetySource.INITIAL_DISPLAY_STATE_HIDDEN;
92         }
93         Log.w(TAG, "Unexpected safety source type: " + safetySourceType);
94         return false;
95     }
96 
97     /** Returns whether a {@link SafetySource} default entry should be disabled in the UI. */
isDefaultEntryDisabled(SafetySource safetySource)98     static boolean isDefaultEntryDisabled(SafetySource safetySource) {
99         int safetySourceType = safetySource.getType();
100         switch (safetySourceType) {
101             case SafetySource.SAFETY_SOURCE_TYPE_STATIC:
102             case SafetySource.SAFETY_SOURCE_TYPE_ISSUE_ONLY:
103                 return false;
104             case SafetySource.SAFETY_SOURCE_TYPE_DYNAMIC:
105                 return safetySource.getInitialDisplayState()
106                         == SafetySource.INITIAL_DISPLAY_STATE_DISABLED;
107         }
108         Log.w(TAG, "Unexpected safety source type: " + safetySourceType);
109         return false;
110     }
111 
112     /**
113      * Returns whether a {@link SafetySource} can be logged, without requiring a check of source
114      * type first.
115      */
isLoggable(SafetySource safetySource)116     public static boolean isLoggable(SafetySource safetySource) {
117         // Only external sources can have logging allowed values. Non-external sources cannot have
118         // their loggability configured. Unfortunately isLoggingAllowed throws if called on a
119         // non-external source.
120         if (isExternal(safetySource)) {
121             return safetySource.isLoggingAllowed();
122         } else {
123             return true;
124         }
125     }
126 
SafetySources()127     private SafetySources() {}
128 }
129