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