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 package com.android.launcher3.widget; 17 18 import static com.android.launcher3.logging.LoggerUtils.newContainerTarget; 19 20 import android.content.Context; 21 import android.graphics.Point; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.view.View.OnLongClickListener; 26 import android.widget.Toast; 27 28 import com.android.launcher3.DragSource; 29 import com.android.launcher3.DropTarget.DragObject; 30 import com.android.launcher3.ItemInfo; 31 import com.android.launcher3.R; 32 import com.android.launcher3.Utilities; 33 import com.android.launcher3.dragndrop.DragOptions; 34 import com.android.launcher3.graphics.ColorScrim; 35 import com.android.launcher3.touch.ItemLongClickListener; 36 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; 37 import com.android.launcher3.userevent.nano.LauncherLogProto.Target; 38 import com.android.launcher3.util.SystemUiController; 39 import com.android.launcher3.util.Themes; 40 import com.android.launcher3.views.AbstractSlideInView; 41 42 /** 43 * Base class for various widgets popup 44 */ 45 abstract class BaseWidgetSheet extends AbstractSlideInView 46 implements OnClickListener, OnLongClickListener, DragSource { 47 48 49 /* Touch handling related member variables. */ 50 private Toast mWidgetInstructionToast; 51 52 protected final ColorScrim mColorScrim; 53 BaseWidgetSheet(Context context, AttributeSet attrs, int defStyleAttr)54 public BaseWidgetSheet(Context context, AttributeSet attrs, int defStyleAttr) { 55 super(context, attrs, defStyleAttr); 56 mColorScrim = ColorScrim.createExtractedColorScrim(this); 57 } 58 59 @Override onClick(View v)60 public final void onClick(View v) { 61 // Let the user know that they have to long press to add a widget 62 if (mWidgetInstructionToast != null) { 63 mWidgetInstructionToast.cancel(); 64 } 65 66 CharSequence msg = Utilities.wrapForTts( 67 getContext().getText(R.string.long_press_widget_to_add), 68 getContext().getString(R.string.long_accessible_way_to_add)); 69 mWidgetInstructionToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT); 70 mWidgetInstructionToast.show(); 71 } 72 73 @Override onLongClick(View v)74 public final boolean onLongClick(View v) { 75 if (!ItemLongClickListener.canStartDrag(mLauncher)) return false; 76 77 if (v instanceof WidgetCell) { 78 return beginDraggingWidget((WidgetCell) v); 79 } 80 return true; 81 } 82 setTranslationShift(float translationShift)83 protected void setTranslationShift(float translationShift) { 84 super.setTranslationShift(translationShift); 85 mColorScrim.setProgress(1 - mTranslationShift); 86 } 87 beginDraggingWidget(WidgetCell v)88 private boolean beginDraggingWidget(WidgetCell v) { 89 // Get the widget preview as the drag representation 90 WidgetImageView image = v.getWidgetView(); 91 92 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and 93 // we abort the drag. 94 if (image.getBitmap() == null) { 95 return false; 96 } 97 98 int[] loc = new int[2]; 99 mLauncher.getDragLayer().getLocationInDragLayer(image, loc); 100 101 new PendingItemDragHelper(v).startDrag( 102 image.getBitmapBounds(), image.getBitmap().getWidth(), image.getWidth(), 103 new Point(loc[0], loc[1]), this, new DragOptions()); 104 close(true); 105 return true; 106 } 107 108 // 109 // Drag related handling methods that implement {@link DragSource} interface. 110 // 111 112 @Override onDropCompleted(View target, DragObject d, boolean success)113 public void onDropCompleted(View target, DragObject d, boolean success) { } 114 115 onCloseComplete()116 protected void onCloseComplete() { 117 super.onCloseComplete(); 118 clearNavBarColor(); 119 } 120 clearNavBarColor()121 protected void clearNavBarColor() { 122 mLauncher.getSystemUiController().updateUiState( 123 SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0); 124 } 125 setupNavBarColor()126 protected void setupNavBarColor() { 127 boolean isSheetDark = Themes.getAttrBoolean(mLauncher, R.attr.isMainColorDark); 128 mLauncher.getSystemUiController().updateUiState( 129 SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 130 isSheetDark ? SystemUiController.FLAG_DARK_NAV : SystemUiController.FLAG_LIGHT_NAV); 131 } 132 133 @Override fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent)134 public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) { 135 targetParent.containerType = ContainerType.WIDGETS; 136 targetParent.cardinality = getElementsRowCount(); 137 } 138 139 @Override logActionCommand(int command)140 public final void logActionCommand(int command) { 141 Target target = newContainerTarget(ContainerType.WIDGETS); 142 target.cardinality = getElementsRowCount(); 143 mLauncher.getUserEventDispatcher().logActionCommand(command, target); 144 } 145 getElementsRowCount()146 protected abstract int getElementsRowCount(); 147 148 } 149