@ NonNull @ Override public Loader < SortedList < FtpFile > > getLoader ( ) { return new AsyncTaskLoader < SortedList < FtpFile > > ( getContext ( ) ) { @ Override public SortedList < FtpFile > loadInBackground ( ) { SortedList < FtpFile > sortedList = new SortedList <> ( FtpFile . class , new SortedListAdapterCallback < FtpFile > ( getDummyAdapter ( ) ) { @ Override public int compare ( FtpFile lhs , FtpFile rhs ) { if ( lhs . isDirectory ( ) && ! rhs . isDirectory ( ) ) { return - 1 ; } else if ( rhs . isDirectory ( ) && ! lhs . isDirectory ( ) ) { return 1 ; } else { return lhs . getName ( ) . compareToIgnoreCase ( rhs . getName ( ) ) ; } } @ Override public boolean areContentsTheSame ( FtpFile oldItem , FtpFile newItem ) { return oldItem . getName ( ) . equals ( newItem . getName ( ) ) ; } @ Override public boolean areItemsTheSame ( FtpFile item1 , FtpFile item2 ) { return item1 . getName ( ) . equals ( item2 . getName ( ) ) ; } } ) ; if ( ! ftp . isConnected ( ) ) { // Connect try { ftp . connect ( server , port ) ; ftp . setFileType ( FTP . ASCII_FILE_TYPE ) ; ftp . enterLocalPassiveMode ( ) ; ftp . setUseEPSVwithIPv4 ( false ) ; if ( ! ( loggedIn = ftp . login ( username , password ) ) ) { ftp . logout ( ) ; Log . e ( TAG , "Login failed" ) ; } } catch ( IOException e ) { if ( ftp . isConnected ( ) ) { try { ftp . disconnect ( ) ; } catch ( IOException ignored ) { } } Log . e ( TAG , "Could not connect to server." ) ; } } if ( loggedIn ) { try { // handle if directory does not exist. Fall back to root. if ( mCurrentPath == null || ! mCurrentPath . isDirectory ( ) ) { mCurrentPath = getRoot ( ) ; } sortedList . beginBatchedUpdates ( ) ; for ( FTPFile f : ftp . listFiles ( mCurrentPath . getPath ( ) ) ) { FtpFile file ; if ( f . isDirectory ( ) ) { file = new FtpDir ( mCurrentPath , f . getName ( ) ) ; } else { file = new FtpFile ( mCurrentPath , f . getName ( ) ) ; } if ( isItemVisible ( file ) ) { sortedList . add ( file ) ; } } sortedList . endBatchedUpdates ( ) ; } catch ( IOException e ) { Log . e ( TAG , "IOException: " + e . getMessage ( ) ) ; } } return sortedList ; } /** * Handles a request to start the Loader. */ @ Override protected void onStartLoading ( ) { super . onStartLoading ( ) ; // handle if directory does not exist. Fall back to root. if ( mCurrentPath == null || ! mCurrentPath . isDirectory ( ) ) { mCurrentPath = getRoot ( ) ; } forceLoad ( ) ; } } ; }
Get a loader that lists the files in the current path and monitors changes .