blob: 01d75934aaa2c5670fb793f5975aa3ef813b64e2 [file] [log] [blame]
Winson Chung4e6a9762011-05-09 11:56:34 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.view.MotionEvent;
21import android.view.View;
Winson Chung4e6a9762011-05-09 11:56:34 -070022import android.widget.FrameLayout;
Winson Chungfd3385f2011-06-15 19:51:24 -070023import android.widget.GridLayout;
Winson Chung4e6a9762011-05-09 11:56:34 -070024
25/**
26 * The grid based layout used strictly for the widget/wallpaper tab of the AppsCustomize pane
27 */
Winson Chungfd3385f2011-06-15 19:51:24 -070028public class PagedViewGridLayout extends GridLayout implements Page {
Winson Chung4e6a9762011-05-09 11:56:34 -070029 static final String TAG = "PagedViewGridLayout";
30
31 private int mCellCountX;
32 private int mCellCountY;
Michael Jurka038f9d82011-11-03 13:50:45 -070033 private Runnable mOnLayoutListener;
Winson Chung4e6a9762011-05-09 11:56:34 -070034
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 Cohen22f823d2011-09-01 17:22:18 -070044
Winson Chung4e6a9762011-05-09 11:56:34 -070045 int getCellCountY() {
46 return mCellCountY;
47 }
48
49 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
50 // PagedView currently has issues with different-sized pages since it calculates the
51 // offset of each page to scroll to before it updates the actual size of each page
52 // (which can change depending on the content if the contents aren't a fixed size).
53 // We work around this by having a minimum size on each widget page).
Winson Chungfd3385f2011-06-15 19:51:24 -070054 int widthSpecSize = Math.min(getSuggestedMinimumWidth(),
Winson Chung4e6a9762011-05-09 11:56:34 -070055 MeasureSpec.getSize(widthMeasureSpec));
Winson Chungfd3385f2011-06-15 19:51:24 -070056 int widthSpecMode = MeasureSpec.EXACTLY;
Winson Chung4e6a9762011-05-09 11:56:34 -070057 super.onMeasure(MeasureSpec.makeMeasureSpec(widthSpecSize, widthSpecMode),
58 heightMeasureSpec);
59 }
60
Michael Jurka038f9d82011-11-03 13:50:45 -070061 public void setOnLayoutListener(Runnable r) {
62 mOnLayoutListener = r;
63 }
64
65 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
66 super.onLayout(changed, left, top, right, bottom);
67 if (mOnLayoutListener != null) {
68 mOnLayoutListener.run();
69 }
70 }
71
Winson Chung4e6a9762011-05-09 11:56:34 -070072 @Override
73 public boolean onTouchEvent(MotionEvent event) {
Winson Chungf0ea4d32011-06-06 14:27:16 -070074 boolean result = super.onTouchEvent(event);
75 int count = getPageChildCount();
76 if (count > 0) {
77 // We only intercept the touch if we are tapping in empty space after the final row
78 View child = getChildOnPageAt(count - 1);
79 int bottom = child.getBottom();
80 result = result || (event.getY() < bottom);
81 }
82 return result;
Winson Chung4e6a9762011-05-09 11:56:34 -070083 }
84
Adam Cohen22f823d2011-09-01 17:22:18 -070085 void destroyHardwareLayer() {
86 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung4e6a9762011-05-09 11:56:34 -070087 }
88
Adam Cohen22f823d2011-09-01 17:22:18 -070089 void createHardwareLayer() {
90 setLayerType(LAYER_TYPE_HARDWARE, null);
Winson Chung4e6a9762011-05-09 11:56:34 -070091 }
92
93 @Override
94 public void removeAllViewsOnPage() {
95 removeAllViews();
Adam Cohen22f823d2011-09-01 17:22:18 -070096 destroyHardwareLayer();
Winson Chung4e6a9762011-05-09 11:56:34 -070097 }
98
99 @Override
100 public void removeViewOnPageAt(int index) {
101 removeViewAt(index);
102 }
103
104 @Override
105 public int getPageChildCount() {
106 return getChildCount();
107 }
108
109 @Override
110 public View getChildOnPageAt(int i) {
111 return getChildAt(i);
112 }
113
114 @Override
115 public int indexOfChildOnPage(View v) {
116 return indexOfChild(v);
117 }
118
119 public static class LayoutParams extends FrameLayout.LayoutParams {
120 public LayoutParams(int width, int height) {
121 super(width, height);
122 }
123 }
124}