1 /* 2 * Copyright (C) 2018 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; 16 17 import java.util.ArrayList; 18 19 import javax.inject.Inject; 20 import javax.inject.Singleton; 21 22 /** 23 * Created by {@link Dependency} on SystemUI startup. Add tasks which need to be executed only 24 * after all other dependencies have been created. 25 */ 26 @Singleton 27 public class InitController { 28 29 /** 30 * If a task is added after all tasks are executed, then we've done something terribly wrong 31 */ 32 private boolean mTasksExecuted = false; 33 34 private final ArrayList<Runnable> mTasks = new ArrayList<>(); 35 36 @Inject InitController()37 public InitController() { 38 } 39 40 /** 41 * Add a task to be executed after {@link Dependency#start()} 42 * @param runnable the task to be executed 43 */ addPostInitTask(Runnable runnable)44 public void addPostInitTask(Runnable runnable) { 45 if (mTasksExecuted) { 46 throw new IllegalStateException("post init tasks have already been executed!"); 47 } 48 mTasks.add(runnable); 49 } 50 51 /** 52 * Run post-init tasks and remove them from the tasks list 53 */ executePostInitTasks()54 public void executePostInitTasks() { 55 while (!mTasks.isEmpty()) { 56 mTasks.remove(0).run(); 57 } 58 59 mTasksExecuted = true; 60 } 61 } 62