1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.toast;
18 
19 import android.content.Context;
20 import android.view.LayoutInflater;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.android.systemui.Dumpable;
25 import com.android.systemui.dagger.SysUISingleton;
26 import com.android.systemui.dump.DumpManager;
27 import com.android.systemui.plugins.PluginListener;
28 import com.android.systemui.plugins.PluginManager;
29 import com.android.systemui.plugins.ToastPlugin;
30 
31 import java.io.PrintWriter;
32 
33 import javax.inject.Inject;
34 
35 /**
36  * Factory for creating toasts to be shown by ToastUI.
37  * These toasts can be customized by {@link ToastPlugin}.
38  */
39 @SysUISingleton
40 public class ToastFactory implements Dumpable {
41     // only one ToastPlugin can be connected at a time.
42     private ToastPlugin mPlugin;
43     private final LayoutInflater mLayoutInflater;
44 
45     @Inject
ToastFactory( LayoutInflater layoutInflater, PluginManager pluginManager, DumpManager dumpManager)46     public ToastFactory(
47             LayoutInflater layoutInflater,
48             PluginManager pluginManager,
49             DumpManager dumpManager) {
50         mLayoutInflater = layoutInflater;
51         dumpManager.registerDumpable("ToastFactory", this);
52         pluginManager.addPluginListener(
53                 new PluginListener<ToastPlugin>() {
54                     @Override
55                     public void onPluginConnected(ToastPlugin plugin, Context pluginContext) {
56                         mPlugin = plugin;
57                     }
58 
59                     @Override
60                     public void onPluginDisconnected(ToastPlugin plugin) {
61                         if (plugin.equals(mPlugin)) {
62                             mPlugin = null;
63                         }
64                     }
65                 }, ToastPlugin.class, false /* Allow multiple plugins */);
66     }
67 
68     /**
69      * Create a toast to be shown by ToastUI.
70      */
createToast(Context context, CharSequence text, String packageName, int userId, int orientation)71     public SystemUIToast createToast(Context context, CharSequence text, String packageName,
72             int userId, int orientation) {
73         if (isPluginAvailable()) {
74             return new SystemUIToast(mLayoutInflater, context, text, mPlugin.createToast(text,
75                     packageName, userId), packageName, userId, orientation);
76         }
77         return new SystemUIToast(mLayoutInflater, context, text, packageName, userId,
78                 orientation);
79     }
80 
isPluginAvailable()81     private boolean isPluginAvailable() {
82         return mPlugin != null;
83     }
84 
85     @Override
dump(@onNull PrintWriter pw, @NonNull String[] args)86     public void dump(@NonNull PrintWriter pw, @NonNull String[] args) {
87         pw.println("ToastFactory:");
88         pw.println("    mAttachedPlugin=" + mPlugin);
89     }
90 }
91