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 com.android.documentsui.DragAndDropManager.State;
20 import com.android.documentsui.base.MimeTypes;
21 
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 {
33     private static final int[] STATE_REJECT_DROP = { R.attr.state_reject_drop };
34     private static final int[] STATE_COPY = { R.attr.state_copy };
35 
36     private @State int mState;
37     private LayerDrawable mBackground;
38 
DropBadgeView(Context context, AttributeSet attrs)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);
46         final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size);
47 
48         Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
49         Drawable defaultIcon = IconUtils.loadMimeIcon(context, MimeTypes.GENERIC_TYPE);
50 
51         Drawable[] list = {defaultIcon, okBadge};
52         mBackground = new LayerDrawable(list);
53 
54         mBackground.setLayerGravity(1, Gravity.BOTTOM | Gravity.RIGHT);
55         mBackground.setLayerGravity(0, Gravity.TOP | Gravity.LEFT);
56         mBackground.setLayerSize(1, badgeWidth, badgeHeight);
57         mBackground.setLayerSize(0, iconSize, iconSize);
58 
59         setBackground(mBackground);
60     }
61 
62     @Override
onCreateDrawableState(int extraSpace)63     public int[] onCreateDrawableState(int extraSpace) {
64         // STATE_REJECT_DROP and STATE_COPY can't exist at the same time.
65         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
66 
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;
74         }
75 
76         return drawableState;
77     }
78 
updateState(@tate int state)79     void updateState(@State int state) {
80         mState = state;
81         refreshDrawableState();
82     }
83 
updateIcon(Drawable icon)84     void updateIcon(Drawable icon) {
85         mBackground.setDrawable(0, icon);
86     }
87 }