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