Home | History | Annotate | Download | only in stack
      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 
     41     public StackScrollState(ViewGroup hostView) {
     42         mHostView = hostView;
     43         mStateMap = new WeakHashMap<>();
     44     }
     45 
     46     public ViewGroup getHostView() {
     47         return mHostView;
     48     }
     49 
     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 
     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     }
     88 
     89     public ExpandableViewState getViewStateForView(View requestedView) {
     90         return mStateMap.get(requestedView);
     91     }
     92 
     93     public void removeViewStateForView(View child) {
     94         mStateMap.remove(child);
     95     }
     96 
     97     /**
     98      * Apply the properties saved in {@link #mStateMap} to the children of the {@link #mHostView}.
     99      * The properties are only applied if they effectively changed.
    100      */
    101     public void apply() {
    102         int numChildren = mHostView.getChildCount();
    103         for (int i = 0; i < numChildren; i++) {
    104             ExpandableView child = (ExpandableView) mHostView.getChildAt(i);
    105             ExpandableViewState state = mStateMap.get(child);
    106             if (state == null) {
    107                 Log.wtf(CHILD_NOT_FOUND_TAG, "No child state was found when applying this state " +
    108                         "to the hostView");
    109                 continue;
    110             }
    111             if (state.gone) {
    112                 continue;
    113             }
    114             state.applyToView(child);
    115         }
    116     }
    117 }
    118