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.launcher3.shortcuts;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.util.AttributeSet;
24 import android.view.MotionEvent;
25 
26 import com.android.launcher3.BubbleTextView;
27 import com.android.launcher3.R;
28 import com.android.launcher3.Utilities;
29 
30 /**
31  * A {@link BubbleTextView} that has the shortcut icon on the left and drag handle on the right.
32  */
33 public class DeepShortcutTextView extends BubbleTextView {
34     private final Rect mDragHandleBounds = new Rect();
35     private final int mDragHandleWidth;
36     private boolean mShouldPerformClick = true;
37 
DeepShortcutTextView(Context context)38     public DeepShortcutTextView(Context context) {
39         this(context, null, 0);
40     }
41 
DeepShortcutTextView(Context context, AttributeSet attrs)42     public DeepShortcutTextView(Context context, AttributeSet attrs) {
43         this(context, attrs, 0);
44     }
45 
DeepShortcutTextView(Context context, AttributeSet attrs, int defStyle)46     public DeepShortcutTextView(Context context, AttributeSet attrs, int defStyle) {
47         super(context, attrs, defStyle);
48 
49         Resources resources = getResources();
50         mDragHandleWidth = resources.getDimensionPixelSize(R.dimen.popup_padding_end)
51                 + resources.getDimensionPixelSize(R.dimen.deep_shortcut_drag_handle_size)
52                 + resources.getDimensionPixelSize(R.dimen.deep_shortcut_drawable_padding) / 2;
53     }
54 
55     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)56     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
57         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
58 
59         mDragHandleBounds.set(0, 0, mDragHandleWidth, getMeasuredHeight());
60         if (!Utilities.isRtl(getResources())) {
61             mDragHandleBounds.offset(getMeasuredWidth() - mDragHandleBounds.width(), 0);
62         }
63     }
64 
65     @Override
applyCompoundDrawables(Drawable icon)66     protected void applyCompoundDrawables(Drawable icon) {
67         // The icon is drawn in a separate view.
68     }
69 
70     @Override
onTouchEvent(MotionEvent ev)71     public boolean onTouchEvent(MotionEvent ev) {
72         if (ev.getAction() == MotionEvent.ACTION_DOWN) {
73             // Ignore clicks on the drag handle (long clicks still start the drag).
74             mShouldPerformClick = !mDragHandleBounds.contains((int) ev.getX(), (int) ev.getY());
75         }
76         return super.onTouchEvent(ev);
77     }
78 
79     @Override
performClick()80     public boolean performClick() {
81         return mShouldPerformClick && super.performClick();
82     }
83 }
84