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.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.Paint;
     25 import android.graphics.Point;
     26 import android.graphics.Rect;
     27 import android.graphics.drawable.Drawable;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.widget.TextView;
     31 
     32 class DragShadowBuilder extends View.DragShadowBuilder {
     33 
     34     private final View mShadowView;
     35     private final TextView mTitle;
     36     private final DropBadgeView mIcon;
     37     private final int mWidth;
     38     private final int mHeight;
     39     private final int mShadowRadius;
     40     private int mPadding;
     41     private Paint paint;
     42 
     43     DragShadowBuilder(Context context) {
     44         mWidth = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_width);
     45         mHeight = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_height);
     46         mShadowRadius = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_radius);
     47         mPadding = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_padding);
     48 
     49         mShadowView = LayoutInflater.from(context).inflate(R.layout.drag_shadow_layout, null);
     50         mTitle = (TextView) mShadowView.findViewById(android.R.id.title);
     51         mIcon = (DropBadgeView) mShadowView.findViewById(android.R.id.icon);
     52 
     53         // Important for certain APIs
     54         mShadowView.setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
     55         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     56     }
     57 
     58     @Override
     59     public void onProvideShadowMetrics(
     60             Point shadowSize, Point shadowTouchPoint) {
     61         shadowSize.set(mWidth, mHeight);
     62         shadowTouchPoint.set(mWidth, mHeight);
     63     }
     64 
     65     @Override
     66     public void onDrawShadow(Canvas canvas) {
     67         Rect r = canvas.getClipBounds();
     68         // Calling measure is necessary in order for all child views to get correctly laid out.
     69         mShadowView.measure(
     70                 View.MeasureSpec.makeMeasureSpec(r.right- r.left, View.MeasureSpec.EXACTLY),
     71                 View.MeasureSpec.makeMeasureSpec(r.bottom - r.top , View.MeasureSpec.EXACTLY));
     72         mShadowView.layout(r.left, r.top, r.right, r.bottom);
     73 
     74         // Since DragShadow is not an actual view drawn in hardware-accelerated window,
     75         // android:elevation does not work; we need to draw the shadow ourselves manually.
     76         paint.setColor(Color.TRANSPARENT);
     77         // Shadow 1
     78         int opacity = (int) (255 * 0.1);
     79         paint.setShadowLayer(mShadowRadius, 0, 0, Color.argb(opacity, 0, 0, 0));
     80         canvas.drawRect(r.left + mPadding, r.top + mPadding, r.right - mPadding,
     81                 r.bottom - mPadding, paint);
     82         // Shadow 2
     83         opacity = (int) (255 * 0.24);
     84         paint.setShadowLayer(mShadowRadius, 0, mShadowRadius, Color.argb(opacity, 0, 0, 0));
     85         canvas.drawRect(r.left + mPadding, r.top + mPadding, r.right - mPadding,
     86                 r.bottom - mPadding, paint);
     87         mShadowView.draw(canvas);
     88     }
     89 
     90     void updateTitle(String title) {
     91         mTitle.setText(title);
     92     }
     93 
     94     void updateIcon(Drawable icon) {
     95         mIcon.updateIcon(icon);
     96     }
     97 
     98     void onStateUpdated(@State int state) {
     99         mIcon.updateState(state);
    100     }
    101 }
    102