blob: 552e2ecd39c0e6c9371ca38d5a9b89406f4a0f03 [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
Garfield Tanda2c0f02017-04-11 13:47:58 -070019import com.android.documentsui.DragAndDropManager.State;
Bill Lin49bef292018-11-30 21:51:47 +080020import com.android.documentsui.base.MimeTypes;
Garfield Tanda2c0f02017-04-11 13:47:58 -070021
Ben Lin166c5c62016-11-01 12:14:38 -070022import android.content.Context;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.LayerDrawable;
25import android.util.AttributeSet;
26import android.view.Gravity;
27import android.widget.ImageView;
28
29/**
30 * Provides a way to encapsulate droppable badge toggling logic into a single class.
31 */
32public final class DropBadgeView extends ImageView {
Garfield Tanda2c0f02017-04-11 13:47:58 -070033 private static final int[] STATE_REJECT_DROP = { R.attr.state_reject_drop };
34 private static final int[] STATE_COPY = { R.attr.state_copy };
Ben Lin166c5c62016-11-01 12:14:38 -070035
Garfield Tanda2c0f02017-04-11 13:47:58 -070036 private @State int mState;
Ben Lin166c5c62016-11-01 12:14:38 -070037 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 Lind8d7c252016-11-16 13:21:40 -080046 final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size);
Ben Lin166c5c62016-11-01 12:14:38 -070047
48 Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
Bill Lin49bef292018-11-30 21:51:47 +080049 Drawable defaultIcon = IconUtils.loadMimeIcon(context, MimeTypes.GENERIC_TYPE);
Ben Lin166c5c62016-11-01 12:14:38 -070050
51 Drawable[] list = {defaultIcon, okBadge};
52 mBackground = new LayerDrawable(list);
53
54 mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT);
Ben Lind8d7c252016-11-16 13:21:40 -080055 mBackground.setLayerGravity(0, Gravity.TOP | Gravity.LEFT);
Ben Lin166c5c62016-11-01 12:14:38 -070056 mBackground.setLayerSize(1, badgeWidth, badgeHeight);
Ben Lind8d7c252016-11-16 13:21:40 -080057 mBackground.setLayerSize(0, iconSize, iconSize);
Ben Lin166c5c62016-11-01 12:14:38 -070058
59 setBackground(mBackground);
60 }
61
62 @Override
63 public int[] onCreateDrawableState(int extraSpace) {
Garfield Tanda2c0f02017-04-11 13:47:58 -070064 // STATE_REJECT_DROP and STATE_COPY can't exist at the same time.
65 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
Ben Lin166c5c62016-11-01 12:14:38 -070066
Garfield Tanda2c0f02017-04-11 13:47:58 -070067 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 Lin166c5c62016-11-01 12:14:38 -070074 }
75
76 return drawableState;
77 }
78
Garfield Tanda2c0f02017-04-11 13:47:58 -070079 void updateState(@State int state) {
80 mState = state;
Ben Lin166c5c62016-11-01 12:14:38 -070081 refreshDrawableState();
82 }
83
Garfield Tanda2c0f02017-04-11 13:47:58 -070084 void updateIcon(Drawable icon) {
Ben Lin166c5c62016-11-01 12:14:38 -070085 mBackground.setDrawable(0, icon);
86 }
87}