Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.graphics.drawable.LayerDrawable; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.Gravity; |
| 24 | import android.widget.ImageView; |
| 25 | |
| 26 | /** |
| 27 | * Provides a way to encapsulate droppable badge toggling logic into a single class. |
| 28 | */ |
| 29 | public 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 Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 44 | final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 45 | |
| 46 | Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null); |
| 47 | Drawable defaultIcon = context.getResources() |
Ben Lin | fc84b34 | 2017-02-17 11:44:45 -0800 | [diff] [blame] | 48 | .getDrawable(R.drawable.ic_doc_generic, null); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 49 | |
| 50 | Drawable[] list = {defaultIcon, okBadge}; |
| 51 | mBackground = new LayerDrawable(list); |
| 52 | |
| 53 | mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT); |
Ben Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 54 | mBackground.setLayerGravity(0, Gravity.TOP | Gravity.LEFT); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 55 | mBackground.setLayerSize(1, badgeWidth, badgeHeight); |
Ben Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 56 | mBackground.setLayerSize(0, iconSize, iconSize); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 57 | |
| 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 | } |