1/*
2 * Copyright (C) 2017 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.notification;
18
19import android.view.View;
20import android.view.ViewGroup;
21
22import com.android.internal.annotations.VisibleForTesting;
23import com.android.systemui.statusbar.ExpandableNotificationRow;
24
25/**
26 * An observer that listens to the above shelf state and can notify listeners
27 */
28public class AboveShelfObserver implements AboveShelfChangedListener {
29
30    private final ViewGroup mHostLayout;
31    private boolean mHasViewsAboveShelf = false;
32    private HasViewAboveShelfChangedListener mListener;
33
34    public AboveShelfObserver(ViewGroup hostLayout) {
35        mHostLayout = hostLayout;
36    }
37
38    public void setListener(HasViewAboveShelfChangedListener listener) {
39        mListener = listener;
40    }
41
42    @Override
43    public void onAboveShelfStateChanged(boolean aboveShelf) {
44        boolean hasViewsAboveShelf = aboveShelf;
45        if (!hasViewsAboveShelf && mHostLayout != null) {
46            int n = mHostLayout.getChildCount();
47            for (int i = 0; i < n; i++) {
48                View child = mHostLayout.getChildAt(i);
49                if (child instanceof ExpandableNotificationRow) {
50                    if (((ExpandableNotificationRow) child).isAboveShelf()) {
51                        hasViewsAboveShelf = true;
52                        break;
53                    }
54                }
55            }
56        }
57        if (mHasViewsAboveShelf != hasViewsAboveShelf) {
58            mHasViewsAboveShelf = hasViewsAboveShelf;
59            if (mListener != null) {
60                mListener.onHasViewsAboveShelfChanged(hasViewsAboveShelf);
61            }
62        }
63    }
64
65    @VisibleForTesting
66    boolean hasViewsAboveShelf() {
67        return mHasViewsAboveShelf;
68    }
69
70    public interface HasViewAboveShelfChangedListener {
71        void onHasViewsAboveShelfChanged(boolean hasViewsAboveShelf);
72    }
73}
74