/**
     * Search and retrieve list of artists
     *
     * @param params
     * @return
     */
    @Override
    protected ArtistsPager doInBackground(String... params) {

      SpotifyApi api = new SpotifyApi();
      ;
      SpotifyService spotify = null;

      try {

        if (params.length == 0) {
          return null;
        }
        spotify = api.getService();
        Log.v(LOG_TAG, "Artist to search " + params[0]);

        return spotify.searchArtists(params[0]);

      } catch (RetrofitError ex) {
        Log.e(LOG_TAG, "Error at retrieving Artist data " + ex.getMessage());
        Toast.makeText(getActivity().getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT)
            .show();
      }

      return null;
    }
  @Override
  protected List<Artist> doInBackground(String... params) {
    List<Artist> pResults = new ArrayList<>();

    try {
      SpotifyApi api = new SpotifyApi();
      SpotifyService service = api.getService();
      String artistName = params[0];
      ArtistsPager results = service.searchArtists(artistName);
      for (kaaes.spotify.webapi.android.models.Artist artist : results.artists.items) {
        ArrayList<Image> pImages = new ArrayList<>();

        if (artist.images != null && artist.images.size() > 0) {
          for (kaaes.spotify.webapi.android.models.Image image : artist.images) {
            Image pImage = new Image(image.height, image.width, image.url);
            pImages.add(pImage);
          }
        }

        Artist pArtist = new Artist(artist.id, artist.name, pImages);
        pResults.add(pArtist);
      }
    } catch (Exception e) {
      Artist artist = new Artist("", "ERROR: " + e.getMessage(), null);
      pResults.add(artist);
    }

    return pResults;
  }
    @Override
    protected List<Artist> doInBackground(String... params) {

      SpotifyApi spotifyApi = new SpotifyApi();
      SpotifyService spotifyService = spotifyApi.getService();
      ArtistsPager artistsPager = spotifyService.searchArtists(params[0]);

      return artistsPager.artists.items;
    }
    @Override
    protected ArtistsPager doInBackground(String... params) {

      try {
        SpotifyApi api = new SpotifyApi();
        SpotifyService spotify = api.getService();
        return spotify.searchArtists(params[0]);
      } catch (Exception e) {
        return null;
      }
    }
    @Override
    protected Void doInBackground(String... params) {
      SpotifyApi api = new SpotifyApi();
      SpotifyService spotify = api.getService();

      // Load Data into db
      if (params != null) {
        getArtistDataFromSpotifyWrapper(spotify, params[0]);
      }
      return null;
    }
Example #6
0
    @Override
    protected ArtistsPager doInBackground(String... params) {

      SpotifyApi api = new SpotifyApi();
      SpotifyService spotify = api.getService();

      try {
        return spotify.searchArtists(params[0]);

      } catch (Exception e) {
        Log.e(LOG_TAG, "Error -> " + e.getMessage(), e);
        return null;
      }
    }
    @Override
    protected List<SearchResult> doInBackground(String... params) {

      if (params.length == 0) {
        Log.i(LOG_TAG, "No parameter received for task");
        return null;
      }

      SpotifyApi api = new SpotifyApi();
      SpotifyService spotify = api.getService();

      try {
        ArtistsPager artistsPager = spotify.searchArtists(params[0]);

        List<Artist> artists = artistsPager.artists.items;

        if (artists.size() > 0) {

          List<SearchResult> results = new ArrayList<SearchResult>();

          for (Artist artist : artistsPager.artists.items) {
            Log.v(LOG_TAG, "Found artist: " + artist.name);
            String imgUrl =
                SpotifyWrapperUtil.getImageUrlForSearchResultList(
                    artist.images,
                    getResources()
                        .getDimensionPixelSize(R.dimen.search_result_thumbnail_max_height),
                    getResources()
                        .getDimensionPixelSize(R.dimen.search_result_thumbnail_max_width));
            SearchResult res = new SearchResult(artist.id, artist.name, imgUrl);
            results.add(res);
          }

          return results;
        }

      } catch (RetrofitError error) {
        Log.e(LOG_TAG, "Error while searching artist with param: [" + params[0] + "]");
        SpotifyError spotifyError = SpotifyError.fromRetrofitError(error);
        if (spotifyError.hasErrorDetails()) {
          Log.e(LOG_TAG, spotifyError.getErrorDetails().message);
        }
      }

      return null;
    }
    @Override
    protected String doInBackground(String... params) {
      try {
        SpotifyApi spotifyApi = new SpotifyApi();
        SpotifyService spotify = spotifyApi.getService();

        // Look for Track
        Track track = spotify.getTrack(params[0]);

        StringBuilder result = new StringBuilder();
        result.append(track.album.name + ",");
        result.append(track.name + ",");
        result.append(track.album.images.get(track.album.images.size() - 1).url);

        Log.v(LOG_TAG + " doInBackground: ", result.toString());

        return result.toString();
      } catch (Exception e) {
        this.exception = e;

        return null;
      }
    }
Example #9
0
public class Top10Songs extends AppCompatActivity {

  public final String LOG_TAG = ArtistSearch.class.getSimpleName();
  public String song_path;
  ListView listView; // top 10 in a list

  // Spotify Stuff
  SpotifyApi api = new SpotifyApi();
  SpotifyService service = api.getService();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_top10_songs);
    Intent intent = getIntent();
    String artist = intent.getStringExtra("musician");
    listView = (ListView) findViewById(R.id.top_10_by_artist);
    getArtistID(artist);

    listView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String item = listView.getItemAtPosition(position).toString();
            getSongURI(item);
          }
        });
  }
  // get song track uri...
  public void getSongURI(String uri) {
    service.searchTracks(
        uri,
        new Callback<TracksPager>() {
          @Override
          public void success(TracksPager tracksPager, Response response) {
            song_path = tracksPager.tracks.items.get(0).uri;
            Log.e(LOG_TAG, "song uri: " + song_path);
            Intent intent = new Intent(Top10Songs.this, Player.class);
            intent.putExtra("track", song_path);
            startActivity(intent);
          }

          @Override
          public void failure(RetrofitError error) {}
        });
  }

  // get artists uri
  public void getArtistID(String uri) {

    service.searchArtists(
        uri,
        new Callback<ArtistsPager>() {
          @Override
          public void success(ArtistsPager artistsPager, Response response) {
            String artist_uri = artistsPager.artists.items.get(0).id;
            Log.e(LOG_TAG, "artist uri: " + artist_uri);
            getTopTenParams(artist_uri);
          }

          @Override
          public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), "Failed...", Toast.LENGTH_LONG).show();
          }
        });
  }

  public void getTopTenParams(String uri) {

    final List<String> songs_list = new ArrayList<>();
    final List<String> album_list = new ArrayList<>();
    final Map<String, String> artist = new HashMap<>();
    String countryID = "GB";
    String artistUri = uri;
    Map<String, Object> options = new HashMap<>();
    options.put(SpotifyService.COUNTRY, countryID);

    service.getArtistTopTrack(
        artistUri,
        options,
        new Callback<Tracks>() {
          @Override
          public void success(Tracks tracks, Response response) {
            for (int i = 0; i < tracks.tracks.size(); i++) {
              String songNames = tracks.tracks.get(i).name;
              String albumName = tracks.tracks.get(i).album.name;
              artist.put(tracks.tracks.get(i).name, tracks.tracks.get(i).album.name);
              songs_list.add(songNames);
              album_list.add(albumName);
            }

            String[] songs = songs_list.toArray(new String[songs_list.size()]);
            String[] albums = album_list.toArray(new String[album_list.size()]);

            FlowAdapter adapter = new FlowAdapter(Top10Songs.this, songs, albums);
            listView.setAdapter(adapter);
          }

          @Override
          public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), "An Error has occured", Toast.LENGTH_LONG)
                .show();
          }
        });
  }
  // Adapter for listview
  public class FlowAdapter extends ArrayAdapter<String> {

    Context context;
    String[] topTracks; // names of the top songs
    String[] albumNames; // image urls as a string array

    // Constructor
    FlowAdapter(Context context, String[] songRank, String[] coverNames) {

      super(context, R.layout.artist_top_10_layout, R.id.top10songnames, songRank);
      this.context = context;
      this.topTracks = songRank;
      this.albumNames = coverNames;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      View row;
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.artist_top_10_layout, parent, false);

      ImageView imageView = (ImageView) row.findViewById(R.id.artist_picture_imageview);
      TextView top10_songs = (TextView) row.findViewById(R.id.top10songnames);
      TextView top10_albums = (TextView) row.findViewById(R.id.top10albumnames);

      top10_songs.setText(topTracks[position]);
      top10_albums.setText(albumNames[position]);

      //          Picasso.with(context).load(imagesURL).resize(90, 90).into(imageView);

      return row;
    }
  }
}
    protected FetchResults doInBackground(String... params) {

      Tracks tracks = null;
      String artistID = params[0];
      int rowsInserted = 0;
      FetchResults results = new FetchResults();
      results.numRecords = 0;
      results.message = "Tracks not found";

      try {
        SpotifyApi api = new SpotifyApi();
        SpotifyService spotifyService = api.getService();
        // Get the market country code
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String market_code =
            prefs.getString(
                getString(R.string.pref_country_code_key),
                getString(R.string.pref_country_code_default));

        Map<String, Object> options = new HashMap<>();
        options.put(SpotifyService.COUNTRY, market_code);
        // This only returns 10 tracks for artists.

        tracks = spotifyService.getArtistTopTrack(artistID, options);
      } catch (RetrofitError error) {
        SpotifyError spotifyError = SpotifyError.fromRetrofitError(error);
        Log.d(LOG_TAG, spotifyError.toString());
        results.message = spotifyError.getErrorDetails().message;
      }

      if (tracks != null) {
        // API returns at most 10 tracks
        Vector<ContentValues> cVVector = new Vector<>(10);

        for (Track track : tracks.tracks) {
          String imageLargeURL = null;
          String imageSmallURL = null;

          if (track.album.images.size() > 0) {

            Image imageLarge = track.album.images.get(0);
            Image imageSmall = imageLarge;

            for (int index = 1; index < track.album.images.size(); index++) {
              Image image = track.album.images.get(index);
              // Check for small
              if (Math.abs(imageSmall.height - 200) > (Math.abs(image.height - 200))) {
                imageSmall = image;
              }

              // Check for large
              if (Math.abs(imageLarge.height - 640) > (Math.abs(image.height - 640))) {
                imageLarge = image;
              }
            }

            imageLargeURL = imageLarge.url;
            imageSmallURL = imageSmall.url;
          }

          // Insert trackinfo into the database
          // Save the information to put into the database
          ContentValues tracksValues = new ContentValues();

          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_TRACK_NAME, track.name);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_ALBUM_NAME, track.album.name);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_ARTIST_SPOTIFY_ID, artistID);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_THUMBNAL_SMALL_URL, imageSmallURL);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_THUMBNAL_LARGE_URL, imageLargeURL);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_PREVIEW_URL, track.preview_url);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_DURATION, track.duration_ms);
          tracksValues.put(SpotifyContract.TracksEntry.COLUMN_ARTIST_NAME, mArtistName);

          cVVector.add(tracksValues);
        }

        // table should always be empty as it is deleted before the fetch task is called
        // in the onCreateView method

        // add to database
        if (cVVector.size() > 0) {
          // call bulkInsert to add the new tracks to the database here
          rowsInserted =
              mContext
                  .getContentResolver()
                  .bulkInsert(
                      SpotifyContract.TracksEntry.CONTENT_URI,
                      cVVector.toArray(new ContentValues[cVVector.size()]));
          if (rowsInserted != cVVector.size()) {
            Log.e(
                LOG_TAG,
                "Inserted "
                    + String.valueOf(rowsInserted)
                    + " and expected "
                    + String.valueOf(cVVector.size()));
          }
        }
      }

      if (rowsInserted > 0) {
        results.message = null;
        results.numRecords = rowsInserted;
      }

      return results;
    }