blob: 10a0106d52c781eefb53a7486e8a10203c2107a4 [file] [log] [blame]
Steve McKay84769b82016-06-09 10:46:07 -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
Ben Lin1c456292016-10-07 16:43:18 -070017package com.android.documentsui;
Steve McKay84769b82016-06-09 10:46:07 -070018
Garfield Tanda2c0f02017-04-11 13:47:58 -070019import com.android.documentsui.DragAndDropManager.State;
20
Steve McKay84769b82016-06-09 10:46:07 -070021import android.content.Context;
22import android.graphics.Canvas;
Ben Lind8d7c252016-11-16 13:21:40 -080023import android.graphics.Color;
24import android.graphics.Paint;
Steve McKay84769b82016-06-09 10:46:07 -070025import android.graphics.Point;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
28import android.view.LayoutInflater;
29import android.view.View;
Steve McKay84769b82016-06-09 10:46:07 -070030import android.widget.TextView;
31
Garfield Tanda2c0f02017-04-11 13:47:58 -070032class DragShadowBuilder extends View.DragShadowBuilder {
Steve McKay84769b82016-06-09 10:46:07 -070033
34 private final View mShadowView;
35 private final TextView mTitle;
Ben Lin166c5c62016-11-01 12:14:38 -070036 private final DropBadgeView mIcon;
Steve McKay84769b82016-06-09 10:46:07 -070037 private final int mWidth;
38 private final int mHeight;
Ben Lind8d7c252016-11-16 13:21:40 -080039 private final int mShadowRadius;
40 private int mPadding;
41 private Paint paint;
Steve McKay84769b82016-06-09 10:46:07 -070042
Garfield Tanda2c0f02017-04-11 13:47:58 -070043 DragShadowBuilder(Context context) {
Steve McKay84769b82016-06-09 10:46:07 -070044 mWidth = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_width);
Ben Lin1c456292016-10-07 16:43:18 -070045 mHeight = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_height);
Ben Lind8d7c252016-11-16 13:21:40 -080046 mShadowRadius = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_radius);
47 mPadding = context.getResources().getDimensionPixelSize(R.dimen.drag_shadow_padding);
Steve McKay84769b82016-06-09 10:46:07 -070048
49 mShadowView = LayoutInflater.from(context).inflate(R.layout.drag_shadow_layout, null);
50 mTitle = (TextView) mShadowView.findViewById(android.R.id.title);
Ben Lin166c5c62016-11-01 12:14:38 -070051 mIcon = (DropBadgeView) mShadowView.findViewById(android.R.id.icon);
Steve McKay84769b82016-06-09 10:46:07 -070052
Ben Lind8d7c252016-11-16 13:21:40 -080053 // Important for certain APIs
54 mShadowView.setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
55 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Steve McKay84769b82016-06-09 10:46:07 -070056 }
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),
Ben Lind8d7c252016-11-16 13:21:40 -080071 View.MeasureSpec.makeMeasureSpec(r.bottom - r.top , View.MeasureSpec.EXACTLY));
Steve McKay84769b82016-06-09 10:46:07 -070072 mShadowView.layout(r.left, r.top, r.right, r.bottom);
Ben Lind8d7c252016-11-16 13:21:40 -080073
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);
Steve McKay84769b82016-06-09 10:46:07 -070087 mShadowView.draw(canvas);
88 }
Steve McKayf0fceb42016-09-07 17:35:55 -070089
Garfield Tanda2c0f02017-04-11 13:47:58 -070090 void updateTitle(String title) {
Ben Lin1c456292016-10-07 16:43:18 -070091 mTitle.setText(title);
92 }
93
Garfield Tanda2c0f02017-04-11 13:47:58 -070094 void updateIcon(Drawable icon) {
Ben Lin166c5c62016-11-01 12:14:38 -070095 mIcon.updateIcon(icon);
Ben Lin1c456292016-10-07 16:43:18 -070096 }
97
Garfield Tanda2c0f02017-04-11 13:47:58 -070098 void onStateUpdated(@State int state) {
99 mIcon.updateState(state);
Steve McKayf0fceb42016-09-07 17:35:55 -0700100 }
Steve McKay84769b82016-06-09 10:46:07 -0700101}