NotificationContentView.java revision be565dfc1c17b7ddafa9753851b8f82849fd3f42
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 void setClipTopAmount(int clipTopAmount) { 88 super.setClipTopAmount(clipTopAmount); 89 updateClipping(); 90 } 91 92 public int getClipTopAmount() { 93 return mClipTopAmount; 94 } 95 96 private void updateClipping() { 97 mClipBounds.set(0, mClipTopAmount, getWidth(), mActualHeight); 98 setClipBounds(mClipBounds); 99 } 100 101 private void sanitizeContractedLayoutParams(View contractedChild) { 102 LayoutParams lp = (LayoutParams) contractedChild.getLayoutParams(); 103 lp.height = mSmallHeight; 104 contractedChild.setLayoutParams(lp); 105 } 106 107 private void selectLayout() { 108 if (mActualHeight <= mSmallHeight || mExpandedChild == null) { 109 if (mContractedChild.getVisibility() != View.VISIBLE) { 110 mContractedChild.setVisibility(View.VISIBLE); 111 } 112 if (mExpandedChild != null && mExpandedChild.getVisibility() != View.INVISIBLE) { 113 mExpandedChild.setVisibility(View.INVISIBLE); 114 } 115 } else { 116 if (mExpandedChild.getVisibility() != View.VISIBLE) { 117 mExpandedChild.setVisibility(View.VISIBLE); 118 } 119 if (mContractedChild.getVisibility() != View.INVISIBLE) { 120 mContractedChild.setVisibility(View.INVISIBLE); 121 } 122 } 123 } 124 125 public void notifyContentUpdated() { 126 selectLayout(); 127 } 128} 129