Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2017 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.widget;
     18 
     19 import android.animation.PropertyValuesHolder;
     20 import android.content.Context;
     21 import android.graphics.Rect;
     22 import android.util.AttributeSet;
     23 import android.util.Pair;
     24 import android.view.Gravity;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.TextView;
     29 
     30 import com.android.launcher3.Insettable;
     31 import com.android.launcher3.ItemInfo;
     32 import com.android.launcher3.LauncherAppState;
     33 import com.android.launcher3.R;
     34 import com.android.launcher3.Utilities;
     35 import com.android.launcher3.anim.Interpolators;
     36 import com.android.launcher3.model.WidgetItem;
     37 import com.android.launcher3.util.PackageUserKey;
     38 
     39 import java.util.List;
     40 
     41 /**
     42  * Bottom sheet for the "Widgets" system shortcut in the long-press popup.
     43  */
     44 public class WidgetsBottomSheet extends BaseWidgetSheet implements Insettable {
     45 
     46     private static final int DEFAULT_CLOSE_DURATION = 200;
     47     private ItemInfo mOriginalItemInfo;
     48     private Rect mInsets;
     49 
     50     public WidgetsBottomSheet(Context context, AttributeSet attrs) {
     51         this(context, attrs, 0);
     52     }
     53 
     54     public WidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) {
     55         super(context, attrs, defStyleAttr);
     56         setWillNotDraw(false);
     57         mInsets = new Rect();
     58         mContent = this;
     59     }
     60 
     61     @Override
     62     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     63         super.onLayout(changed, l, t, r, b);
     64         setTranslationShift(mTranslationShift);
     65     }
     66 
     67     public void populateAndShow(ItemInfo itemInfo) {
     68         mOriginalItemInfo = itemInfo;
     69         ((TextView) findViewById(R.id.title)).setText(getContext().getString(
     70                 R.string.widgets_bottom_sheet_title, mOriginalItemInfo.title));
     71 
     72         onWidgetsBound();
     73 
     74         mLauncher.getDragLayer().addView(this);
     75         mIsOpen = false;
     76         animateOpen();
     77     }
     78 
     79     @Override
     80     protected void onWidgetsBound() {
     81         List<WidgetItem> widgets = mLauncher.getPopupDataProvider().getWidgetsForPackageUser(
     82                 new PackageUserKey(
     83                         mOriginalItemInfo.getTargetComponent().getPackageName(),
     84                         mOriginalItemInfo.user));
     85 
     86         ViewGroup widgetRow = findViewById(R.id.widgets);
     87         ViewGroup widgetCells = widgetRow.findViewById(R.id.widgets_cell_list);
     88 
     89         widgetCells.removeAllViews();
     90 
     91         for (int i = 0; i < widgets.size(); i++) {
     92             WidgetCell widget = addItemCell(widgetCells);
     93             widget.applyFromCellItem(widgets.get(i), LauncherAppState.getInstance(mLauncher)
     94                     .getWidgetCache());
     95             widget.ensurePreview();
     96             widget.setVisibility(View.VISIBLE);
     97             if (i < widgets.size() - 1) {
     98                 addDivider(widgetCells);
     99             }
    100         }
    101 
    102         if (widgets.size() == 1) {
    103             // If there is only one widget, we want to center it instead of left-align.
    104             WidgetsBottomSheet.LayoutParams params = (WidgetsBottomSheet.LayoutParams)
    105                     widgetRow.getLayoutParams();
    106             params.gravity = Gravity.CENTER_HORIZONTAL;
    107         } else {
    108             // Otherwise, add an empty view to the start as padding (but still scroll edge to edge).
    109             View leftPaddingView = LayoutInflater.from(getContext()).inflate(
    110                     R.layout.widget_list_divider, widgetRow, false);
    111             leftPaddingView.getLayoutParams().width = Utilities.pxFromDp(
    112                     16, getResources().getDisplayMetrics());
    113             widgetCells.addView(leftPaddingView, 0);
    114         }
    115     }
    116 
    117     private void addDivider(ViewGroup parent) {
    118         LayoutInflater.from(getContext()).inflate(R.layout.widget_list_divider, parent, true);
    119     }
    120 
    121     private WidgetCell addItemCell(ViewGroup parent) {
    122         WidgetCell widget = (WidgetCell) LayoutInflater.from(getContext()).inflate(
    123                 R.layout.widget_cell, parent, false);
    124 
    125         widget.setOnClickListener(this);
    126         widget.setOnLongClickListener(this);
    127         widget.setAnimatePreview(false);
    128 
    129         parent.addView(widget);
    130         return widget;
    131     }
    132 
    133     private void animateOpen() {
    134         if (mIsOpen || mOpenCloseAnimator.isRunning()) {
    135             return;
    136         }
    137         mIsOpen = true;
    138         setupNavBarColor();
    139         mOpenCloseAnimator.setValues(
    140                 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
    141         mOpenCloseAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
    142         mOpenCloseAnimator.start();
    143     }
    144 
    145     @Override
    146     protected void handleClose(boolean animate) {
    147         handleClose(animate, DEFAULT_CLOSE_DURATION);
    148     }
    149 
    150     @Override
    151     protected boolean isOfType(@FloatingViewType int type) {
    152         return (type & TYPE_WIDGETS_BOTTOM_SHEET) != 0;
    153     }
    154 
    155     @Override
    156     public void setInsets(Rect insets) {
    157         // Extend behind left, right, and bottom insets.
    158         int leftInset = insets.left - mInsets.left;
    159         int rightInset = insets.right - mInsets.right;
    160         int bottomInset = insets.bottom - mInsets.bottom;
    161         mInsets.set(insets);
    162         setPadding(getPaddingLeft() + leftInset, getPaddingTop(),
    163                 getPaddingRight() + rightInset, getPaddingBottom() + bottomInset);
    164     }
    165 
    166     @Override
    167     protected int getElementsRowCount() {
    168         return 1;
    169     }
    170 
    171     @Override
    172     protected Pair<View, String> getAccessibilityTarget() {
    173         return Pair.create(findViewById(R.id.title),  getContext().getString(
    174                 mIsOpen ? R.string.widgets_list : R.string.widgets_list_closed));
    175     }
    176 }
    177