diff options
Diffstat (limited to 'docklib-util/src/com/android/car/dockutil/events')
3 files changed, 204 insertions, 0 deletions
diff --git a/docklib-util/src/com/android/car/dockutil/events/DockEvent.java b/docklib-util/src/com/android/car/dockutil/events/DockEvent.java new file mode 100644 index 00000000..b6d53383 --- /dev/null +++ b/docklib-util/src/com/android/car/dockutil/events/DockEvent.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.dockutil.events; + +import androidx.annotation.Nullable; + +/** + * Enum for events sent by the system to trigger change in the Dock. + */ +public enum DockEvent { + LAUNCH("com.android.car.docklib.events.LAUNCH"), + PIN("com.android.car.docklib.events.PIN"), + UNPIN("com.android.car.docklib.events.UNPIN"); + + private final String mStr; + + DockEvent(String str) { + mStr = str; + } + + @Override + public String toString() { + return mStr; + } + + /** + * Converts the string to {@link DockEvent}. Returns {@code null} if the string is not + * convertible. + */ + @Nullable + public static DockEvent toDockEvent(@Nullable String str) { + if (LAUNCH.toString().equals(str)) { + return LAUNCH; + } else if (PIN.toString().equals(str)) { + return PIN; + } else if (UNPIN.toString().equals(str)) { + return UNPIN; + } else { + return null; + } + } +} diff --git a/docklib-util/src/com/android/car/dockutil/events/DockEventSenderHelper.java b/docklib-util/src/com/android/car/dockutil/events/DockEventSenderHelper.java new file mode 100644 index 00000000..4f8b52f6 --- /dev/null +++ b/docklib-util/src/com/android/car/dockutil/events/DockEventSenderHelper.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.dockutil.events; + +import android.app.ActivityManager; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.view.Display; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; + +import com.android.car.dockutil.R; + +/** + * Helper used to send Dock Events. + */ +public class DockEventSenderHelper { + public static final String EXTRA_COMPONENT = "EXTRA_COMPONENT"; + + private final Context mContext; + private final boolean mIsDockEnabled; + + public DockEventSenderHelper(Context context) { + mContext = context; + mIsDockEnabled = mContext.getResources().getBoolean(R.bool.config_enableDock); + } + + /** + * Used to send launch event to the dock. Generally used when an app is launched. + */ + public void sendLaunchEvent(@NonNull ActivityManager.RunningTaskInfo taskInfo) { + sendEventBroadcast(DockEvent.LAUNCH, taskInfo); + } + + /** + * @see #sendPinEvent(ComponentName) + */ + public void sendPinEvent(@NonNull ActivityManager.RunningTaskInfo taskInfo) { + sendEventBroadcast(DockEvent.PIN, taskInfo); + } + + /** + * Used to send pin event to the dock. Generally used when an app should be pinned to the dock. + */ + public void sendPinEvent(@NonNull ComponentName componentName) { + sendEventBroadcast(DockEvent.PIN, componentName); + } + + /** + * @see #sendUnpinEvent(ComponentName) + */ + public void sendUnpinEvent(@NonNull ActivityManager.RunningTaskInfo taskInfo) { + sendEventBroadcast(DockEvent.UNPIN, taskInfo); + } + + /** + * Used to send unpin event to the dock. Generally used when an app should be unpinned from the + * dock. + */ + public void sendUnpinEvent(@NonNull ComponentName componentName) { + sendEventBroadcast(DockEvent.UNPIN, componentName); + } + + @VisibleForTesting + void sendEventBroadcast(@NonNull DockEvent event, + @NonNull ActivityManager.RunningTaskInfo taskInfo) { + if (taskInfo.getDisplayId() != Display.DEFAULT_DISPLAY) { + return; + } + ComponentName component = getComponentName(taskInfo); + if (component != null) { + sendEventBroadcast(event, component); + } + } + + private void sendEventBroadcast(@NonNull DockEvent event, @NonNull ComponentName component) { + if (!mIsDockEnabled) { + return; + } + + Intent intent = new Intent(); + intent.setAction(event.toString()); + intent.putExtra(EXTRA_COMPONENT, component); + mContext.sendBroadcast(intent, DockPermission.DOCK_RECEIVER_PERMISSION.toString()); + } + + @Nullable + private ComponentName getComponentName(@NonNull ActivityManager.RunningTaskInfo taskInfo) { + if (taskInfo.baseActivity == null && taskInfo.baseIntent.getComponent() == null) { + return null; + } + return taskInfo.baseActivity != null ? taskInfo.baseActivity + : taskInfo.baseIntent.getComponent(); + } +} diff --git a/docklib-util/src/com/android/car/dockutil/events/DockPermission.java b/docklib-util/src/com/android/car/dockutil/events/DockPermission.java new file mode 100644 index 00000000..d82b01f4 --- /dev/null +++ b/docklib-util/src/com/android/car/dockutil/events/DockPermission.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.car.dockutil.events; + +/** + * Permission stings declared to be used by dock. + */ +public enum DockPermission { + DOCK_SENDER_PERMISSION("com.android.car.docklib.permission.BROADCAST_SENDER"), + DOCK_RECEIVER_PERMISSION("com.android.car.docklib.permission.BROADCAST_RECEIVER"); + + private final String mStr; + + DockPermission(String str) { + mStr = str; + } + + @Override + public String toString() { + return mStr; + } +} |