Android Open Source - PlayTunes Search 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.search;
/*from   w  ww .j  a v  a 2s.c  o  m*/
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.ideabag.playtunes.R;
import com.ideabag.playtunes.activity.MainActivity;
import com.ideabag.playtunes.util.IMusicBrowser;
import com.ideabag.playtunes.util.SearchHistory;
import com.ideabag.playtunes.util.ISearchableAdapter;
import com.ideabag.playtunes.util.TrackerSingleton;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SearchFragment extends Fragment implements IMusicBrowser {
  
  public static final String TAG = "Search Fragment";
  
    private MainActivity mActivity;
    private Tracker mTracker;
    
  public AutoCompleteTextView mQueryTextView;
    
  //SongSearchAdapter adapter;
  
  protected String mSearchQuery = null;
  
  public SearchFragment() { }
  
  public SearchFragment( String query ) {
    
    setMediaID( query );
    
    
    
  }
  
  
  
  @Override public void onAttach( Activity activity ) {
    super.onAttach( activity );
    
    mActivity = ( MainActivity ) activity;
    mTracker = TrackerSingleton.getDefaultTracker( mActivity.getBaseContext() );
    mTracker.setScreenName( TAG );
    
    mActivity.setActionbarTitle( null );
      mActivity.setActionbarSubtitle( null );
    
  }
  
  @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
    
    return inflater.inflate( R.layout.fragment_search, container, false );
    
  }
  
  @Override public void onSaveInstanceState( Bundle outState ) {
    super.onSaveInstanceState( outState );
    outState.putString( getString( R.string.key_state_query_string ), mSearchQuery );
    
  }
    
  @Override public void onActivityCreated( Bundle savedInstanceState ) {
    super.onActivityCreated( savedInstanceState );
    
    if ( null != savedInstanceState ) {
      
      mSearchQuery = savedInstanceState.getString( getString( R.string.key_state_query_string ) );
      
    }
    
    setHasOptionsMenu( true );
      
    
    ActionBar mBar = mActivity.getSupportActionBar();
    
    mBar.setCustomView( R.layout.view_search_compat );
    mBar.setDisplayShowCustomEnabled( true );
    
    mQueryTextView = ( AutoCompleteTextView ) mBar.getCustomView().findViewById( R.id.SearchQuery );
    
    mBar.getCustomView().findViewById( R.id.SearchButton ).setOnClickListener( new OnClickListener() {
      
      @Override public void onClick( View v ) {
        
        String mQueryString = mQueryTextView.getEditableText().toString();
        setMediaID( mQueryString );
        
      }
      
    });
    
    mBar.getCustomView().findViewById( R.id.ClearSearchButton ).setOnClickListener( new OnClickListener() {

      @Override public void onClick( View v ) {
        
        mQueryTextView.setText( "" );
        setMediaID( null );
        
      }
      
    });
    
    mQueryTextView.setOnEditorActionListener( new OnEditorActionListener() {

      @Override public boolean onEditorAction( TextView view, int actionId, KeyEvent event ) {
        
        boolean mConsumed = false;
        String query = mQueryTextView.getEditableText().toString();
        
        if ( event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER ) {
          
          setMediaID( query );
          
          mConsumed = true;
          
        } else if ( event != null && event.getAction() == KeyEvent.ACTION_DOWN ) {
          
          setMediaID( query );
          
          mConsumed = true;
          
        } else if ( actionId == EditorInfo.IME_ACTION_SEARCH ) {
          
          setMediaID( query );
          
          mConsumed = true;
          
        }
        
        return mConsumed;
        
      }
      
    });
    
    
    if ( null != mSearchQuery ) {
      
      //android.util.Log.i( TAG, "Search query restore from saved instance state '" + mSearchQuery + "'" );
      mQueryTextView.setText( mSearchQuery );
      
      SearchAllFragment mSearchAllFragment = new SearchAllFragment();
      mSearchAllFragment.setSearchFragment( this );
      mSearchAllFragment.setSearchTerms( mSearchQuery );
      //mSearchAllFragment.setP
      
      FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
      
      ft.replace( R.id.SearchFragment, mSearchAllFragment );
      // Don't add to back stack
      
      // Commit the transaction
      ft.commit();
      
    } else {
      
      
      SearchSuggestionsFragment mSuggestionsFragment = new SearchSuggestionsFragment( this );
      
      FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
      
      ft.replace( R.id.SearchFragment, mSuggestionsFragment );
      // Don't add to back stack
      
      // Commit the transaction
      ft.commit();
      
    }
    
    
  }
  
  
  @Override public void onResume() {
    super.onResume();
    
    mActivity.setActionbarTitle( "*" );
      mActivity.setActionbarSubtitle( null );
      mActivity.getSupportActionBar().setDisplayShowCustomEnabled( true );
      
      mTracker.send( new HitBuilders.AppViewBuilder().build() );
    
  }
  
  @Override public void onDestroyView() {
    super.onDestroyView();
      
      //setListAdapter( null );
    ActionBar bar = mActivity.getSupportActionBar();
    bar.getCustomView().setVisibility( View.GONE );
    bar.setCustomView( null );
    mActivity.supportInvalidateOptionsMenu();
    //mActivity.supportInvalidateOptionsMenu();
    
  }
  
  @Override public void onDestroy() {
    super.onDestroy();
    
    setHasOptionsMenu( false );
    
    
    
    //getActivity().getContentResolver().unregisterContentObserver( mediaStoreChanged );
    
  }
  
  // PlaylistBrowser interface methods
  
  @Override public void setMediaID( String media_id ) {
    
    if ( null != media_id && media_id.length() > 1 ) {
      
      mSearchQuery = media_id;
      /*
      if ( null != mCurrentFragment ) {
        
        ISearchable mSearchable = ( ISearchable ) mCurrentFragment;
        
        mSearchable.setQuery( mSearchQuery );
        
      }
      */
      
      // Check if there is a Searchable fragment 
      // Load SearchAllFragment if there isn't
      // Otherwise set the Searchable's query to mSearchQuery
      
      if ( this.isResumed() ) {
        
        SearchHistory.addSearchQuery( getActivity(), media_id );
        
        Fragment mFragment = getActivity().getSupportFragmentManager().findFragmentById( R.id.SearchFragment );
        
        if ( mFragment instanceof SearchSuggestionsFragment ) {
          
          SearchAllFragment mSearchAllFragment = new SearchAllFragment();
          
          mSearchAllFragment.setSearchFragment( this );
          
          mSearchAllFragment.setSearchTerms( mSearchQuery );
          //mSearchAllFragment.setP
          
          
          transactFragment( mSearchAllFragment );
          
        } else {
          
          ISearchableAdapter mSearchable = ( ISearchableAdapter ) mFragment;
          
          mSearchable.setSearchTerms( mSearchQuery );
          
        }
        
        
      }
      
    }
    
  }
  
  public void transactFragment( Fragment mFrag ) {
    
    //mCurrentFragment = (ISearchable) mFrag;
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    
    ft.replace( R.id.SearchFragment, mFrag );
      ft.addToBackStack( null );
      ft.commit();
    
  }
  
  @Override public String getMediaID() {
    
    return mSearchQuery;
    
  }

}




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