Home | History | Annotate | Download | only in documentsui
      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 
     21 import android.content.Context;
     22 import android.graphics.drawable.Drawable;
     23 import android.graphics.drawable.LayerDrawable;
     24 import android.util.AttributeSet;
     25 import android.view.Gravity;
     26 import android.widget.ImageView;
     27 
     28 /**
     29  * Provides a way to encapsulate droppable badge toggling logic into a single class.
     30  */
     31 public final class DropBadgeView extends ImageView {
     32     private static final int[] STATE_REJECT_DROP = { R.attr.state_reject_drop };
     33     private static final int[] STATE_COPY = { R.attr.state_copy };
     34 
     35     private @State int mState;
     36     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);
     45         final int iconSize = context.getResources().getDimensionPixelSize(R.dimen.root_icon_size);
     46 
     47         Drawable okBadge = context.getResources().getDrawable(R.drawable.drop_badge_states, null);
     48         Drawable defaultIcon = context.getResources()
     49                 .getDrawable(R.drawable.ic_doc_generic, null);
     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
     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 
     79     void updateState(@State int state) {
     80         mState = state;
     81         refreshDrawableState();
     82     }
     83 
     84     void updateIcon(Drawable icon) {
     85         mBackground.setDrawable(0, icon);
     86     }
     87 }