• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         try {
59             Fragment.class.cast(extension);
60             mFragmentHostManager.getExtensionManager().setCurrentExtension(mId, mTag,
61                     mOldClass, extension.getClass().getName(), mExtension.getContext());
62             mOldClass = extension.getClass().getName();
63         } catch (ClassCastException e) {
64             Log.e(TAG, extension.getClass().getName() + " must be a Fragment", e);
65         }
66         mExtension.clearItem(true);
67     }
68 
attachExtensonToFragment( FragmentService fragmentService, View view, String tag, int id, Extension<T> extension)69     public static <T> void attachExtensonToFragment(
70             FragmentService fragmentService,
71             View view,
72             String tag,
73             int id,
74             Extension<T> extension) {
75         extension.addCallback(
76                 new ExtensionFragmentListener(fragmentService, view, tag, id, extension));
77     }
78 }
79