@@ -211,31 +211,44 @@ public static SpiderQueen obtainSpiderQueen(@NonNull Context context,
211211
212212 @ UiThread
213213 public static int findStartPage (@ NonNull Context context , @ NonNull GalleryInfo galleryInfo ) {
214- SpiderInfo spiderInfo = null ;
215- SimpleDiskCache msic ;
216- EhApplication application = (EhApplication ) context .getApplicationContext ();
217- msic = EhApplication .getSpiderInfoCache (application );
218- InputStreamPipe pipe = msic .getInputStreamPipe (Long .toString (galleryInfo .gid ));
219- if (null != pipe ) {
220- try {
221- pipe .obtain ();
222- spiderInfo = SpiderInfo .read (pipe .open ());
223- } catch (IOException ignore ) {
224- // Ignore
225- // Crashes.trackError(ignore);
226- } finally {
227- pipe .close ();
228- pipe .release ();
229- }
230- }
214+ SpiderInfo fromDownload = SpiderInfo .getSpiderInfo (galleryInfo );
215+ SpiderInfo fromCache = readSpiderInfoFromCache (context , galleryInfo .gid );
231216
232217 int startPage = 0 ;
233- if (spiderInfo != null ) {
234- startPage = spiderInfo .startPage ;
218+ if (isValidSpiderInfo (fromDownload , galleryInfo )) {
219+ startPage = fromDownload .startPage ;
220+ }
221+ if (isValidSpiderInfo (fromCache , galleryInfo )) {
222+ startPage = Math .max (startPage , fromCache .startPage );
235223 }
236224 return startPage ;
237225 }
238226
227+ @ Nullable
228+ private static SpiderInfo readSpiderInfoFromCache (@ NonNull Context context , long gid ) {
229+ EhApplication application = (EhApplication ) context .getApplicationContext ();
230+ SimpleDiskCache cache = EhApplication .getSpiderInfoCache (application );
231+ InputStreamPipe pipe = cache .getInputStreamPipe (Long .toString (gid ));
232+ if (pipe == null ) {
233+ return null ;
234+ }
235+ try {
236+ pipe .obtain ();
237+ return SpiderInfo .read (pipe .open ());
238+ } catch (IOException ignore ) {
239+ return null ;
240+ } finally {
241+ pipe .close ();
242+ pipe .release ();
243+ }
244+ }
245+
246+ private static boolean isValidSpiderInfo (@ Nullable SpiderInfo spiderInfo ,
247+ @ NonNull GalleryInfo galleryInfo ) {
248+ return spiderInfo != null && spiderInfo .gid == galleryInfo .gid
249+ && TextUtils .equals (spiderInfo .token , galleryInfo .token );
250+ }
251+
239252 @ UiThread
240253 public static void releaseSpiderQueen (@ NonNull SpiderQueen queen , @ Mode int mode ) {
241254 OSUtils .checkMainLoop ();
@@ -721,13 +734,20 @@ public int getStartPage() {
721734
722735 @ SuppressLint ("StaticFieldLeak" )
723736 public void putStartPage (int page ) {
724- final SpiderInfo spiderInfo = mSpiderInfo .get ();
737+ SpiderInfo spiderInfo = mSpiderInfo .get ();
738+ if (spiderInfo == null ) {
739+ spiderInfo = readSpiderInfoFromLocal ();
740+ if (spiderInfo != null ) {
741+ mSpiderInfo .lazySet (spiderInfo );
742+ }
743+ }
725744 if (spiderInfo != null ) {
726745 spiderInfo .startPage = page ;
746+ final SpiderInfo infoToWrite = spiderInfo ;
727747 new AsyncTask <Void , Void , Void >() {
728748 @ Override
729749 protected Void doInBackground (Void ... params ) {
730- writeSpiderInfoToLocal (spiderInfo );
750+ writeSpiderInfoToLocal (infoToWrite );
731751 return null ;
732752 }
733753 }.executeOnExecutor (IoThreadPoolExecutor .Companion .getInstance ());
@@ -740,36 +760,46 @@ private synchronized SpiderInfo readSpiderInfoFromLocal() {
740760 return spiderInfo ;
741761 }
742762
743- // Read from download dir
763+ SpiderInfo fromDownload = null ;
744764 UniFile downloadDir = mSpiderDen .getDownloadDir ();
745765 if (downloadDir != null ) {
746766 UniFile file = downloadDir .findFile (SPIDER_INFO_FILENAME );
747- spiderInfo = SpiderInfo .read (file );
748- if (spiderInfo != null && spiderInfo .gid == mGalleryInfo .gid &&
749- spiderInfo .token .equals (mGalleryInfo .token )) {
750- return spiderInfo ;
767+ SpiderInfo read = SpiderInfo .read (file );
768+ if (isValidSpiderInfo (read , mGalleryInfo )) {
769+ fromDownload = read ;
751770 }
752771 }
753772
754- // Read from cache
755- InputStreamPipe pipe = mSpiderInfoCache .getInputStreamPipe (Long .toString (mGalleryInfo .gid ));
756- if (null != pipe ) {
757- try {
758- pipe .obtain ();
759- spiderInfo = SpiderInfo .read (pipe .open ());
760- if (spiderInfo != null && spiderInfo .gid == mGalleryInfo .gid &&
761- spiderInfo .token .equals (mGalleryInfo .token )) {
762- return spiderInfo ;
763- }
764- } catch (IOException e ) {
765- // Ignore
766- } finally {
767- pipe .close ();
768- pipe .release ();
769- }
773+ SpiderInfo fromCache = readSpiderInfoFromCache (mGalleryInfo .gid );
774+ if (!isValidSpiderInfo (fromCache , mGalleryInfo )) {
775+ fromCache = null ;
770776 }
771777
772- return null ;
778+ if (fromDownload == null ) {
779+ return fromCache ;
780+ }
781+ if (fromCache == null ) {
782+ return fromDownload ;
783+ }
784+ fromDownload .startPage = Math .max (fromDownload .startPage , fromCache .startPage );
785+ return fromDownload ;
786+ }
787+
788+ @ Nullable
789+ private SpiderInfo readSpiderInfoFromCache (long gid ) {
790+ InputStreamPipe pipe = mSpiderInfoCache .getInputStreamPipe (Long .toString (gid ));
791+ if (pipe == null ) {
792+ return null ;
793+ }
794+ try {
795+ pipe .obtain ();
796+ return SpiderInfo .read (pipe .open ());
797+ } catch (IOException e ) {
798+ return null ;
799+ } finally {
800+ pipe .close ();
801+ pipe .release ();
802+ }
773803 }
774804
775805 private void readPreviews (String body , int index , SpiderInfo spiderInfo ) throws ParseException {
@@ -862,21 +892,19 @@ private String getPTokenFromInternet(int index) {
862892 }
863893
864894 private synchronized void writeSpiderInfoToLocal (@ NonNull SpiderInfo spiderInfo ) {
865- // Only download mode or sync-while-reading is allowed to write into download dir.
866- if (mSpiderDen .shouldWriteToDownloadDir ()) {
867- UniFile downloadDir = mSpiderDen .getDownloadDir ();
868- if (downloadDir != null ) {
869- UniFile file = downloadDir .createFile (SPIDER_INFO_FILENAME );
870- try {
871- spiderInfo .write (file .openOutputStream ());
872- } catch (Throwable e ) {
873- ExceptionUtils .throwIfFatal (e );
874- // Ignore
875- }
895+ // Sync reading progress into an existing download folder; does not create one.
896+ UniFile downloadDir = mSpiderDen .getDownloadDir ();
897+ if (downloadDir != null ) {
898+ UniFile file = downloadDir .createFile (SPIDER_INFO_FILENAME );
899+ try {
900+ spiderInfo .write (file .openOutputStream ());
901+ } catch (Throwable e ) {
902+ ExceptionUtils .throwIfFatal (e );
903+ // Ignore
876904 }
877905 }
878906
879- // Read from cache
907+ // Write to cache
880908 OutputStreamPipe pipe = mSpiderInfoCache .getOutputStreamPipe (Long .toString (mGalleryInfo .gid ));
881909 try {
882910 pipe .obtain ();
0 commit comments