@@ -7,13 +7,13 @@ import android.content.Intent
77import android.content.pm.PackageManager
88import android.os.*
99import android.os.Environment.MEDIA_MOUNTED
10- import android.support.v4 .app.ActivityCompat
11- import android.support.v4 .content.ContextCompat
12- import android.support.v4.util .ArrayMap
13- import android.support.v4 .widget.SwipeRefreshLayout
14- import android.support.v7 .app.AppCompatActivity
15- import android.support.v7 .widget.LinearLayoutManager
16- import android.support.v7 .widget.RecyclerView
10+ import androidx.core .app.ActivityCompat
11+ import androidx.core .content.ContextCompat
12+ import androidx.collection .ArrayMap
13+ import androidx.swiperefreshlayout .widget.SwipeRefreshLayout
14+ import androidx.appcompat .app.AppCompatActivity
15+ import androidx.recyclerview .widget.LinearLayoutManager
16+ import androidx.recyclerview .widget.RecyclerView
1717import android.util.Log
1818import android.view.LayoutInflater
1919import android.view.View
@@ -48,28 +48,15 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
4848 private var btnGoBack: ImageView ? = null
4949 private var mainHandler = Handler (Looper .getMainLooper())
5050
51- private val loadingFileWorkerQueue: BlockingQueue <Runnable > = LinkedBlockingQueue ()
52-
5351 // Creates a thread pool manager
54- private var loadingThreadPool: ThreadPoolExecutor = ThreadPoolExecutor (
55- 1 , // Initial pool size
56- 1 , // Max pool size
57- KEEP_ALIVE_TIME ,
58- TimeUnit .MINUTES ,
59- loadingFileWorkerQueue
60- )
61- get() {
62- if (field.isShutdown) {
63- field = ThreadPoolExecutor (
64- 1 ,
65- 1 ,
66- KEEP_ALIVE_TIME ,
67- TimeUnit .MINUTES ,
68- loadingFileWorkerQueue
69- )
70- }
71- return field
72- }
52+ private val loadingThreadPool: ExecutorService =
53+ FilePickerManager .config.threadPool ? : ThreadPoolExecutor (
54+ 1 , // Initial pool size
55+ 1 , // Max pool size
56+ KEEP_ALIVE_TIME ,
57+ TimeUnit .MINUTES ,
58+ LinkedBlockingDeque ()
59+ )
7360
7461 private val loadFileRunnable: Runnable by lazy {
7562 Runnable {
@@ -190,7 +177,12 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
190177
191178 override fun onDestroy () {
192179 super .onDestroy()
193- if (! loadingThreadPool.isShutdown) {
180+ Log .i(TAG , " onDestroy" )
181+ val shouldShutDownThreadPool = pickerConfig.threadPool != loadingThreadPool
182+ || pickerConfig.threadPoolAutoShutDown
183+
184+ if (! loadingThreadPool.isShutdown && shouldShutDownThreadPool) {
185+ Log .i(TAG , " shutdown thread pool" )
194186 loadingThreadPool.shutdown()
195187 }
196188 currOffsetMap.clear()
@@ -218,6 +210,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
218210 permissions : Array <out String >,
219211 grantResults : IntArray
220212 ) {
213+ super .onRequestPermissionsResult(requestCode, permissions, grantResults)
221214 when (requestCode) {
222215 FILE_PICKER_PERMISSION_REQUEST_CODE -> {
223216 if (grantResults.isEmpty() || grantResults[0 ] != PackageManager .PERMISSION_GRANTED ) {
@@ -303,7 +296,11 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
303296
304297 rvNav = findViewById<RecyclerView >(R .id.rv_nav_file_picker).apply {
305298 layoutManager =
306- LinearLayoutManager (this @FilePickerActivity, LinearLayoutManager .HORIZONTAL , false )
299+ LinearLayoutManager (
300+ this @FilePickerActivity,
301+ LinearLayoutManager .HORIZONTAL ,
302+ false
303+ )
307304 adapter = navAdapter
308305 }
309306 rvList = findViewById<RecyclerViewFilePicker >(R .id.rv_list_file_picker).apply {
@@ -328,9 +325,18 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
328325 return
329326 }
330327 if (Environment .getExternalStorageState() != MEDIA_MOUNTED ) {
331- throw Throwable (cause = IllegalStateException (" External storage is not available ====>>> Environment.getExternalStorageState() != MEDIA_MOUNTED" ))
328+ Log .e(
329+ TAG , " External storage is not available ====>>> "
330+ + " Environment.getExternalStorageState() != MEDIA_MOUNTED"
331+ )
332+ return
333+ }
334+ try {
335+ Log .i(TAG , " loadList in ${Thread .currentThread()} in $loadingThreadPool " )
336+ loadingThreadPool.submit(loadFileRunnable)
337+ } catch (e: RejectedExecutionException ) {
338+ Log .e(TAG , " submit job failed" )
332339 }
333- loadingThreadPool.submit(loadFileRunnable)
334340 }
335341
336342 private fun initRv (
@@ -652,6 +658,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
652658 private fun isCanSelect () = selectedCount < maxSelectable
653659
654660 companion object {
661+ private const val TAG = " FilePickerActivity"
655662 private const val FILE_PICKER_PERMISSION_REQUEST_CODE = 10201
656663
657664 // Sets the amount of time an idle thread waits before terminating
0 commit comments