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