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 import android.widget.Toast; 26 27 import com.android.launcher3.BubbleTextView; 28 import com.android.launcher3.R; 29 import com.android.launcher3.Utilities; 30 31 /** 32 * A {@link BubbleTextView} that has the shortcut icon on the left and drag handle on the right. 33 */ 34 public class DeepShortcutTextView extends BubbleTextView { 35 private final Rect mDragHandleBounds = new Rect(); 36 private final int mDragHandleWidth; 37 private boolean mShowInstructionToast = false; 38 39 private Toast mInstructionToast; 40 DeepShortcutTextView(Context context)41 public DeepShortcutTextView(Context context) { 42 this(context, null, 0); 43 } 44 DeepShortcutTextView(Context context, AttributeSet attrs)45 public DeepShortcutTextView(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 DeepShortcutTextView(Context context, AttributeSet attrs, int defStyle)49 public DeepShortcutTextView(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 52 Resources resources = getResources(); 53 mDragHandleWidth = resources.getDimensionPixelSize(R.dimen.popup_padding_end) 54 + resources.getDimensionPixelSize(R.dimen.deep_shortcut_drag_handle_size) 55 + resources.getDimensionPixelSize(R.dimen.deep_shortcut_drawable_padding) / 2; 56 } 57 58 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)59 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 60 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 61 62 mDragHandleBounds.set(0, 0, mDragHandleWidth, getMeasuredHeight()); 63 if (!Utilities.isRtl(getResources())) { 64 mDragHandleBounds.offset(getMeasuredWidth() - mDragHandleBounds.width(), 0); 65 } 66 } 67 68 @Override applyCompoundDrawables(Drawable icon)69 protected void applyCompoundDrawables(Drawable icon) { 70 // The icon is drawn in a separate view. 71 } 72 73 @Override onTouchEvent(MotionEvent ev)74 public boolean onTouchEvent(MotionEvent ev) { 75 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 76 // Show toast if user touches the drag handle (long clicks still start the drag). 77 mShowInstructionToast = mDragHandleBounds.contains((int) ev.getX(), (int) ev.getY()); 78 } 79 return super.onTouchEvent(ev); 80 } 81 82 @Override performClick()83 public boolean performClick() { 84 if (mShowInstructionToast) { 85 showToast(); 86 return true; 87 } 88 return super.performClick(); 89 } 90 showToast()91 private void showToast() { 92 if (mInstructionToast != null) { 93 mInstructionToast.cancel(); 94 } 95 CharSequence msg = Utilities.wrapForTts( 96 getContext().getText(R.string.long_press_shortcut_to_add), 97 getContext().getString(R.string.long_accessible_way_to_add_shortcut)); 98 mInstructionToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT); 99 mInstructionToast.show(); 100 } 101 } 102