summaryrefslogtreecommitdiff
path: root/src/com/android/car/media/AnalyticsHelper.java
blob: bb94fa90d902aa2e0d2ebee0b1da93c384330982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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.media;

import static androidx.car.app.mediaextensions.analytics.event.AnalyticsEvent.VIEW_ACTION_HIDE;
import static androidx.car.app.mediaextensions.analytics.event.AnalyticsEvent.VIEW_ACTION_MODE_NONE;
import static androidx.car.app.mediaextensions.analytics.event.AnalyticsEvent.VIEW_ACTION_MODE_SCROLL;
import static androidx.car.app.mediaextensions.analytics.event.AnalyticsEvent.VIEW_COMPONENT_BROWSE_LIST;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;

import androidx.annotation.OptIn;
import androidx.car.app.mediaextensions.analytics.event.AnalyticsEvent;

import com.android.car.media.common.MediaItemMetadata;
import com.android.car.media.common.browse.MediaItemsRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/** Helpers for Analytics */
@OptIn(markerClass = androidx.car.app.annotations2.ExperimentalCarApi.class)
public class AnalyticsHelper {

    /**
     * Creates a sends a visible items event for the items that became visible and another event
     * for the items that became hidden.
     * @return the currently visible items.
     */
    public static List<String> sendVisibleItemsInc(
            MediaItemsRepository repo, MediaItemMetadata parentItem, List<String> prevItems,
            List<MediaItemMetadata> items, int currFirst, int currLast, boolean fromScroll) {

        //Handle empty list by hiding previous and returning empty.
        if (items.isEmpty() && !prevItems.isEmpty()) {
            repo.getAnalyticsManager().sendVisibleItemsEvents(
                    parentItem != null ? parentItem.getId() : null,
                    VIEW_COMPONENT_BROWSE_LIST, VIEW_ACTION_HIDE,
                    fromScroll ? VIEW_ACTION_MODE_SCROLL : VIEW_ACTION_MODE_NONE,
                    new ArrayList<>(prevItems));
            return List.of();
        }

        //If for any reason there are no visible items or error state
        // we have nothing to show, hide prev
        if (currFirst == NO_POSITION
                || currLast == NO_POSITION
                || currLast > items.size()
                || items == null) {

            if (!prevItems.isEmpty()) {
                repo.getAnalyticsManager().sendVisibleItemsEvents(
                        parentItem != null ? parentItem.getId() : null,
                        VIEW_COMPONENT_BROWSE_LIST, VIEW_ACTION_HIDE,
                        fromScroll ? VIEW_ACTION_MODE_SCROLL : VIEW_ACTION_MODE_NONE,
                        new ArrayList<>(prevItems));
            }

            return List.of();
        }

        //Needed because wide search RV is sometimes given first and last swapped.
        //TODO(b/309150765): remove when fixed.
        int limitedMin = Math.min(currFirst, currLast + 1);
        int limitedMax = Math.max(currFirst, currLast + 1);

        List<String> currItemsSublist = items
                    .subList(limitedMin, Math.min(limitedMax, items.size()))
                    .stream()
                    .map(MediaItemMetadata::getId)
                    .collect(Collectors.toCollection(ArrayList::new));

        List<String> delta = new ArrayList<>(prevItems);
        List<String> deltaNew = new ArrayList<>(currItemsSublist);
        currItemsSublist.forEach(delta::remove);
        prevItems.forEach(deltaNew::remove);

        if (!delta.isEmpty()) {
            repo.getAnalyticsManager().sendVisibleItemsEvents(
                    parentItem != null ? parentItem.getId() : null,
                    VIEW_COMPONENT_BROWSE_LIST, VIEW_ACTION_HIDE,
                    fromScroll ? VIEW_ACTION_MODE_SCROLL : VIEW_ACTION_MODE_NONE,
                    new ArrayList<>(delta));
        }
        if (!deltaNew.isEmpty()) {
            repo.getAnalyticsManager().sendVisibleItemsEvents(
                    parentItem != null ? parentItem.getId() : null,
                    VIEW_COMPONENT_BROWSE_LIST, AnalyticsEvent.VIEW_ACTION_SHOW,
                    fromScroll ? VIEW_ACTION_MODE_SCROLL : VIEW_ACTION_MODE_NONE,
                    new ArrayList<>(deltaNew));
        }

        return currItemsSublist;
    }
}