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; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 33 | private Runnable mOnLayoutListener; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 34 | |
| 35 | public PagedViewGridLayout(Context context, int cellCountX, int cellCountY) { |
| 36 | super(context, null, 0); |
| 37 | mCellCountX = cellCountX; |
| 38 | mCellCountY = cellCountY; |
| 39 | } |
| 40 | |
| 41 | int getCellCountX() { |
| 42 | return mCellCountX; |
| 43 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 44 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 45 | int getCellCountY() { |
| 46 | return mCellCountY; |
| 47 | } |
| 48 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Clears all the key listeners for the individual widgets. |
| 51 | */ |
| 52 | public void resetChildrenOnKeyListeners() { |
| 53 | int childCount = getChildCount(); |
| 54 | for (int j = 0; j < childCount; ++j) { |
| 55 | getChildAt(j).setOnKeyListener(null); |
| 56 | } |
| 57 | } |
| 58 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 59 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 60 | // PagedView currently has issues with different-sized pages since it calculates the |
| 61 | // offset of each page to scroll to before it updates the actual size of each page |
| 62 | // (which can change depending on the content if the contents aren't a fixed size). |
| 63 | // 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] | 64 | int widthSpecSize = Math.min(getSuggestedMinimumWidth(), |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 65 | MeasureSpec.getSize(widthMeasureSpec)); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 66 | int widthSpecMode = MeasureSpec.EXACTLY; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 67 | super.onMeasure(MeasureSpec.makeMeasureSpec(widthSpecSize, widthSpecMode), |
| 68 | heightMeasureSpec); |
| 69 | } |
| 70 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 71 | @Override |
| 72 | protected void onDetachedFromWindow() { |
| 73 | super.onDetachedFromWindow(); |
| 74 | mOnLayoutListener = null; |
| 75 | } |
| 76 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 77 | public void setOnLayoutListener(Runnable r) { |
| 78 | mOnLayoutListener = r; |
| 79 | } |
| 80 | |
| 81 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 82 | super.onLayout(changed, left, top, right, bottom); |
| 83 | if (mOnLayoutListener != null) { |
| 84 | mOnLayoutListener.run(); |
| 85 | } |
| 86 | } |
| 87 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 90 | boolean result = super.onTouchEvent(event); |
| 91 | int count = getPageChildCount(); |
| 92 | if (count > 0) { |
| 93 | // We only intercept the touch if we are tapping in empty space after the final row |
| 94 | View child = getChildOnPageAt(count - 1); |
| 95 | int bottom = child.getBottom(); |
| 96 | result = result || (event.getY() < bottom); |
| 97 | } |
| 98 | return result; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 101 | void destroyHardwareLayer() { |
| 102 | setLayerType(LAYER_TYPE_NONE, null); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 105 | void createHardwareLayer() { |
| 106 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void removeAllViewsOnPage() { |
| 111 | removeAllViews(); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 112 | mOnLayoutListener = null; |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 113 | destroyHardwareLayer(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void removeViewOnPageAt(int index) { |
| 118 | removeViewAt(index); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int getPageChildCount() { |
| 123 | return getChildCount(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public View getChildOnPageAt(int i) { |
| 128 | return getChildAt(i); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int indexOfChildOnPage(View v) { |
| 133 | return indexOfChild(v); |
| 134 | } |
| 135 | |
| 136 | public static class LayoutParams extends FrameLayout.LayoutParams { |
| 137 | public LayoutParams(int width, int height) { |
| 138 | super(width, height); |
| 139 | } |
| 140 | } |
| 141 | } |