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.util.Log;
19 import android.view.View;
20 
21 import com.android.systemui.plugins.FragmentBase;
22 import com.android.systemui.statusbar.policy.ExtensionController.Extension;
23 
24 import java.util.function.Consumer;
25 
26 /**
27  * Wires up an Extension to a Fragment tag/id so that it always contains the class
28  * selected by the extension.
29  */
30 public class ExtensionFragmentListener<T extends FragmentBase> implements Consumer<T> {
31 
32     private static final String TAG = "ExtensionFragmentListener";
33 
34     private final FragmentHostManager mFragmentHostManager;
35     private final String mTag;
36     private final Extension<T> mExtension;
37     private final int mId;
38     private String mOldClass;
39 
ExtensionFragmentListener( FragmentService fragmentService, View view, String tag, int id, Extension<T> extension)40     private ExtensionFragmentListener(
41             FragmentService fragmentService,
42             View view,
43             String tag,
44             int id,
45             Extension<T> extension) {
46         mTag = tag;
47         mFragmentHostManager = fragmentService.getFragmentHostManager(view);
48         mExtension = extension;
49         mId = id;
50         mFragmentHostManager.getFragmentManager().beginTransaction()
51                 .replace(id, (Fragment) mExtension.get(), mTag)
52                 .commit();
53         mExtension.clearItem(false);
54     }
55 
56     @Override
accept(T extension)57     public void accept(T extension) {
58         if (Fragment.class.isInstance(extension)) {
59             mFragmentHostManager.getExtensionManager().setCurrentExtension(mId, mTag,
60                     mOldClass, extension.getClass().getName(), mExtension.getContext());
61             mOldClass = extension.getClass().getName();
62         } else {
63             Log.e(TAG, extension.getClass().getName() + " must be a Fragment");
64         }
65         mExtension.clearItem(true);
66     }
67 
attachExtensonToFragment( FragmentService fragmentService, View view, String tag, int id, Extension<T> extension)68     public static <T> void attachExtensonToFragment(
69             FragmentService fragmentService,
70             View view,
71             String tag,
72             int id,
73             Extension<T> extension) {
74         extension.addCallback(
75                 new ExtensionFragmentListener(fragmentService, view, tag, id, extension));
76     }
77 }
78