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
17package com.android.launcher3.shortcuts;
18
19import android.content.Context;
20import android.graphics.Point;
21import android.graphics.Rect;
22import android.text.TextUtils;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.FrameLayout;
26
27import com.android.launcher3.BubbleTextView;
28import com.android.launcher3.Launcher;
29import com.android.launcher3.R;
30import com.android.launcher3.ShortcutInfo;
31import 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 */
37public 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
49    public DeepShortcutView(Context context) {
50        this(context, null, 0);
51    }
52
53    public DeepShortcutView(Context context, AttributeSet attrs) {
54        this(context, attrs, 0);
55    }
56
57    public DeepShortcutView(Context context, AttributeSet attrs, int defStyle) {
58        super(context, attrs, defStyle);
59
60        mPillRect = new Rect();
61    }
62
63    @Override
64    protected void onFinishInflate() {
65        super.onFinishInflate();
66        mBubbleText = findViewById(R.id.bubble_text);
67        mIconView = findViewById(R.id.icon);
68    }
69
70    public BubbleTextView getBubbleText() {
71        return mBubbleText;
72    }
73
74    public void setWillDrawIcon(boolean willDraw) {
75        mIconView.setVisibility(willDraw ? View.VISIBLE : View.INVISIBLE);
76    }
77
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     */
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
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 **/
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     */
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
133    public View getIconView() {
134        return mIconView;
135    }
136}
137