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.graphics.drawable.Animatable2 20 import android.graphics.drawable.AnimatedVectorDrawable 21 import android.graphics.drawable.Drawable 22 import android.safetycenter.SafetyCenterIssue 23 import android.util.Log 24 import android.widget.ImageView 25 import android.widget.TextView 26 import androidx.annotation.DrawableRes 27 import com.android.permissioncontroller.R 28 29 class MoreIssuesCardAnimator { 30 private val textFadeAnimator = TextFadeAnimator(R.id.widget_title) 31 animateStatusIconsChangenull32 fun animateStatusIconsChange( 33 statusIcon: ImageView, 34 startSeverityLevel: Int, 35 endSeverityLevel: Int, 36 @DrawableRes endSeverityLevelResId: Int 37 ) { 38 val severityLevelAnimationResId = 39 selectIconAnimationResId(startSeverityLevel, endSeverityLevel) 40 statusIcon.setImageResource(severityLevelAnimationResId) 41 val setStatusIconDrawable = statusIcon.drawable 42 if (setStatusIconDrawable is AnimatedVectorDrawable) { 43 setStatusIconDrawable.registerAnimationCallback( 44 object : Animatable2.AnimationCallback() { 45 override fun onAnimationEnd(drawable: Drawable) { 46 super.onAnimationEnd(drawable) 47 statusIcon.setImageResource(endSeverityLevelResId) 48 } 49 }) 50 setStatusIconDrawable.start() 51 } 52 } 53 cancelStatusAnimationnull54 fun cancelStatusAnimation(statusIcon: ImageView) { 55 val statusDrawable: Drawable? = statusIcon.drawable 56 if (statusDrawable != null && 57 statusDrawable is AnimatedVectorDrawable && 58 statusDrawable.isRunning) { 59 statusDrawable.clearAnimationCallbacks() 60 statusDrawable.stop() 61 } 62 } 63 animateChangeTextnull64 fun animateChangeText(textView: TextView, text: String) { 65 textFadeAnimator.animateChangeText(textView, text) 66 } 67 cancelTextChangeAnimationnull68 fun cancelTextChangeAnimation(textView: TextView) { 69 textFadeAnimator.cancelTextChangeAnimation(textView) 70 } 71 72 @DrawableRes selectIconAnimationResIdnull73 private fun selectIconAnimationResId(startSeverityLevel: Int, endSeverityLevel: Int): Int { 74 return when (endSeverityLevel) { 75 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK -> 76 when (startSeverityLevel) { 77 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK -> 78 R.drawable.safety_status_small_info_to_info_anim 79 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION -> 80 R.drawable.safety_status_small_recommendation_to_info_anim 81 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING -> 82 R.drawable.safety_status_small_warn_to_info_anim 83 else -> R.drawable.ic_safety_info 84 } 85 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION -> 86 when (startSeverityLevel) { 87 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK -> 88 R.drawable.safety_status_small_info_to_recommendation_anim 89 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION -> 90 R.drawable.safety_status_small_recommendation_to_recommendation_anim 91 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING -> 92 R.drawable.safety_status_small_warn_to_recommendation_anim 93 else -> R.drawable.ic_safety_recommendation 94 } 95 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING -> 96 when (startSeverityLevel) { 97 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK -> 98 R.drawable.safety_status_small_info_to_warn_anim 99 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION -> 100 R.drawable.safety_status_small_recommendation_to_warn_anim 101 SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING -> 102 R.drawable.safety_status_small_warn_to_warn_anim 103 else -> R.drawable.ic_safety_warn 104 } 105 else -> { 106 Log.e( 107 MoreIssuesCardPreference.TAG, 108 String.format( 109 "Unexpected SafetyCenterIssue.IssueSeverityLevel: %d", endSeverityLevel)) 110 R.drawable.ic_safety_null_state 111 } 112 } 113 } 114 } 115