Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2010 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.graphics.Rect;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 
     24 /**
     25  * An abstraction of the original CellLayout which supports laying out items
     26  * which span multiple cells into a grid-like layout.  Also supports dimming
     27  * to give a preview of its contents.
     28  */
     29 public class PagedViewCellLayoutChildren extends ViewGroup {
     30     static final String TAG = "PagedViewCellLayout";
     31 
     32     private boolean mCenterContent;
     33 
     34     private int mCellWidth;
     35     private int mCellHeight;
     36     private int mWidthGap;
     37     private int mHeightGap;
     38 
     39     public PagedViewCellLayoutChildren(Context context) {
     40         super(context);
     41     }
     42 
     43     @Override
     44     public void cancelLongPress() {
     45         super.cancelLongPress();
     46 
     47         // Cancel long press for all children
     48         final int count = getChildCount();
     49         for (int i = 0; i < count; i++) {
     50             final View child = getChildAt(i);
     51             child.cancelLongPress();
     52         }
     53     }
     54 
     55     public void setGap(int widthGap, int heightGap) {
     56         mWidthGap = widthGap;
     57         mHeightGap = heightGap;
     58         requestLayout();
     59     }
     60 
     61     public void setCellDimensions(int width, int height) {
     62         mCellWidth = width;
     63         mCellHeight = height;
     64         requestLayout();
     65     }
     66 
     67     @Override
     68     public void requestChildFocus(View child, View focused) {
     69         super.requestChildFocus(child, focused);
     70         if (child != null) {
     71             Rect r = new Rect();
     72             child.getDrawingRect(r);
     73             requestRectangleOnScreen(r);
     74         }
     75     }
     76 
     77     @Override
     78     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     79         int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
     80         int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
     81 
     82         int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
     83         int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
     84 
     85         if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
     86             throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
     87         }
     88 
     89         final int count = getChildCount();
     90         for (int i = 0; i < count; i++) {
     91             View child = getChildAt(i);
     92             PagedViewCellLayout.LayoutParams lp =
     93                 (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
     94             lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
     95                     getPaddingLeft(),
     96                     getPaddingTop());
     97 
     98             int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width,
     99                     MeasureSpec.EXACTLY);
    100             int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
    101                     MeasureSpec.EXACTLY);
    102 
    103             child.measure(childWidthMeasureSpec, childheightMeasureSpec);
    104         }
    105 
    106         setMeasuredDimension(widthSpecSize, heightSpecSize);
    107     }
    108 
    109     @Override
    110     protected void onLayout(boolean changed, int l, int t, int r, int b) {
    111         int count = getChildCount();
    112 
    113         int offsetX = 0;
    114         if (mCenterContent && count > 0) {
    115             // determine the max width of all the rows and center accordingly
    116             int maxRowX = 0;
    117             int minRowX = Integer.MAX_VALUE;
    118             for (int i = 0; i < count; i++) {
    119                 View child = getChildAt(i);
    120                 if (child.getVisibility() != GONE) {
    121                     PagedViewCellLayout.LayoutParams lp =
    122                         (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
    123                     minRowX = Math.min(minRowX, lp.x);
    124                     maxRowX = Math.max(maxRowX, lp.x + lp.width);
    125                 }
    126             }
    127             int maxRowWidth = maxRowX - minRowX;
    128             offsetX = (getMeasuredWidth() - maxRowWidth) / 2;
    129         }
    130 
    131         for (int i = 0; i < count; i++) {
    132             View child = getChildAt(i);
    133             if (child.getVisibility() != GONE) {
    134                 PagedViewCellLayout.LayoutParams lp =
    135                     (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
    136 
    137                 int childLeft = offsetX + lp.x;
    138                 int childTop = lp.y;
    139                 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
    140             }
    141         }
    142     }
    143 
    144     public void enableCenteredContent(boolean enabled) {
    145         mCenterContent = enabled;
    146     }
    147 
    148     @Override
    149     protected void setChildrenDrawingCacheEnabled(boolean enabled) {
    150         final int count = getChildCount();
    151         for (int i = 0; i < count; i++) {
    152             final View view = getChildAt(i);
    153             view.setDrawingCacheEnabled(enabled);
    154             // Update the drawing caches
    155             if (!view.isHardwareAccelerated()) {
    156                 view.buildDrawingCache(true);
    157             }
    158         }
    159     }
    160 }
    161