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));
    }
  }
 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));
     }
   }
 }
 /**
  * Called every time an item inside a ListView or GridView is clicked
  *
  * @param view the clicked view
  * @param item the Object which corresponds to the click
  */
 @Override
 public void onItemClick(View view, Object item) {
   if (getMediaController() == null) {
     Log.e(TAG, "onItemClick failed because getMediaController() is null");
     return;
   }
   if (item instanceof PlaylistEntry) {
     PlaylistEntry entry = (PlaylistEntry) item;
     if (entry.getQuery().isPlayable()) {
       if (getPlaybackManager().getCurrentEntry() == entry) {
         // if the user clicked on an already playing track
         int playState = getMediaController().getPlaybackState().getState();
         if (playState == PlaybackStateCompat.STATE_PLAYING) {
           getMediaController().getTransportControls().pause();
         } else if (playState == PlaybackStateCompat.STATE_PAUSED) {
           getMediaController().getTransportControls().play();
         }
       } else {
         getPlaybackManager().setPlaylist(mCurrentPlaylist, entry);
         getMediaController().getTransportControls().play();
       }
     }
   }
 }
 @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 setupContextMenuItems(View view) {
    final TomahawkMainActivity activity = (TomahawkMainActivity) getActivity();

    // set up "Add to playlist" context menu item
    if (mAlbum != null || mQuery != null || mPlaylistEntry != null || mPlaylist != null) {
      View v =
          ViewUtils.ensureInflation(
              view, R.id.context_menu_addtoplaylist_stub, R.id.context_menu_addtoplaylist);
      TextView textView = (TextView) v.findViewById(R.id.textview);
      ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
      imageView.setImageResource(R.drawable.ic_action_playlist_light);
      textView.setText(R.string.context_menu_add_to_playlist);
      v.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              getActivity().getSupportFragmentManager().popBackStack();
              if (mAlbum != null) {
                mCollection
                    .getAlbumTracks(mAlbum)
                    .done(
                        new DoneCallback<CollectionCursor<Query>>() {
                          @Override
                          public void onDone(CollectionCursor<Query> cursor) {
                            List<Query> queries = new ArrayList<>();
                            if (cursor != null) {
                              for (int i = 0; i < cursor.size(); i++) {
                                Query query = cursor.get(i);
                                if (query == null) {
                                  Log.e(
                                      TAG,
                                      "setupContextMenuItems - Can't call "
                                          + "showAddToPlaylist. Cursor has been "
                                          + "closed.");
                                  cursor.close();
                                  return;
                                }
                                queries.add(cursor.get(i));
                              }
                              cursor.close();
                            }
                            showAddToPlaylist(activity, queries);
                          }
                        });
              } else if (mQuery != null) {
                ArrayList<Query> queries = new ArrayList<>();
                queries.add(mQuery);
                showAddToPlaylist(activity, queries);
              } else if (mPlaylistEntry != null) {
                ArrayList<Query> queries = new ArrayList<>();
                queries.add(mPlaylistEntry.getQuery());
                showAddToPlaylist(activity, queries);
              } else if (mPlaylist != null) {
                showAddToPlaylist(activity, mPlaylist.getQueries());
              }
            }
          });
    }

    // set up "Add to collection" context menu item
    if (mAlbum != null || mArtist != null) {
      int drawableResId;
      int stringResId;
      if ((mAlbum != null && DatabaseHelper.get().isItemLoved(mAlbum))
          || (mArtist != null && DatabaseHelper.get().isItemLoved(mArtist))) {
        drawableResId = R.drawable.ic_action_collection_underlined;
        stringResId = R.string.context_menu_removefromcollection;
      } else {
        drawableResId = R.drawable.ic_action_collection;
        stringResId = R.string.context_menu_addtocollection;
      }
      View v =
          ViewUtils.ensureInflation(
              view, R.id.context_menu_addtocollection_stub, R.id.context_menu_addtocollection);
      TextView textView = (TextView) v.findViewById(R.id.textview);
      ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
      imageView.setImageResource(drawableResId);
      textView.setText(stringResId);
      v.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              getActivity().getSupportFragmentManager().popBackStack();
              if (mAlbum != null) {
                CollectionManager.get().toggleLovedItem(mAlbum);
              } else {
                CollectionManager.get().toggleLovedItem(mArtist);
              }
            }
          });
    }

    // set up "Add to favorites" context menu item
    if (mQuery != null || mPlaylistEntry != null) {
      final Query query = mQuery != null ? mQuery : mPlaylistEntry.getQuery();
      int drawableResId;
      int stringResId;
      if (DatabaseHelper.get().isItemLoved(query)) {
        drawableResId = R.drawable.ic_action_favorites_underlined;
        stringResId = R.string.context_menu_unlove;
      } else {
        drawableResId = R.drawable.ic_action_favorites;
        stringResId = R.string.context_menu_love;
      }
      View v =
          ViewUtils.ensureInflation(
              view, R.id.context_menu_favorite_stub, R.id.context_menu_favorite);
      TextView textView = (TextView) v.findViewById(R.id.textview);
      ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
      imageView.setImageResource(drawableResId);
      textView.setText(stringResId);
      v.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              getActivity().getSupportFragmentManager().popBackStack();
              CollectionManager.get().toggleLovedItem(query);
            }
          });
    }

    // set up "Share" context menu item
    View v = ViewUtils.ensureInflation(view, R.id.context_menu_share_stub, R.id.context_menu_share);
    TextView textView = (TextView) v.findViewById(R.id.textview);
    ImageView imageView = (ImageView) v.findViewById(R.id.imageview);
    imageView.setImageResource(R.drawable.ic_action_share);
    textView.setText(R.string.context_menu_share);
    v.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            getActivity().getSupportFragmentManager().popBackStack();
            if (mAlbum != null) {
              ShareUtils.sendShareIntent(activity, mAlbum);
            } else if (mArtist != null) {
              ShareUtils.sendShareIntent(activity, mArtist);
            } else if (mQuery != null) {
              ShareUtils.sendShareIntent(activity, mQuery);
            } else if (mPlaylistEntry != null) {
              ShareUtils.sendShareIntent(activity, mPlaylistEntry.getQuery());
            } else if (mPlaylist != null) {
              ShareUtils.sendShareIntent(activity, mPlaylist);
            }
          }
        });

    // set up "Remove" context menu item
    if (mPlaylist != null || mPlaylistEntry != null) {
      final String playlistId =
          mPlaylist != null ? mPlaylist.getId() : mPlaylistEntry.getPlaylistId();
      if (!mHideRemoveButton) {
        int stringResId;
        if (mPlaylistEntry != null) {
          stringResId = R.string.context_menu_removefromplaylist;
        } else {
          stringResId = R.string.context_menu_delete;
        }
        v =
            ViewUtils.ensureInflation(
                view, R.id.context_menu_remove_stub, R.id.context_menu_remove);
        textView = (TextView) v.findViewById(R.id.textview);
        imageView = (ImageView) v.findViewById(R.id.imageview);
        imageView.setImageResource(R.drawable.ic_player_exit_light);
        textView.setText(stringResId);
        v.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
                if (mPlaylistEntry != null) {
                  CollectionManager.get().deletePlaylistEntry(playlistId, mPlaylistEntry.getId());
                } else {
                  CollectionManager.get().deletePlaylist(playlistId);
                }
              }
            });
      }
    }

    // set up "Add to queue" context menu item
    if (mAlbum != null || mQuery != null || mPlaylistEntry != null || mPlaylist != null) {
      int drawableResId = R.drawable.ic_action_queue;
      int stringResId = R.string.context_menu_add_to_queue;
      v =
          ViewUtils.ensureInflation(
              view, R.id.context_menu_addtoqueue_stub, R.id.context_menu_addtoqueue);
      textView = (TextView) v.findViewById(R.id.textview);
      imageView = (ImageView) v.findViewById(R.id.imageview);
      imageView.setImageResource(drawableResId);
      textView.setText(stringResId);
      v.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              getActivity().getSupportFragmentManager().popBackStack();
              if (mAlbum != null) {
                mCollection
                    .getAlbumTracks(mAlbum)
                    .done(
                        new DoneCallback<CollectionCursor<Query>>() {
                          @Override
                          public void onDone(CollectionCursor<Query> collectionCursor) {
                            List<Query> albumTracks = new ArrayList<>();
                            for (int i = 0; i < collectionCursor.size(); i++) {
                              albumTracks.add(collectionCursor.get(i));
                            }
                            ((TomahawkMainActivity) getActivity())
                                .getPlaybackService()
                                .addQueriesToQueue(albumTracks);
                          }
                        });
              } else if (mQuery != null) {
                ((TomahawkMainActivity) getActivity()).getPlaybackService().addQueryToQueue(mQuery);
              } else if (mPlaylistEntry != null) {
                ((TomahawkMainActivity) getActivity())
                    .getPlaybackService()
                    .addQueryToQueue(mPlaylistEntry.getQuery());
              } else if (mPlaylist != null) {
                ((TomahawkMainActivity) getActivity())
                    .getPlaybackService()
                    .addQueriesToQueue(mPlaylist.getQueries());
              }
            }
          });
    }
  }