Android Open Source - lastfm-android-music-discovery Artist Activity






From Project

Back to project page lastfm-android-music-discovery.

License

The source code is released under:

Apache License

If you think the Android project lastfm-android-music-discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mikeladze.musicdiscovery.artist.activity;
/*from   w ww.  ja va 2s.co m*/
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.mikeladze.musicdiscovery.R;
import com.mikeladze.musicdiscovery.artist.fragment.ArtistAlbumsFragment;
import com.mikeladze.musicdiscovery.artist.fragment.ArtistInfoFragment;
import com.mikeladze.musicdiscovery.artist.fragment.SimilarArtistsFragment;
import com.mikeladze.musicdiscovery.base.activity.BaseFragmentActivity;
import com.mikeladze.musicdiscovery.base.adapter.SectionsPagerAdapter;
import com.mikeladze.musicdiscovery.http.RestClient;

import fm.last.artist.Artist;

/**
 * This activity holds the fragments for an artist.
 * 
 * @author Tim Mikeladze
 */
public class ArtistActivity extends BaseFragmentActivity implements ActionBar.TabListener {
  
  private SectionsPagerAdapter sectionsPagerAdapter;
  private ViewPager viewPager;
  private ArtistInfoFragment artistInfoFragment;
  private ArtistAlbumsFragment artistAlbumsFragment;
  private SimilarArtistsFragment similarArtistsFragment;
  
  private String name;
  private String mbid;
  
  @Override
  /**
   * Creates the activity.
   */
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_artist);
    
    if (savedInstanceState != null && (savedInstanceState.containsKey("name") || savedInstanceState.containsKey("mbid"))) {
      name = savedInstanceState.getString("name");
      mbid = savedInstanceState.getString("mbid");
    } else {
      Bundle b = getIntent().getExtras();
      name = b.getString("name");
      mbid = b.getString("mbid");
    }
    
    actionBar.setTitle(name);
    
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    artistInfoFragment = (ArtistInfoFragment) Fragment.instantiate(this, ArtistInfoFragment.class.getName());
    artistAlbumsFragment = (ArtistAlbumsFragment) Fragment.instantiate(this, ArtistAlbumsFragment.class.getName());
    similarArtistsFragment = (SimilarArtistsFragment) Fragment.instantiate(this, SimilarArtistsFragment.class.getName());
    
    sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    
    sectionsPagerAdapter.addFragment(artistInfoFragment);
    sectionsPagerAdapter.addFragment(artistAlbumsFragment);
    sectionsPagerAdapter.addFragment(similarArtistsFragment);
    
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setOffscreenPageLimit(2);
    viewPager.setAdapter(sectionsPagerAdapter);
    
    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
      
      @Override
      public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
      }
    });
    
    for (int i = 0; i < sectionsPagerAdapter.getCount(); i++) {
      actionBar.addTab(actionBar.newTab()
                    .setText(sectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
    }
    
    loadArtist();
  }
  
  @Override
  /**
   * Stores the received bundle for device orientation changed purposes
   */
  protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("name", name);
    savedInstanceState.putString("mbid", mbid);
  }
  
  /**
   * Load artist and update the fragments.
   */
  private void loadArtist() {
    RestClient.get(Artist.<String> getInfo(name, true, mbid, null, null), new JsonHttpResponseHandler() {
      
      @Override
      public void onStart() {
        setProgressBarIndeterminateVisibility(true);
      }
      
      @Override
      public void onSuccess(JSONObject response) {
        setProgressBarIndeterminateVisibility(false);
        try {
          similarArtistsFragment.displaySimilarArtists(response.getJSONObject("artist")
                                      .getJSONObject("similar")
                                      .getJSONArray("artist"));
          artistInfoFragment.displayArtistInfo(response.getJSONObject("artist"));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    });
    
    RestClient.get(Artist.<String> getTopAlbums(name, true, null, null, null), new JsonHttpResponseHandler() {
      
      @Override
      public void onStart() {
        setProgressBarIndeterminateVisibility(true);
      }
      
      @Override
      public void onSuccess(JSONObject response) {
        setProgressBarIndeterminateVisibility(false);
        
        try {
          artistAlbumsFragment.displayAlbums(response.getJSONObject("topalbums")
                                .getJSONArray("album"));
        } catch (JSONException e) {
          e.printStackTrace();
        }
      }
    });
    
  }
  
  @Override
  public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    viewPager.setCurrentItem(tab.getPosition());
  }
  
  @Override
  public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  }
  
  @Override
  public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  }
}




Java Source Code List

com.mikeladze.musicdiscovery.album.activity.AlbumActivity.java
com.mikeladze.musicdiscovery.album.adapter.LastFMTrack.java
com.mikeladze.musicdiscovery.album.adapter.OnTrackClickListener.java
com.mikeladze.musicdiscovery.album.adapter.TrackListAdapter.java
com.mikeladze.musicdiscovery.album.fragment.AlbumInfoFragment.java
com.mikeladze.musicdiscovery.album.fragment.AlbumTracksFragment.java
com.mikeladze.musicdiscovery.artist.activity.ArtistActivity.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistAlbumsFragment.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistInfoFragment.java
com.mikeladze.musicdiscovery.artist.fragment.SimilarArtistsFragment.java
com.mikeladze.musicdiscovery.base.activity.BaseFragmentActivity.java
com.mikeladze.musicdiscovery.base.adapter.SectionsPagerAdapter.java
com.mikeladze.musicdiscovery.base.fragment.BaseTabFragment.java
com.mikeladze.musicdiscovery.base.fragment.ImageGridViewFragment.java
com.mikeladze.musicdiscovery.http.LastFMResult.java
com.mikeladze.musicdiscovery.http.RestClient.java
com.mikeladze.musicdiscovery.main.activity.MainActivity.java
com.mikeladze.musicdiscovery.main.fragment.HypedArtistsFragment.java
com.mikeladze.musicdiscovery.main.fragment.TopArtistsFragment.java
com.mikeladze.musicdiscovery.search.activity.SearchResultsActivity.java
com.mikeladze.musicdiscovery.search.adapter.SearchResultsAdapter.java
com.mikeladze.musicdiscovery.util.IntentUtils.java
com.mikeladze.musicdiscovery.view.gridview.DynamicImageView.java
com.mikeladze.musicdiscovery.view.gridview.ImageGridAdapter.java
com.mikeladze.musicdiscovery.view.gridview.loader.FileCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.ImageLoader.java
com.mikeladze.musicdiscovery.view.gridview.loader.MemoryCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.Utils.java