Android Open Source - PlayTunes Music Browser Fragment






From Project

Back to project page PlayTunes.

License

The source code is released under:

Copyright (c) 2014, Impeccable Labs, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions ar...

If you think the Android project PlayTunes 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.ideabag.playtunes.fragment;
/* www.j a va  2  s  . co  m*/
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.ideabag.playtunes.R;
import com.ideabag.playtunes.activity.MainActivity;
import com.ideabag.playtunes.util.AdmobUtil;
import com.ideabag.playtunes.util.IMusicBrowser;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MusicBrowserFragment extends Fragment {
  
  public static final String TAG = "MusicBrowserFragment";
  
  private static final String PREF_KEY_CLASSNAME = "class_name";
  private static final String PREF_KEY_MEDIAID = "media_id";
  
  private MainActivity mActivity;
  
  private AdView mAdView;
  
  
  
  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    return inflater.inflate( R.layout.fragment_music_browser, container, false );
    
  }
  
  @Override public void onAttach( Activity activity ) {
    
    super.onAttach( activity );
    
    mActivity = ( MainActivity ) activity;
    
    //mActivity.getFragmentManager().getBackStackEntryCount()
    
  }
  
  @SuppressWarnings("unchecked")
  @Override public void onActivityCreated( Bundle savedInstanceState ) {
    
    super.onActivityCreated( savedInstanceState );
        
      mAdView = ( AdView ) getView().findViewById( R.id.adView );
      
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder().addTestDevice( AdRequest.DEVICE_ID_EMULATOR );
      AdmobUtil.AddTestDevices( getActivity(), adRequestBuilder );
      
      AdRequest adRequest = adRequestBuilder.build();
    
      //getActivity().getSupportFragmentManager().findFragmentById( R.id.MusicBrowserContainer );
    // Start loading the ad in the background.
      mAdView.loadAd( adRequest );
      
      SharedPreferences prefs = getActivity().getSharedPreferences( getString( R.string.prefs_file ), Context.MODE_PRIVATE );
      //IMusicBrowser TopFragment = null;
      
      if ( prefs.contains( "class_name" ) ) {// else check if we saved it in prefs
        
        String className = prefs.getString( PREF_KEY_CLASSNAME, "" );
        String mediaID = prefs.getString( PREF_KEY_MEDIAID, null );
        
        Fragment initialFragment = null;
        
        Class < ? extends Fragment > nowPlayingFragmentClass;
        
        try {
          
        nowPlayingFragmentClass = ( Class< ? extends Fragment > ) Class.forName( className );
        
        initialFragment = nowPlayingFragmentClass.newInstance();
        
        if ( null != initialFragment ) {
          
          IMusicBrowser mBrowserFragment = ( IMusicBrowser ) initialFragment;
          
          mBrowserFragment.setMediaID( mediaID );
          
        }
        
      } catch ( ClassNotFoundException e ) {
        e.printStackTrace();
      } catch ( java.lang.InstantiationException e ) {
        e.printStackTrace();
      } catch ( IllegalAccessException e ) {
        e.printStackTrace();
      }
        
        if ( null == initialFragment ) {
          
          // Fall back to "All Songs"
          //android.util.Log.i( TAG, "Failed to instantiate... falling back to SongsFragment.");
          
          initialFragment = new SongsFragment();
          
        }
        
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace( R.id.MusicBrowserContainer, initialFragment );
        // Don't add to back stack
        
        // Commit the transaction
        transaction.commit();
        
        
        
      } else {
        
        SongsFragment initialFragment = new SongsFragment();
        
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace( R.id.MusicBrowserContainer, initialFragment );
        // Don't add to back stack
        
        // Commit the transaction
        transaction.commit();
        
      }
      
      
  }
  
  @Override public void onResume() {
    super.onResume();
    
    mAdView.resume();
    
  }
  
  @Override public void onPause() {
    
    mAdView.pause();
    
    super.onPause();
    
    // Save current showing state
    
  }
  
  @Override public void onStop() {
    super.onStop();
    
    SharedPreferences prefs = getActivity().getSharedPreferences( getString( R.string.prefs_file ), Context.MODE_PRIVATE );
    SharedPreferences.Editor edit = prefs.edit();
    //android.util.Log.i( TAG, "Class name: " + TopFragment.getClass().getName() );
    
    IMusicBrowser TopFragment = ( IMusicBrowser ) getActivity().getSupportFragmentManager().findFragmentById( R.id.MusicBrowserContainer );
    
    edit.putString( PREF_KEY_CLASSNAME, TopFragment.getClass().getName() );
    edit.putString( PREF_KEY_MEDIAID, TopFragment.getMediaID() );
    
    edit.commit();
    
  }
  
  @Override public void onDestroy() {
    super.onDestroy();
    
    mAdView.destroy();
    
    
  }
  
  
  public void showNowPlaying() {
    
    Class < ? extends Fragment > nowPlayingFragmentClass = null;
    
    String nowPlayingMediaID = null;
    
    
    try {
      
      nowPlayingFragmentClass = mActivity.mBoundService.mPlaylistFragmentClass;
      nowPlayingMediaID = mActivity.mBoundService.mPlaylistMediaID;
      
      // 
      // Check to see if the currently playing Fragment is already showing
      // only create the new fragment if it isn't already showing.
      //
      IMusicBrowser TopFragment = ( IMusicBrowser ) getActivity().getSupportFragmentManager().findFragmentById( R.id.MusicBrowserContainer );
      
      if ( TopFragment != null ) {
        
        String showingMediaID = ( ( IMusicBrowser ) TopFragment ).getMediaID();
        
        boolean isSameClass = TopFragment.getClass().equals( nowPlayingFragmentClass );
        
        boolean isSameMediaID = showingMediaID.equals( nowPlayingMediaID );
        
        if ( !( isSameClass && isSameMediaID ) ) {
          
          Fragment nowPlayingFragment;
          
          nowPlayingFragment = nowPlayingFragmentClass.newInstance();
          ( ( IMusicBrowser ) nowPlayingFragment ).setMediaID( nowPlayingMediaID );
          
          FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            
            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace( R.id.MusicBrowserContainer, nowPlayingFragment );
            transaction.addToBackStack( null );
            
            
            // Commit the transaction
            transaction.commitAllowingStateLoss();
          
        }
        
      }
      

        
    } catch ( java.lang.InstantiationException e ) {
      e.printStackTrace();
    } catch ( IllegalAccessException e ) {
      e.printStackTrace();
    } catch ( ClassCastException e ) {
      e.printStackTrace();
    } catch( NullPointerException e ) {
      
      e.printStackTrace();
      
    }
    
  }
  
    public void transactFragment( Fragment newFragment ) {
      
    // 
    // Check to see if the currently playing Fragment is already showing
    // only create the new fragment if it isn't already showing.
    //
      IMusicBrowser TopFragment = ( IMusicBrowser ) getActivity().getSupportFragmentManager().findFragmentById( R.id.MusicBrowserContainer );
    
    if ( TopFragment != null ) {
      
      String showingMediaID = TopFragment.getMediaID();
      
      boolean isSameClass = TopFragment.getClass().equals( newFragment.getClass() );
      
      boolean isSameMediaID = showingMediaID != null && showingMediaID.equals( ((IMusicBrowser)newFragment).getMediaID() );
      
      if ( !( isSameClass && isSameMediaID ) ) {
        
          FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
          TopFragment = (IMusicBrowser) newFragment;
          // Replace whatever is in the fragment_container view with this fragment,
          // and add the transaction to the back stack
          transaction.replace( R.id.MusicBrowserContainer, newFragment );
          transaction.addToBackStack( null );
          
          
          // Commit the transaction
          transaction.commit();
        
      }
      
    } else {
      
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        TopFragment = (IMusicBrowser) newFragment;
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace( R.id.MusicBrowserContainer, newFragment );
        transaction.addToBackStack( null );
        
        
        // Commit the transaction
        transaction.commit();
      
    }
    
    mActivity.NavigationFragment.hideNavigation();
      
    }
    
}




Java Source Code List

com.ideabag.playtunes.PlaylistManager.java
com.ideabag.playtunes.DragNDrop.DragListener.java
com.ideabag.playtunes.DragNDrop.DragNDropListView.java
com.ideabag.playtunes.DragNDrop.DropListener.java
com.ideabag.playtunes.DragNDrop.RemoveListener.java
com.ideabag.playtunes.activity.MainActivity.java
com.ideabag.playtunes.activity.NowPlayingActivity.java
com.ideabag.playtunes.activity.SettingsActivity.java
com.ideabag.playtunes.adapter.AlbumListAdapter.java
com.ideabag.playtunes.adapter.AlbumsAllAdapter.java
com.ideabag.playtunes.adapter.AlbumsOneAdapter.java
com.ideabag.playtunes.adapter.ArtistAlbumsAdapter.java
com.ideabag.playtunes.adapter.ArtistAllSongsAdapter.java
com.ideabag.playtunes.adapter.ArtistListAdapter.java
com.ideabag.playtunes.adapter.ArtistSinglesAdapter.java
com.ideabag.playtunes.adapter.ArtistsAllAdapter.java
com.ideabag.playtunes.adapter.AsyncQueryAdapter.java
com.ideabag.playtunes.adapter.GenresAllAdapter.java
com.ideabag.playtunes.adapter.GenresOneAdapter.java
com.ideabag.playtunes.adapter.NavigationListAdapter.java
com.ideabag.playtunes.adapter.PlaylistsAllAdapter.java
com.ideabag.playtunes.adapter.PlaylistsOneAdapter.java
com.ideabag.playtunes.adapter.SongListAdapter.java
com.ideabag.playtunes.adapter.SongsAllAdapter.java
com.ideabag.playtunes.adapter.search.SearchAlbumsAdapter.java
com.ideabag.playtunes.adapter.search.SearchArtistsAdapter.java
com.ideabag.playtunes.adapter.search.SearchSongsAdapter.java
com.ideabag.playtunes.database.MediaQuery.java
com.ideabag.playtunes.dialog.AddToPlaylistDialogFragment.java
com.ideabag.playtunes.dialog.CreatePlaylistDialogFragment.java
com.ideabag.playtunes.dialog.FeedbackDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistDeleteDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistMenuDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistRenameDialogFragment.java
com.ideabag.playtunes.dialog.RateAppDialogFragment.java
com.ideabag.playtunes.dialog.SongMenuDialogFragment.java
com.ideabag.playtunes.fragment.AlbumsAllFragment.java
com.ideabag.playtunes.fragment.AlbumsOneFragment.java
com.ideabag.playtunes.fragment.ArtistSinglesFragment.java
com.ideabag.playtunes.fragment.ArtistsAllFragment.java
com.ideabag.playtunes.fragment.ArtistsOneFragment.java
com.ideabag.playtunes.fragment.FooterControlsFragment.java
com.ideabag.playtunes.fragment.GenresAllFragment.java
com.ideabag.playtunes.fragment.GenresOneFragment.java
com.ideabag.playtunes.fragment.MusicBrowserFragment.java
com.ideabag.playtunes.fragment.NavigationDrawerFragment.java
com.ideabag.playtunes.fragment.NavigationFragment.java
com.ideabag.playtunes.fragment.PlaylistsAllFragment.java
com.ideabag.playtunes.fragment.PlaylistsOneFragment.java
com.ideabag.playtunes.fragment.SaveScrollListFragment.java
com.ideabag.playtunes.fragment.SongsFragment.java
com.ideabag.playtunes.fragment.TrackProgressFragment.java
com.ideabag.playtunes.fragment.search.SearchAlbumsFragment.java
com.ideabag.playtunes.fragment.search.SearchAllFragment.java
com.ideabag.playtunes.fragment.search.SearchArtistsFragment.java
com.ideabag.playtunes.fragment.search.SearchFragment.java
com.ideabag.playtunes.fragment.search.SearchSongsFragment.java
com.ideabag.playtunes.fragment.search.SearchSuggestionsFragment.java
com.ideabag.playtunes.media.AudioFocusHelper.java
com.ideabag.playtunes.media.MusicFocusable.java
com.ideabag.playtunes.media.MusicIntentReceiver.java
com.ideabag.playtunes.media.PlaylistMediaPlayer.java
com.ideabag.playtunes.media.RemoteControlClientCompat.java
com.ideabag.playtunes.media.RemoteControlHelper.java
com.ideabag.playtunes.service.LockscreenManager.java
com.ideabag.playtunes.service.MusicPlayerService.java
com.ideabag.playtunes.service.PlaybackNotification.java
com.ideabag.playtunes.util.AdmobUtil.java
com.ideabag.playtunes.util.AlbumSongsCountTask.java
com.ideabag.playtunes.util.AsyncDrawable.java
com.ideabag.playtunes.util.BitmapWorkerTask.java
com.ideabag.playtunes.util.CheckRemoteVersionFileTask.java
com.ideabag.playtunes.util.DrawerLayout.java
com.ideabag.playtunes.util.GAEvent.java
com.ideabag.playtunes.util.IMusicBrowser.java
com.ideabag.playtunes.util.ISearchableAdapter.java
com.ideabag.playtunes.util.LoadAlbumStackTask.java
com.ideabag.playtunes.util.MergeAdapter.java
com.ideabag.playtunes.util.QueryCountTask.java
com.ideabag.playtunes.util.SackOfViewsAdapter.java
com.ideabag.playtunes.util.SearchHistory.java
com.ideabag.playtunes.util.StarToggleTask.java
com.ideabag.playtunes.util.TrackerSingleton.java