-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathSample.java
More file actions
243 lines (211 loc) · 7.3 KB
/
Sample.java
File metadata and controls
243 lines (211 loc) · 7.3 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.example.android.classicalmusicquiz;
/*
* Copyright (C) 2017 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.
*/
import static com.google.android.exoplayer2.upstream.DataSourceUtil.closeQuietly;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.JsonReader;
import android.widget.Toast;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.util.Util;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Java Object representing a single sample. Also includes utility methods for obtaining samples
* from assets.
*/
class Sample {
private int mSampleID;
private String mComposer;
private String mTitle;
private String mUri;
private String mAlbumArtID;
private Sample(int sampleID, String composer, String title, String uri, String albumArtID) {
mSampleID = sampleID;
mComposer = composer;
mTitle = title;
mUri = uri;
mAlbumArtID = albumArtID;
}
/**
* Gets portrait of the composer for a sample by the sample ID.
* @param context The application context.
* @param sampleID The sample ID.
* @return The portrait Bitmap.
*/
static Bitmap getComposerArtBySampleID(Context context, int sampleID){
Sample sample = Sample.getSampleByID(context, sampleID);
int albumArtID = context.getResources().getIdentifier(
sample != null ? sample.getAlbumArtID() : null, "drawable",
context.getPackageName());
return BitmapFactory.decodeResource(context.getResources(), albumArtID);
}
/**
* Gets a single sample by its ID.
* @param context The application context.
* @param sampleID The sample ID.
* @return The sample object.
*/
static Sample getSampleByID(Context context, int sampleID) {
JsonReader reader;
try {
reader = readJSONFile(context);
reader.beginArray();
while (reader.hasNext()) {
Sample currentSample = readEntry(reader);
if (currentSample.getSampleID() == sampleID) {
reader.close();
return currentSample;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Gets and ArrayList of the IDs for all of the Samples from the JSON file.
* @param context The application context.
* @return The ArrayList of all sample IDs.
*/
static ArrayList<Integer> getAllSampleIDs(Context context){
JsonReader reader;
ArrayList<Integer> sampleIDs = new ArrayList<>();
try {
reader = readJSONFile(context);
reader.beginArray();
while (reader.hasNext()) {
sampleIDs.add(readEntry(reader).getSampleID());
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return sampleIDs;
}
/**
* Method used for obtaining a single sample from the JSON file.
* @param reader The JSON reader object pointing a single sample JSON object.
* @return The Sample the JsonReader is pointing to.
*/
private static Sample readEntry(JsonReader reader) {
Integer id = -1;
String composer = null;
String title = null;
String uri = null;
String albumArtID = null;
try {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "name":
title = reader.nextString();
break;
case "id":
id = reader.nextInt();
break;
case "composer":
composer = reader.nextString();
break;
case "uri":
uri = reader.nextString();
break;
case "albumArtID":
albumArtID = reader.nextString();
break;
default:
break;
}
}
reader.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return new Sample(id, composer, title, uri, albumArtID);
}
/**
* Method for creating a JsonReader object that points to the JSON array of samples.
* @param context The application context.
* @return The JsonReader object pointing to the JSON array of samples.
* @throws IOException Exception thrown if the sample file can't be found.
*/
private static JsonReader readJSONFile(Context context) throws IOException {
AssetManager assetManager = context.getAssets();
String uri = null;
try {
for (String asset : assetManager.list("")) {
if (asset.endsWith(".exolist.json")) {
uri = "asset:///" + asset;
}
}
} catch (IOException e) {
Toast.makeText(context, R.string.sample_list_load_error, Toast.LENGTH_LONG)
.show();
}
String userAgent = Util.getUserAgent(context, "ClassicalMusicQuiz");
DataSource dataSource = new DefaultDataSource(context, userAgent,1000,1000, false);
DataSpec dataSpec = new DataSpec(Uri.parse(uri));
InputStream inputStream = new DataSourceInputStream(dataSource, dataSpec);
JsonReader reader = null;
try {
reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
} finally {
closeQuietly(dataSource);
}
return reader;
}
// Getters and Setters
String getTitle() {
return mTitle;
}
void setTitle(String title) {
mTitle = title;
}
String getUri() {
return mUri;
}
void setUri(String uri) {
mUri = uri;
}
int getSampleID() {
return mSampleID;
}
void setSampleID(int sampleID) {
mSampleID = sampleID;
}
String getComposer() {
return mComposer;
}
void setComposer(String composer) {
mComposer = composer;
}
String getAlbumArtID() {
return mAlbumArtID;
}
void setAlbumArtID(String albumArtID) {
mAlbumArtID = albumArtID;
}
}