Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.view.MotionEvent; |
| 21 | import android.view.View; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 22 | import android.widget.FrameLayout; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 23 | import android.widget.GridLayout; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * The grid based layout used strictly for the widget/wallpaper tab of the AppsCustomize pane |
| 27 | */ |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 28 | public class PagedViewGridLayout extends GridLayout implements Page { |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 29 | static final String TAG = "PagedViewGridLayout"; |
| 30 | |
| 31 | private int mCellCountX; |
| 32 | private int mCellCountY; |
| 33 | |
| 34 | public PagedViewGridLayout(Context context, int cellCountX, int cellCountY) { |
| 35 | super(context, null, 0); |
| 36 | mCellCountX = cellCountX; |
| 37 | mCellCountY = cellCountY; |
| 38 | } |
| 39 | |
| 40 | int getCellCountX() { |
| 41 | return mCellCountX; |
| 42 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame^] | 43 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 44 | int getCellCountY() { |
| 45 | return mCellCountY; |
| 46 | } |
| 47 | |
| 48 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 49 | // PagedView currently has issues with different-sized pages since it calculates the |
| 50 | // offset of each page to scroll to before it updates the actual size of each page |
| 51 | // (which can change depending on the content if the contents aren't a fixed size). |
| 52 | // We work around this by having a minimum size on each widget page). |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 53 | int widthSpecSize = Math.min(getSuggestedMinimumWidth(), |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 54 | MeasureSpec.getSize(widthMeasureSpec)); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 55 | int widthSpecMode = MeasureSpec.EXACTLY; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 56 | super.onMeasure(MeasureSpec.makeMeasureSpec(widthSpecSize, widthSpecMode), |
| 57 | heightMeasureSpec); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 62 | boolean result = super.onTouchEvent(event); |
| 63 | int count = getPageChildCount(); |
| 64 | if (count > 0) { |
| 65 | // We only intercept the touch if we are tapping in empty space after the final row |
| 66 | View child = getChildOnPageAt(count - 1); |
| 67 | int bottom = child.getBottom(); |
| 68 | result = result || (event.getY() < bottom); |
| 69 | } |
| 70 | return result; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame^] | 73 | void destroyHardwareLayer() { |
| 74 | setLayerType(LAYER_TYPE_NONE, null); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame^] | 77 | void createHardwareLayer() { |
| 78 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void removeAllViewsOnPage() { |
| 83 | removeAllViews(); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame^] | 84 | destroyHardwareLayer(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void removeViewOnPageAt(int index) { |
| 89 | removeViewAt(index); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int getPageChildCount() { |
| 94 | return getChildCount(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public View getChildOnPageAt(int i) { |
| 99 | return getChildAt(i); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int indexOfChildOnPage(View v) { |
| 104 | return indexOfChild(v); |
| 105 | } |
| 106 | |
| 107 | public static class LayoutParams extends FrameLayout.LayoutParams { |
| 108 | public LayoutParams(int width, int height) { |
| 109 | super(width, height); |
| 110 | } |
| 111 | } |
| 112 | } |