NotificationContentView.java revision 4222d9a7fb87d73e1443ec1a2de9782b05741af6
1/* 2 * Copyright (C) 2014 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.systemui.statusbar; 18 19import android.content.Context; 20import android.graphics.Rect; 21import android.util.AttributeSet; 22import android.view.Gravity; 23import android.view.View; 24 25import com.android.systemui.R; 26 27/** 28 * A frame layout containing the actual payload of the notification, including the contracted and 29 * expanded layout. This class is responsible for clipping the content and and switching between the 30 * expanded and contracted view depending on its clipped size. 31 */ 32public class NotificationContentView extends ExpandableView { 33 34 private final Rect mClipBounds = new Rect(); 35 36 private View mContractedChild; 37 private View mExpandedChild; 38 39 private int mSmallHeight; 40 41 public NotificationContentView(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 mSmallHeight = getResources().getDimensionPixelSize(R.dimen.notification_min_height); 44 mActualHeight = mSmallHeight; 45 } 46 47 @Override 48 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 49 super.onLayout(changed, left, top, right, bottom); 50 updateClipping(); 51 } 52 53 public void setContractedChild(View child) { 54 if (mContractedChild != null) { 55 removeView(mContractedChild); 56 } 57 sanitizeContractedLayoutParams(child); 58 addView(child); 59 mContractedChild = child; 60 selectLayout(); 61 } 62 63 public void setExpandedChild(View child) { 64 if (mExpandedChild != null) { 65 removeView(mExpandedChild); 66 } 67 addView(child); 68 mExpandedChild = child; 69 selectLayout(); 70 } 71 72 @Override 73 public void setActualHeight(int actualHeight) { 74 super.setActualHeight(actualHeight); 75 selectLayout(); 76 updateClipping(); 77 } 78 79 @Override 80 public int getMaxHeight() { 81 82 // The maximum height is just the laid out height. 83 return getHeight(); 84 } 85 86 @Override 87 public int getMinHeight() { 88 return mSmallHeight; 89 } 90 91 @Override 92 public void setClipTopAmount(int clipTopAmount) { 93 super.setClipTopAmount(clipTopAmount); 94 updateClipping(); 95 } 96 97 public int getClipTopAmount() { 98 return mClipTopAmount; 99 } 100 101 private void updateClipping() { 102 mClipBounds.set(0, mClipTopAmount, getWidth(), mActualHeight); 103 setClipBounds(mClipBounds); 104 } 105 106 private void sanitizeContractedLayoutParams(View contractedChild) { 107 LayoutParams lp = (LayoutParams) contractedChild.getLayoutParams(); 108 lp.height = mSmallHeight; 109 contractedChild.setLayoutParams(lp); 110 } 111 112 private void selectLayout() { 113 if (mActualHeight <= mSmallHeight || mExpandedChild == null) { 114 if (mContractedChild.getVisibility() != View.VISIBLE) { 115 mContractedChild.setVisibility(View.VISIBLE); 116 } 117 if (mExpandedChild != null && mExpandedChild.getVisibility() != View.INVISIBLE) { 118 mExpandedChild.setVisibility(View.INVISIBLE); 119 } 120 } else { 121 if (mExpandedChild.getVisibility() != View.VISIBLE) { 122 mExpandedChild.setVisibility(View.VISIBLE); 123 } 124 if (mContractedChild.getVisibility() != View.INVISIBLE) { 125 mContractedChild.setVisibility(View.INVISIBLE); 126 } 127 } 128 } 129 130 public void notifyContentUpdated() { 131 selectLayout(); 132 } 133 134 @Override 135 public boolean isContentExpandable() { 136 return mExpandedChild != null; 137 } 138} 139