blob: 13912fe0c16d9fbf614fb04ba868960a103a595f [file] [log] [blame]
Jason Monk395617f2017-05-05 14:07:58 -04001/*
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
15package com.android.systemui.qs.tileimpl;
16
Evan Laird929bd542017-07-12 16:36:06 -040017import android.annotation.Nullable;
Jason Monk395617f2017-05-05 14:07:58 -040018import android.content.Context;
19import android.graphics.drawable.Drawable;
Evan Laird929bd542017-07-12 16:36:06 -040020import android.support.annotation.NonNull;
Jason Monk395617f2017-05-05 14:07:58 -040021import android.widget.ImageView;
22
Evan Laird418ffe42017-06-13 15:09:21 -040023import com.android.internal.annotations.VisibleForTesting;
Jason Monk395617f2017-05-05 14:07:58 -040024import com.android.systemui.plugins.qs.QSTile.SlashState;
25import com.android.systemui.qs.SlashDrawable;
26
27public class SlashImageView extends ImageView {
28
Evan Laird418ffe42017-06-13 15:09:21 -040029 @VisibleForTesting
30 protected SlashDrawable mSlash;
Evan Laird929bd542017-07-12 16:36:06 -040031 private boolean mAnimationEnabled = true;
Jason Monk395617f2017-05-05 14:07:58 -040032
33 public SlashImageView(Context context) {
34 super(context);
35 }
36
37 private void ensureSlashDrawable() {
38 if (mSlash == null) {
39 mSlash = new SlashDrawable(getDrawable());
Evan Laird929bd542017-07-12 16:36:06 -040040 mSlash.setAnimationEnabled(mAnimationEnabled);
Jason Monk395617f2017-05-05 14:07:58 -040041 super.setImageDrawable(mSlash);
42 }
43 }
44
45 @Override
46 public void setImageDrawable(Drawable drawable) {
Evan Laird418ffe42017-06-13 15:09:21 -040047 if (drawable == null) {
48 mSlash = null;
49 super.setImageDrawable(null);
50 } else if (mSlash == null) {
Jason Monk395617f2017-05-05 14:07:58 -040051 super.setImageDrawable(drawable);
Evan Laird418ffe42017-06-13 15:09:21 -040052 } else {
Evan Laird929bd542017-07-12 16:36:06 -040053 mSlash.setAnimationEnabled(mAnimationEnabled);
Evan Laird418ffe42017-06-13 15:09:21 -040054 mSlash.setDrawable(drawable);
Jason Monk395617f2017-05-05 14:07:58 -040055 }
56 }
57
Evan Laird929bd542017-07-12 16:36:06 -040058 public void setAnimationEnabled(boolean enabled) {
59 mAnimationEnabled = enabled;
60 }
61
62 private void setSlashState(@NonNull SlashState slashState) {
Jason Monk395617f2017-05-05 14:07:58 -040063 ensureSlashDrawable();
64 mSlash.setRotation(slashState.rotation);
65 mSlash.setSlashed(slashState.isSlashed);
66 }
Evan Laird929bd542017-07-12 16:36:06 -040067
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 Monk395617f2017-05-05 14:07:58 -040077}