1 /* 2 * Copyright (C) 2021 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.SafetyCenterStatus; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.widget.ImageView; 24 import android.widget.TextView; 25 26 import androidx.annotation.Nullable; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceViewHolder; 29 30 import com.android.permissioncontroller.R; 31 32 /** Preference which displays a visual representation of {@link SafetyCenterStatus}. */ 33 public class SafetyStatusPreference extends Preference { 34 35 @Nullable 36 private SafetyCenterStatus mStatus; 37 @Nullable 38 private View.OnClickListener mRescanButtonOnClickListener; 39 SafetyStatusPreference(Context context, AttributeSet attrs)40 public SafetyStatusPreference(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 setLayoutResource(R.layout.preference_safety_status); 43 } 44 45 @Override onBindViewHolder(PreferenceViewHolder holder)46 public void onBindViewHolder(PreferenceViewHolder holder) { 47 super.onBindViewHolder(holder); 48 49 if (mStatus == null) { 50 return; 51 } 52 53 ((ImageView) holder.findViewById(R.id.status_image)) 54 .setImageResource(toStatusImageResId(mStatus.getSeverityLevel())); 55 56 ((TextView) holder.findViewById(R.id.status_title)).setText(mStatus.getTitle()); 57 ((TextView) holder.findViewById(R.id.status_summary)).setText(mStatus.getSummary()); 58 59 if (mRescanButtonOnClickListener != null) { 60 holder.findViewById(R.id.rescan_button) 61 .setOnClickListener(mRescanButtonOnClickListener); 62 } 63 } 64 setSafetyStatus(SafetyCenterStatus status)65 void setSafetyStatus(SafetyCenterStatus status) { 66 mStatus = status; 67 notifyChanged(); 68 } 69 setRescanButtonOnClickListener(View.OnClickListener listener)70 void setRescanButtonOnClickListener(View.OnClickListener listener) { 71 mRescanButtonOnClickListener = listener; 72 notifyChanged(); 73 } 74 toStatusImageResId(int overallSeverityLevel)75 private static int toStatusImageResId(int overallSeverityLevel) { 76 switch (overallSeverityLevel) { 77 case SafetyCenterStatus.OVERALL_SEVERITY_LEVEL_UNKNOWN: 78 return R.drawable.safety_status_info; 79 case SafetyCenterStatus.OVERALL_SEVERITY_LEVEL_OK: 80 return R.drawable.safety_status_info; 81 case SafetyCenterStatus.OVERALL_SEVERITY_LEVEL_RECOMMENDATION: 82 return R.drawable.safety_status_recommendation; 83 case SafetyCenterStatus.OVERALL_SEVERITY_LEVEL_CRITICAL_WARNING: 84 return R.drawable.safety_status_warn; 85 } 86 throw new IllegalArgumentException( 87 String.format("Unexpected SafetyCenterStatus.OverallSeverityLevel: %s", 88 overallSeverityLevel)); 89 } 90 } 91