1616package com.hippo.ehviewer.preference
1717
1818import android.app.Activity
19- import android .app.ProgressDialog
19+ import com.hippo .app.ProgressDialog
2020import android.content.Context
21- import android.os.AsyncTask
21+ import android.os.Handler
22+ import android.os.Looper
2223import android.os.Parcel
2324import android.os.Parcelable
2425import android.util.AttributeSet
2526import android.widget.Toast
2627import androidx.preference.Preference
28+ import com.google.firebase.crashlytics.FirebaseCrashlytics
2729import com.hippo.ehviewer.EhApplication
2830import com.hippo.ehviewer.EhDB
2931import com.hippo.ehviewer.R
@@ -37,13 +39,15 @@ import com.hippo.ehviewer.spider.SpiderQueen
3739import com.hippo.lib.yorozuya.IOUtils
3840import com.hippo.unifile.UniFile
3941import com.hippo.util.ExceptionUtils.throwIfFatal
42+ import com.hippo.util.IoThreadPoolExecutor
4043import okhttp3.OkHttpClient
4144import java.io.IOException
4245import java.io.InputStream
4346import java.util.Collections
47+ import java.util.concurrent.atomic.AtomicBoolean
4448
4549class RestoreDownloadPreference : Preference {
46- private var mTask: AsyncTask < Void ?, Any ?, Any ?> ? = null
50+ private var mTask: RestoreTask ? = null
4751
4852 constructor (context: Context , attrs: AttributeSet ? ) : super (context, attrs)
4953
@@ -56,33 +60,46 @@ class RestoreDownloadPreference : Preference {
5660 override fun onClick () {
5761 super .onClick()
5862 if (mTask == null ) {
59- mTask = RestoreTask (getContext()).execute()
63+ mTask = RestoreTask (context). also { it.start() }
6064 }
6165 }
6266
6367 override fun onDetached () {
64- if (mTask != null ) {
65- mTask!! .cancel(true )
66- mTask = null
67- }
68+ mTask?.cancel()
69+ mTask = null
6870 super .onDetached()
6971 }
7072
71- private inner class RestoreTask (private val mContext : Context ) :
72- AsyncTask <Void ?, Any ?, Any ?>() {
73- private val mApplication: EhApplication
74- private val mManager: DownloadManager
75- private val mHttpClient: OkHttpClient
73+ private inner class RestoreTask (private val mContext : Context ) {
74+ private val mApplication: EhApplication =
75+ mContext.applicationContext as EhApplication
76+ private val mManager: DownloadManager =
77+ EhApplication .getDownloadManager(mApplication)
78+ private val mHttpClient: OkHttpClient =
79+ EhApplication .getOkHttpClient(mApplication)
80+ private val mHandler = Handler (Looper .getMainLooper())
81+ private val mCancelled = AtomicBoolean (false )
7682 private var mProgressDialog: ProgressDialog ? = null
7783
78- init {
79- mApplication = mContext.getApplicationContext() as EhApplication
80- mManager = EhApplication .getDownloadManager(mApplication)
81- mHttpClient = EhApplication .getOkHttpClient(mApplication)
84+ fun start () {
85+ onPreExecute()
86+ IoThreadPoolExecutor .instance.execute {
87+ val result = doInBackground()
88+ mHandler.post {
89+ if (mCancelled.get()) {
90+ onCancelled()
91+ } else {
92+ onPostExecute(result)
93+ }
94+ }
95+ }
96+ }
97+
98+ fun cancel () {
99+ mCancelled.set(true )
82100 }
83101
84- override fun onPreExecute () {
85- super .onPreExecute()
102+ private fun onPreExecute () {
86103 mProgressDialog = ProgressDialog (mContext)
87104 mProgressDialog!! .setTitle(R .string.settings_download_restore_download_items)
88105 mProgressDialog!! .setIndeterminate(false )
@@ -95,18 +112,12 @@ class RestoreDownloadPreference : Preference {
95112 if (null == file || ! file.isDirectory()) {
96113 return null
97114 }
98- val siFile = file.findFile(SpiderQueen .SPIDER_INFO_FILENAME )
99- if (null == siFile) {
100- return null
101- }
115+ val siFile = file.findFile(SpiderQueen .SPIDER_INFO_FILENAME ) ? : return null
102116
103117 var `is `: InputStream ? = null
104118 try {
105119 `is ` = siFile.openInputStream()
106- val spiderInfo = SpiderInfo .read(`is `)
107- if (spiderInfo == null ) {
108- return null
109- }
120+ val spiderInfo = SpiderInfo .read(`is `) ? : return null
110121 val gid = spiderInfo.gid
111122 if (mManager.containDownloadInfo(gid)) {
112123 return null
@@ -118,32 +129,33 @@ class RestoreDownloadPreference : Preference {
118129 restoreItem.dirname = file.getName()
119130 return restoreItem
120131 } catch (e: IOException ) {
132+ FirebaseCrashlytics .getInstance().recordException(e)
121133 return null
122134 } finally {
123135 IOUtils .closeQuietly(`is `)
124136 }
125137 }
126138
127- override fun doInBackground (vararg params : Void ? ): Any? {
128- val dir = Settings .getDownloadLocation()
129- if (null == dir) {
139+ private fun doInBackground (): Any? {
140+ if (mCancelled.get()) {
130141 return null
131142 }
143+ val dir = Settings .getDownloadLocation() ? : return null
132144
133- val restoreItemList: MutableList <RestoreItem ?> = ArrayList < RestoreItem ?> ()
145+ val restoreItemList: MutableList <RestoreItem ?> = ArrayList ()
134146
135- val files = dir.listFiles()
136- if (files == null ) {
137- return null
138- }
147+ val files = dir.listFiles() ? : return null
139148
140149 val total = files.size
141150 publishProgress(0 , total)
142151
143152 for (i in 0 .. < total) {
153+ if (mCancelled.get()) {
154+ return null
155+ }
144156 val file = files[i]
145157 val restoreItem = getRestoreItem(file)
146- if (null != restoreItem ) {
158+ if (restoreItem != null ) {
147159 restoreItemList.add(restoreItem)
148160 }
149161 publishProgress(i + 1 , total)
@@ -155,8 +167,8 @@ class RestoreDownloadPreference : Preference {
155167
156168 publishProgress(- 1 , - 1 )
157169
158- try {
159- return EhEngine .fillGalleryListByApi(
170+ return try {
171+ EhEngine .fillGalleryListByApi(
160172 null ,
161173 mHttpClient,
162174 ArrayList <GalleryInfo ?>(restoreItemList),
@@ -165,10 +177,14 @@ class RestoreDownloadPreference : Preference {
165177 } catch (e: Throwable ) {
166178 throwIfFatal(e)
167179 e.printStackTrace()
168- return null
180+ null
169181 }
170182 }
171183
184+ private fun publishProgress (progress : Int , max : Int ) {
185+ mHandler.post { onProgressUpdate(progress, max) }
186+ }
187+
172188 val isContextValid: Boolean
173189 get() {
174190 if (mContext is Activity ) {
@@ -184,7 +200,7 @@ class RestoreDownloadPreference : Preference {
184200 }
185201 if (this .isContextValid) {
186202 try {
187- if (mProgressDialog!! .isShowing() ) {
203+ if (mProgressDialog!! .isShowing) {
188204 mProgressDialog!! .dismiss()
189205 }
190206 } catch (e: IllegalArgumentException ) {
@@ -194,11 +210,8 @@ class RestoreDownloadPreference : Preference {
194210 mProgressDialog = null
195211 }
196212
197- override fun onProgressUpdate (vararg values : Any? ) {
198- super .onProgressUpdate(* values)
213+ private fun onProgressUpdate (progress : Int , max : Int ) {
199214 if (mProgressDialog != null && this .isContextValid) {
200- val progress = values[0 ] as Int
201- val max = values[1 ] as Int
202215 if (progress == - 1 && max == - 1 ) {
203216 mProgressDialog!! .setIndeterminate(true )
204217 mProgressDialog!! .setMessage(mApplication.getString(R .string.settings_download_restore_download_items_get_gallery_info))
@@ -210,12 +223,12 @@ class RestoreDownloadPreference : Preference {
210223 }
211224 }
212225
213- override fun onCancelled () {
226+ private fun onCancelled () {
214227 mTask = null
215228 dismissProgressDialog()
216229 }
217230
218- override fun onPostExecute (o : Any? ) {
231+ private fun onPostExecute (o : Any? ) {
219232 mTask = null
220233 dismissProgressDialog()
221234 if (o !is MutableList <* >) {
@@ -284,6 +297,7 @@ class RestoreDownloadPreference : Preference {
284297 }
285298
286299 companion object {
300+ @JvmField
287301 val CREATOR : Parcelable .Creator <RestoreItem ?> =
288302 object : Parcelable .Creator <RestoreItem ?> {
289303 override fun createFromParcel (source : Parcel ): RestoreItem {
0 commit comments