Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 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 | |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 17 | import android.app.Fragment; |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 18 | import android.content.res.Configuration; |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.util.ArrayMap; |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 21 | import android.view.View; |
| 22 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 23 | import com.android.systemui.ConfigurationChangedReceiver; |
Jason Monk | 88e3a97 | 2018-05-17 10:56:00 -0400 | [diff] [blame] | 24 | import com.android.systemui.Dumpable; |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 25 | import com.android.systemui.SystemUIFactory; |
Jason Monk | 48f85be | 2018-12-19 15:16:44 -0500 | [diff] [blame] | 26 | import com.android.systemui.qs.QSFragment; |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 27 | import com.android.systemui.statusbar.phone.NavigationBarFragment; |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 28 | |
Jason Monk | 88e3a97 | 2018-05-17 10:56:00 -0400 | [diff] [blame] | 29 | import java.io.FileDescriptor; |
| 30 | import java.io.PrintWriter; |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 31 | import java.lang.reflect.Method; |
| 32 | import java.lang.reflect.Modifier; |
| 33 | |
| 34 | import javax.inject.Inject; |
| 35 | import javax.inject.Singleton; |
| 36 | |
| 37 | import dagger.Subcomponent; |
Jason Monk | 88e3a97 | 2018-05-17 10:56:00 -0400 | [diff] [blame] | 38 | |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 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 | */ |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 43 | @Singleton |
Jason Monk | 88e3a97 | 2018-05-17 10:56:00 -0400 | [diff] [blame] | 44 | public class FragmentService implements ConfigurationChangedReceiver, Dumpable { |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 45 | |
| 46 | private static final String TAG = "FragmentService"; |
| 47 | |
| 48 | private final ArrayMap<View, FragmentHostState> mHosts = new ArrayMap<>(); |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 49 | private final ArrayMap<String, Method> mInjectionMap = new ArrayMap<>(); |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 50 | private final Handler mHandler = new Handler(); |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 51 | private final FragmentCreator mFragmentCreator; |
| 52 | |
| 53 | @Inject |
| 54 | public FragmentService(SystemUIFactory.SystemUIRootComponent rootComponent) { |
| 55 | mFragmentCreator = rootComponent.createFragmentCreator(); |
| 56 | initInjectionMap(); |
| 57 | } |
| 58 | |
| 59 | ArrayMap<String, Method> getInjectionMap() { |
| 60 | return mInjectionMap; |
| 61 | } |
| 62 | |
| 63 | FragmentCreator getFragmentCreator() { |
| 64 | return mFragmentCreator; |
| 65 | } |
| 66 | |
| 67 | private void initInjectionMap() { |
| 68 | for (Method method : FragmentCreator.class.getDeclaredMethods()) { |
| 69 | if (Fragment.class.isAssignableFrom(method.getReturnType()) |
| 70 | && (method.getModifiers() & Modifier.PUBLIC) != 0) { |
| 71 | mInjectionMap.put(method.getReturnType().getName(), method); |
| 72 | } |
| 73 | } |
| 74 | } |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 75 | |
| 76 | public FragmentHostManager getFragmentHostManager(View view) { |
| 77 | View root = view.getRootView(); |
| 78 | FragmentHostState state = mHosts.get(root); |
| 79 | if (state == null) { |
| 80 | state = new FragmentHostState(root); |
| 81 | mHosts.put(root, state); |
| 82 | } |
| 83 | return state.getFragmentHostManager(); |
| 84 | } |
| 85 | |
Riddle Hsu | 1960777 | 2018-11-01 18:11:26 +0800 | [diff] [blame] | 86 | public void removeAndDestroy(View view) { |
| 87 | final FragmentHostState state = mHosts.remove(view.getRootView()); |
| 88 | if (state != null) { |
| 89 | state.mFragmentHostManager.destroy(); |
| 90 | } |
| 91 | } |
| 92 | |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 93 | public void destroyAll() { |
| 94 | for (FragmentHostState state : mHosts.values()) { |
| 95 | state.mFragmentHostManager.destroy(); |
| 96 | } |
| 97 | } |
| 98 | |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 99 | @Override |
Jason Monk | 790442e | 2017-02-13 17:49:39 -0500 | [diff] [blame] | 100 | public void onConfigurationChanged(Configuration newConfig) { |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 101 | for (FragmentHostState state : mHosts.values()) { |
| 102 | state.sendConfigurationChange(newConfig); |
| 103 | } |
| 104 | } |
| 105 | |
Jason Monk | 88e3a97 | 2018-05-17 10:56:00 -0400 | [diff] [blame] | 106 | @Override |
| 107 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 108 | pw.println("Dumping fragments:"); |
| 109 | for (FragmentHostState state : mHosts.values()) { |
| 110 | state.mFragmentHostManager.getFragmentManager().dump(" ", fd, pw, args); |
| 111 | } |
| 112 | } |
| 113 | |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 114 | /** |
| 115 | * The subcomponent of dagger that holds all fragments that need injection. |
| 116 | */ |
| 117 | @Subcomponent |
| 118 | public interface FragmentCreator { |
| 119 | /** |
| 120 | * Inject a NavigationBarFragment. |
| 121 | */ |
| 122 | NavigationBarFragment createNavigationBarFragment(); |
Jason Monk | 48f85be | 2018-12-19 15:16:44 -0500 | [diff] [blame] | 123 | /** |
| 124 | * Inject a QSFragment. |
| 125 | */ |
| 126 | QSFragment createQSFragment(); |
Jason Monk | 9424af7 | 2018-12-19 14:17:26 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 129 | private class FragmentHostState { |
| 130 | private final View mView; |
| 131 | |
| 132 | private FragmentHostManager mFragmentHostManager; |
| 133 | |
| 134 | public FragmentHostState(View view) { |
| 135 | mView = view; |
Riddle Hsu | 1960777 | 2018-11-01 18:11:26 +0800 | [diff] [blame] | 136 | mFragmentHostManager = new FragmentHostManager(FragmentService.this, mView); |
Jason Monk | bbac121 | 2016-10-31 10:18:20 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public void sendConfigurationChange(Configuration newConfig) { |
| 140 | mHandler.post(() -> handleSendConfigurationChange(newConfig)); |
| 141 | } |
| 142 | |
| 143 | public FragmentHostManager getFragmentHostManager() { |
| 144 | return mFragmentHostManager; |
| 145 | } |
| 146 | |
| 147 | private void handleSendConfigurationChange(Configuration newConfig) { |
| 148 | mFragmentHostManager.onConfigurationChanged(newConfig); |
| 149 | } |
| 150 | } |
| 151 | } |