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));
             }
           });
 }
 private void setupPlaybackTextViews(View view, PlaybackPanel playbackPanel) {
   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.view_album_button_stub, R.id.view_album_button);
     TextView viewAlbumButtonText = (TextView) v.findViewById(R.id.textview);
     viewAlbumButtonText.setText(
         TomahawkApp.getContext().getString(R.string.view_album).toUpperCase());
     String cacheKey;
     if (mAlbum != null) {
       cacheKey = mAlbum.getCacheKey();
     } else if (mQuery != null) {
       cacheKey = mQuery.getAlbum().getCacheKey();
     } else {
       cacheKey = mPlaylistEntry.getAlbum().getCacheKey();
     }
     v.setOnClickListener(constructAlbumNameClickListener(cacheKey));
   }
   if (mAlbum != null || mQuery != null || mPlaylistEntry != null || mArtist != null) {
     View artistNameButton = playbackPanel.findViewById(R.id.artist_name_button);
     String cacheKey;
     if (mAlbum != null) {
       cacheKey = mAlbum.getArtist().getCacheKey();
     } else if (mQuery != null) {
       cacheKey = mQuery.getArtist().getCacheKey();
     } else if (mPlaylistEntry != null) {
       cacheKey = mPlaylistEntry.getArtist().getCacheKey();
     } else {
       cacheKey = mArtist.getCacheKey();
     }
     artistNameButton.setOnClickListener(constructArtistNameClickListener(cacheKey));
   }
 }
  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));
    }
  }
 @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);
   }
 }
 private void setupTextViews(View view) {
   if (mAlbum != null) {
     View v = ViewUtils.ensureInflation(view, R.id.album_name_button_stub, R.id.album_name_button);
     TextView textView = (TextView) v.findViewById(R.id.textview);
     textView.setText(mAlbum.getName());
     v.setOnClickListener(constructAlbumNameClickListener(mAlbum.getCacheKey()));
   } else if (mQuery != null || mPlaylistEntry != null || mPlaylist != null) {
     View v = ViewUtils.ensureInflation(view, R.id.track_name_stub, R.id.track_name);
     TextView textView = (TextView) v;
     if (mQuery != null) {
       textView.setText(mQuery.getName());
     } else if (mPlaylistEntry != null) {
       textView.setText(mPlaylistEntry.getName());
     } else if (mPlaylist != null) {
       textView.setText(mPlaylist.getName());
     }
   }
   if (mAlbum != null || mQuery != null || mPlaylistEntry != null || mArtist != null) {
     View v =
         ViewUtils.ensureInflation(view, R.id.artist_name_button_stub, R.id.artist_name_button);
     TextView textView = (TextView) v.findViewById(R.id.textview);
     String cacheKey;
     if (mQuery != null) {
       textView.setText(mQuery.getArtist().getPrettyName());
       cacheKey = mQuery.getArtist().getCacheKey();
     } else if (mAlbum != null) {
       textView.setText(mAlbum.getArtist().getPrettyName());
       cacheKey = mAlbum.getArtist().getCacheKey();
     } else if (mPlaylistEntry != null) {
       textView.setText(mPlaylistEntry.getArtist().getPrettyName());
       cacheKey = mPlaylistEntry.getArtist().getCacheKey();
     } else {
       textView.setText(mArtist.getPrettyName());
       cacheKey = mArtist.getCacheKey();
     }
     v.setOnClickListener(constructArtistNameClickListener(cacheKey));
   }
 }
 private void unpackArgs() {
   if (getArguments() != null) {
     if (getArguments().containsKey(TomahawkFragment.HIDE_REMOVE_BUTTON)) {
       mHideRemoveButton = getArguments().getBoolean(TomahawkFragment.HIDE_REMOVE_BUTTON);
     }
     if (getArguments().containsKey(TomahawkFragment.FROM_PLAYBACKFRAGMENT)) {
       mFromPlaybackFragment = getArguments().getBoolean(TomahawkFragment.FROM_PLAYBACKFRAGMENT);
     }
     if (getArguments().containsKey(TomahawkFragment.TOMAHAWKLISTITEM_TYPE)
         && getArguments().containsKey(TomahawkFragment.TOMAHAWKLISTITEM)) {
       String type = getArguments().getString(TomahawkFragment.TOMAHAWKLISTITEM_TYPE);
       String key = getArguments().getString(TomahawkFragment.TOMAHAWKLISTITEM);
       switch (type) {
         case TomahawkFragment.ALBUM:
           mAlbum = Album.getByKey(key);
           break;
         case TomahawkFragment.PLAYLIST:
           mPlaylist = Playlist.getByKey(key);
           break;
         case TomahawkFragment.ARTIST:
           mArtist = Artist.getByKey(key);
           break;
         case TomahawkFragment.QUERY:
           mQuery = Query.getByKey(key);
           break;
         case TomahawkFragment.SOCIALACTION:
           SocialAction socialAction = SocialAction.getByKey(key);
           Object targetObject = socialAction.getTargetObject();
           if (targetObject instanceof Artist) {
             mArtist = (Artist) targetObject;
           } else if (targetObject instanceof Album) {
             mAlbum = (Album) targetObject;
           } else if (targetObject instanceof Query) {
             mQuery = (Query) targetObject;
           } else if (targetObject instanceof Playlist) {
             mPlaylist = (Playlist) targetObject;
           }
           break;
         case TomahawkFragment.PLAYLISTENTRY:
           mPlaylistEntry = PlaylistEntry.getByKey(key);
           break;
       }
     }
     if (getArguments().containsKey(TomahawkFragment.COLLECTION_ID)) {
       mCollection =
           CollectionManager.get()
               .getCollection(getArguments().getString(TomahawkFragment.COLLECTION_ID));
     }
   }
 }
  @Override
  protected void parseIntent(Context ctx, String action, Bundle bundle)
      throws IllegalArgumentException {
    Log.d(TAG, "Will read data from SEMC intent");

    setTimestamp(System.currentTimeMillis());

    CharSequence ar = bundle.getCharSequence("ARTIST_NAME");
    CharSequence al = bundle.getCharSequence("ALBUM_NAME");
    CharSequence tr = bundle.getCharSequence("TRACK_NAME");

    if (ar == null || tr == null) {
      throw new IllegalArgumentException("null track values");
    }

    Artist artist = Artist.get(ar.toString());
    Album album = null;
    if (al != null) {
      album = Album.get(al.toString(), artist);
    }
    Track track = Track.get(tr.toString(), album, artist);
    setTrack(track);
  }
 /**
  * Parses the given {@link JSONArray} into a {@link ArrayList} of {@link Result}s.
  *
  * @param resList {@link JSONArray} containing the raw result information
  * @return a {@link ArrayList} of {@link Result}s containing the parsed data
  */
 private ArrayList<Result> parseResultList(final JSONArray resList) {
   ArrayList<Result> resultList = new ArrayList<Result>();
   for (int i = 0; i < resList.length(); i++) {
     if (!resList.isNull(i)) {
       try {
         JSONObject obj = resList.getJSONObject(i);
         if (obj.has("url")) {
           Artist artist;
           Album album;
           Track track;
           if (obj.has("artist")) {
             artist = Artist.get(obj.get("artist").toString());
           } else {
             artist = Artist.get("");
           }
           if (obj.has("album")) {
             album = Album.get(obj.get("album").toString(), artist);
           } else {
             album = Album.get("", artist);
           }
           if (obj.has("track")) {
             track = Track.get(obj.get("track").toString(), album, artist);
           } else {
             track = Track.get("", album, artist);
           }
           if (obj.has("albumpos")) {
             track.setAlbumPos(Integer.valueOf(obj.get("albumpos").toString()));
           }
           if (obj.has("discnumber")) {
             track.setAlbumPos(Integer.valueOf(obj.get("discnumber").toString()));
           }
           if (obj.has("year")) {
             String yearString = obj.get("year").toString();
             if (yearString.matches("-?\\d+")) {
               track.setYear(Integer.valueOf(yearString));
             }
           }
           if (obj.has("duration")) {
             track.setDuration(Math.round(Float.valueOf(obj.get("duration").toString()) * 1000));
           }
           artist.addAlbum(album);
           Result result = new Result(obj.get("url").toString(), track);
           if (obj.has("bitrate")) {
             result.setBitrate(Integer.valueOf(obj.get("bitrate").toString()));
           }
           if (obj.has("size")) {
             result.setSize(Integer.valueOf(obj.get("size").toString()));
           }
           if (obj.has("purchaseUrl")) {
             result.setPurchaseUrl(obj.get("purchaseUrl").toString());
           }
           if (obj.has("linkUrl")) {
             result.setLinkUrl(obj.get("linkUrl").toString());
           }
           if (obj.has("score")) {
             result.setTrackScore(Float.valueOf(obj.get("score").toString()));
           }
           result.setResolvedBy(this);
           result.setArtist(artist);
           result.setAlbum(album);
           result.setTrack(track);
           album.addQuery(Query.get(result, false));
           artist.addQuery(Query.get(result, false));
           resultList.add(result);
         }
       } catch (JSONException e) {
         Log.e(TAG, "parseResultList: " + e.getClass() + ": " + e.getLocalizedMessage());
       }
     }
   }
   return resultList;
 }
  /** Update this {@link TomahawkFragment}'s {@link TomahawkListAdapter} content */
  @Override
  protected void updateAdapter() {
    if (!mIsResumed) {
      return;
    }

    List<TomahawkListItem> albumsAndTopHits = new ArrayList<TomahawkListItem>();
    TomahawkMainActivity activity = (TomahawkMainActivity) getActivity();
    Context context = getActivity();
    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    View rootView = getView();
    if (mArtist != null) {
      activity.setTitle(mArtist.getName());
      if (mIsLocal) {
        albumsAndTopHits.addAll(mArtist.getLocalAlbums());
      } else {
        albumsAndTopHits.addAll(mArtist.getTopHits());
        albumsAndTopHits.addAll(mArtist.getAlbums());
        mShownQueries = mArtist.getTopHits();
        int precedingItemCount = 0;
        if (getListAdapter() != null
            && ((TomahawkListAdapter) getListAdapter()).isShowingContentHeader()) {
          precedingItemCount++;
        }
        mQueryPositions.clear();
        for (int i = 0; i < mShownQueries.size(); i++) {
          mQueryPositions.put(i, i + precedingItemCount);
        }
      }
      if (getListAdapter() == null) {
        TomahawkListAdapter tomahawkListAdapter =
            new TomahawkListAdapter(context, layoutInflater, albumsAndTopHits);
        tomahawkListAdapter.setShowCategoryHeaders(
            true, TomahawkListAdapter.SHOW_QUERIES_AS_TOPHITS);
        tomahawkListAdapter.showContentHeader(rootView, mArtist, mIsLocal);
        tomahawkListAdapter.setShowResolvedBy(true);
        setListAdapter(tomahawkListAdapter);
      } else {
        ((TomahawkListAdapter) getListAdapter()).setListItems(albumsAndTopHits);
        ((TomahawkListAdapter) getListAdapter()).showContentHeader(rootView, mArtist, mIsLocal);
      }
      getListView().setOnItemClickListener(this);
    } else if (mShowMode == SHOW_MODE_STARREDALBUMS) {
      ArrayList<Album> albums = DatabaseHelper.getInstance().getStarredAlbums();
      albumsAndTopHits.addAll(albums);
      if (getListAdapter() == null) {
        TomahawkListAdapter tomahawkListAdapter =
            new TomahawkListAdapter(context, layoutInflater, albumsAndTopHits);
        setListAdapter(tomahawkListAdapter);
      } else {
        ((TomahawkListAdapter) getListAdapter()).setListItems(albumsAndTopHits);
      }
      getListView().setOnItemClickListener(this);
    } else {
      if (mIsLocal) {
        albumsAndTopHits.addAll(Album.getLocalAlbums());
      } else {
        albumsAndTopHits.addAll(Album.getAlbums());
      }
      if (getGridAdapter() == null) {
        TomahawkGridAdapter tomahawkGridAdapter =
            new TomahawkGridAdapter(activity, layoutInflater, albumsAndTopHits);
        setGridAdapter(tomahawkGridAdapter);
      } else {
        getGridAdapter().setListArray(albumsAndTopHits);
      }
      getGridView().setOnItemClickListener(this);
      adaptColumnCount();
    }

    updateShowPlaystate();
  }
Beispiel #10
0
  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;
  }