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.support.annotation.NonNull;
21 import android.widget.ImageView;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 import com.android.systemui.plugins.qs.QSTile.SlashState;
25 import com.android.systemui.qs.SlashDrawable;
26 
27 public class SlashImageView extends ImageView {
28 
29     @VisibleForTesting
30     protected SlashDrawable mSlash;
31     private boolean mAnimationEnabled = true;
32 
SlashImageView(Context context)33     public SlashImageView(Context context) {
34         super(context);
35     }
36 
getSlash()37     protected SlashDrawable getSlash() {
38         return mSlash;
39     }
40 
setSlash(SlashDrawable slash)41     protected void setSlash(SlashDrawable slash) {
42         mSlash = slash;
43     }
44 
ensureSlashDrawable()45     protected void ensureSlashDrawable() {
46         if (mSlash == null) {
47             mSlash = new SlashDrawable(getDrawable());
48             mSlash.setAnimationEnabled(mAnimationEnabled);
49             super.setImageDrawable(mSlash);
50         }
51     }
52 
53     @Override
setImageDrawable(Drawable drawable)54     public void setImageDrawable(Drawable drawable) {
55         if (drawable == null) {
56             mSlash = null;
57             super.setImageDrawable(null);
58         } else if (mSlash == null) {
59             setImageLevel(drawable.getLevel());
60             super.setImageDrawable(drawable);
61         } else {
62             mSlash.setAnimationEnabled(mAnimationEnabled);
63             mSlash.setDrawable(drawable);
64         }
65     }
66 
setImageViewDrawable(SlashDrawable slash)67     protected void setImageViewDrawable(SlashDrawable slash) {
68         super.setImageDrawable(slash);
69     }
70 
setAnimationEnabled(boolean enabled)71     public void setAnimationEnabled(boolean enabled) {
72         mAnimationEnabled = enabled;
73     }
74 
getAnimationEnabled()75     public boolean getAnimationEnabled() {
76         return mAnimationEnabled;
77     }
78 
setSlashState(@onNull SlashState slashState)79     private void setSlashState(@NonNull SlashState slashState) {
80         ensureSlashDrawable();
81         mSlash.setRotation(slashState.rotation);
82         mSlash.setSlashed(slashState.isSlashed);
83     }
84 
setState(@ullable SlashState state, @Nullable Drawable drawable)85     public void setState(@Nullable SlashState state, @Nullable Drawable drawable) {
86         if (state != null) {
87             setImageDrawable(drawable);
88             setSlashState(state);
89         } else {
90             mSlash = null;
91             setImageDrawable(drawable);
92         }
93     }
94 }
95