3131import java .util .Date ;
3232import java .util .LinkedList ;
3333import java .util .List ;
34+ import java .util .concurrent .CancellationException ;
3435
3536import org .slf4j .Logger ;
3637import org .slf4j .LoggerFactory ;
5859import com .amaze .filemanager .utils .DataUtils ;
5960import com .amaze .filemanager .utils .GenericExtKt ;
6061import com .amaze .filemanager .utils .OTGUtil ;
61- import com .amaze .filemanager .utils .OnAsyncTaskFinished ;
6262import com .amaze .filemanager .utils .OnFileFound ;
6363import com .amaze .filemanager .utils .Utils ;
6464import com .amaze .trashbin .TrashBin ;
7272import android .content .pm .ResolveInfo ;
7373import android .database .Cursor ;
7474import android .net .Uri ;
75- import android .os .AsyncTask ;
7675import android .os .Bundle ;
7776import android .provider .MediaStore ;
7877import android .text .format .Formatter ;
8887import jcifs .smb .SmbFile ;
8988import kotlin .collections .CollectionsKt ;
9089
91- public class LoadFilesListTask
92- extends AsyncTask <Void , Throwable , Pair <OpenMode , List <LayoutElementParcelable >>> {
90+ public class LoadFilesListTask {
9391
9492 private static final Logger LOG = LoggerFactory .getLogger (LoadFilesListTask .class );
9593
96- private String path ;
97- private WeakReference <MainFragment > mainFragmentReference ;
98- private WeakReference <Context > context ;
99- private OpenMode openmode ;
100- private boolean showHiddenFiles , showThumbs ;
101- private DataUtils dataUtils = DataUtils .getInstance ();
102- private OnAsyncTaskFinished <Pair <OpenMode , List <LayoutElementParcelable >>> listener ;
103- private boolean forceReload ;
104-
105- public LoadFilesListTask (
106- Context context ,
107- String path ,
108- MainFragment mainFragment ,
109- OpenMode openmode ,
110- boolean showThumbs ,
111- boolean showHiddenFiles ,
112- boolean forceReload ,
113- OnAsyncTaskFinished <Pair <OpenMode , List <LayoutElementParcelable >>> l ) {
114- this .path = path ;
115- this .mainFragmentReference = new WeakReference <>(mainFragment );
116- this .openmode = openmode ;
117- this .context = new WeakReference <>(context );
118- this .showThumbs = showThumbs ;
119- this .showHiddenFiles = showHiddenFiles ;
120- this .listener = l ;
121- this .forceReload = forceReload ;
122- }
94+ public Pair <OpenMode , List <LayoutElementParcelable >> load () throws Exception {
12395
124- @ Override
125- @ SuppressWarnings ({"PMD.NPathComplexity" , "ComplexMethod" , "LongMethod" })
126- protected @ Nullable Pair <OpenMode , List <LayoutElementParcelable >> doInBackground (Void ... p ) {
12796 final MainFragment mainFragment = this .mainFragmentReference .get ();
12897 final Context context = this .context .get ();
12998
@@ -132,89 +101,113 @@ public LoadFilesListTask(
132101 || mainFragment .getMainFragmentViewModel () == null
133102 || mainFragment .getMainActivityViewModel () == null
134103 || path == null ) {
135- cancel ( true );
136- return null ;
104+
105+ throw new CancellationException () ;
137106 }
138107
139108 HybridFile hFile = null ;
109+
140110 MainFragmentViewModel mainFragmentViewModel = mainFragment .getMainFragmentViewModel ();
111+
141112 MainActivityViewModel mainActivityViewModel = mainFragment .getMainActivityViewModel ();
142113
143114 if (OpenMode .UNKNOWN .equals (openmode )
144115 || OpenMode .CUSTOM .equals (openmode )
145116 || OpenMode .TRASH_BIN .equals (openmode )) {
117+
146118 hFile = new HybridFile (openmode , path );
119+
147120 hFile .generateMode (mainFragment .getActivity ());
121+
148122 openmode = hFile .getMode ();
149123
150124 if (hFile .isSmb ()) {
151125 mainFragmentViewModel .setSmbPath (path );
152126 }
153127 }
154128
155- if (isCancelled ()) return null ;
156-
157129 mainFragmentViewModel .setFolderCount (0 );
158130 mainFragmentViewModel .setFileCount (0 );
131+
159132 final List <LayoutElementParcelable > list ;
160133
161134 switch (openmode ) {
162135 case SMB :
163136 list = listSmb (hFile , mainActivityViewModel , mainFragment );
164137 break ;
138+
165139 case FTP :
166140 case SFTP :
167141 list = listSftp (mainActivityViewModel );
168142 break ;
143+
169144 case CUSTOM :
170145 case TRASH_BIN :
171146 list = getCachedMediaList (mainActivityViewModel );
172147 break ;
148+
173149 case OTG :
174150 list = listOtg ();
175151 openmode = OpenMode .OTG ;
176152 break ;
153+
177154 case DOCUMENT_FILE :
178155 list = listDocumentFiles (mainActivityViewModel );
179156 openmode = OpenMode .DOCUMENT_FILE ;
180157 break ;
158+
181159 case DROPBOX :
182160 case BOX :
183161 case GDRIVE :
184162 case ONEDRIVE :
185- try {
186- list = listCloud (mainActivityViewModel );
187- } catch (CloudPluginException e ) {
188- LOG .warn ("failed to load cloud files" , e );
189- AppConfig .toast (context , context .getResources ().getString (R .string .failed_no_connection ));
190- return new Pair <>(openmode , Collections .emptyList ());
191- }
163+ list = listCloud (mainActivityViewModel );
192164 break ;
165+
193166 case ANDROID_DATA :
194167 list = listAppDataDirectories (path );
195168 break ;
169+
196170 default :
197- // we're neither in OTG not in SMB, load the list based on root/general filesystem
198171 list = listDefault (mainActivityViewModel , mainFragment );
199172 break ;
200173 }
201174
202175 if (list != null
203176 && !(openmode == OpenMode .CUSTOM
204- && (("5" ).equals (path ) || ("6" ).equals (path ) || ("7" ).equals (path )))) {
177+ && ("5" .equals (path ) || "6" .equals (path ) || "7" .equals (path )))) {
178+
205179 postListCustomPathProcess (list , mainFragmentViewModel );
206180 }
207181
208182 return new Pair <>(openmode , list );
209183 }
210184
211- @ Override
212- protected void onCancelled () {
213- listener .onAsyncTaskFinished (null );
185+ private String path ;
186+ private WeakReference <MainFragment > mainFragmentReference ;
187+ private WeakReference <Context > context ;
188+ private OpenMode openmode ;
189+ private boolean showHiddenFiles , showThumbs ;
190+ private DataUtils dataUtils = DataUtils .getInstance ();
191+ private boolean forceReload ;
192+
193+ public LoadFilesListTask (
194+ Context context ,
195+ String path ,
196+ MainFragment mainFragment ,
197+ OpenMode openmode ,
198+ boolean showThumbs ,
199+ boolean showHiddenFiles ,
200+ boolean forceReload ) {
201+ this .path = path ;
202+ this .mainFragmentReference = new WeakReference <>(mainFragment );
203+ this .openmode = openmode ;
204+ this .context = new WeakReference <>(context );
205+ this .showThumbs = showThumbs ;
206+ this .showHiddenFiles = showHiddenFiles ;
207+ this .forceReload = forceReload ;
214208 }
215209
216- @ Override
217- protected void onProgressUpdate (Throwable ... values ) {
210+ public void onProgressUpdate (Throwable ... values ) {
218211 for (Throwable exception : values ) {
219212 if (exception instanceof SmbException ) {
220213 if ("/" .equals (Uri .parse (path ).getPath ())) {
@@ -248,11 +241,6 @@ protected void onProgressUpdate(Throwable... values) {
248241 }
249242 }
250243
251- @ Override
252- protected void onPostExecute (@ Nullable Pair <OpenMode , List <LayoutElementParcelable >> list ) {
253- listener .onAsyncTaskFinished (list );
254- }
255-
256244 private List <LayoutElementParcelable > getCachedMediaList (
257245 MainActivityViewModel mainActivityViewModel ) throws IllegalStateException {
258246 List <LayoutElementParcelable > list ;
@@ -339,8 +327,7 @@ private void postListCustomPathProcess(
339327 final Context context = this .context .get ();
340328
341329 if (mainFragment == null || context == null ) {
342- cancel (true );
343- return null ;
330+ throw new CancellationException ();
344331 }
345332
346333 String size = "" ;
@@ -410,8 +397,7 @@ private List<LayoutElementParcelable> listaudio() {
410397 final Context context = this .context .get ();
411398
412399 if (context == null ) {
413- cancel (true );
414- return null ;
400+ throw new CancellationException ();
415401 }
416402
417403 Cursor cursor =
@@ -437,8 +423,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
437423 final Context context = this .context .get ();
438424
439425 if (context == null ) {
440- cancel (true );
441- return null ;
426+ throw new CancellationException ();
442427 }
443428
444429 ArrayList <LayoutElementParcelable > docs = new ArrayList <>();
@@ -499,8 +484,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
499484 final Context context = this .context .get ();
500485
501486 if (context == null ) {
502- cancel (true );
503- return null ;
487+ throw new CancellationException ();
504488 }
505489
506490 ArrayList <LayoutElementParcelable > apks = new ArrayList <>();
@@ -537,8 +521,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
537521 private @ Nullable List <LayoutElementParcelable > listRecent () {
538522 final MainFragment mainFragment = this .mainFragmentReference .get ();
539523 if (mainFragment == null ) {
540- cancel (true );
541- return null ;
524+ throw new CancellationException ();
542525 }
543526
544527 UtilsHandler utilsHandler = AppConfig .getInstance ().getUtilsHandler ();
@@ -566,8 +549,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
566549 final Context context = this .context .get ();
567550
568551 if (context == null ) {
569- cancel (true );
570- return null ;
552+ throw new CancellationException ();
571553 }
572554
573555 List <LayoutElementParcelable > recentFiles = new ArrayList <>(20 );
@@ -634,8 +616,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
634616 final Context context = this .context .get ();
635617
636618 if (context == null ) {
637- cancel (true );
638- return null ;
619+ throw new CancellationException ();
639620 }
640621
641622 TrashBin trashBin = AppConfig .getInstance ().getTrashBinInstance ();
@@ -729,7 +710,6 @@ private List<LayoutElementParcelable> listSmb(
729710 mainFragment .reauthenticateSmb ();
730711 }
731712 LOG .warn ("failed to load smb list, authentication issue: " , e );
732- publishProgress (e );
733713 return null ;
734714 } catch (SmbException | NullPointerException e ) {
735715 LOG .warn ("Failed to load smb files for path: " + path , e );
@@ -863,8 +843,7 @@ private void listOtgInternal(String path, OnFileFound fileFound) {
863843 final Context context = this .context .get ();
864844
865845 if (context == null ) {
866- cancel (true );
867- return ;
846+ throw new CancellationException ();
868847 }
869848
870849 OTGUtil .getDocumentFiles (path , context , fileFound );
@@ -874,8 +853,7 @@ private void listDocumentFilesInternal(OnFileFound fileFound) {
874853 final Context context = this .context .get ();
875854
876855 if (context == null ) {
877- cancel (true );
878- return ;
856+ throw new CancellationException ();
879857 }
880858
881859 OTGUtil .getDocumentFiles (
@@ -888,8 +866,7 @@ private void listCloudInternal(
888866 final Context context = this .context .get ();
889867
890868 if (context == null ) {
891- cancel (true );
892- return ;
869+ throw new CancellationException ();
893870 }
894871
895872 if (!CloudSheetFragment .isCloudProviderAvailable (context )) {
0 commit comments