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