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 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 19 | import com.android.documentsui.DragAndDropManager.State; |
Bill Lin | 49bef29 | 2018-11-30 21:51:47 +0800 | [diff] [blame] | 20 | import com.android.documentsui.base.MimeTypes; |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 21 | |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.graphics.drawable.LayerDrawable; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.Gravity; |
| 27 | import android.widget.ImageView; |
| 28 | |
| 29 | /** |
| 30 | * Provides a way to encapsulate droppable badge toggling logic into a single class. |
| 31 | */ |
| 32 | public final class DropBadgeView extends ImageView { |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 33 | private static final int[] STATE_REJECT_DROP = { R.attr.state_reject_drop }; |
| 34 | private static final int[] STATE_COPY = { R.attr.state_copy }; |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 35 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 36 | private @State int mState; |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 37 | private LayerDrawable mBackground; |
| 38 | |
| 39 | public DropBadgeView(Context context, AttributeSet attrs) { |
| 40 | super(context, attrs); |
| 41 | |
| 42 | final int badgeHeight = context.getResources() |
| 43 | .getDimensionPixelSize(R.dimen.drop_icon_height); |
| 44 | final int badgeWidth = context.getResources() |
| 45 | .getDimensionPixelSize(R.dimen.drop_icon_width); |
Ben Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 46 | final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 47 | |
| 48 | Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null); |
Bill Lin | 49bef29 | 2018-11-30 21:51:47 +0800 | [diff] [blame] | 49 | Drawable defaultIcon = IconUtils.loadMimeIcon(context, MimeTypes.GENERIC_TYPE); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 50 | |
| 51 | Drawable[] list = {defaultIcon, okBadge}; |
| 52 | mBackground = new LayerDrawable(list); |
| 53 | |
| 54 | mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT); |
Ben Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 55 | mBackground.setLayerGravity(0, Gravity.TOP | Gravity.LEFT); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 56 | mBackground.setLayerSize(1, badgeWidth, badgeHeight); |
Ben Lin | d8d7c25 | 2016-11-16 13:21:40 -0800 | [diff] [blame] | 57 | mBackground.setLayerSize(0, iconSize, iconSize); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 58 | |
| 59 | setBackground(mBackground); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public int[] onCreateDrawableState(int extraSpace) { |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 64 | // STATE_REJECT_DROP and STATE_COPY can't exist at the same time. |
| 65 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 66 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 67 | switch (mState) { |
| 68 | case DragAndDropManager.STATE_NOT_ALLOWED: |
| 69 | mergeDrawableStates(drawableState, STATE_REJECT_DROP); |
| 70 | break; |
| 71 | case DragAndDropManager.STATE_COPY: |
| 72 | mergeDrawableStates(drawableState, STATE_COPY); |
| 73 | break; |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return drawableState; |
| 77 | } |
| 78 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 79 | void updateState(@State int state) { |
| 80 | mState = state; |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 81 | refreshDrawableState(); |
| 82 | } |
| 83 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 84 | void updateIcon(Drawable icon) { |
Ben Lin | 166c5c6 | 2016-11-01 12:14:38 -0700 | [diff] [blame] | 85 | mBackground.setDrawable(0, icon); |
| 86 | } |
| 87 | } |