blob: 8ed67013d7aa95730302580dd1f8adc2f48004bb [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);
44
45 Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
46 Drawable defaultIcon = context.getResources()
47 .getDrawable(com.android.internal.R.drawable.ic_doc_generic, null);
48
49 Drawable[] list = {defaultIcon, okBadge};
50 mBackground = new LayerDrawable(list);
51
52 mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT);
53 mBackground.setLayerSize(1, badgeWidth, badgeHeight);
54
55 setBackground(mBackground);
56 }
57
58 @Override
59 public int[] onCreateDrawableState(int extraSpace) {
60 final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
61
62 if (mDroppable) {
63 mergeDrawableStates(drawableState, STATE_DROPPABLE);
64 }
65
66 if (mDropHovered) {
67 mergeDrawableStates(drawableState, STATE_DROP_HOVERED);
68 }
69
70 return drawableState;
71 }
72
73 public void setDroppable(boolean droppable) {
74 mDroppable = droppable;
75 refreshDrawableState();
76 }
77
78 public void setDropHovered(boolean hovered) {
79 mDropHovered = hovered;
80 refreshDrawableState();
81 }
82
83 public void updateIcon(Drawable icon) {
84 mBackground.setDrawable(0, icon);
85 }
86}