Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.systemui.statusbar; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 21 | import android.view.View; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 22 | import android.view.ViewGroup; |
| 23 | import android.widget.FrameLayout; |
| 24 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 25 | import com.android.internal.widget.SizeAdaptiveLayout; |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 26 | import com.android.systemui.R; |
| 27 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 28 | public class ExpandableNotificationRow extends FrameLayout { |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 29 | private int mRowMinHeight; |
| 30 | private int mRowMaxHeight; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 31 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 32 | /** Does this row contain layouts that can adapt to row expansion */ |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 33 | private boolean mExpandable; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 34 | /** Has the user actively changed the expansion state of this row */ |
| 35 | private boolean mHasUserChangedExpansion; |
| 36 | /** If {@link #mHasUserChangedExpansion}, has the user expanded this row */ |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 37 | private boolean mUserExpanded; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 38 | /** Is the user touching this row */ |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 39 | private boolean mUserLocked; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 40 | /** Are we showing the "public" version */ |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 41 | private boolean mShowingPublic; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 42 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 43 | private LatestItemView mLatestItemView; |
| 44 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Is this notification expanded by the system. The expansion state can be overridden by the |
| 47 | * user expansion. |
| 48 | */ |
| 49 | private boolean mIsSystemExpanded; |
| 50 | private SizeAdaptiveLayout mPublicLayout; |
| 51 | private SizeAdaptiveLayout mPrivateLayout; |
| 52 | private int mMaxExpandHeight; |
| 53 | private boolean mMaxHeightNeedsUpdate; |
| 54 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 55 | public ExpandableNotificationRow(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | } |
| 58 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 59 | @Override |
| 60 | protected void onFinishInflate() { |
| 61 | super.onFinishInflate(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 62 | mPublicLayout = (SizeAdaptiveLayout) findViewById(R.id.expandedPublic); |
| 63 | mPrivateLayout = (SizeAdaptiveLayout) findViewById(R.id.expanded); |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 64 | mLatestItemView = (LatestItemView) findViewById(R.id.container); |
| 65 | } |
| 66 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 67 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 68 | public void setHeightRange(int rowMinHeight, int rowMaxHeight) { |
| 69 | mRowMinHeight = rowMinHeight; |
| 70 | mRowMaxHeight = rowMaxHeight; |
| 71 | mMaxHeightNeedsUpdate = true; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public boolean isExpandable() { |
| 75 | return mExpandable; |
| 76 | } |
| 77 | |
| 78 | public void setExpandable(boolean expandable) { |
| 79 | mExpandable = expandable; |
| 80 | } |
| 81 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 82 | /** |
| 83 | * @return whether the user has changed the expansion state |
| 84 | */ |
| 85 | public boolean hasUserChangedExpansion() { |
| 86 | return mHasUserChangedExpansion; |
| 87 | } |
| 88 | |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 89 | public boolean isUserExpanded() { |
| 90 | return mUserExpanded; |
| 91 | } |
| 92 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 93 | /** |
| 94 | * Set this notification to be expanded by the user |
| 95 | * |
| 96 | * @param userExpanded whether the user wants this notification to be expanded |
| 97 | */ |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 98 | public void setUserExpanded(boolean userExpanded) { |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 99 | mHasUserChangedExpansion = true; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 100 | mUserExpanded = userExpanded; |
| 101 | } |
| 102 | |
| 103 | public boolean isUserLocked() { |
| 104 | return mUserLocked; |
| 105 | } |
| 106 | |
| 107 | public void setUserLocked(boolean userLocked) { |
| 108 | mUserLocked = userLocked; |
| 109 | } |
| 110 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 111 | /** |
| 112 | * @return has the system set this notification to be expanded |
| 113 | */ |
| 114 | public boolean isSystemExpanded() { |
| 115 | return mIsSystemExpanded; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Set this notification to be expanded by the system. |
| 120 | * |
| 121 | * @param expand whether the system wants this notification to be expanded. |
| 122 | */ |
| 123 | public void setSystemExpanded(boolean expand) { |
| 124 | mIsSystemExpanded = expand; |
| 125 | applyExpansionToLayout(expand); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Apply an expansion state to the layout. |
| 130 | * |
| 131 | * @param expand should the layout be in the expanded state |
| 132 | */ |
| 133 | public void applyExpansionToLayout(boolean expand) { |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 134 | ViewGroup.LayoutParams lp = getLayoutParams(); |
| 135 | if (expand && mExpandable) { |
| 136 | lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 137 | } else { |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 138 | lp.height = mRowMinHeight; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 139 | } |
| 140 | setLayoutParams(lp); |
| 141 | } |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 142 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 143 | /** |
| 144 | * If {@link #isExpanded()} then this is the greatest possible height this view can |
| 145 | * get and otherwise it is {@link #mRowMinHeight}. |
| 146 | * |
| 147 | * @return the maximum allowed expansion height of this view. |
| 148 | */ |
| 149 | public int getMaximumAllowedExpandHeight() { |
| 150 | boolean inExpansionState = isExpanded(); |
| 151 | if (!inExpansionState) { |
| 152 | // not expanded, so we return the collapsed size |
| 153 | return mRowMinHeight; |
| 154 | } |
| 155 | |
| 156 | return mShowingPublic ? mRowMinHeight : getMaxExpandHeight(); |
| 157 | } |
| 158 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 159 | private void updateMaxExpandHeight() { |
| 160 | ViewGroup.LayoutParams lp = getLayoutParams(); |
| 161 | int oldHeight = lp.height; |
| 162 | lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 163 | setLayoutParams(lp); |
| 164 | measure(View.MeasureSpec.makeMeasureSpec(getMeasuredWidth(), View.MeasureSpec.EXACTLY), |
| 165 | View.MeasureSpec.makeMeasureSpec(mRowMaxHeight, View.MeasureSpec.AT_MOST)); |
| 166 | lp.height = oldHeight; |
| 167 | setLayoutParams(lp); |
| 168 | mMaxExpandHeight = getMeasuredHeight(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Check whether the view state is currently expanded. This is given by the system in {@link |
| 173 | * #setSystemExpanded(boolean)} and can be overridden by user expansion or |
| 174 | * collapsing in {@link #setUserExpanded(boolean)}. Note that the visual appearance of this |
| 175 | * view can differ from this state, if layout params are modified from outside. |
| 176 | * |
| 177 | * @return whether the view state is currently expanded. |
| 178 | */ |
| 179 | private boolean isExpanded() { |
| 180 | return !hasUserChangedExpansion() && isSystemExpanded() || isUserExpanded(); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 185 | super.onLayout(changed, left, top, right, bottom); |
| 186 | mMaxHeightNeedsUpdate = true; |
| 187 | } |
| 188 | |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 189 | public void setShowingPublic(boolean show) { |
| 190 | mShowingPublic = show; |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 191 | |
| 192 | // bail out if no public version |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 193 | if (mPublicLayout.getChildCount() == 0) return; |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 194 | |
| 195 | // TODO: animation? |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 196 | mPublicLayout.setVisibility(show ? View.VISIBLE : View.GONE); |
| 197 | mPrivateLayout.setVisibility(show ? View.GONE : View.VISIBLE); |
Dan Sandler | a5e0f41 | 2014-01-23 15:11:54 -0500 | [diff] [blame] | 198 | } |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Sets the notification as dimmed, meaning that it will appear in a more gray variant. |
| 202 | */ |
| 203 | public void setDimmed(boolean dimmed) { |
| 204 | mLatestItemView.setDimmed(dimmed); |
| 205 | } |
| 206 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 207 | public int getMaxExpandHeight() { |
| 208 | if (mMaxHeightNeedsUpdate) { |
| 209 | updateMaxExpandHeight(); |
| 210 | mMaxHeightNeedsUpdate = false; |
| 211 | } |
| 212 | return mMaxExpandHeight; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 213 | } |
Jorim Jaggi | 584a7aa | 2014-04-10 23:26:13 +0200 | [diff] [blame^] | 214 | |
Jorim Jaggi | 251957d | 2014-04-09 04:24:09 +0200 | [diff] [blame] | 215 | /** |
| 216 | * Sets the notification as locked. In the locked state, the first tap will produce a quantum |
| 217 | * ripple to make the notification brighter and only the second tap will cause a click. |
| 218 | */ |
| 219 | public void setLocked(boolean locked) { |
| 220 | mLatestItemView.setLocked(locked); |
| 221 | } |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 222 | } |