Home | History | Annotate | Download | only in util
      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 
     17 import android.view.View;
     18 
     19 import com.android.systemui.SysUiServiceProvider;
     20 import com.android.systemui.statusbar.CommandQueue;
     21 
     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
     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     }
     36 
     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     }
     84 }
     85