Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2014 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 org.copticchurchlibrary.arabicreader.model;
  18.  
  19. import android.support.v4.media.MediaMetadataCompat;
  20.  
  21. import org.copticchurchlibrary.arabicreader.utils.LogHelper;
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.net.URL;
  30. import java.net.URLConnection;
  31. import java.util.ArrayList;
  32. import java.util.Iterator;
  33.  
  34. /**
  35. * Utility class to get a list of MusicTrack's based on a server-side JSON
  36. * configuration.
  37. */
  38. public class RemoteJSONSource implements MusicProviderSource {
  39.  
  40. private static final String TAG = LogHelper.makeLogTag(RemoteJSONSource.class);
  41.  
  42. protected static final String CATALOG_URL =
  43. "http://copticchurch-library.org/media/music.json";
  44.  
  45. private static final String JSON_MUSIC = "music";
  46. private static final String JSON_TITLE = "title";
  47. private static final String JSON_ALBUM = "album";
  48. private static final String JSON_ARTIST = "artist";
  49. private static final String JSON_GENRE = "genre";
  50. private static final String JSON_SOURCE = "source";
  51. private static final String JSON_IMAGE = "image";
  52. private static final String JSON_TRACK_NUMBER = "trackNumber";
  53. private static final String JSON_TOTAL_TRACK_COUNT = "totalTrackCount";
  54. private static final String JSON_DURATION = "duration";
  55.  
  56. @Override
  57. public Iterator<MediaMetadataCompat> iterator() {
  58. try {
  59. int slashPos = CATALOG_URL.lastIndexOf('/');
  60. String path = CATALOG_URL.substring(0, slashPos + 1);
  61. JSONObject jsonObj = fetchJSONFromUrl(CATALOG_URL);
  62. ArrayList<MediaMetadataCompat> tracks = new ArrayList<>();
  63. if (jsonObj != null) {
  64. JSONArray jsonTracks = jsonObj.getJSONArray(JSON_MUSIC);
  65.  
  66. if (jsonTracks != null) {
  67. for (int j = 0; j < jsonTracks.length(); j++) {
  68. tracks.add(buildFromJSON(jsonTracks.getJSONObject(j), path));
  69. }
  70. }
  71. }
  72. return tracks.iterator();
  73. } catch (JSONException e) {
  74. LogHelper.e(TAG, e, "Could not retrieve music list");
  75. throw new RuntimeException("Could not retrieve music list", e);
  76. }
  77. }
  78.  
  79. private MediaMetadataCompat buildFromJSON(JSONObject json, String basePath) throws JSONException {
  80. String title = json.getString(JSON_TITLE);
  81. String album = json.getString(JSON_ALBUM);
  82. String artist = json.getString(JSON_ARTIST);
  83. String genre = json.getString(JSON_GENRE);
  84. String source = json.getString(JSON_SOURCE);
  85. String iconUrl = json.getString(JSON_IMAGE);
  86. int trackNumber = json.getInt(JSON_TRACK_NUMBER);
  87. int totalTrackCount = json.getInt(JSON_TOTAL_TRACK_COUNT);
  88. int duration = json.getInt(JSON_DURATION) * 1000; // ms
  89.  
  90. LogHelper.d(TAG, "Found music track: ", json);
  91.  
  92. // Media is stored relative to JSON file
  93. if (!source.startsWith("http")) {
  94. source = basePath + source;
  95. }
  96. if (!iconUrl.startsWith("http")) {
  97. iconUrl = basePath + iconUrl;
  98. }
  99. // Since we don't have a unique ID in the server, we fake one using the hashcode of
  100. // the music source. In a real world app, this could come from the server.
  101. String id = String.valueOf(source.hashCode());
  102.  
  103. // Adding the music source to the MediaMetadata (and consequently using it in the
  104. // mediaSession.setMetadata) is not a good idea for a real world music app, because
  105. // the session metadata can be accessed by notification listeners. This is done in this
  106. // sample for convenience only.
  107. //noinspection ResourceType
  108. return new MediaMetadataCompat.Builder()
  109. .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, id)
  110. .putString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE, source)
  111. .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, album)
  112. .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
  113. .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration)
  114. .putString(MediaMetadataCompat.METADATA_KEY_GENRE, genre)
  115. .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, iconUrl)
  116. .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
  117. .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, trackNumber)
  118. .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, totalTrackCount)
  119. .build();
  120. }
  121.  
  122. /**
  123. * Download a JSON file from a server, parse the content and return the JSON
  124. * object.
  125. *
  126. * @return result JSONObject containing the parsed representation.
  127. */
  128. private JSONObject fetchJSONFromUrl(String urlString) throws JSONException {
  129. BufferedReader reader = null;
  130. try {
  131. URLConnection urlConnection = new URL(urlString).openConnection();
  132. reader = new BufferedReader(new InputStreamReader(
  133. urlConnection.getInputStream(), "iso-8859-1"));
  134. StringBuilder sb = new StringBuilder();
  135. String line;
  136. while ((line = reader.readLine()) != null) {
  137. sb.append(line);
  138. }
  139. return new JSONObject(sb.toString());
  140. } catch (JSONException e) {
  141. throw e;
  142. } catch (Exception e) {
  143. LogHelper.e(TAG, "Failed to parse the json for media list", e);
  144. return null;
  145. } finally {
  146. if (reader != null) {
  147. try {
  148. reader.close();
  149. } catch (IOException e) {
  150. // ignore
  151. }
  152. }
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement