Android Open Source - lastfm-android-music-discovery Album 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.album.activity;
// www  . j  ava  2 s .com
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 android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.mikeladze.musicdiscovery.R;
import com.mikeladze.musicdiscovery.album.fragment.AlbumInfoFragment;
import com.mikeladze.musicdiscovery.album.fragment.AlbumTracksFragment;
import com.mikeladze.musicdiscovery.base.activity.BaseFragmentActivity;
import com.mikeladze.musicdiscovery.base.adapter.SectionsPagerAdapter;
import com.mikeladze.musicdiscovery.http.RestClient;

import fm.last.album.Album;

/**
 * The AlbumActivity, contains the fragments for the album.
 * 
 * @author Tim Mikeladze
 */
public class AlbumActivity extends BaseFragmentActivity implements ActionBar.TabListener {
  
  private SectionsPagerAdapter sectionsPagerAdapter;
  private ViewPager viewPager;
  private AlbumInfoFragment albumInfoFragment;
  private AlbumTracksFragment albumTracksFragment;
  
  private String name;
  private String artist;
  private String mbid;
  
  @Override
  /**
   * Creates the activity for the given album
   */
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_album);
    
    if (savedInstanceState != null) {
      name = savedInstanceState.getString("name");
      artist = savedInstanceState.getString("artist");
      mbid = savedInstanceState.getString("mbid");
      
    } else {
      Bundle b = getIntent().getExtras();
      name = b.getString("name");
      artist = b.getString("artist");
      mbid = b.getString("mbid");
    }
    
    Log.d("info", name + " " + artist + " " + mbid);
    
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    albumInfoFragment = (AlbumInfoFragment) Fragment.instantiate(this, AlbumInfoFragment.class.getName());
    albumTracksFragment = (AlbumTracksFragment) Fragment.instantiate(this, AlbumTracksFragment.class.getName());
    
    sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    
    sectionsPagerAdapter.addFragment(albumInfoFragment);
    sectionsPagerAdapter.addFragment(albumTracksFragment);
    
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setOffscreenPageLimit(1);
    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));
    }
    
    loadAlbum();
  }
  
  @Override
  /**
   * Stores the received search query for device orientation changed purposes
   */
  protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("name", name);
    savedInstanceState.putString("artist", artist);
    savedInstanceState.putString("mbid", mbid);
  }
  
  /**
   * Load the album information and update underlying fragments.
   */
  private void loadAlbum() {
    String url = Album.<String> getInfo(name, artist, true, mbid, null, null);
    RestClient.get(url, new JsonHttpResponseHandler() {
      
      @Override
      public void onStart() {
        setProgressBarIndeterminateVisibility(true);
      }
      
      @Override
      public void onSuccess(JSONObject response) {
        setProgressBarIndeterminateVisibility(false);
        try {
          actionBar.setTitle(response.getJSONObject("album")
                        .getString("name") + " - " + response.getJSONObject("album")
                                            .getString("artist"));
          albumInfoFragment.displayAlbumInfo(response.getJSONObject("album"));
          albumTracksFragment.displayAlbumTracks(artist, response.getJSONObject("album")
                                      .getJSONObject("tracks")
                                      .getJSONArray("track"));
          
        } 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