blob: ae200763b3b0a89b61551068c47b525521addbe3 [file] [log] [blame]
Jason Monkbbac1212016-10-31 10:18:20 -04001/*
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
15package com.android.systemui.fragments;
16
Jason Monk9424af72018-12-19 14:17:26 -050017import android.app.Fragment;
Jason Monkbbac1212016-10-31 10:18:20 -040018import android.content.res.Configuration;
Jason Monkbbac1212016-10-31 10:18:20 -040019import android.os.Handler;
20import android.util.ArrayMap;
Jason Monkbbac1212016-10-31 10:18:20 -040021import android.view.View;
22
Jason Monk88e3a972018-05-17 10:56:00 -040023import com.android.systemui.Dumpable;
Dave Mankofff4736812019-10-18 17:25:50 -040024import com.android.systemui.dagger.SystemUIRootComponent;
Jason Monk48f85be2018-12-19 15:16:44 -050025import com.android.systemui.qs.QSFragment;
Jason Monk9424af72018-12-19 14:17:26 -050026import com.android.systemui.statusbar.phone.NavigationBarFragment;
Dave Mankoffbba732d2019-11-20 13:04:54 -050027import com.android.systemui.statusbar.policy.ConfigurationController;
Jason Monkbbac1212016-10-31 10:18:20 -040028
Jason Monk88e3a972018-05-17 10:56:00 -040029import java.io.FileDescriptor;
30import java.io.PrintWriter;
Jason Monk9424af72018-12-19 14:17:26 -050031import java.lang.reflect.Method;
32import java.lang.reflect.Modifier;
33
34import javax.inject.Inject;
35import javax.inject.Singleton;
36
37import dagger.Subcomponent;
Jason Monk88e3a972018-05-17 10:56:00 -040038
Jason Monkbbac1212016-10-31 10:18:20 -040039/**
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 */
Jason Monk9424af72018-12-19 14:17:26 -050043@Singleton
Dave Mankoffbba732d2019-11-20 13:04:54 -050044public class FragmentService implements Dumpable {
Jason Monkbbac1212016-10-31 10:18:20 -040045
46 private static final String TAG = "FragmentService";
47
48 private final ArrayMap<View, FragmentHostState> mHosts = new ArrayMap<>();
Jason Monk9424af72018-12-19 14:17:26 -050049 private final ArrayMap<String, Method> mInjectionMap = new ArrayMap<>();
Jason Monkbbac1212016-10-31 10:18:20 -040050 private final Handler mHandler = new Handler();
Jason Monk9424af72018-12-19 14:17:26 -050051 private final FragmentCreator mFragmentCreator;
52
Dave Mankoffbba732d2019-11-20 13:04:54 -050053 private ConfigurationController.ConfigurationListener mConfigurationListener =
54 new ConfigurationController.ConfigurationListener() {
55 @Override
56 public void onConfigChanged(Configuration newConfig) {
57 for (FragmentHostState state : mHosts.values()) {
58 state.sendConfigurationChange(newConfig);
59 }
60 }
61 };
62
Jason Monk9424af72018-12-19 14:17:26 -050063 @Inject
Dave Mankoffbba732d2019-11-20 13:04:54 -050064 public FragmentService(SystemUIRootComponent rootComponent,
65 ConfigurationController configurationController) {
Jason Monk9424af72018-12-19 14:17:26 -050066 mFragmentCreator = rootComponent.createFragmentCreator();
67 initInjectionMap();
Dave Mankoffbba732d2019-11-20 13:04:54 -050068 configurationController.addCallback(mConfigurationListener);
Jason Monk9424af72018-12-19 14:17:26 -050069 }
70
71 ArrayMap<String, Method> getInjectionMap() {
72 return mInjectionMap;
73 }
74
75 FragmentCreator getFragmentCreator() {
76 return mFragmentCreator;
77 }
78
79 private void initInjectionMap() {
80 for (Method method : FragmentCreator.class.getDeclaredMethods()) {
81 if (Fragment.class.isAssignableFrom(method.getReturnType())
82 && (method.getModifiers() & Modifier.PUBLIC) != 0) {
83 mInjectionMap.put(method.getReturnType().getName(), method);
84 }
85 }
86 }
Jason Monkbbac1212016-10-31 10:18:20 -040087
88 public FragmentHostManager getFragmentHostManager(View view) {
89 View root = view.getRootView();
90 FragmentHostState state = mHosts.get(root);
91 if (state == null) {
92 state = new FragmentHostState(root);
93 mHosts.put(root, state);
94 }
95 return state.getFragmentHostManager();
96 }
97
Riddle Hsu19607772018-11-01 18:11:26 +080098 public void removeAndDestroy(View view) {
99 final FragmentHostState state = mHosts.remove(view.getRootView());
100 if (state != null) {
101 state.mFragmentHostManager.destroy();
102 }
103 }
104
Jason Monk790442e2017-02-13 17:49:39 -0500105 public void destroyAll() {
106 for (FragmentHostState state : mHosts.values()) {
107 state.mFragmentHostManager.destroy();
108 }
109 }
110
Jason Monkbbac1212016-10-31 10:18:20 -0400111 @Override
Jason Monk88e3a972018-05-17 10:56:00 -0400112 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
113 pw.println("Dumping fragments:");
114 for (FragmentHostState state : mHosts.values()) {
115 state.mFragmentHostManager.getFragmentManager().dump(" ", fd, pw, args);
116 }
117 }
118
Jason Monk9424af72018-12-19 14:17:26 -0500119 /**
120 * The subcomponent of dagger that holds all fragments that need injection.
121 */
122 @Subcomponent
123 public interface FragmentCreator {
124 /**
125 * Inject a NavigationBarFragment.
126 */
127 NavigationBarFragment createNavigationBarFragment();
Jason Monk48f85be2018-12-19 15:16:44 -0500128 /**
129 * Inject a QSFragment.
130 */
131 QSFragment createQSFragment();
Jason Monk9424af72018-12-19 14:17:26 -0500132 }
133
Jason Monkbbac1212016-10-31 10:18:20 -0400134 private class FragmentHostState {
135 private final View mView;
136
137 private FragmentHostManager mFragmentHostManager;
138
139 public FragmentHostState(View view) {
140 mView = view;
Riddle Hsu19607772018-11-01 18:11:26 +0800141 mFragmentHostManager = new FragmentHostManager(FragmentService.this, mView);
Jason Monkbbac1212016-10-31 10:18:20 -0400142 }
143
144 public void sendConfigurationChange(Configuration newConfig) {
145 mHandler.post(() -> handleSendConfigurationChange(newConfig));
146 }
147
148 public FragmentHostManager getFragmentHostManager() {
149 return mFragmentHostManager;
150 }
151
152 private void handleSendConfigurationChange(Configuration newConfig) {
153 mFragmentHostManager.onConfigurationChanged(newConfig);
154 }
155 }
156}