1 /* 2 * Copyright (C) 2016 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.fragments; 16 17 import android.app.Fragment; 18 import android.content.res.Configuration; 19 import android.os.Handler; 20 import android.util.ArrayMap; 21 import android.util.Log; 22 import android.view.View; 23 24 import com.android.systemui.Dumpable; 25 import com.android.systemui.dagger.SysUISingleton; 26 import com.android.systemui.dump.DumpManager; 27 import com.android.systemui.qs.QSFragment; 28 import com.android.systemui.statusbar.policy.ConfigurationController; 29 30 import java.io.PrintWriter; 31 import java.lang.reflect.Method; 32 import java.lang.reflect.Modifier; 33 34 import javax.inject.Inject; 35 36 import dagger.Subcomponent; 37 38 /** 39 * Holds a map of root views to FragmentHostStates and generates them as needed. 40 * Also dispatches the configuration changes to all current FragmentHostStates. 41 */ 42 @SysUISingleton 43 public class FragmentService implements Dumpable { 44 45 private static final String TAG = "FragmentService"; 46 47 private final ArrayMap<View, FragmentHostState> mHosts = new ArrayMap<>(); 48 /** 49 * A map with the means to create fragments via Dagger injection. 50 * 51 * key: the fragment class name. 52 * value: see {@link FragmentInstantiationInfo}. 53 */ 54 private final ArrayMap<String, FragmentInstantiationInfo> mInjectionMap = new ArrayMap<>(); 55 private final Handler mHandler = new Handler(); 56 private final FragmentHostManager.Factory mFragmentHostManagerFactory; 57 58 private ConfigurationController.ConfigurationListener mConfigurationListener = 59 new ConfigurationController.ConfigurationListener() { 60 @Override 61 public void onConfigChanged(Configuration newConfig) { 62 for (FragmentHostState state : mHosts.values()) { 63 state.sendConfigurationChange(newConfig); 64 } 65 } 66 }; 67 68 @Inject FragmentService( FragmentCreator.Factory fragmentCreatorFactory, FragmentHostManager.Factory fragmentHostManagerFactory, ConfigurationController configurationController, DumpManager dumpManager)69 public FragmentService( 70 FragmentCreator.Factory fragmentCreatorFactory, 71 FragmentHostManager.Factory fragmentHostManagerFactory, 72 ConfigurationController configurationController, 73 DumpManager dumpManager) { 74 mFragmentHostManagerFactory = fragmentHostManagerFactory; 75 addFragmentInstantiationProvider(fragmentCreatorFactory.build()); 76 configurationController.addCallback(mConfigurationListener); 77 78 dumpManager.registerDumpable(getClass().getSimpleName(), this); 79 } 80 getInjectionMap()81 ArrayMap<String, FragmentInstantiationInfo> getInjectionMap() { 82 return mInjectionMap; 83 } 84 85 /** 86 * Adds a new Dagger component object that provides method(s) to create fragments via injection. 87 */ addFragmentInstantiationProvider(Object daggerComponent)88 public void addFragmentInstantiationProvider(Object daggerComponent) { 89 for (Method method : daggerComponent.getClass().getDeclaredMethods()) { 90 if (Fragment.class.isAssignableFrom(method.getReturnType()) 91 && (method.getModifiers() & Modifier.PUBLIC) != 0) { 92 String fragmentName = method.getReturnType().getName(); 93 if (mInjectionMap.containsKey(fragmentName)) { 94 Log.w(TAG, "Fragment " + fragmentName + " is already provided by different" 95 + " Dagger component; Not adding method"); 96 continue; 97 } 98 mInjectionMap.put( 99 fragmentName, new FragmentInstantiationInfo(method, daggerComponent)); 100 } 101 } 102 } 103 getFragmentHostManager(View view)104 public FragmentHostManager getFragmentHostManager(View view) { 105 View root = view.getRootView(); 106 FragmentHostState state = mHosts.get(root); 107 if (state == null) { 108 state = new FragmentHostState(root); 109 mHosts.put(root, state); 110 } 111 return state.getFragmentHostManager(); 112 } 113 removeAndDestroy(View view)114 public void removeAndDestroy(View view) { 115 final FragmentHostState state = mHosts.remove(view.getRootView()); 116 if (state != null) { 117 state.mFragmentHostManager.destroy(); 118 } 119 } 120 destroyAll()121 public void destroyAll() { 122 for (FragmentHostState state : mHosts.values()) { 123 state.mFragmentHostManager.destroy(); 124 } 125 } 126 127 @Override dump(PrintWriter pw, String[] args)128 public void dump(PrintWriter pw, String[] args) { 129 pw.println("Dumping fragments:"); 130 for (FragmentHostState state : mHosts.values()) { 131 state.mFragmentHostManager.getFragmentManager().dump(" ", null, pw, args); 132 } 133 } 134 135 /** 136 * The subcomponent of dagger that holds all fragments that need injection. 137 */ 138 @Subcomponent 139 public interface FragmentCreator { 140 /** Factory for creating a FragmentCreator. */ 141 @Subcomponent.Factory 142 interface Factory { build()143 FragmentCreator build(); 144 } 145 /** 146 * Inject a QSFragment. 147 */ createQSFragment()148 QSFragment createQSFragment(); 149 } 150 151 private class FragmentHostState { 152 private final View mView; 153 154 private FragmentHostManager mFragmentHostManager; 155 FragmentHostState(View view)156 public FragmentHostState(View view) { 157 mView = view; 158 mFragmentHostManager = mFragmentHostManagerFactory.create(mView); 159 } 160 sendConfigurationChange(Configuration newConfig)161 public void sendConfigurationChange(Configuration newConfig) { 162 mHandler.post(() -> handleSendConfigurationChange(newConfig)); 163 } 164 getFragmentHostManager()165 public FragmentHostManager getFragmentHostManager() { 166 return mFragmentHostManager; 167 } 168 handleSendConfigurationChange(Configuration newConfig)169 private void handleSendConfigurationChange(Configuration newConfig) { 170 mFragmentHostManager.onConfigurationChanged(newConfig); 171 } 172 } 173 174 /** An object containing the information needed to instantiate a fragment. */ 175 static class FragmentInstantiationInfo { 176 /** The method that returns a newly-created fragment of the given class. */ 177 final Method mMethod; 178 /** The Dagger component that the method should be invoked on. */ 179 final Object mDaggerComponent; FragmentInstantiationInfo(Method method, Object daggerComponent)180 FragmentInstantiationInfo(Method method, Object daggerComponent) { 181 this.mMethod = method; 182 this.mDaggerComponent = daggerComponent; 183 } 184 } 185 } 186