2016-03-15 14:42:53 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Martijn Brekhof. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
package org.xbmc.kore.testutils;
|
2016-03-15 14:42:53 +01:00
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import org.xbmc.kore.host.HostInfo;
|
|
|
|
import org.xbmc.kore.host.HostManager;
|
|
|
|
import org.xbmc.kore.jsonrpc.ApiException;
|
2016-09-26 19:31:22 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.ApiList;
|
2016-03-15 14:42:53 +01:00
|
|
|
import org.xbmc.kore.jsonrpc.method.AudioLibrary;
|
|
|
|
import org.xbmc.kore.jsonrpc.method.VideoLibrary;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.AudioType;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.LibraryType;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.VideoType;
|
|
|
|
import org.xbmc.kore.provider.MediaContract;
|
2016-09-23 20:08:22 +02:00
|
|
|
import org.xbmc.kore.service.library.SyncMusic;
|
2016-03-15 14:42:53 +01:00
|
|
|
import org.xbmc.kore.service.library.SyncUtils;
|
|
|
|
import org.xbmc.kore.utils.LogUtils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class Database {
|
|
|
|
public static final String TAG = LogUtils.makeLogTag(Database.class);
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
public static HostInfo fill(Context context, ContentResolver contentResolver) throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
HostInfo hostInfo = addHost(context);
|
2016-11-08 12:43:10 +01:00
|
|
|
|
2016-09-23 20:08:22 +02:00
|
|
|
SyncMusic syncMusic = new SyncMusic(hostInfo.getId(), null);
|
2016-11-08 12:43:10 +01:00
|
|
|
|
|
|
|
insertMovies(context, contentResolver, hostInfo.getId());
|
|
|
|
insertArtists(context, contentResolver, syncMusic);
|
|
|
|
insertGenres(context, contentResolver, syncMusic);
|
|
|
|
insertAlbums(context, contentResolver, syncMusic);
|
|
|
|
insertSongs(context, contentResolver, syncMusic);
|
2016-03-15 14:42:53 +01:00
|
|
|
|
|
|
|
return hostInfo;
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
public static void flush(ContentResolver contentResolver, HostInfo hostInfo) {
|
|
|
|
contentResolver.delete(MediaContract.Hosts.buildHostUri(hostInfo.getId()), null, null);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static HostInfo addHost(Context context) {
|
2016-11-08 12:43:10 +01:00
|
|
|
return HostManager.getInstance(context).addHost("TestHost", "127.0.0.1", 1, 80, 9090, null,
|
|
|
|
null, "52:54:00:12:35:02", 9, false, 9777);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
public static void insertMovies(Context context, ContentResolver contentResolver, int hostId)
|
|
|
|
throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
VideoLibrary.GetMovies getMovies = new VideoLibrary.GetMovies();
|
2016-11-08 12:43:10 +01:00
|
|
|
String result = FileUtils.readFile(context, "Video.Details.Movie.json");
|
2016-09-26 19:31:22 +02:00
|
|
|
ApiList<VideoType.DetailsMovie> movieList = getMovies.resultFromJson(result);
|
2016-03-15 14:42:53 +01:00
|
|
|
|
|
|
|
|
2016-09-26 19:31:22 +02:00
|
|
|
ContentValues movieValuesBatch[] = new ContentValues[movieList.items.size()];
|
2016-03-15 14:42:53 +01:00
|
|
|
int castCount = 0;
|
|
|
|
|
|
|
|
// Iterate on each movie
|
2016-09-26 19:31:22 +02:00
|
|
|
for (int i = 0; i < movieList.items.size(); i++) {
|
|
|
|
VideoType.DetailsMovie movie = movieList.items.get(i);
|
2016-03-15 14:42:53 +01:00
|
|
|
movieValuesBatch[i] = SyncUtils.contentValuesFromMovie(hostId, movie);
|
|
|
|
castCount += movie.cast.size();
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
contentResolver.bulkInsert(MediaContract.Movies.CONTENT_URI, movieValuesBatch);
|
2016-03-15 14:42:53 +01:00
|
|
|
|
|
|
|
ContentValues movieCastValuesBatch[] = new ContentValues[castCount];
|
|
|
|
int count = 0;
|
|
|
|
// Iterate on each movie/cast
|
2016-09-26 19:31:22 +02:00
|
|
|
for (VideoType.DetailsMovie movie : movieList.items) {
|
2016-03-15 14:42:53 +01:00
|
|
|
for (VideoType.Cast cast : movie.cast) {
|
|
|
|
movieCastValuesBatch[count] = SyncUtils.contentValuesFromCast(hostId, cast);
|
|
|
|
movieCastValuesBatch[count].put(MediaContract.MovieCastColumns.MOVIEID, movie.movieid);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
contentResolver.bulkInsert(MediaContract.MovieCast.CONTENT_URI, movieCastValuesBatch);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
private static void insertArtists(Context context, ContentResolver contentResolver, SyncMusic syncMusic) throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
AudioLibrary.GetArtists getArtists = new AudioLibrary.GetArtists(false);
|
2016-11-08 12:43:10 +01:00
|
|
|
String result = FileUtils.readFile(context, "AudioLibrary.GetArtists.json");
|
2016-03-15 14:42:53 +01:00
|
|
|
ArrayList<AudioType.DetailsArtist> artistList = (ArrayList) getArtists.resultFromJson(result).items;
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
syncMusic.insertArtists(artistList, contentResolver);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
private static void insertGenres(Context context, ContentResolver contentResolver, SyncMusic syncMusic) throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
AudioLibrary.GetGenres getGenres = new AudioLibrary.GetGenres();
|
2016-11-08 12:43:10 +01:00
|
|
|
ArrayList<LibraryType.DetailsGenre> genreList =
|
|
|
|
(ArrayList) getGenres.resultFromJson(FileUtils.readFile(context,
|
|
|
|
"AudioLibrary.GetGenres.json"));
|
2016-03-15 14:42:53 +01:00
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
syncMusic.insertGenresItems(genreList, contentResolver);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
private static void insertAlbums(Context context, ContentResolver contentResolver, SyncMusic syncMusic) throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
AudioLibrary.GetAlbums getAlbums = new AudioLibrary.GetAlbums();
|
2016-11-08 12:43:10 +01:00
|
|
|
String result = FileUtils.readFile(context, "AudioLibrary.GetAlbums.json");
|
2016-03-15 14:42:53 +01:00
|
|
|
ArrayList<AudioType.DetailsAlbum> albumList = (ArrayList) getAlbums.resultFromJson(result).items;
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
syncMusic.insertAlbumsItems(albumList, contentResolver);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
private static void insertSongs(Context context, ContentResolver contentResolver, SyncMusic syncMusic) throws ApiException, IOException {
|
2016-03-15 14:42:53 +01:00
|
|
|
AudioLibrary.GetSongs getSongs = new AudioLibrary.GetSongs();
|
2016-09-23 20:08:22 +02:00
|
|
|
ArrayList<AudioType.DetailsSong> songList = (ArrayList)
|
2016-11-08 12:43:10 +01:00
|
|
|
getSongs.resultFromJson(FileUtils.readFile(context, "AudioLibrary.GetSongs.json")).items;
|
2016-03-15 14:42:53 +01:00
|
|
|
|
2016-11-08 12:43:10 +01:00
|
|
|
syncMusic.insertSongsItems(songList, contentResolver);
|
2016-03-15 14:42:53 +01:00
|
|
|
}
|
|
|
|
}
|