blob: 6f71d018e4bb41ae355b50278a4507b8f26fd917 [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;
20
Ben Lin166c5c62016-11-01 12:14:38 -070021import android.content.Context;
22import android.graphics.drawable.Drawable;
23import android.graphics.drawable.LayerDrawable;
24import android.util.AttributeSet;
25import android.view.Gravity;
26import android.widget.ImageView;
27
28/**
29 * Provides a way to encapsulate droppable badge toggling logic into a single class.
30 */
31public final class DropBadgeView extends ImageView {
Garfield Tanda2c0f02017-04-11 13:47:58 -070032 private static final int[] STATE_REJECT_DROP = { R.attr.state_reject_drop };
33 private static final int[] STATE_COPY = { R.attr.state_copy };
Ben Lin166c5c62016-11-01 12:14:38 -070034
Garfield Tanda2c0f02017-04-11 13:47:58 -070035 private @State int mState;
Ben Lin166c5c62016-11-01 12:14:38 -070036 private LayerDrawable mBackground;
37
38 public DropBadgeView(Context context, AttributeSet attrs) {
39 super(context, attrs);
40
41 final int badgeHeight = context.getResources()
42 .getDimensionPixelSize(R.dimen.drop_icon_height);
43 final int badgeWidth = context.getResources()
44 .getDimensionPixelSize(R.dimen.drop_icon_width);
Ben Lind8d7c252016-11-16 13:21:40 -080045 final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size);
Ben Lin166c5c62016-11-01 12:14:38 -070046
47 Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
48 Drawable defaultIcon = context.getResources()
Ben Linfc84b342017-02-17 11:44:45 -080049 .getDrawable(R.drawable.ic_doc_generic, null);
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}