1 /*
2  * Copyright (C) 2019 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 
17 package com.android.settings.panel;
18 
19 import android.net.Uri;
20 
21 import androidx.slice.Slice;
22 
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.concurrent.CountDownLatch;
26 
27 /**
28  * Helper class to isolate the work tracking all of the {@link Slice Slices} being loaded.
29  * <p>
30  *     Uses a {@link CountDownLatch} and a {@link Set} of Slices to track how many
31  *     Slices have been loaded. A Slice can only be counted as being loaded a single time, even
32  *     when they get updated later.
33  * <p>
34  *     To use the class, pass the number of expected Slices to load into the constructor. For
35  *     every Slice that loads, call {@link #markSliceLoaded(Uri)} with the corresponding
36  *     {@link Uri}. Then check if all of the Slices have loaded with
37  *     {@link #isPanelReadyToLoad()}, which will return {@code true} the first time after all
38  *     Slices have loaded.
39  *
40  * @deprecated this is no longer used after V and will be removed.
41  */
42 @Deprecated(forRemoval = true)
43 public class PanelSlicesLoaderCountdownLatch {
44     private final Set<Uri> mLoadedSlices;
45     private final CountDownLatch mCountDownLatch;
46     private boolean slicesReadyToLoad = false;
47 
PanelSlicesLoaderCountdownLatch(int countdownSize)48     public PanelSlicesLoaderCountdownLatch(int countdownSize) {
49         mLoadedSlices = new HashSet<>();
50         mCountDownLatch = new CountDownLatch(countdownSize);
51     }
52 
53     /**
54      * Checks if the {@param sliceUri} has been loaded: if not, then decrement the countdown
55      * latch, and if so, then do nothing.
56      */
markSliceLoaded(Uri sliceUri)57     public void markSliceLoaded(Uri sliceUri) {
58         if (mLoadedSlices.contains(sliceUri)) {
59             return;
60         }
61         mLoadedSlices.add(sliceUri);
62         mCountDownLatch.countDown();
63     }
64 
65     /**
66      * @return {@code true} if the Slice has already been loaded.
67      */
isSliceLoaded(Uri uri)68     public boolean isSliceLoaded(Uri uri) {
69         return mLoadedSlices.contains(uri);
70     }
71 
72     /**
73      * @return {@code true} when all Slices have loaded, and the Panel has not yet been loaded.
74      */
isPanelReadyToLoad()75     public boolean isPanelReadyToLoad() {
76         /**
77          * Use {@link slicesReadyToLoad} to track whether or not the Panel has been loaded. We
78          * only want to animate the Panel a single time.
79          */
80         if ((mCountDownLatch.getCount() == 0) && !slicesReadyToLoad) {
81             slicesReadyToLoad = true;
82             return true;
83         }
84         return false;
85     }
86 }