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