aboutsummaryrefslogtreecommitdiff
path: root/TestMediaApp/src/com/android/car/media/testmediaapp/TmaLibrary.java
blob: 327a2b6b290d0606d37bb2ec2d36086aa786b5e1 (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
/*
 * Copyright 2019 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.testmediaapp;

import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.android.car.media.testmediaapp.loader.TmaLoader;
import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaBrowseNodeType;

import java.util.HashMap;
import java.util.Map;

/**
 * Delegates the loading of {@link TmaMediaItem}s to {@link TmaLoader} and caches the results
 * for {@link TmaBrowser}.
 */
class TmaLibrary {

    private static final String TAG = "TmaLibrary";

    private final TmaLoader mLoader;
    private final Map<TmaBrowseNodeType, String> mRootAssetPaths = new HashMap<>(5);

    /** Stores the root item of each loaded media asset file, keyed by the file's path. */
    private final Map<String, TmaMediaItem> mCachedFilesByPath = new HashMap<>(50);

    /** Stores every item of every loaded media asset file, keyed by the media id. */
    private final Map<String, TmaMediaItem> mMediaItemsByMediaId = new HashMap<>(500);

    TmaLibrary(TmaLoader loader) {
        mLoader = loader;
        mRootAssetPaths.put(TmaBrowseNodeType.NULL, null);
        mRootAssetPaths.put(TmaBrowseNodeType.EMPTY, "media_items/empty.json");
        mRootAssetPaths.put(TmaBrowseNodeType.QUEUE_ONLY, "media_items/empty.json");
        mRootAssetPaths.put(TmaBrowseNodeType.SINGLE_TAB, "media_items/single_node.json");
        mRootAssetPaths.put(TmaBrowseNodeType.NODE_CHILDREN, "media_items/only_nodes.json");
        mRootAssetPaths.put(TmaBrowseNodeType.LEAF_CHILDREN, "media_items/simple_leaves.json");
        mRootAssetPaths.put(TmaBrowseNodeType.MIXED_CHILDREN, "media_items/mixed.json");
        mRootAssetPaths.put(TmaBrowseNodeType.UNTAGGED, "media_items/untagged.json");
    }

    @Nullable
    TmaMediaItem getRoot(TmaBrowseNodeType rootType) {
        String filePath = mRootAssetPaths.get(rootType);
        return (filePath != null) ? loadAssetFile(filePath) : null;
    }

    @Nullable
    TmaMediaItem getMediaItemById(String mediaId) {
        TmaMediaItem result = mMediaItemsByMediaId.get(mediaId);
        // Processing includes only on request allows recursive structures :-)
        if (result != null && !TextUtils.isEmpty(result.mInclude)) {
            result = result.append(loadAssetFile(result.mInclude).mChildren);
        }
        return result;
    }

    private TmaMediaItem loadAssetFile(String filePath) {
        TmaMediaItem result = mCachedFilesByPath.get(filePath);
        if (result == null) {
            result = mLoader.loadAssetFile(filePath);
            if (result != null) {
                mCachedFilesByPath.put(filePath, result);
                cacheMediaItem(result);
            } else {
                Log.e(TAG, "Unable to load: " + filePath);
            }
        }
        return result;
    }

    private void cacheMediaItem(TmaMediaItem item) {
        String key = item.getMediaId();
        if (mMediaItemsByMediaId.putIfAbsent(key, item) == null) {
            for (TmaMediaItem child : item.mChildren) {
                cacheMediaItem(child);
            }
        } else {
            Log.e(TAG, "Ignoring item with duplicate media id: " + key);
        }
    }
}