Jason Monk | f66d377 | 2017-05-30 14:31:51 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.util; |
| 16 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 17 | import android.view.View; |
| 18 | |
| 19 | import com.android.systemui.SysUiServiceProvider; |
| 20 | import com.android.systemui.statusbar.CommandQueue; |
| 21 | |
Jason Monk | f66d377 | 2017-05-30 14:31:51 -0400 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.function.Consumer; |
| 24 | |
| 25 | public class Utils { |
| 26 | |
| 27 | /** |
| 28 | * Allows lambda iteration over a list. It is done in reverse order so it is safe |
Beverly | 94c28e7 | 2018-06-15 11:04:02 -0400 | [diff] [blame] | 29 | * to add or remove items during the iteration. Skips over null items. |
Jason Monk | f66d377 | 2017-05-30 14:31:51 -0400 | [diff] [blame] | 30 | */ |
| 31 | public static <T> void safeForeach(List<T> list, Consumer<T> c) { |
| 32 | for (int i = list.size() - 1; i >= 0; i--) { |
Beverly | 94c28e7 | 2018-06-15 11:04:02 -0400 | [diff] [blame] | 33 | T item = list.get(i); |
| 34 | if (item != null) { |
| 35 | c.accept(item); |
| 36 | } |
Jason Monk | f66d377 | 2017-05-30 14:31:51 -0400 | [diff] [blame] | 37 | } |
| 38 | } |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 39 | |
| 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 Monk | f66d377 | 2017-05-30 14:31:51 -0400 | [diff] [blame] | 87 | } |