• 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.permissioncontroller.safetycenter.ui;
18 
19 import android.content.Context;
20 import android.safetycenter.SafetyCenterEntry;
21 import android.util.Log;
22 
23 import androidx.preference.Preference;
24 
25 /** A preference that displays a visual representation of a {@link SafetyCenterEntry}. */
26 public class SafetyEntryPreference extends Preference {
27 
28     private static final String TAG = SafetyEntryPreference.class.getSimpleName();
29 
SafetyEntryPreference(Context context, SafetyCenterEntry entry)30     public SafetyEntryPreference(Context context, SafetyCenterEntry entry) {
31         super(context);
32         setTitle(entry.getTitle());
33         setSummary(entry.getSummary());
34         setIcon(toSeverityLevel(entry.getSeverityLevel()).getEntryIconResId());
35         if (entry.getPendingIntent() != null) {
36             setOnPreferenceClickListener(unused -> {
37                 try {
38                     entry.getPendingIntent().send();
39                 } catch (Exception ex) {
40                     Log.e(TAG,
41                             String.format("Failed to execute pending intent for entry: %s", entry),
42                             ex);
43                 }
44                 return true;
45             });
46         }
47     }
48 
toSeverityLevel(int entrySeverityLevel)49     private static SeverityLevel toSeverityLevel(int entrySeverityLevel) {
50         switch (entrySeverityLevel) {
51             case SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNKNOWN:
52                 return SeverityLevel.SEVERITY_LEVEL_UNKNOWN;
53             case SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_UNSPECIFIED:
54                 return SeverityLevel.NONE;
55             case SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_OK:
56                 return SeverityLevel.INFORMATION;
57             case SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_RECOMMENDATION:
58                 return SeverityLevel.RECOMMENDATION;
59             case SafetyCenterEntry.ENTRY_SEVERITY_LEVEL_CRITICAL_WARNING:
60                 return SeverityLevel.CRITICAL_WARNING;
61         }
62         throw new IllegalArgumentException(
63                 String.format("Unexpected SafetyCenterEntry.EntrySeverityLevel: %s",
64                         entrySeverityLevel));
65     }
66 }
67