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