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.graphics.Point; 21 import android.graphics.Rect; 22 import android.text.TextUtils; 23 import android.util.AttributeSet; 24 import android.view.View; 25 import android.widget.FrameLayout; 26 27 import com.android.launcher3.BubbleTextView; 28 import com.android.launcher3.Launcher; 29 import com.android.launcher3.R; 30 import com.android.launcher3.ShortcutInfo; 31 import com.android.launcher3.Utilities; 32 33 /** 34 * A {@link android.widget.FrameLayout} that contains a {@link DeepShortcutView}. 35 * This lets us animate the DeepShortcutView (icon and text) separately from the background. 36 */ 37 public class DeepShortcutView extends FrameLayout { 38 39 private static final Point sTempPoint = new Point(); 40 41 private final Rect mPillRect; 42 43 private BubbleTextView mBubbleText; 44 private View mIconView; 45 46 private ShortcutInfo mInfo; 47 private ShortcutInfoCompat mDetail; 48 DeepShortcutView(Context context)49 public DeepShortcutView(Context context) { 50 this(context, null, 0); 51 } 52 DeepShortcutView(Context context, AttributeSet attrs)53 public DeepShortcutView(Context context, AttributeSet attrs) { 54 this(context, attrs, 0); 55 } 56 DeepShortcutView(Context context, AttributeSet attrs, int defStyle)57 public DeepShortcutView(Context context, AttributeSet attrs, int defStyle) { 58 super(context, attrs, defStyle); 59 60 mPillRect = new Rect(); 61 } 62 63 @Override onFinishInflate()64 protected void onFinishInflate() { 65 super.onFinishInflate(); 66 mBubbleText = findViewById(R.id.bubble_text); 67 mIconView = findViewById(R.id.icon); 68 } 69 getBubbleText()70 public BubbleTextView getBubbleText() { 71 return mBubbleText; 72 } 73 setWillDrawIcon(boolean willDraw)74 public void setWillDrawIcon(boolean willDraw) { 75 mIconView.setVisibility(willDraw ? View.VISIBLE : View.INVISIBLE); 76 } 77 willDrawIcon()78 public boolean willDrawIcon() { 79 return mIconView.getVisibility() == View.VISIBLE; 80 } 81 82 /** 83 * Returns the position of the center of the icon relative to the container. 84 */ getIconCenter()85 public Point getIconCenter() { 86 sTempPoint.y = sTempPoint.x = getMeasuredHeight() / 2; 87 if (Utilities.isRtl(getResources())) { 88 sTempPoint.x = getMeasuredWidth() - sTempPoint.x; 89 } 90 return sTempPoint; 91 } 92 93 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)94 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 95 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 96 mPillRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight()); 97 } 98 99 /** package private **/ applyShortcutInfo(ShortcutInfo info, ShortcutInfoCompat detail, ShortcutsItemView container)100 public void applyShortcutInfo(ShortcutInfo info, ShortcutInfoCompat detail, 101 ShortcutsItemView container) { 102 mInfo = info; 103 mDetail = detail; 104 mBubbleText.applyFromShortcutInfo(info); 105 mIconView.setBackground(mBubbleText.getIcon()); 106 107 // Use the long label as long as it exists and fits. 108 CharSequence longLabel = mDetail.getLongLabel(); 109 int availableWidth = mBubbleText.getWidth() - mBubbleText.getTotalPaddingLeft() 110 - mBubbleText.getTotalPaddingRight(); 111 boolean usingLongLabel = !TextUtils.isEmpty(longLabel) 112 && mBubbleText.getPaint().measureText(longLabel.toString()) <= availableWidth; 113 mBubbleText.setText(usingLongLabel ? longLabel : mDetail.getShortLabel()); 114 115 // TODO: Add the click handler to this view directly and not the child view. 116 mBubbleText.setOnClickListener(Launcher.getLauncher(getContext())); 117 mBubbleText.setOnLongClickListener(container); 118 mBubbleText.setOnTouchListener(container); 119 } 120 121 /** 122 * Returns the shortcut info that is suitable to be added on the homescreen 123 */ getFinalInfo()124 public ShortcutInfo getFinalInfo() { 125 final ShortcutInfo badged = new ShortcutInfo(mInfo); 126 // Queue an update task on the worker thread. This ensures that the badged 127 // shortcut eventually gets its icon updated. 128 Launcher.getLauncher(getContext()).getModel() 129 .updateAndBindShortcutInfo(badged, mDetail); 130 return badged; 131 } 132 getIconView()133 public View getIconView() { 134 return mIconView; 135 } 136 } 137