• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.car.docklib.events;
18 
19 import static android.content.Context.RECEIVER_EXPORTED;
20 
21 import static com.android.car.dockutil.events.DockCompatUtils.isDockSupportedOnDisplay;
22 import static com.android.car.dockutil.events.DockEventSenderHelper.EXTRA_COMPONENT;
23 
24 import android.content.BroadcastReceiver;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Build;
30 import android.util.Log;
31 
32 import androidx.annotation.NonNull;
33 
34 import com.android.car.docklib.DockInterface;
35 import com.android.car.dockutil.events.DockEvent;
36 import com.android.car.dockutil.events.DockPermission;
37 
38 /**
39  * BroadcastReceiver for Dock Events.
40  */
41 public class DockEventsReceiver extends BroadcastReceiver {
42     private static final String TAG = "DockEventsReceiver";
43     private static final boolean DEBUG = Build.isDebuggable();
44     private final DockInterface mDockController;
45 
DockEventsReceiver(DockInterface dockController)46     public DockEventsReceiver(DockInterface dockController) {
47         mDockController = dockController;
48     }
49 
50     @Override
onReceive(Context context, Intent intent)51     public void onReceive(Context context, Intent intent) {
52         if (!isDockSupportedOnDisplay(context, context.getDisplayId())) {
53             Log.e(TAG, "Dock event received on unsupported display " + context.getDisplayId());
54             return;
55         }
56         DockEvent event = DockEvent.toDockEvent(intent.getAction());
57         ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT, ComponentName.class);
58 
59         if (event == null || component == null) {
60             return;
61         }
62 
63         if (DEBUG) {
64             Log.d(TAG, "DockEvent received of type " + event + " with component: "
65                     + component);
66         }
67 
68         switch (event) {
69             case LAUNCH:
70                 mDockController.appLaunched(component);
71                 break;
72             case PIN:
73                 mDockController.appPinned(component);
74                 break;
75             case UNPIN:
76                 mDockController.appUnpinned(component);
77                 break;
78         }
79     }
80 
81     /**
82      * Helper method to register {@link DockEventsReceiver} through context and listen to dock
83      * events from packages with required permissions.
84      *
85      * @param context the context through which the DockEventsReceiver is registered
86      * @return successfully registered DockEventsReceiver.
87      */
88     @NonNull
registerDockReceiver( @onNull Context context, @NonNull DockInterface dockController )89     public static DockEventsReceiver registerDockReceiver(
90             @NonNull Context context,
91             @NonNull DockInterface dockController
92     ) {
93         IntentFilter intentFilter = new IntentFilter();
94         intentFilter.addAction(DockEvent.LAUNCH.toString());
95         intentFilter.addAction(DockEvent.PIN.toString());
96         intentFilter.addAction(DockEvent.UNPIN.toString());
97         DockEventsReceiver receiver = new DockEventsReceiver(dockController);
98         context.registerReceiver(receiver, intentFilter,
99                 DockPermission.DOCK_SENDER_PERMISSION.toString(),
100                 /* handler= */null, RECEIVER_EXPORTED);
101         if (DEBUG) {
102             Log.d(TAG, "DockReceiver registered from: " + context.getPackageName());
103         }
104         return receiver;
105     }
106 }
107