1 /*
2  * Copyright (C) 2022 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.media.dialog;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 
23 import com.android.settingslib.media.MediaDevice;
24 import com.android.systemui.res.R;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.util.Optional;
29 
30 /**
31  * MediaItem represents an item in OutputSwitcher list (could be a MediaDevice, group divider or
32  * connect new device item).
33  */
34 public class MediaItem {
35     private final Optional<MediaDevice> mMediaDeviceOptional;
36     private final String mTitle;
37     @MediaItemType
38     private final int mMediaItemType;
39 
40     @Retention(RetentionPolicy.SOURCE)
41     @IntDef({
42             MediaItemType.TYPE_DEVICE,
43             MediaItemType.TYPE_GROUP_DIVIDER,
44             MediaItemType.TYPE_PAIR_NEW_DEVICE})
45     public @interface MediaItemType {
46         int TYPE_DEVICE = 0;
47         int TYPE_GROUP_DIVIDER = 1;
48         int TYPE_PAIR_NEW_DEVICE = 2;
49     }
50 
51     /**
52      * Returns a new {@link MediaItemType#TYPE_DEVICE} {@link MediaItem} with its {@link
53      * #getMediaDevice() media device} set to {@code device} and its title set to {@code device}'s
54      * name.
55      */
createDeviceMediaItem(@onNull MediaDevice device)56     public static MediaItem createDeviceMediaItem(@NonNull MediaDevice device) {
57         return new MediaItem(device, device.getName(), MediaItemType.TYPE_DEVICE);
58     }
59 
60     /**
61      * Returns a new {@link MediaItemType#TYPE_PAIR_NEW_DEVICE} {@link MediaItem} with both {@link
62      * #getMediaDevice() media device} and title set to {@code null}.
63      */
createPairNewDeviceMediaItem()64     public static MediaItem createPairNewDeviceMediaItem() {
65         return new MediaItem(
66                 /* device */ null, /* title */ null, MediaItemType.TYPE_PAIR_NEW_DEVICE);
67     }
68 
69     /**
70      * Returns a new {@link MediaItemType#TYPE_GROUP_DIVIDER} {@link MediaItem} with the specified
71      * title and a {@code null} {@link #getMediaDevice() media device}.
72      */
createGroupDividerMediaItem(@ullable String title)73     public static MediaItem createGroupDividerMediaItem(@Nullable String title) {
74         return new MediaItem(/* device */ null, title, MediaItemType.TYPE_GROUP_DIVIDER);
75     }
76 
MediaItem( @ullable MediaDevice device, @Nullable String title, @MediaItemType int type)77     private MediaItem(
78             @Nullable MediaDevice device, @Nullable String title, @MediaItemType int type) {
79         this.mMediaDeviceOptional = Optional.ofNullable(device);
80         this.mTitle = title;
81         this.mMediaItemType = type;
82     }
83 
getMediaDevice()84     public Optional<MediaDevice> getMediaDevice() {
85         return mMediaDeviceOptional;
86     }
87 
88     /** Get layout id based on media item Type. */
getMediaLayoutId(@ediaItemType int mediaItemType)89     public static int getMediaLayoutId(@MediaItemType int mediaItemType) {
90         return switch (mediaItemType) {
91             case MediaItemType.TYPE_DEVICE, MediaItemType.TYPE_PAIR_NEW_DEVICE ->
92                     R.layout.media_output_list_item_advanced;
93             default -> R.layout.media_output_list_group_divider;
94         };
95     }
96 
getTitle()97     public String getTitle() {
98         return mTitle;
99     }
100 
isMutingExpectedDevice()101     public boolean isMutingExpectedDevice() {
102         return mMediaDeviceOptional.isPresent()
103                 && mMediaDeviceOptional.get().isMutingExpectedDevice();
104     }
105 
getMediaItemType()106     public int getMediaItemType() {
107         return mMediaItemType;
108     }
109 }
110