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 
17 package com.android.systemui.statusbar.stack;
18 
19 import android.util.Log;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import com.android.systemui.R;
24 import com.android.systemui.statusbar.ExpandableNotificationRow;
25 import com.android.systemui.statusbar.ExpandableView;
26 
27 import java.util.List;
28 import java.util.WeakHashMap;
29 
30 /**
31  * A state of a {@link com.android.systemui.statusbar.stack.NotificationStackScrollLayout} which
32  * can be applied to a viewGroup.
33  */
34 public class StackScrollState {
35 
36     private static final String CHILD_NOT_FOUND_TAG = "StackScrollStateNoSuchChild";
37 
38     private final ViewGroup mHostView;
39     private WeakHashMap<ExpandableView, ExpandableViewState> mStateMap;
40 
StackScrollState(ViewGroup hostView)41     public StackScrollState(ViewGroup hostView) {
42         mHostView = hostView;
43         mStateMap = new WeakHashMap<>();
44     }
45 
getHostView()46     public ViewGroup getHostView() {
47         return mHostView;
48     }
49 
resetViewStates()50     public void resetViewStates() {
51         int numChildren = mHostView.getChildCount();
52         for (int i = 0; i < numChildren; i++) {
53             ExpandableView child = (ExpandableView) mHostView.getChildAt(i);
54             resetViewState(child);
55 
56             // handling reset for child notifications
57             if (child instanceof ExpandableNotificationRow) {
58                 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
59                 List<ExpandableNotificationRow> children =
60                         row.getNotificationChildren();
61                 if (row.isSummaryWithChildren() && children != null) {
62                     for (ExpandableNotificationRow childRow : children) {
63                         resetViewState(childRow);
64                     }
65                 }
66             }
67         }
68     }
69 
resetViewState(ExpandableView view)70     private void resetViewState(ExpandableView view) {
71         ExpandableViewState viewState = mStateMap.get(view);
72         if (viewState == null) {
73             viewState = view.createNewViewState(this);
74             mStateMap.put(view, viewState);
75         }
76         // initialize with the default values of the view
77         viewState.height = view.getIntrinsicHeight();
78         viewState.gone = view.getVisibility() == View.GONE;
79         viewState.alpha = 1f;
80         viewState.shadowAlpha = 1f;
81         viewState.notGoneIndex = -1;
82         viewState.xTranslation = view.getTranslationX();
83         viewState.hidden = false;
84         viewState.scaleX = view.getScaleX();
85         viewState.scaleY = view.getScaleY();
86         viewState.inShelf = false;
87         viewState.headsUpIsVisible = false;
88     }
89 
getViewStateForView(View requestedView)90     public ExpandableViewState getViewStateForView(View requestedView) {
91         return mStateMap.get(requestedView);
92     }
93 
removeViewStateForView(View child)94     public void removeViewStateForView(View child) {
95         mStateMap.remove(child);
96     }
97 
98     /**
99      * Apply the properties saved in {@link #mStateMap} to the children of the {@link #mHostView}.
100      * The properties are only applied if they effectively changed.
101      */
apply()102     public void apply() {
103         int numChildren = mHostView.getChildCount();
104         for (int i = 0; i < numChildren; i++) {
105             ExpandableView child = (ExpandableView) mHostView.getChildAt(i);
106             ExpandableViewState state = mStateMap.get(child);
107             if (state == null) {
108                 Log.wtf(CHILD_NOT_FOUND_TAG, "No child state was found when applying this state " +
109                         "to the hostView");
110                 continue;
111             }
112             if (state.gone) {
113                 continue;
114             }
115             state.applyToView(child);
116         }
117     }
118 }
119