blob: 4bf3f74c506ff2927100ca58c8106ff84cbe2946 [file] [log] [blame]
Ben Lin166c5c62016-11-01 12:14:38 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.LayerDrawable;
22import android.util.AttributeSet;
23import android.view.Gravity;
24import android.widget.ImageView;
25
26/**
27 * Provides a way to encapsulate droppable badge toggling logic into a single class.
28 */
29public final class DropBadgeView extends ImageView {
30 private static final int[] STATE_DROPPABLE = {R.attr.state_droppable};
31 private static final int[] STATE_DROP_HOVERED = {R.attr.state_drop_hovered};
32
33 private boolean mDroppable = false;
34 private boolean mDropHovered = false;
35 private LayerDrawable mBackground;
36
37 public DropBadgeView(Context context, AttributeSet attrs) {
38 super(context, attrs);
39
40 final int badgeHeight = context.getResources()
41 .getDimensionPixelSize(R.dimen.drop_icon_height);
42 final int badgeWidth = context.getResources()
43 .getDimensionPixelSize(R.dimen.drop_icon_width);
Ben Lind8d7c252016-11-16 13:21:40 -080044 final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size);
Ben Lin166c5c62016-11-01 12:14:38 -070045
46 Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
47 Drawable defaultIcon = context.getResources()
Ben Linfc84b342017-02-17 11:44:45 -080048 .getDrawable(R.drawable.ic_doc_generic, null);
Ben Lin166c5c62016-11-01 12:14:38 -070049
50 Drawable[] list = {defaultIcon, okBadge};
51 mBackground = new LayerDrawable(list);
52
53 mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT);
Ben Lind8d7c252016-11-16 13:21:40 -080054 mBackground.setLayerGravity(0, Gravity.TOP | Gravity.LEFT);
Ben Lin166c5c62016-11-01 12:14:38 -070055 mBackground.setLayerSize(1, badgeWidth, badgeHeight);
Ben Lind8d7c252016-11-16 13:21:40 -080056 mBackground.setLayerSize(0, iconSize, iconSize);
Ben Lin166c5c62016-11-01 12:14:38 -070057
58 setBackground(mBackground);
59 }
60
61 @Override
62 public int[] onCreateDrawableState(int extraSpace) {
63 final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
64
65 if (mDroppable) {
66 mergeDrawableStates(drawableState, STATE_DROPPABLE);
67 }
68
69 if (mDropHovered) {
70 mergeDrawableStates(drawableState, STATE_DROP_HOVERED);
71 }
72
73 return drawableState;
74 }
75
76 public void setDroppable(boolean droppable) {
77 mDroppable = droppable;
78 refreshDrawableState();
79 }
80
81 public void setDropHovered(boolean hovered) {
82 mDropHovered = hovered;
83 refreshDrawableState();
84 }
85
86 public void updateIcon(Drawable icon) {
87 mBackground.setDrawable(0, icon);
88 }
89}