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