• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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;
18 
19 import static android.view.MotionEvent.ACTION_DOWN;
20 
21 import android.app.WallpaperManager;
22 import android.content.Context;
23 import android.graphics.Rect;
24 import android.view.MotionEvent;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import com.android.launcher3.CellLayout.ContainerType;
29 import com.android.launcher3.widget.LauncherAppWidgetHostView;
30 
31 public class ShortcutAndWidgetContainer extends ViewGroup {
32     static final String TAG = "ShortcutAndWidgetContainer";
33 
34     // These are temporary variables to prevent having to allocate a new object just to
35     // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
36     private final int[] mTmpCellXY = new int[2];
37 
38     @ContainerType private final int mContainerType;
39     private final WallpaperManager mWallpaperManager;
40 
41     private int mCellWidth;
42     private int mCellHeight;
43 
44     private int mCountX;
45 
46     private Launcher mLauncher;
47     private boolean mInvertIfRtl = false;
48 
ShortcutAndWidgetContainer(Context context, @ContainerType int containerType)49     public ShortcutAndWidgetContainer(Context context, @ContainerType int containerType) {
50         super(context);
51         mLauncher = Launcher.getLauncher(context);
52         mWallpaperManager = WallpaperManager.getInstance(context);
53         mContainerType = containerType;
54     }
55 
setCellDimensions(int cellWidth, int cellHeight, int countX, int countY)56     public void setCellDimensions(int cellWidth, int cellHeight, int countX, int countY) {
57         mCellWidth = cellWidth;
58         mCellHeight = cellHeight;
59         mCountX = countX;
60     }
61 
getChildAt(int x, int y)62     public View getChildAt(int x, int y) {
63         final int count = getChildCount();
64         for (int i = 0; i < count; i++) {
65             View child = getChildAt(i);
66             CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
67 
68             if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
69                     (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
70                 return child;
71             }
72         }
73         return null;
74     }
75 
76     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)77     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
78         int count = getChildCount();
79 
80         int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
81         int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
82         setMeasuredDimension(widthSpecSize, heightSpecSize);
83 
84         for (int i = 0; i < count; i++) {
85             View child = getChildAt(i);
86             if (child.getVisibility() != GONE) {
87                 measureChild(child);
88             }
89         }
90     }
91 
setupLp(View child)92     public void setupLp(View child) {
93         CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
94         if (child instanceof LauncherAppWidgetHostView) {
95             DeviceProfile profile = mLauncher.getDeviceProfile();
96             lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
97                     profile.appWidgetScale.x, profile.appWidgetScale.y);
98         } else {
99             lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX);
100         }
101     }
102 
103     // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
setInvertIfRtl(boolean invert)104     public void setInvertIfRtl(boolean invert) {
105         mInvertIfRtl = invert;
106     }
107 
getCellContentHeight()108     public int getCellContentHeight() {
109         return Math.min(getMeasuredHeight(),
110                 mLauncher.getDeviceProfile().getCellHeight(mContainerType));
111     }
112 
measureChild(View child)113     public void measureChild(View child) {
114         CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
115         final DeviceProfile profile = mLauncher.getDeviceProfile();
116 
117         if (child instanceof LauncherAppWidgetHostView) {
118             lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
119                     profile.appWidgetScale.x, profile.appWidgetScale.y);
120             // Widgets have their own padding
121         } else {
122             lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX);
123             // Center the icon/folder
124             int cHeight = getCellContentHeight();
125             int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
126             int cellPaddingX = mContainerType == CellLayout.WORKSPACE
127                     ? profile.workspaceCellPaddingXPx
128                     : (int) (profile.edgeMarginPx / 2f);
129             child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
130         }
131         int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
132         int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
133         child.measure(childWidthMeasureSpec, childheightMeasureSpec);
134     }
135 
invertLayoutHorizontally()136     public boolean invertLayoutHorizontally() {
137         return mInvertIfRtl && Utilities.isRtl(getResources());
138     }
139 
140     @Override
onLayout(boolean changed, int l, int t, int r, int b)141     protected void onLayout(boolean changed, int l, int t, int r, int b) {
142         int count = getChildCount();
143         for (int i = 0; i < count; i++) {
144             final View child = getChildAt(i);
145             if (child.getVisibility() != GONE) {
146                 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
147 
148                 if (child instanceof LauncherAppWidgetHostView) {
149                     LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
150 
151                     // Scale and center the widget to fit within its cells.
152                     DeviceProfile profile = mLauncher.getDeviceProfile();
153                     float scaleX = profile.appWidgetScale.x;
154                     float scaleY = profile.appWidgetScale.y;
155 
156                     lahv.setScaleToFit(Math.min(scaleX, scaleY));
157                     lahv.setTranslationForCentering(-(lp.width - (lp.width * scaleX)) / 2.0f,
158                             -(lp.height - (lp.height * scaleY)) / 2.0f);
159                 }
160 
161                 int childLeft = lp.x;
162                 int childTop = lp.y;
163                 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
164 
165                 if (lp.dropped) {
166                     lp.dropped = false;
167 
168                     final int[] cellXY = mTmpCellXY;
169                     getLocationOnScreen(cellXY);
170                     mWallpaperManager.sendWallpaperCommand(getWindowToken(),
171                             WallpaperManager.COMMAND_DROP,
172                             cellXY[0] + childLeft + lp.width / 2,
173                             cellXY[1] + childTop + lp.height / 2, 0, null);
174                 }
175             }
176         }
177     }
178 
179     @Override
onInterceptTouchEvent(MotionEvent ev)180     public boolean onInterceptTouchEvent(MotionEvent ev) {
181         if (ev.getAction() == ACTION_DOWN && getAlpha() == 0) {
182             // Dont let children handle touch, if we are not visible.
183             return true;
184         }
185         return super.onInterceptTouchEvent(ev);
186     }
187 
188     @Override
shouldDelayChildPressedState()189     public boolean shouldDelayChildPressedState() {
190         return false;
191     }
192 
193     @Override
requestChildFocus(View child, View focused)194     public void requestChildFocus(View child, View focused) {
195         super.requestChildFocus(child, focused);
196         if (child != null) {
197             Rect r = new Rect();
198             child.getDrawingRect(r);
199             requestRectangleOnScreen(r);
200         }
201     }
202 
203     @Override
cancelLongPress()204     public void cancelLongPress() {
205         super.cancelLongPress();
206 
207         // Cancel long press for all children
208         final int count = getChildCount();
209         for (int i = 0; i < count; i++) {
210             final View child = getChildAt(i);
211             child.cancelLongPress();
212         }
213     }
214 }
215