blob: eca612776f2122ee22fc74f58905d5f76972eda5 [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
29 * to add or remove items during the iteration.
30 */
31 public static <T> void safeForeach(List<T> list, Consumer<T> c) {
32 for (int i = list.size() - 1; i >= 0; i--) {
33 c.accept(list.get(i));
34 }
35 }
Charles He6a79b0d2017-09-18 09:50:58 +010036
37 /**
38 * Sets the visibility of an UI element according to the DISABLE_* flags in
39 * {@link android.app.StatusBarManager}.
40 */
41 public static class DisableStateTracker implements CommandQueue.Callbacks,
42 View.OnAttachStateChangeListener {
43 private final int mMask1;
44 private final int mMask2;
45 private View mView;
46 private boolean mDisabled;
47
48 public DisableStateTracker(int disableMask, int disable2Mask) {
49 mMask1 = disableMask;
50 mMask2 = disable2Mask;
51 }
52
53 @Override
54 public void onViewAttachedToWindow(View v) {
55 mView = v;
56 SysUiServiceProvider.getComponent(v.getContext(), CommandQueue.class)
57 .addCallbacks(this);
58 }
59
60 @Override
61 public void onViewDetachedFromWindow(View v) {
62 SysUiServiceProvider.getComponent(mView.getContext(), CommandQueue.class)
63 .removeCallbacks(this);
64 mView = null;
65 }
66
67 /**
68 * Sets visibility of this {@link View} given the states passed from
69 * {@link com.android.systemui.statusbar.CommandQueue.Callbacks#disable(int, int)}.
70 */
71 @Override
72 public void disable(int state1, int state2, boolean animate) {
73 final boolean disabled = ((state1 & mMask1) != 0) || ((state2 & mMask2) != 0);
74 if (disabled == mDisabled) return;
75 mDisabled = disabled;
76 mView.setVisibility(disabled ? View.GONE : View.VISIBLE);
77 }
78
79 /** @return {@code true} if and only if this {@link View} is currently disabled */
80 public boolean isDisabled() {
81 return mDisabled;
82 }
83 }
Jason Monkf66d3772017-05-30 14:31:51 -040084}