Android Open Source - PlayTunes Main Activity






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.activity;
// w  w w .  j av  a  2s.c om
import com.crashlytics.android.Crashlytics;
import com.ideabag.playtunes.R;
import com.ideabag.playtunes.PlaylistManager;
import com.ideabag.playtunes.dialog.RateAppDialogFragment;
import com.ideabag.playtunes.fragment.FooterControlsFragment;
import com.ideabag.playtunes.fragment.NavigationFragment;
import com.ideabag.playtunes.service.MusicPlayerService;
import com.ideabag.playtunes.service.PlaybackNotification;
import com.ideabag.playtunes.util.CheckRemoteVersionFileTask;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import io.fabric.sdk.android.Fabric;

public class MainActivity extends ActionBarActivity {
  
  public MusicPlayerService mBoundService;
  public boolean mIsBound = false;
  
  private FooterControlsFragment mFooterControlsFragment;
  public NavigationFragment NavigationFragment;
  
  public PlaylistManager PlaylistManager;
  
  public boolean mShouldHideActionItems;
  
  @Override public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    Fabric.with(this, new Crashlytics());
    
    setContentView( R.layout.activity_main );
    
    PlaylistManager = new PlaylistManager( this );
        
      mFooterControlsFragment = ( FooterControlsFragment ) getSupportFragmentManager().findFragmentById( R.id.FooterControlsFragment );
        NavigationFragment = ( NavigationFragment ) getSupportFragmentManager().findFragmentById( R.id.left_drawer );
      
        doBindService();
        
  }
  
  @Override protected void onNewIntent( Intent intent ) {
    super.onNewIntent( intent );
    
      
      if ( intent.hasExtra( PlaybackNotification.NOW_PLAYING_EXTRA ) 
          && mBoundService != null
          && mBoundService.mPlaylistFragmentClass != null ) {
        
        NavigationFragment.showNowPlaying();//loadNowPlayingFragment();
        showNowPlayingActivity();
        
      }
    
  }
  
  public void setActionbarTitle( String titleString ) {
    
    if ( null != NavigationFragment ) {
      
      NavigationFragment.setActionbarTitle( titleString );
      
    }
    
  }
  
  public void setActionbarSubtitle( String subtitleString ) {
    if ( null != NavigationFragment ) {
      
      NavigationFragment.setActionbarSubtitle( subtitleString );
      
    }
    
  }
  
  @Override public void onStart() {
    super.onStart();
    
    if ( !mIsBound || mBoundService == null ) {
      
      doBindService();
      
    }
    
      SharedPreferences prefs = getSharedPreferences( getString( R.string.prefs_file) , Context.MODE_PRIVATE );
      //SharedPreferences.Editor edit = prefs.edit();
      
      int openCount = prefs.getInt( getString( R.string.pref_key_appopen ), 0 );
      int rateAppPromptCount = getResources().getInteger( R.integer.rate_app_prompt_count );
      
      if ( openCount == rateAppPromptCount ) {
        
        // Show rate dialog
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          
      Fragment prev = getSupportFragmentManager().findFragmentByTag( "dialog" );
      
        if ( prev != null ) {
            
          ft.remove( prev );
          
        }
      
        RateAppDialogFragment rateFragment = new RateAppDialogFragment();
      
      rateFragment.show( ft, "dialog" );
      
      } else {
        
        // We don't want to show multiple dialogs
        
        new CheckRemoteVersionFileTask( this ).execute( );
        
        
      }
      
      if ( openCount <= getResources().getInteger( R.integer.rate_app_prompt_count ) ) {
        
        openCount++;
        SharedPreferences.Editor edit = prefs.edit();
        edit.putInt( getString( R.string.pref_key_appopen ), openCount );
        edit.commit();
        
      }
      
    
  }
  
  @Override public void onStop() {
    super.onStop();
    
    if ( mIsBound || mBoundService != null ) {
      
      doUnbindService();
      
    }
    
    
  }
  
  @Override public void onDestroy() {
    super.onDestroy();
    
    if ( mIsBound || mBoundService != null ) {
      
      doUnbindService();
      
    }
    
  }
  
  
    // 
    // Now the hardware menu button will toggle the drawer layout
    // 
    @Override public boolean onKeyDown( int keycode, KeyEvent e ) {
      
        if ( NavigationFragment.onKeyDown( keycode, e ) ) {
          
          return true;
          
        }
        
        return super.onKeyDown( keycode, e );
        
    }
    
    public void transactFragment( Fragment mFragment ) {
      
      NavigationFragment.transactFragment( mFragment );
      
    }
    
  private ServiceConnection mConnection = new ServiceConnection() {
    
      public void onServiceConnected( ComponentName className, IBinder service ) {
          // This is called when the connection with the service has been
          // established, giving us the service object we can use to
          // interact with the service.  Because we have bound to a explicit
          // service that we know is running in our own process, we can
          // cast its IBinder to a concrete class and directly access it.
        mBoundService = ( ( MusicPlayerService.MusicPlayerServiceBinder ) service ).getService();
        
        mBoundService.addPlaybackListener( mFooterControlsFragment.PlaybackListener );
          
        mIsBound = true;
        
      }

      public void onServiceDisconnected( ComponentName className ) {
          // This is called when the connection with the service has been
          // unexpectedly disconnected -- that is, its process crashed.
          // Because it is running in our same process, we should never
          // see this happen.
        
        mBoundService = null;
          
        mIsBound = false;
        
      }
      
  };
  
  void doBindService() {
      // Establish a connection with the service.  We use an explicit
      // class name because we want a specific service implementation that
      // we know will be running in our own process (and thus won't be
      // supporting component replacement by other applications).
      bindService( new Intent( MainActivity.this, MusicPlayerService.class ), mConnection, Context.BIND_AUTO_CREATE );
      
      
      
  }
  
  void doUnbindService() {
    
      if ( mIsBound ) {
          
        
        // Remove service's reference to local object
        mBoundService.removePlaybackListener( mFooterControlsFragment.PlaybackListener );
        //BoundService.doDetachActivity();
        //android.util.Log.i("Detached from service", "Main Activity disconnected from service." );
        // Detach our existing connection.
          unbindService( mConnection );
          mIsBound = false;
          
      }
      
  }
    
  //
  // Used by FooterControlFragment
  //
  
  private void showNowPlayingActivity() {
    
    Intent startNowPlayingActivity = new Intent( this, NowPlayingActivity.class );
    
    startActivityForResult( startNowPlayingActivity, 0 );
    
  }
  
  // 
  // We use the onActivityResult mechanism to return from the NowPlayingActivity
  // and display the Fragment of the currently playing playlist, if it's not already displayed.
  // 
  @Override protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
    
    if ( resultCode == RESULT_OK ) {
      
      NavigationFragment.showNowPlaying();
      
    }
    
    
  }
  
  //
  // Menu and MenuItem related code goes here
  //
  //
  //
  /*
  @Override public boolean onCreateOptionsMenu( Menu menu ) {
    
    MenuInflater inflater = getMenuInflater();
      inflater.inflate( R.menu.menu_search, menu );
      
      return true;
    
  }
  */
    @Override public boolean onOptionsItemSelected( MenuItem item ) {
      
        if ( NavigationFragment.onOptionsItemSelected( item ) ) {
          
          return true;
          
        }
        
        return super.onOptionsItemSelected( item );
        
    }
  /*
  @Override public boolean onPrepareOptionsMenu( Menu menu ) {

      // If the nav drawer is open, hide action items related to the content view
      boolean drawerOpen = mShouldHideActionItems;
      
      hideMenuItems( menu, !drawerOpen );
      
      return super.onPrepareOptionsMenu( menu );
      
  }
  /*
  public boolean mShowSearch = true;
  
  private void hideMenuItems( Menu menu, boolean visible ) {
    
      for ( int i = 0; i < menu.size(); i++ ) {
        
        MenuItem item = menu.getItem( i );
        int id = item.getItemId();
        
        if ( id == R.id.MenuSearch ) {
          
          item.setVisible( mShowSearch );
          
        } else {
          
          item.setVisible( visible );
            
        }
        
      }
      
  }
  */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        NavigationFragment.mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        NavigationFragment.mDrawerToggle.onConfigurationChanged(newConfig);
    }
  
}




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