1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.qs.tileimpl;
16 
17 import android.annotation.Nullable;
18 import android.content.Context;
19 import android.graphics.drawable.Drawable;
20 import android.widget.ImageView;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.systemui.plugins.qs.QSTile.SlashState;
26 import com.android.systemui.qs.SlashDrawable;
27 
28 public class SlashImageView extends ImageView {
29 
30     @Nullable
31     @VisibleForTesting
32     protected SlashDrawable mSlash;
33     private boolean mAnimationEnabled = true;
34 
SlashImageView(Context context)35     public SlashImageView(Context context) {
36         super(context);
37     }
38 
39     @Nullable
getSlash()40     protected SlashDrawable getSlash() {
41         return mSlash;
42     }
43 
setSlash(SlashDrawable slash)44     protected void setSlash(SlashDrawable slash) {
45         mSlash = slash;
46     }
47 
ensureSlashDrawable()48     protected void ensureSlashDrawable() {
49         if (mSlash == null) {
50             mSlash = new SlashDrawable(getDrawable());
51             mSlash.setAnimationEnabled(mAnimationEnabled);
52             super.setImageDrawable(mSlash);
53         }
54     }
55 
56     @Override
setImageDrawable(@ullable Drawable drawable)57     public void setImageDrawable(@Nullable Drawable drawable) {
58         if (drawable == null) {
59             mSlash = null;
60             super.setImageDrawable(null);
61         } else if (mSlash == null) {
62             setImageLevel(drawable.getLevel());
63             super.setImageDrawable(drawable);
64         } else {
65             mSlash.setAnimationEnabled(mAnimationEnabled);
66             mSlash.setDrawable(drawable);
67         }
68     }
69 
setImageViewDrawable(SlashDrawable slash)70     protected void setImageViewDrawable(SlashDrawable slash) {
71         super.setImageDrawable(slash);
72     }
73 
setAnimationEnabled(boolean enabled)74     public void setAnimationEnabled(boolean enabled) {
75         mAnimationEnabled = enabled;
76     }
77 
getAnimationEnabled()78     public boolean getAnimationEnabled() {
79         return mAnimationEnabled;
80     }
81 
setSlashState(@onNull SlashState slashState)82     private void setSlashState(@NonNull SlashState slashState) {
83         ensureSlashDrawable();
84         mSlash.setRotation(slashState.rotation);
85         mSlash.setSlashed(slashState.isSlashed);
86     }
87 
setState(@ullable SlashState state, @Nullable Drawable drawable)88     public void setState(@Nullable SlashState state, @Nullable Drawable drawable) {
89         if (state != null) {
90             setImageDrawable(drawable);
91             setSlashState(state);
92         } else {
93             mSlash = null;
94             setImageDrawable(drawable);
95         }
96     }
97 }
98