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.content.Context; 18 import android.content.res.Configuration; 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.util.ArrayMap; 22 import android.util.Log; 23 import android.view.View; 24 25 import com.android.systemui.ConfigurationChangedReceiver; 26 import com.android.systemui.SystemUI; 27 import com.android.systemui.SystemUIApplication; 28 29 /** 30 * Holds a map of root views to FragmentHostStates and generates them as needed. 31 * Also dispatches the configuration changes to all current FragmentHostStates. 32 */ 33 public class FragmentService implements ConfigurationChangedReceiver { 34 35 private static final String TAG = "FragmentService"; 36 37 private final ArrayMap<View, FragmentHostState> mHosts = new ArrayMap<>(); 38 private final Handler mHandler = new Handler(); 39 private final Context mContext; 40 41 public FragmentService(Context context) { 42 mContext = context; 43 } 44 45 public FragmentHostManager getFragmentHostManager(View view) { 46 View root = view.getRootView(); 47 FragmentHostState state = mHosts.get(root); 48 if (state == null) { 49 state = new FragmentHostState(root); 50 mHosts.put(root, state); 51 } 52 return state.getFragmentHostManager(); 53 } 54 55 public void destroyAll() { 56 for (FragmentHostState state : mHosts.values()) { 57 state.mFragmentHostManager.destroy(); 58 } 59 } 60 61 @Override 62 public void onConfigurationChanged(Configuration newConfig) { 63 for (FragmentHostState state : mHosts.values()) { 64 state.sendConfigurationChange(newConfig); 65 } 66 } 67 68 private class FragmentHostState { 69 private final View mView; 70 71 private FragmentHostManager mFragmentHostManager; 72 73 public FragmentHostState(View view) { 74 mView = view; 75 mFragmentHostManager = new FragmentHostManager(mContext, FragmentService.this, mView); 76 } 77 78 public void sendConfigurationChange(Configuration newConfig) { 79 mHandler.post(() -> handleSendConfigurationChange(newConfig)); 80 } 81 82 public FragmentHostManager getFragmentHostManager() { 83 return mFragmentHostManager; 84 } 85 86 private void handleSendConfigurationChange(Configuration newConfig) { 87 mFragmentHostManager.onConfigurationChanged(newConfig); 88 } 89 } 90 } 91