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