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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* 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.loader;
import static android.support.v4.media.MediaBrowserCompat.MediaItem.FLAG_BROWSABLE;
import static android.support.v4.media.MediaBrowserCompat.MediaItem.FLAG_PLAYABLE;
import static com.android.car.media.testmediaapp.TmaMediaEvent.INSTANT_PLAYBACK;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.enumNamesToValues;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.getArray;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.getEnum;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.getEnumArray;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.getInt;
import static com.android.car.media.testmediaapp.loader.TmaLoaderUtils.getString;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.car.media.testmediaapp.TmaMediaEvent;
import com.android.car.media.testmediaapp.TmaMediaItem.TmaCustomAction;
import com.android.car.media.testmediaapp.TmaMediaItem;
import com.android.car.media.testmediaapp.TmaMediaItem.ContentStyle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class TmaMediaItemReader {
private final static String TAG = "TmaMediaItemReader";
/** The json keys to retrieve the properties. */
private enum Keys {
FLAGS,
PLAYABLE_HINT,
BROWSABLE_HINT,
METADATA,
SELF_UPDATE_MS,
CHILDREN,
INCLUDE,
CUSTOM_ACTIONS,
EVENTS
}
private static TmaMediaItemReader sInstance;
synchronized static TmaMediaItemReader getInstance() {
if (sInstance == null) {
sInstance = new TmaMediaItemReader();
}
return sInstance;
}
private final TmaMediaMetadataReader mMediaMetadataReader;
private final TmaMediaEventReader mMediaEventReader;
private final Map<String, Integer> mFlags = new HashMap<>(2);
private final Map<String, ContentStyle> mContentStyles;
private final Map<String, TmaCustomAction> mCustomActions;
private TmaMediaItemReader() {
mMediaMetadataReader = TmaMediaMetadataReader.getInstance();
mMediaEventReader = TmaMediaEventReader.getInstance();
mContentStyles = enumNamesToValues(ContentStyle.values());
mCustomActions = enumNamesToValues(TmaMediaItem.TmaCustomAction.values());
mFlags.put("browsable", FLAG_BROWSABLE);
mFlags.put("playable", FLAG_PLAYABLE);
}
@Nullable
TmaMediaItem fromJson(@Nullable JSONObject json) {
if (json == null) return null;
try {
// Media events
JSONArray events = getArray(json, Keys.EVENTS);
int eventsCount = (events != null) ? events.length() : 0;
List<TmaMediaEvent> mediaEvents = new ArrayList<>(eventsCount);
for (int i = 0; i < eventsCount ; i++) {
mediaEvents.add(mMediaEventReader.fromJson(events.getJSONObject(i)));
}
if (mediaEvents.size() <= 0) {
mediaEvents.add(INSTANT_PLAYBACK);
}
// Child items
JSONArray children = getArray(json, Keys.CHILDREN);
int childrenCount = (children != null) ? children.length() : 0;
List<TmaMediaItem> mediaItems = new ArrayList<>(childrenCount);
for (int i = 0; i < childrenCount ; i++) {
mediaItems.add(fromJson(children.getJSONObject(i)));
}
return new TmaMediaItem(TmaLoaderUtils.parseFlags(getString(json, Keys.FLAGS), mFlags),
getEnum(json, Keys.PLAYABLE_HINT, mContentStyles, ContentStyle.NONE),
getEnum(json, Keys.BROWSABLE_HINT, mContentStyles, ContentStyle.NONE),
mMediaMetadataReader.fromJson(json.getJSONObject(Keys.METADATA.name())),
getInt(json, Keys.SELF_UPDATE_MS),
getEnumArray(json, Keys.CUSTOM_ACTIONS, mCustomActions),
mediaEvents, mediaItems, getString(json, Keys.INCLUDE));
} catch (JSONException e) {
Log.e(TAG, "Json failure: " + e);
return null;
}
}
}
|