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             )
51             setStatusIconDrawable.start()
52         }
53     }
54 
cancelStatusAnimationnull55     fun cancelStatusAnimation(statusIcon: ImageView) {
56         val statusDrawable: Drawable? = statusIcon.drawable
57         if (
58             statusDrawable != null &&
59                 statusDrawable is AnimatedVectorDrawable &&
60                 statusDrawable.isRunning
61         ) {
62             statusDrawable.clearAnimationCallbacks()
63             statusDrawable.stop()
64         }
65     }
66 
animateChangeTextnull67     fun animateChangeText(textView: TextView, text: String) {
68         textFadeAnimator.animateChangeText(textView, text)
69     }
70 
cancelTextChangeAnimationnull71     fun cancelTextChangeAnimation(textView: TextView) {
72         textFadeAnimator.cancelTextChangeAnimation(textView)
73     }
74 
75     @DrawableRes
selectIconAnimationResIdnull76     private fun selectIconAnimationResId(startSeverityLevel: Int, endSeverityLevel: Int): Int {
77         return when (endSeverityLevel) {
78             SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK ->
79                 when (startSeverityLevel) {
80                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK ->
81                         R.drawable.safety_status_small_info_to_info_anim
82                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION ->
83                         R.drawable.safety_status_small_recommendation_to_info_anim
84                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING ->
85                         R.drawable.safety_status_small_warn_to_info_anim
86                     else -> R.drawable.ic_safety_info
87                 }
88             SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION ->
89                 when (startSeverityLevel) {
90                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK ->
91                         R.drawable.safety_status_small_info_to_recommendation_anim
92                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION ->
93                         R.drawable.safety_status_small_recommendation_to_recommendation_anim
94                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING ->
95                         R.drawable.safety_status_small_warn_to_recommendation_anim
96                     else -> R.drawable.ic_safety_recommendation
97                 }
98             SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING ->
99                 when (startSeverityLevel) {
100                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_OK ->
101                         R.drawable.safety_status_small_info_to_warn_anim
102                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_RECOMMENDATION ->
103                         R.drawable.safety_status_small_recommendation_to_warn_anim
104                     SafetyCenterIssue.ISSUE_SEVERITY_LEVEL_CRITICAL_WARNING ->
105                         R.drawable.safety_status_small_warn_to_warn_anim
106                     else -> R.drawable.ic_safety_warn
107                 }
108             else -> {
109                 Log.e(
110                     MoreIssuesCardPreference.TAG,
111                     String.format(
112                         "Unexpected SafetyCenterIssue.IssueSeverityLevel: %d",
113                         endSeverityLevel
114                     )
115                 )
116                 R.drawable.ic_safety_null_state
117             }
118         }
119     }
120 }
121