Home | History | Annotate | Download | only in fragments
      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.Dumpable;
     27 import com.android.systemui.SystemUI;
     28 import com.android.systemui.SystemUIApplication;
     29 
     30 import java.io.FileDescriptor;
     31 import java.io.PrintWriter;
     32 
     33 /**
     34  * Holds a map of root views to FragmentHostStates and generates them as needed.
     35  * Also dispatches the configuration changes to all current FragmentHostStates.
     36  */
     37 public class FragmentService implements ConfigurationChangedReceiver, Dumpable {
     38 
     39     private static final String TAG = "FragmentService";
     40 
     41     private final ArrayMap<View, FragmentHostState> mHosts = new ArrayMap<>();
     42     private final Handler mHandler = new Handler();
     43     private final Context mContext;
     44 
     45     public FragmentService(Context context) {
     46         mContext = context;
     47     }
     48 
     49     public FragmentHostManager getFragmentHostManager(View view) {
     50         View root = view.getRootView();
     51         FragmentHostState state = mHosts.get(root);
     52         if (state == null) {
     53             state = new FragmentHostState(root);
     54             mHosts.put(root, state);
     55         }
     56         return state.getFragmentHostManager();
     57     }
     58 
     59     public void destroyAll() {
     60         for (FragmentHostState state : mHosts.values()) {
     61             state.mFragmentHostManager.destroy();
     62         }
     63     }
     64 
     65     @Override
     66     public void onConfigurationChanged(Configuration newConfig) {
     67         for (FragmentHostState state : mHosts.values()) {
     68             state.sendConfigurationChange(newConfig);
     69         }
     70     }
     71 
     72     @Override
     73     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     74         pw.println("Dumping fragments:");
     75         for (FragmentHostState state : mHosts.values()) {
     76             state.mFragmentHostManager.getFragmentManager().dump("  ", fd, pw, args);
     77         }
     78     }
     79 
     80     private class FragmentHostState {
     81         private final View mView;
     82 
     83         private FragmentHostManager mFragmentHostManager;
     84 
     85         public FragmentHostState(View view) {
     86             mView = view;
     87             mFragmentHostManager = new FragmentHostManager(mContext, FragmentService.this, mView);
     88         }
     89 
     90         public void sendConfigurationChange(Configuration newConfig) {
     91             mHandler.post(() -> handleSendConfigurationChange(newConfig));
     92         }
     93 
     94         public FragmentHostManager getFragmentHostManager() {
     95             return mFragmentHostManager;
     96         }
     97 
     98         private void handleSendConfigurationChange(Configuration newConfig) {
     99             mFragmentHostManager.onConfigurationChanged(newConfig);
    100         }
    101     }
    102 }
    103