• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.util;
17 
18 import java.util.ArrayList;
19 
20 /**
21  * Utility class to hold a list of runnable
22  */
23 public class RunnableList {
24 
25     private ArrayList<Runnable> mList = null;
26     private boolean mDestroyed = false;
27 
28     /**
29      * Ads a runnable to this list
30      */
add(Runnable runnable)31     public void add(Runnable runnable) {
32         if (runnable == null) {
33             return;
34         }
35         if (mDestroyed) {
36             runnable.run();
37             return;
38         }
39         if (mList == null) {
40             mList = new ArrayList<>();
41         }
42         mList.add(runnable);
43     }
44 
45     /**
46      * Destroys the list, executing any pending callbacks. All new callbacks are
47      * immediately executed
48      */
executeAllAndDestroy()49     public void executeAllAndDestroy() {
50         mDestroyed = true;
51         executeAllAndClear();
52     }
53 
54     /**
55      * Executes all previously added runnable and clears the list
56      */
executeAllAndClear()57     public void executeAllAndClear() {
58         if (mList != null) {
59             ArrayList<Runnable> list = mList;
60             mList = null;
61             int count = list.size();
62             for (int i = 0; i < count; i++) {
63                 list.get(i).run();
64             }
65         }
66     }
67 }
68