blob: 6812410c851cf4faee4464e25c39d928c65b25f7 [file] [log] [blame]
Jason Monkf66d3772017-05-30 14:31:51 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.util;
16
Charles He6a79b0d2017-09-18 09:50:58 +010017import android.view.View;
18
19import com.android.systemui.SysUiServiceProvider;
20import com.android.systemui.statusbar.CommandQueue;
21
Jason Monkf66d3772017-05-30 14:31:51 -040022import java.util.List;
23import java.util.function.Consumer;
24
25public class Utils {
26
27 /**
28 * Allows lambda iteration over a list. It is done in reverse order so it is safe
Beverly94c28e72018-06-15 11:04:02 -040029 * to add or remove items during the iteration. Skips over null items.
Jason Monkf66d3772017-05-30 14:31:51 -040030 */
31 public static <T> void safeForeach(List<T> list, Consumer<T> c) {
32 for (int i = list.size() - 1; i >= 0; i--) {
Beverly94c28e72018-06-15 11:04:02 -040033 T item = list.get(i);
34 if (item != null) {
35 c.accept(item);
36 }
Jason Monkf66d3772017-05-30 14:31:51 -040037 }
38 }
Charles He6a79b0d2017-09-18 09:50:58 +010039
40 /**
41 * Sets the visibility of an UI element according to the DISABLE_* flags in
42 * {@link android.app.StatusBarManager}.
43 */
44 public static class DisableStateTracker implements CommandQueue.Callbacks,
45 View.OnAttachStateChangeListener {
46 private final int mMask1;
47 private final int mMask2;
48 private View mView;
49 private boolean mDisabled;
50
51 public DisableStateTracker(int disableMask, int disable2Mask) {
52 mMask1 = disableMask;
53 mMask2 = disable2Mask;
54 }
55
56 @Override
57 public void onViewAttachedToWindow(View v) {
58 mView = v;
59 SysUiServiceProvider.getComponent(v.getContext(), CommandQueue.class)
60 .addCallbacks(this);
61 }
62
63 @Override
64 public void onViewDetachedFromWindow(View v) {
65 SysUiServiceProvider.getComponent(mView.getContext(), CommandQueue.class)
66 .removeCallbacks(this);
67 mView = null;
68 }
69
70 /**
71 * Sets visibility of this {@link View} given the states passed from
72 * {@link com.android.systemui.statusbar.CommandQueue.Callbacks#disable(int, int)}.
73 */
74 @Override
75 public void disable(int state1, int state2, boolean animate) {
76 final boolean disabled = ((state1 & mMask1) != 0) || ((state2 & mMask2) != 0);
77 if (disabled == mDisabled) return;
78 mDisabled = disabled;
79 mView.setVisibility(disabled ? View.GONE : View.VISIBLE);
80 }
81
82 /** @return {@code true} if and only if this {@link View} is currently disabled */
83 public boolean isDisabled() {
84 return mDisabled;
85 }
86 }
Jason Monkf66d3772017-05-30 14:31:51 -040087}