private static String buildImagePath(Image image, int width) { if (image.isHatchetImage()) { int imageSize = Math.min(image.getHeight(), image.getWidth()); int actualWidth; if (NetworkUtils.isWifiAvailable()) { actualWidth = Math.min(imageSize, width); } else { actualWidth = Math.min(imageSize, width * 2 / 3); } return image.getImagePath() + "?width=" + actualWidth + "&height=" + actualWidth; } return image.getImagePath(); }
/** * Load a {@link android.graphics.Bitmap} asynchronously * * @param context the context needed for fetching resources * @param imageView the {@link android.widget.ImageView}, which will be used to show the {@link * android.graphics.Bitmap} * @param image the path to load the image from * @param width the width in pixels to scale the image down to */ public static void loadImageIntoImageView( Context context, ImageView imageView, Image image, int width, boolean fit, boolean isArtistImage) { int placeHolder = isArtistImage ? R.drawable.artist_placeholder_grid : R.drawable.album_placeholder_grid; if (image != null && !TextUtils.isEmpty(image.getImagePath())) { String imagePath = buildImagePath(image, width); RequestCreator creator = Picasso.with(context) .load(TomahawkUtils.preparePathForPicasso(imagePath)) .placeholder(placeHolder) .error(placeHolder); if (fit) { creator.resize(width, width); } creator.into(imageView); } else { RequestCreator creator = Picasso.with(context).load(placeHolder).placeholder(placeHolder).error(placeHolder); if (fit) { creator.resize(width, width); } creator.into(imageView); } }
public void fillView(final Album album, Collection collection) { if (collection == null) { collection = CollectionManager.getInstance().getCollection(TomahawkApp.PLUGINNAME_HATCHET); } TextView textView1 = (TextView) findViewById(R.id.textview1); textView1.setText(album.getPrettyName()); TextView textView2 = (TextView) findViewById(R.id.textview2); textView2.setText(album.getArtist().getPrettyName()); ImageView imageView1 = (ImageView) findViewById(R.id.imageview1); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), imageView1, album.getImage(), Image.getSmallImageSize(), false); final TextView textView3 = (TextView) findViewById(R.id.textview3); textView3.setVisibility(View.INVISIBLE); collection .getAlbumTracks(album) .done( new DoneCallback<CollectionCursor<Query>>() { @Override public void onDone(CollectionCursor<Query> cursor) { int size = 0; if (cursor != null) { size = cursor.size(); cursor.close(); } textView3.setVisibility(View.VISIBLE); textView3.setText( TomahawkApp.getContext() .getResources() .getQuantityString(R.plurals.songs_with_count, size, size)); } }); }
public void fillView(Artist artist) { TextView textView1 = (TextView) findViewById(R.id.textview1); textView1.setText(artist.getPrettyName()); ImageView imageView1 = (ImageView) findViewById(R.id.imageview1); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), imageView1, artist.getImage(), Image.getSmallImageSize(), true); }
/** * Load a {@link android.graphics.Bitmap} asynchronously * * @param context the context needed for fetching resources * @param image the path to load the image from * @param target the Target which the loaded image will be pushed to * @param width the width in pixels to scale the image down to */ public static void loadImageIntoBitmap( Context context, Image image, Target target, int width, boolean isArtistImage) { int placeHolder = isArtistImage ? R.drawable.artist_placeholder_grid : R.drawable.album_placeholder_grid; if (image != null && !TextUtils.isEmpty(image.getImagePath())) { String imagePath = buildImagePath(image, width); Picasso.with(context) .load(TomahawkUtils.preparePathForPicasso(imagePath)) .resize(width, width) .into(target); } else { Picasso.with(context).load(placeHolder).resize(width, width).into(target); } }
public void fillView(User user) { TextView textView1 = (TextView) findViewById(R.id.textview1); textView1.setText(user.getName()); if (mLayoutId == R.layout.list_item_user) { TextView textView2 = (TextView) findViewById(R.id.textview2); textView2.setText( TomahawkApp.getContext() .getString( R.string.followers_count, user.getFollowersCount(), user.getFollowCount())); } TextView userTextView1 = (TextView) findViewById(R.id.usertextview1); ImageView userImageView1 = (ImageView) findViewById(R.id.userimageview1); TomahawkUtils.loadUserImageIntoImageView( TomahawkApp.getContext(), userImageView1, user, Image.getSmallImageSize(), userTextView1); }
public void fillHeaderView(SocialAction socialAction, int segmentSize) { ImageView userImageView1 = (ImageView) findViewById(R.id.userimageview1); TextView userTextView = (TextView) findViewById(R.id.usertextview1); TomahawkUtils.loadUserImageIntoImageView( TomahawkApp.getContext(), userImageView1, socialAction.getUser(), Image.getSmallImageSize(), userTextView); Object targetObject = socialAction.getTargetObject(); Resources resources = TomahawkApp.getContext().getResources(); String userName = socialAction.getUser().getName(); String phrase = "!FIXME! type: " + socialAction.getType() + ", action: " + socialAction.getAction() + ", user: "******":"); }
private void setupAlbumArt(View view) { if (mAlbum != null || (mQuery != null && !TextUtils.isEmpty(mQuery.getAlbum().getName())) || (mPlaylistEntry != null && !TextUtils.isEmpty(mPlaylistEntry.getQuery().getAlbum().getName()))) { View v = ViewUtils.ensureInflation( view, R.id.context_menu_albumart_stub, R.id.context_menu_albumart); // load albumart image ImageView albumImageView = (ImageView) v.findViewById(R.id.album_imageview); Album album; String cacheKey; if (mAlbum != null) { album = mAlbum; cacheKey = mAlbum.getCacheKey(); } else if (mQuery != null) { album = mQuery.getAlbum(); cacheKey = mQuery.getAlbum().getCacheKey(); } else { album = mPlaylistEntry.getAlbum(); cacheKey = mPlaylistEntry.getAlbum().getCacheKey(); } if (album.getImage() != null) { ImageUtils.loadImageIntoImageView( TomahawkApp.getContext(), albumImageView, album.getImage(), Image.getLargeImageSize(), true, false); } else { String requestId = InfoSystem.get().resolve(album); if (requestId != null) { mCorrespondingRequestIds.add(requestId); } } // set text on "view album"-button and set up click listener View viewAlbumButton = view.findViewById(R.id.view_album_button); TextView viewAlbumButtonText = (TextView) viewAlbumButton.findViewById(R.id.textview); viewAlbumButtonText.setText( TomahawkApp.getContext().getString(R.string.view_album).toUpperCase()); viewAlbumButton.setOnClickListener(constructAlbumNameClickListener(cacheKey)); } }
private static String buildImagePath(Image image, int width) { if (image.isHatchetImage()) { int squareImageWidth = Math.min(image.getHeight(), image.getWidth()); if (isWifiAvailable()) { if (squareImageWidth > width) { return image.getImagePath() + "?width=" + width + "&height=" + width; } } else if (squareImageWidth > width * 2 / 3) { return image.getImagePath() + "?width=" + width * 2 / 3 + "&height=" + width * 2 / 3; } } return image.getImagePath(); }
@SuppressWarnings("unused") public void onEventMainThread(InfoSystem.ResultsEvent event) { if (mCorrespondingRequestIds.contains(event.mInfoRequestData.getRequestId()) && getView() != null) { ImageView albumImageView = (ImageView) getView().findViewById(R.id.album_imageview); Album album; if (mAlbum != null) { album = mAlbum; } else if (mQuery != null) { album = mQuery.getAlbum(); } else { album = mPlaylistEntry.getAlbum(); } ImageUtils.loadImageIntoImageView( TomahawkApp.getContext(), albumImageView, album.getImage(), Image.getLargeImageSize(), true, false); } }
/** * Load a {@link android.graphics.Bitmap} asynchronously * * @param context the context needed for fetching resources * @param imageView the {@link android.widget.ImageView}, which will be used to show the {@link * android.graphics.Bitmap} * @param image the path to load the image from * @param width the width in density independent pixels to scale the image down to */ public static void loadBlurredImageIntoImageView( Context context, ImageView imageView, Image image, int width, int placeHolderResId) { if (image != null && !TextUtils.isEmpty(image.getImagePath())) { String imagePath = buildImagePath(image, width); RequestCreator creator = Picasso.with(context) .load(TomahawkUtils.preparePathForPicasso(imagePath)) .resize(width, width); if (placeHolderResId > 0) { creator.placeholder(placeHolderResId); creator.error(placeHolderResId); } creator.transform(new BlurTransformation()); creator.into(imageView); } else { RequestCreator creator = Picasso.with(context) .load(placeHolderResId) .placeholder(placeHolderResId) .error(placeHolderResId); creator.into(imageView); } }
/** * Load a {@link android.graphics.Bitmap} asynchronously * * @param context the context needed for fetching resources * @param image the path to load the image from * @param width the width in pixels to scale the image down to */ public static void loadImageIntoNotification( Context context, Image image, RemoteViews remoteViews, int viewId, int notificationId, Notification notification, int width, boolean isArtistImage) { int placeHolder = isArtistImage ? R.drawable.artist_placeholder_grid : R.drawable.album_placeholder_grid; if (image != null && !TextUtils.isEmpty(image.getImagePath())) { String imagePath = buildImagePath(image, width); Picasso.with(context) .load(TomahawkUtils.preparePathForPicasso(imagePath)) .resize(width, width) .into(remoteViews, viewId, notificationId, notification); } else { Picasso.with(context) .load(placeHolder) .resize(width, width) .into(remoteViews, viewId, notificationId, notification); } }
private static void fillView( View view, List<Image> artistImages, int height, boolean isPagerFragment) { View v; int gridOneResId = isPagerFragment ? R.id.imageview_grid_one_pager : R.id.imageview_grid_one; int gridTwoResId = isPagerFragment ? R.id.imageview_grid_two_pager : R.id.imageview_grid_two; int gridThreeResId = isPagerFragment ? R.id.imageview_grid_three_pager : R.id.imageview_grid_three; int gridOneStubId = isPagerFragment ? R.id.imageview_grid_one_pager_stub : R.id.imageview_grid_one_stub; int gridTwoStubId = isPagerFragment ? R.id.imageview_grid_two_pager_stub : R.id.imageview_grid_two_stub; int gridThreeStubId = isPagerFragment ? R.id.imageview_grid_three_pager_stub : R.id.imageview_grid_three_stub; if (artistImages.size() > 2) { v = view.findViewById(gridOneResId); if (v != null) { v.setVisibility(View.GONE); } v = view.findViewById(gridTwoResId); if (v != null) { v.setVisibility(View.GONE); } v = TomahawkUtils.ensureInflation(view, gridThreeStubId, gridThreeResId); v.setVisibility(View.VISIBLE); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview1), artistImages.get(0), Image.getLargeImageSize(), false); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview2), artistImages.get(1), Image.getSmallImageSize(), false); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview3), artistImages.get(2), Image.getSmallImageSize(), false); } else if (artistImages.size() > 1) { v = view.findViewById(gridOneResId); if (v != null) { v.setVisibility(View.GONE); } v = view.findViewById(gridThreeResId); if (v != null) { v.setVisibility(View.GONE); } v = TomahawkUtils.ensureInflation(view, gridTwoStubId, gridTwoResId); v.setVisibility(View.VISIBLE); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview1), artistImages.get(0), Image.getLargeImageSize(), false); TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview2), artistImages.get(1), Image.getSmallImageSize(), false); } else { v = view.findViewById(gridTwoResId); if (v != null) { v.setVisibility(View.GONE); } v = view.findViewById(gridThreeResId); if (v != null) { v.setVisibility(View.GONE); } v = TomahawkUtils.ensureInflation(view, gridOneStubId, gridOneResId); v.setVisibility(View.VISIBLE); if (artistImages.size() > 0) { TomahawkUtils.loadImageIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview1), artistImages.get(0), Image.getLargeImageSize(), false); } else { TomahawkUtils.loadDrawableIntoImageView( TomahawkApp.getContext(), (ImageView) v.findViewById(R.id.imageview1), R.drawable.album_placeholder_grid); } } if (height > 0) { v.getLayoutParams().height = height; } }
public List storeRecords( JsonObject object, int resultType, int requestType, boolean isBackgroundRequest, QueryParams params) throws IOException { List results = new ArrayList(); JsonElement elements = object.get("images"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Image image = (Image) mCache.get(TYPE_IMAGES).get(id); if (image == null) { String url = getAsString(o, "url"); int width = getAsInt(o, "width"); int height = getAsInt(o, "height"); image = Image.get(url, true, width, height); mCache.get(TYPE_IMAGES).put(id, image); } if (resultType == TYPE_IMAGES) { results.add(image); } } } } elements = object.get("artists"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Artist artist = (Artist) mCache.get(TYPE_ARTISTS).get(id); if (artist == null) { String name = getAsString(o, "name"); String wiki = getAsString(o, "wikiabstract"); artist = Artist.get(name); artist.setBio(new ListItemString(wiki)); JsonElement images = get(o, "images"); if (images instanceof JsonArray && ((JsonArray) images).size() > 0) { String imageId = ((JsonArray) images).get(0).getAsString(); Image image = (Image) findRecord(imageId, TYPE_IMAGES, isBackgroundRequest); artist.setImage(image); } mCache.get(TYPE_ARTISTS).put(id, artist); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_ALBUMS) { JsonElement rawAlbums = get(o, "albums"); if (rawAlbums instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawAlbums, TYPE_ALBUMS, isBackgroundRequest)); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_TOPHITS) { JsonElement rawTopHits = get(o, "topHits"); if (rawTopHits instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawTopHits, TYPE_TRACKS, isBackgroundRequest)); } } if (resultType == TYPE_ARTISTS) { results.add(artist); } } } } elements = object.get("albums"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Album album = (Album) mCache.get(TYPE_ALBUMS).get(id); if (album == null) { String name = getAsString(o, "name"); String artistId = getAsString(o, "artist"); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); album = Album.get(name, artist); JsonElement images = get(o, "images"); if (images instanceof JsonArray && ((JsonArray) images).size() > 0) { String imageId = ((JsonArray) images).get(0).getAsString(); Image image = (Image) findRecord(imageId, TYPE_IMAGES, isBackgroundRequest); album.setImage(image); } mCache.get(TYPE_ALBUMS).put(id, album); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_ALBUMS_TRACKS) { JsonElement rawTracks = get(o, "tracks"); if (rawTracks instanceof JsonObject) { results.addAll( storeRecords((JsonObject) rawTracks, TYPE_TRACKS, isBackgroundRequest)); } } if (resultType == TYPE_ALBUMS) { results.add(album); } } } } elements = object.get("tracks"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_TRACKS).get(id); if (query == null) { String name = getAsString(o, "name"); String artistId = getAsString(o, "artist"); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); query = Query.get(name, null, artist.getName(), false, true); mCache.get(TYPE_TRACKS).put(id, query); } if (resultType == TYPE_TRACKS) { results.add(query); } } } } elements = object.get("users"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); User user = User.get(id); String name = getAsString(o, "name"); user.setName(name); String about = getAsString(o, "about"); user.setAbout(about); int followersCount = getAsInt(o, "followersCount"); user.setFollowersCount(followersCount); int followCount = getAsInt(o, "followCount"); user.setFollowCount(followCount); String nowplayingId = getAsString(o, "nowplaying"); if (nowplayingId != null) { Query nowplaying = (Query) findRecord(nowplayingId, TYPE_TRACKS, isBackgroundRequest); user.setNowPlaying(nowplaying); } String nowplayingtimestamp = getAsString(o, "nowplayingtimestamp"); user.setNowPlayingTimeStamp(ISO8601Utils.parse(nowplayingtimestamp)); String avatar = getAsString(o, "avatar"); if (avatar != null) { Image image = (Image) findRecord(avatar, TYPE_IMAGES, isBackgroundRequest); user.setImage(image); } if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYLISTS) { JsonElement rawPlaylists = get(o, "playlists"); if (rawPlaylists instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaylists, TYPE_PLAYLISTS, isBackgroundRequest); user.setPlaylists(playlists); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDITEMS) { JsonElement rawLovedItems = get(o, "lovedItems"); if (rawLovedItems instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawLovedItems, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setFavorites((Playlist) playlists.get(0)); } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDALBUMS) { JsonElement rawLovedAlbums = get(o, "lovedAlbums"); if (rawLovedAlbums instanceof JsonObject) { List albums = storeRecords((JsonObject) rawLovedAlbums, TYPE_ALBUMS, isBackgroundRequest); user.setStarredAlbums(albums); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDARTISTS) { JsonElement rawLovedArtists = get(o, "lovedArtists"); if (rawLovedArtists instanceof JsonObject) { List artists = storeRecords((JsonObject) rawLovedArtists, TYPE_ARTISTS, isBackgroundRequest); user.setStarredArtists(artists); } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG) { JsonElement rawPlaybackLog = get(o, "playbacklog"); if (rawPlaybackLog instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaybackLog, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setPlaybackLog((Playlist) playlists.get(0)); } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWS || requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWERS) { boolean isFollows = requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FOLLOWS; JsonElement rawFollows = get(o, isFollows ? "follows" : "followers"); if (rawFollows instanceof JsonObject) { JsonObject follows = (JsonObject) rawFollows; storeRecords(follows, -1, isBackgroundRequest); JsonElement relationships = get(follows, "relationships"); if (relationships instanceof JsonArray) { TreeMap<User, String> followsMap = new TreeMap<>(new AlphaComparator()); for (JsonElement relationship : (JsonArray) relationships) { JsonObject relationshipObj = (JsonObject) relationship; String relationshipId = getAsString(relationshipObj, "id"); String userId = getAsString(relationshipObj, isFollows ? "targetUser" : "user"); User followedUser = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); followsMap.put(followedUser, relationshipId); } if (isFollows) { user.setFollowings(followsMap); } else { user.setFollowers(followsMap); } } } } else if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG) { JsonElement rawPlaybackLog = get(o, "playbacklog"); if (rawPlaybackLog instanceof JsonObject) { List playlists = storeRecords((JsonObject) rawPlaybackLog, TYPE_PLAYLISTS, isBackgroundRequest); if (playlists != null && playlists.size() > 0) { user.setPlaybackLog((Playlist) playlists.get(0)); } } } mCache.get(TYPE_USERS).put(id, user); if (resultType == TYPE_USERS) { results.add(user); } } } } elements = object.get("playlistEntries"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_PLAYLISTENTRIES).get(id); if (query == null) { String trackId = getAsString(o, "track"); query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); mCache.get(TYPE_PLAYLISTENTRIES).put(id, query); } if (resultType == TYPE_PLAYLISTENTRIES) { results.add(query); } } } } elements = object.get("playlists"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); String title = getAsString(o, "title"); String currentrevision = getAsString(o, "currentrevision"); String localId = DatabaseHelper.get().getPlaylistLocalId(id); if (localId == null) { localId = id; } Playlist playlist = null; if (requestType == InfoRequestData.INFOREQUESTDATA_TYPE_PLAYLISTS_PLAYLISTENTRIES) { JsonElement rawEntries = get(o, "playlistEntries"); if (rawEntries instanceof JsonObject) { List<PlaylistEntry> entries = storeRecords((JsonObject) rawEntries, TYPE_PLAYLISTENTRIES, isBackgroundRequest); if (entries != null) { playlist = Playlist.fromEntriesList(localId, null, null, entries); playlist.setFilled(true); } } } else { JsonElement entryIds = o.get("playlistEntries"); if (entryIds instanceof JsonArray) { List<Query> queries = new ArrayList<>(); for (JsonElement entryId : (JsonArray) entryIds) { Query query = (Query) findRecord( entryId.getAsString(), TYPE_PLAYLISTENTRIES, isBackgroundRequest); queries.add(query); } playlist = Playlist.fromQueryList(localId, null, null, queries); playlist.setFilled(true); } } if (playlist == null) { playlist = Playlist.get(localId); } playlist.setName(title); playlist.setCurrentRevision(currentrevision); playlist.setHatchetId(id); JsonElement popularArtists = get(o, "popularArtists"); if (popularArtists instanceof JsonArray) { ArrayList<String> topArtistNames = new ArrayList<>(); for (JsonElement popularArtist : (JsonArray) popularArtists) { String artistId = popularArtist.getAsString(); Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); if (artist != null) { topArtistNames.add(artist.getName()); } } playlist.setTopArtistNames(topArtistNames.toArray(new String[topArtistNames.size()])); } mCache.get(TYPE_PLAYLISTS).put(id, playlist); if (resultType == TYPE_PLAYLISTS) { results.add(playlist); } } } } elements = object.get("playbacklogEntries"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); Query query = (Query) mCache.get(TYPE_PLAYLISTENTRIES).get(id); if (query == null) { String trackId = getAsString(o, "track"); query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); mCache.get(TYPE_PLAYLISTENTRIES).put(id, query); } if (resultType == TYPE_PLAYLISTENTRIES) { results.add(query); } } } } elements = object.get("playbacklogs"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); JsonArray playbacklogEntries = get(o, "playbacklogEntries").getAsJsonArray(); ArrayList<Query> queries = new ArrayList<>(); for (JsonElement entry : playbacklogEntries) { String entryId = entry.getAsString(); Query query = (Query) findRecord(entryId, TYPE_PLAYLISTENTRIES, isBackgroundRequest); queries.add(query); } Playlist playlist = Playlist.fromQueryList( TomahawkMainActivity.getLifetimeUniqueStringId(), "Playbacklog", null, queries); playlist.setHatchetId(id); playlist.setFilled(true); mCache.get(TYPE_PLAYLISTS).put(id, playlist); if (resultType == TYPE_PLAYLISTS) { results.add(playlist); } } } } elements = object.get("socialActions"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); SocialAction socialAction = (SocialAction) mCache.get(TYPE_SOCIALACTIONS).get(id); if (socialAction == null) { socialAction = SocialAction.get(id); String action = getAsString(o, "action"); socialAction.setAction(action); String date = getAsString(o, "date"); socialAction.setDate(ISO8601Utils.parse(date)); String actionType = getAsString(o, "type"); socialAction.setType(actionType); String trackId = getAsString(o, "track"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); socialAction.setQuery(query); } String artistId = getAsString(o, "artist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); socialAction.setArtist(artist); } String albumId = getAsString(o, "album"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); socialAction.setAlbum(album); } String userId = getAsString(o, "user"); if (userId != null) { User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); socialAction.setUser(user); } String targetId = getAsString(o, "target"); if (targetId != null) { User target = (User) findRecord(targetId, TYPE_USERS, isBackgroundRequest); socialAction.setTarget(target); } String playlistId = getAsString(o, "playlist"); if (playlistId != null) { Playlist playlist = (Playlist) findRecord(playlistId, TYPE_PLAYLISTS, isBackgroundRequest); socialAction.setPlaylist(playlist); } mCache.get(TYPE_SOCIALACTIONS).put(id, socialAction); } if (resultType == TYPE_SOCIALACTIONS) { results.add(socialAction); } } } if (params != null) { User user = (User) findRecord(params.userid, TYPE_USERS, false); if (user != null) { if (HatchetInfoPlugin.HATCHET_SOCIALACTION_PARAMTYPE_FRIENDSFEED.equals(params.type)) { user.setFriendsFeed(results, params.before_date); } else { user.setSocialActions(results, params.before_date); } } } } elements = object.get("searchResults"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); SearchResult searchResult = (SearchResult) mCache.get(TYPE_SEARCHRESULTS).get(id); if (searchResult == null) { float score = getAsFloat(o, "score"); String trackId = getAsString(o, "track"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); searchResult = new SearchResult(score, query); } String artistId = getAsString(o, "artist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); searchResult = new SearchResult(score, artist); } String albumId = getAsString(o, "album"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); searchResult = new SearchResult(score, album); } String userId = getAsString(o, "user"); if (userId != null) { User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); searchResult = new SearchResult(score, user); } String playlistId = getAsString(o, "playlist"); if (playlistId != null) { Playlist playlist = (Playlist) findRecord(playlistId, TYPE_PLAYLISTS, isBackgroundRequest); searchResult = new SearchResult(score, playlist); } if (searchResult == null) { throw new IOException("searchResult contained no actual result object!"); } mCache.get(TYPE_SEARCHRESULTS).put(id, searchResult); } if (resultType == TYPE_SEARCHRESULTS) { results.add(searchResult); } } } } elements = object.get("searches"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); JsonArray rawSearchResults = get(o, "searchResults").getAsJsonArray(); ArrayList<SearchResult> searchResults = new ArrayList<>(); for (JsonElement rawSearchResult : rawSearchResults) { String resultId = rawSearchResult.getAsString(); SearchResult searchResult = (SearchResult) findRecord(resultId, TYPE_SEARCHRESULTS, isBackgroundRequest); searchResults.add(searchResult); } Search search = new Search(searchResults); mCache.get(TYPE_SEARCHES).put(id, search); if (resultType == TYPE_SEARCHES) { results.add(search); } } } } elements = object.get("relationships"); if (elements instanceof JsonArray) { for (JsonElement element : (JsonArray) elements) { if (element instanceof JsonObject) { JsonObject o = (JsonObject) element; String id = getAsString(o, "id"); String type = getAsString(o, "type"); if (type.equals(HatchetInfoPlugin.HATCHET_RELATIONSHIPS_TYPE_LOVE)) { String userId = getAsString(o, "user"); User user = (User) findRecord(userId, TYPE_USERS, isBackgroundRequest); String trackId = getAsString(o, "targetTrack"); if (trackId != null) { Query query = (Query) findRecord(trackId, TYPE_TRACKS, isBackgroundRequest); user.putRelationShipId(query, id); } String albumId = getAsString(o, "targetAlbum"); if (albumId != null) { Album album = (Album) findRecord(albumId, TYPE_ALBUMS, isBackgroundRequest); user.putRelationShipId(album, id); } String artistId = getAsString(o, "targetArtist"); if (artistId != null) { Artist artist = (Artist) findRecord(artistId, TYPE_ARTISTS, isBackgroundRequest); user.putRelationShipId(artist, id); } } } } } return results; }