1 /* 2 * Copyright (C) 2020 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.systemui.car.window; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import com.android.systemui.CoreStartable; 23 import com.android.systemui.R; 24 import com.android.systemui.dagger.SysUISingleton; 25 26 import java.lang.reflect.Constructor; 27 import java.lang.reflect.InvocationTargetException; 28 import java.util.Map; 29 30 import javax.inject.Inject; 31 import javax.inject.Provider; 32 33 /** 34 * Registers {@link OverlayViewMediator}(s) and synchronizes their calls to hide/show {@link 35 * OverlayViewController}(s) to allow for the correct visibility of system bars. 36 */ 37 @SysUISingleton 38 public class SystemUIOverlayWindowManager implements CoreStartable { 39 private static final String TAG = "SystemUIOverlayWM"; 40 private final Context mContext; 41 private final Map<Class<?>, Provider<OverlayViewMediator>> 42 mContentMediatorCreators; 43 private final OverlayViewGlobalStateController mOverlayViewGlobalStateController; 44 45 @Inject SystemUIOverlayWindowManager( Context context, Map<Class<?>, Provider<OverlayViewMediator>> contentMediatorCreators, OverlayViewGlobalStateController overlayViewGlobalStateController)46 public SystemUIOverlayWindowManager( 47 Context context, 48 Map<Class<?>, Provider<OverlayViewMediator>> contentMediatorCreators, 49 OverlayViewGlobalStateController overlayViewGlobalStateController) { 50 mContext = context; 51 mContentMediatorCreators = contentMediatorCreators; 52 mOverlayViewGlobalStateController = overlayViewGlobalStateController; 53 } 54 55 @Override start()56 public void start() { 57 String[] names = mContext.getResources().getStringArray( 58 R.array.config_carSystemUIOverlayViewsMediators); 59 startServices(names); 60 } 61 startServices(String[] services)62 private void startServices(String[] services) { 63 for (String clsName : services) { 64 long ti = System.currentTimeMillis(); 65 try { 66 OverlayViewMediator obj = resolveContentMediator(clsName); 67 if (obj == null) { 68 Constructor constructor = Class.forName(clsName).getConstructor(Context.class); 69 obj = (OverlayViewMediator) constructor.newInstance(this); 70 } 71 mOverlayViewGlobalStateController.registerMediator(obj); 72 } catch (ClassNotFoundException 73 | NoSuchMethodException 74 | IllegalAccessException 75 | InstantiationException 76 | InvocationTargetException ex) { 77 throw new RuntimeException(ex); 78 } 79 80 // Warn if initialization of component takes too long 81 ti = System.currentTimeMillis() - ti; 82 if (ti > 200) { 83 Log.w(TAG, "Initialization of " + clsName + " took " + ti + " ms"); 84 } 85 } 86 } 87 resolveContentMediator(String className)88 private OverlayViewMediator resolveContentMediator(String className) { 89 return resolve(className, mContentMediatorCreators); 90 } 91 resolve(String className, Map<Class<?>, Provider<T>> creators)92 private <T> T resolve(String className, Map<Class<?>, Provider<T>> creators) { 93 try { 94 Class<?> clazz = Class.forName(className); 95 Provider<T> provider = creators.get(clazz); 96 return provider == null ? null : provider.get(); 97 } catch (ClassNotFoundException e) { 98 return null; 99 } 100 } 101 } 102