Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 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 | |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 17 | import android.annotation.Nullable; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.graphics.drawable.Drawable; |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 20 | import android.support.annotation.NonNull; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 21 | import android.widget.ImageView; |
| 22 | |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 24 | import com.android.systemui.plugins.qs.QSTile.SlashState; |
| 25 | import com.android.systemui.qs.SlashDrawable; |
| 26 | |
| 27 | public class SlashImageView extends ImageView { |
| 28 | |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 29 | @VisibleForTesting |
| 30 | protected SlashDrawable mSlash; |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 31 | private boolean mAnimationEnabled = true; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 32 | |
| 33 | public SlashImageView(Context context) { |
| 34 | super(context); |
| 35 | } |
| 36 | |
| 37 | private void ensureSlashDrawable() { |
| 38 | if (mSlash == null) { |
| 39 | mSlash = new SlashDrawable(getDrawable()); |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 40 | mSlash.setAnimationEnabled(mAnimationEnabled); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 41 | super.setImageDrawable(mSlash); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void setImageDrawable(Drawable drawable) { |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 47 | if (drawable == null) { |
| 48 | mSlash = null; |
| 49 | super.setImageDrawable(null); |
| 50 | } else if (mSlash == null) { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 51 | super.setImageDrawable(drawable); |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 52 | } else { |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 53 | mSlash.setAnimationEnabled(mAnimationEnabled); |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 54 | mSlash.setDrawable(drawable); |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 58 | public void setAnimationEnabled(boolean enabled) { |
| 59 | mAnimationEnabled = enabled; |
| 60 | } |
| 61 | |
| 62 | private void setSlashState(@NonNull SlashState slashState) { |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 63 | ensureSlashDrawable(); |
| 64 | mSlash.setRotation(slashState.rotation); |
| 65 | mSlash.setSlashed(slashState.isSlashed); |
| 66 | } |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 67 | |
| 68 | public void setState(@Nullable SlashState state, @Nullable Drawable drawable) { |
| 69 | if (state != null) { |
| 70 | setImageDrawable(drawable); |
| 71 | setSlashState(state); |
| 72 | } else { |
| 73 | mSlash = null; |
| 74 | setImageDrawable(drawable); |
| 75 | } |
| 76 | } |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 77 | } |