@@ -23,6 +23,7 @@ class DatabaseDownloadActivity : AppCompatActivity() {
2323 private val databaseFiles = mutableListOf<String >()
2424 private lateinit var preferences: SharedPreferences
2525 private var progressDialog: ProgressDialog ? = null
26+ private val DB_FILENAME = " wikilite.db"
2627
2728 override fun onCreate (savedInstanceState : Bundle ? ) {
2829 super .onCreate(savedInstanceState)
@@ -34,12 +35,17 @@ class DatabaseDownloadActivity : AppCompatActivity() {
3435 loadDatabaseFiles()
3536 }
3637
38+ private fun goToMainActivity () {
39+ startActivity(Intent (this , MainActivity ::class .java))
40+ finish()
41+ }
42+
3743 private fun setupUI () {
3844 recyclerView = findViewById(R .id.recyclerView)
3945 recyclerView.layoutManager = LinearLayoutManager (this )
4046
4147 adapter = DatabaseFileAdapter (databaseFiles) { filePath ->
42- showDownloadOptions (filePath)
48+ startDownload (filePath, filesDir.absolutePath )
4349 }
4450 recyclerView.adapter = adapter
4551 }
@@ -93,35 +99,6 @@ class DatabaseDownloadActivity : AppCompatActivity() {
9399 return files
94100 }
95101
96- private fun getAppStorageLocations (): List <String > {
97- val locationPaths = mutableListOf<String >()
98- val storageDirs = applicationContext.getExternalFilesDirs(null ).filterNotNull()
99-
100- if (storageDirs.isNotEmpty()) {
101- locationPaths.addAll(storageDirs.map { it.absolutePath })
102- }
103-
104- if (locationPaths.isEmpty()) {
105- locationPaths.add(applicationContext.filesDir.absolutePath)
106- }
107- return locationPaths
108- }
109-
110- private fun showDownloadOptions (filePath : String ) {
111- val storageLocations = getAppStorageLocations()
112-
113- if (storageLocations.size <= 1 ) {
114- val downloadPath = storageLocations.first()
115- Toast .makeText(this , " Downloading to app storage..." , Toast .LENGTH_SHORT ).show()
116- startDownload(filePath, downloadPath)
117- } else {
118- val dialog = DownloadDialog (filePath) { selectedFilePath, downloadPath ->
119- startDownload(selectedFilePath, downloadPath)
120- }
121- dialog.show(supportFragmentManager, " download_dialog" )
122- }
123- }
124-
125102 private fun startDownload (filePath : String , downloadPath : String ) {
126103 DownloadAndExtractTask ().execute(filePath to downloadPath)
127104 }
@@ -155,8 +132,7 @@ class DatabaseDownloadActivity : AppCompatActivity() {
155132
156133 val fileLength = connection.contentLength.toLong()
157134
158- val finalFileName = currentFilePath.substringAfterLast(" /" ).replace(" .gz" , " " )
159- val outputFile = File (downloadPath, finalFileName)
135+ val outputFile = File (downloadPath, DB_FILENAME )
160136 finalDbPath = outputFile.absolutePath
161137
162138 val inputStream = connection.getInputStream()
@@ -187,7 +163,6 @@ class DatabaseDownloadActivity : AppCompatActivity() {
187163 true
188164
189165 } catch (e: Exception ) {
190- println (" Download/Extraction error: ${e.message} " )
191166 e.printStackTrace()
192167 false
193168 }
@@ -216,8 +191,7 @@ class DatabaseDownloadActivity : AppCompatActivity() {
216191 if (success) {
217192 preferences.edit().putString(" db_path" , finalDbPath).apply ()
218193 Toast .makeText(this @DatabaseDownloadActivity, " Download successful!" , Toast .LENGTH_SHORT ).show()
219- startActivity(Intent (this @DatabaseDownloadActivity, MainActivity ::class .java))
220- finish()
194+ goToMainActivity()
221195 } else {
222196 Toast .makeText(this @DatabaseDownloadActivity, " Download failed!" , Toast .LENGTH_LONG ).show()
223197 }
0 commit comments