1 /*
2  * Copyright (C) 2021 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.plugins;
18 
19 import android.app.PendingIntent;
20 import android.app.smartspace.SmartspaceAction;
21 import android.app.smartspace.SmartspaceTarget;
22 import android.app.smartspace.SmartspaceTargetEvent;
23 import android.content.ActivityNotFoundException;
24 import android.content.Intent;
25 import android.graphics.drawable.Drawable;
26 import android.os.Parcelable;
27 import android.util.Log;
28 import android.view.View;
29 import android.view.ViewGroup;
30 
31 import androidx.annotation.Nullable;
32 
33 import com.android.systemui.plugins.annotations.ProvidesInterface;
34 
35 import java.util.List;
36 
37 /**
38  * Interface to provide SmartspaceTargets to BcSmartspace.
39  */
40 @ProvidesInterface(action = BcSmartspaceDataPlugin.ACTION, version = BcSmartspaceDataPlugin.VERSION)
41 public interface BcSmartspaceDataPlugin extends Plugin {
42     String ACTION = "com.android.systemui.action.PLUGIN_BC_SMARTSPACE_DATA";
43     int VERSION = 1;
44     String TAG = "BcSmartspaceDataPlugin";
45 
46     /** Register a listener to get Smartspace data. */
registerListener(SmartspaceTargetListener listener)47     void registerListener(SmartspaceTargetListener listener);
48 
49     /** Unregister a listener. */
unregisterListener(SmartspaceTargetListener listener)50     void unregisterListener(SmartspaceTargetListener listener);
51 
52     /** Register a SmartspaceEventNotifier. */
registerSmartspaceEventNotifier(SmartspaceEventNotifier notifier)53     default void registerSmartspaceEventNotifier(SmartspaceEventNotifier notifier) {}
54 
55     /** Push a SmartspaceTargetEvent to the SmartspaceEventNotifier. */
notifySmartspaceEvent(SmartspaceTargetEvent event)56     default void notifySmartspaceEvent(SmartspaceTargetEvent event) {}
57 
58     /** Allows for notifying the SmartspaceSession of SmartspaceTargetEvents. */
59     interface SmartspaceEventNotifier {
60         /** Pushes a given SmartspaceTargetEvent to the SmartspaceSession. */
notifySmartspaceEvent(SmartspaceTargetEvent event)61         void notifySmartspaceEvent(SmartspaceTargetEvent event);
62     }
63 
64     /**
65      * Create a view to be shown within the parent. Do not add the view, as the parent
66      * will be responsible for correctly setting the LayoutParams
67      */
getView(ViewGroup parent)68     default SmartspaceView getView(ViewGroup parent) {
69         return null;
70     }
71 
72     /**
73      * As the smartspace view becomes available, allow listeners to receive an event.
74      */
addOnAttachStateChangeListener(View.OnAttachStateChangeListener listener)75     default void addOnAttachStateChangeListener(View.OnAttachStateChangeListener listener) { }
76 
77     /** Updates Smartspace data and propagates it to any listeners. */
onTargetsAvailable(List<SmartspaceTarget> targets)78     void onTargetsAvailable(List<SmartspaceTarget> targets);
79 
80     /** Provides Smartspace data to registered listeners. */
81     interface SmartspaceTargetListener {
82         /** Each Parcelable is a SmartspaceTarget that represents a card. */
onSmartspaceTargetsUpdated(List<? extends Parcelable> targets)83         void onSmartspaceTargetsUpdated(List<? extends Parcelable> targets);
84     }
85 
86     /** View to which this plugin can be registered, in order to get updates. */
87     interface SmartspaceView {
registerDataProvider(BcSmartspaceDataPlugin plugin)88         void registerDataProvider(BcSmartspaceDataPlugin plugin);
89 
90         /**
91          * Primary color for unprotected text
92          */
setPrimaryTextColor(int color)93         void setPrimaryTextColor(int color);
94 
95         /**
96          * Range [0.0 - 1.0] when transitioning from Lockscreen to/from AOD
97          */
setDozeAmount(float amount)98         void setDozeAmount(float amount);
99 
100         /**
101          * Overrides how Intents/PendingIntents gets launched. Mostly to support auth from
102          * the lockscreen.
103          */
setIntentStarter(IntentStarter intentStarter)104         void setIntentStarter(IntentStarter intentStarter);
105 
106         /**
107          * When on the lockscreen, use the FalsingManager to help detect errant touches
108          */
setFalsingManager(com.android.systemui.plugins.FalsingManager falsingManager)109         void setFalsingManager(com.android.systemui.plugins.FalsingManager falsingManager);
110 
111         /**
112          * Set or clear Do Not Disturb information.
113          */
setDnd(@ullable Drawable image, @Nullable String description)114         void setDnd(@Nullable Drawable image, @Nullable String description);
115 
116         /**
117          * Set or clear next alarm information
118          */
setNextAlarm(@ullable Drawable image, @Nullable String description)119         void setNextAlarm(@Nullable Drawable image, @Nullable String description);
120 
121         /**
122          * Set or clear device media playing
123          */
setMediaTarget(@ullable SmartspaceTarget target)124         void setMediaTarget(@Nullable SmartspaceTarget target);
125     }
126 
127     /** Interface for launching Intents, which can differ on the lockscreen */
128     interface IntentStarter {
startFromAction(SmartspaceAction action, View v, boolean showOnLockscreen)129         default void startFromAction(SmartspaceAction action, View v, boolean showOnLockscreen) {
130             try {
131                 if (action.getIntent() != null) {
132                     startIntent(v, action.getIntent(), showOnLockscreen);
133                 } else if (action.getPendingIntent() != null) {
134                     startPendingIntent(action.getPendingIntent(), showOnLockscreen);
135                 }
136             } catch (ActivityNotFoundException e) {
137                 Log.w(TAG, "Could not launch intent for action: " + action, e);
138             }
139         }
140 
141         /** Start the intent */
startIntent(View v, Intent i, boolean showOnLockscreen)142         void startIntent(View v, Intent i, boolean showOnLockscreen);
143 
144         /** Start the PendingIntent */
startPendingIntent(PendingIntent pi, boolean showOnLockscreen)145         void startPendingIntent(PendingIntent pi, boolean showOnLockscreen);
146     }
147 }
148