forked from penguin86/luna-tracker
		
	Implemented transition from local to webdav logbooks and error management
This commit is contained in:
		@@ -8,7 +8,9 @@ import android.widget.Toast
 | 
				
			|||||||
import androidx.appcompat.app.AppCompatActivity
 | 
					import androidx.appcompat.app.AppCompatActivity
 | 
				
			||||||
import com.google.android.material.progressindicator.LinearProgressIndicator
 | 
					import com.google.android.material.progressindicator.LinearProgressIndicator
 | 
				
			||||||
import com.thegrizzlylabs.sardineandroid.impl.SardineException
 | 
					import com.thegrizzlylabs.sardineandroid.impl.SardineException
 | 
				
			||||||
 | 
					import it.danieleverducci.lunatracker.repository.FileLogbookRepository
 | 
				
			||||||
import it.danieleverducci.lunatracker.repository.LocalSettingsRepository
 | 
					import it.danieleverducci.lunatracker.repository.LocalSettingsRepository
 | 
				
			||||||
 | 
					import it.danieleverducci.lunatracker.repository.LogbookListObtainedListener
 | 
				
			||||||
import it.danieleverducci.lunatracker.repository.LogbookRepository
 | 
					import it.danieleverducci.lunatracker.repository.LogbookRepository
 | 
				
			||||||
import it.danieleverducci.lunatracker.repository.WebDAVLogbookRepository
 | 
					import it.danieleverducci.lunatracker.repository.WebDAVLogbookRepository
 | 
				
			||||||
import okio.IOException
 | 
					import okio.IOException
 | 
				
			||||||
@@ -73,13 +75,34 @@ open class SettingsActivity : AppCompatActivity() {
 | 
				
			|||||||
            textViewWebDAVPass.text.toString()
 | 
					            textViewWebDAVPass.text.toString()
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        progressIndicator.visibility = View.VISIBLE
 | 
					        progressIndicator.visibility = View.VISIBLE
 | 
				
			||||||
        webDAVLogbookRepo.createLogbook(this, LogbookRepository.DEFAULT_LOGBOOK_NAME, object: WebDAVLogbookRepository.LogbookCreatedListener{
 | 
					
 | 
				
			||||||
            override fun onLogbookCreated() {
 | 
					        webDAVLogbookRepo.listLogbooks(this, object: LogbookListObtainedListener{
 | 
				
			||||||
                runOnUiThread({
 | 
					
 | 
				
			||||||
                    progressIndicator.visibility = View.INVISIBLE
 | 
					            override fun onLogbookListObtained(logbooksNames: ArrayList<String>) {
 | 
				
			||||||
                    saveSettings()
 | 
					                if (logbooksNames.isEmpty()) {
 | 
				
			||||||
                    Toast.makeText(this@SettingsActivity, R.string.settings_webdav_creation_ok, Toast.LENGTH_SHORT).show()
 | 
					                    // TODO: Ask the user if he wants to upload the local ones or to create a new one
 | 
				
			||||||
                })
 | 
					                    copyLocalLogbooksToWebdav(webDAVLogbookRepo, object: OnCopyLocalLogbooksToWebdavFinishedListener {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        override fun onCopyLocalLogbooksToWebdavFinished(errors: String?) {
 | 
				
			||||||
 | 
					                            runOnUiThread({
 | 
				
			||||||
 | 
					                                progressIndicator.visibility = View.INVISIBLE
 | 
				
			||||||
 | 
					                                if (errors == null) {
 | 
				
			||||||
 | 
					                                    saveSettings()
 | 
				
			||||||
 | 
					                                    Toast.makeText(this@SettingsActivity, R.string.settings_webdav_creation_ok, Toast.LENGTH_SHORT).show()
 | 
				
			||||||
 | 
					                                } else {
 | 
				
			||||||
 | 
					                                    Toast.makeText(this@SettingsActivity, errors, Toast.LENGTH_SHORT).show()
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                            })
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    })
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    runOnUiThread({
 | 
				
			||||||
 | 
					                        progressIndicator.visibility = View.INVISIBLE
 | 
				
			||||||
 | 
					                        saveSettings()
 | 
				
			||||||
 | 
					                        Toast.makeText(this@SettingsActivity, R.string.settings_webdav_creation_ok, Toast.LENGTH_SHORT).show()
 | 
				
			||||||
 | 
					                    })
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            override fun onIOError(error: IOException) {
 | 
					            override fun onIOError(error: IOException) {
 | 
				
			||||||
@@ -100,6 +123,16 @@ open class SettingsActivity : AppCompatActivity() {
 | 
				
			|||||||
                })
 | 
					                })
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            override fun onError(error: Exception) {
 | 
				
			||||||
 | 
					                runOnUiThread({
 | 
				
			||||||
 | 
					                    progressIndicator.visibility = View.INVISIBLE
 | 
				
			||||||
 | 
					                    Toast.makeText(this@SettingsActivity, getString(R.string.settings_generic_error) + error.toString(), Toast.LENGTH_SHORT).show()
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /*webDAVLogbookRepo.createLogbook(this, LogbookRepository.DEFAULT_LOGBOOK_NAME, object: WebDAVLogbookRepository.LogbookCreatedListener{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            override fun onJSONError(error: JSONException) {
 | 
					            override fun onJSONError(error: JSONException) {
 | 
				
			||||||
                runOnUiThread({
 | 
					                runOnUiThread({
 | 
				
			||||||
                    progressIndicator.visibility = View.INVISIBLE
 | 
					                    progressIndicator.visibility = View.INVISIBLE
 | 
				
			||||||
@@ -107,14 +140,8 @@ open class SettingsActivity : AppCompatActivity() {
 | 
				
			|||||||
                })
 | 
					                })
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            override fun onError(error: Exception) {
 | 
					 | 
				
			||||||
                runOnUiThread({
 | 
					 | 
				
			||||||
                    progressIndicator.visibility = View.INVISIBLE
 | 
					 | 
				
			||||||
                    Toast.makeText(this@SettingsActivity, getString(R.string.settings_generic_error) + error.toString(), Toast.LENGTH_SHORT).show()
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        })
 | 
					        })*/
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun saveSettings() {
 | 
					    fun saveSettings() {
 | 
				
			||||||
@@ -130,4 +157,32 @@ open class SettingsActivity : AppCompatActivity() {
 | 
				
			|||||||
        finish()
 | 
					        finish()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Copies the local logbooks to webdav.
 | 
				
			||||||
 | 
					     * @return success
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private fun copyLocalLogbooksToWebdav(webDAVLogbookRepository: WebDAVLogbookRepository, listener: OnCopyLocalLogbooksToWebdavFinishedListener) {
 | 
				
			||||||
 | 
					        Thread(Runnable {
 | 
				
			||||||
 | 
					            var errors = StringBuilder()
 | 
				
			||||||
 | 
					            val fileLogbookRepo = FileLogbookRepository()
 | 
				
			||||||
 | 
					            val logbooks = fileLogbookRepo.getAllLogbooks(this)
 | 
				
			||||||
 | 
					            for (logbook in logbooks) {
 | 
				
			||||||
 | 
					                // Copy only if does not already exist
 | 
				
			||||||
 | 
					                val error = webDAVLogbookRepository.uploadLogbookIfNotExists(this, logbook.name)
 | 
				
			||||||
 | 
					                if (error != null) {
 | 
				
			||||||
 | 
					                    if (errors.isNotEmpty())
 | 
				
			||||||
 | 
					                        errors.append("\n")
 | 
				
			||||||
 | 
					                    errors.append(String.format(getString(R.string.settings_webdav_upload_error), logbook.name, error))
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            listener.onCopyLocalLogbooksToWebdavFinished(
 | 
				
			||||||
 | 
					                if (errors.isEmpty()) null else errors.toString()
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					        }).start()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private interface OnCopyLocalLogbooksToWebdavFinishedListener {
 | 
				
			||||||
 | 
					        fun onCopyLocalLogbooksToWebdavFinished(errors: String?)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -70,6 +70,18 @@ class FileLogbookRepository: LogbookRepository {
 | 
				
			|||||||
        context: Context,
 | 
					        context: Context,
 | 
				
			||||||
        listener: LogbookListObtainedListener
 | 
					        listener: LogbookListObtainedListener
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
 | 
					        listener.onLogbookListObtained(listLogbooks(context))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fun getAllLogbooks(context: Context): List<Logbook> {
 | 
				
			||||||
 | 
					        val logbooks = arrayListOf<Logbook>()
 | 
				
			||||||
 | 
					        for (logbookName in listLogbooks(context)) {
 | 
				
			||||||
 | 
					            logbooks.add(loadLogbook(context, logbookName))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return logbooks
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private fun listLogbooks(context: Context): ArrayList<String> {
 | 
				
			||||||
        val logbooksFileNames = context.getFilesDir().list(object: FilenameFilter {
 | 
					        val logbooksFileNames = context.getFilesDir().list(object: FilenameFilter {
 | 
				
			||||||
            override fun accept(dir: File?, name: String?): Boolean {
 | 
					            override fun accept(dir: File?, name: String?): Boolean {
 | 
				
			||||||
                if (name == null)
 | 
					                if (name == null)
 | 
				
			||||||
@@ -81,8 +93,7 @@ class FileLogbookRepository: LogbookRepository {
 | 
				
			|||||||
        })
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (logbooksFileNames == null || logbooksFileNames.isEmpty()) {
 | 
					        if (logbooksFileNames == null || logbooksFileNames.isEmpty()) {
 | 
				
			||||||
            listener.onLogbookListObtained(arrayListOf())
 | 
					            return arrayListOf()
 | 
				
			||||||
            return
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        val logbooksNames = arrayListOf<String>()
 | 
					        val logbooksNames = arrayListOf<String>()
 | 
				
			||||||
@@ -91,7 +102,7 @@ class FileLogbookRepository: LogbookRepository {
 | 
				
			|||||||
                it.replace("${FILE_NAME_START}_", "").replace(FILE_NAME_START, "").replace(FILE_NAME_END, "")
 | 
					                it.replace("${FILE_NAME_START}_", "").replace(FILE_NAME_START, "").replace(FILE_NAME_END, "")
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        listener.onLogbookListObtained(logbooksNames)
 | 
					        return logbooksNames
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private fun getFileName(name: String): String {
 | 
					    private fun getFileName(name: String): String {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -137,47 +137,46 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Connect to server and check if a logbook already exists.
 | 
					     * Connect to server and check if a logbook already exists.
 | 
				
			||||||
     * If it does not exist, try to upload the local one (or create a new one).
 | 
					     * If it does not exist, try to upload the local one.
 | 
				
			||||||
 | 
					     * @return error, or null if no error
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    fun createLogbook(context: Context, name: String, listener: LogbookCreatedListener) {
 | 
					    fun uploadLogbookIfNotExists(context: Context, name: String): String? {
 | 
				
			||||||
        Thread(Runnable {
 | 
					        val flr = FileLogbookRepository()
 | 
				
			||||||
            try {
 | 
					        try {
 | 
				
			||||||
                loadLogbook(name)
 | 
					            loadLogbook(name)
 | 
				
			||||||
                listener.onLogbookCreated()
 | 
					            Log.d(TAG, "Logbook file $name already exist on the webDav share: will not overwrite it")
 | 
				
			||||||
            } catch (e: SardineException) {
 | 
					            return null
 | 
				
			||||||
                if (e.toString().contains("404")) {
 | 
					        } catch (e: SardineException) {
 | 
				
			||||||
                    // Connection successful, but no existing save. Upload the local one.
 | 
					            if (e.toString().contains("404")) {
 | 
				
			||||||
                    try {
 | 
					                // Connection successful, but logbook does not exist. Upload the local one.
 | 
				
			||||||
                        val flr = FileLogbookRepository()
 | 
					                try {
 | 
				
			||||||
                        val logbook = flr.loadLogbook(context, name)
 | 
					                    val logbook = flr.loadLogbook(context, name)
 | 
				
			||||||
                        saveLogbook(context, logbook)
 | 
					                    saveLogbook(context, logbook)
 | 
				
			||||||
                        Log.d(TAG, "Local logbook file found, uploaded")
 | 
					                    Log.d(TAG, "Local logbook file $name found, uploaded")
 | 
				
			||||||
                        listener.onLogbookCreated()
 | 
					                    return null
 | 
				
			||||||
                    } catch (e: FileNotFoundException) {
 | 
					                } catch (e: FileNotFoundException) {
 | 
				
			||||||
                        Log.d(TAG, "No local logbook file found, uploading empty file")
 | 
					                    Log.e(TAG, "No local logbook file found, this should not happen!")
 | 
				
			||||||
                        saveLogbook(context, Logbook(name))
 | 
					                    return "No local logbook file found, app is in inconsistent state, please delete and reinstall it"
 | 
				
			||||||
                        listener.onLogbookCreated()
 | 
					                } catch (e: SardineException) {
 | 
				
			||||||
                    } catch (e: SardineException) {
 | 
					                    Log.e(TAG, "Unable to upload logbook: $e")
 | 
				
			||||||
                        Log.e(TAG, "Unable to upload logbook: $e")
 | 
					                    return e.toString()
 | 
				
			||||||
                        listener.onWebDAVError(e)
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    Log.e(TAG, e.toString())
 | 
					 | 
				
			||||||
                    listener.onWebDAVError(e)
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            } catch (e: IOException) {
 | 
					            } else {
 | 
				
			||||||
                Log.e(TAG, e.toString())
 | 
					                Log.e(TAG, e.toString())
 | 
				
			||||||
                listener.onIOError(e)
 | 
					                return e.toString()
 | 
				
			||||||
            } catch (e: SocketTimeoutException) {
 | 
					 | 
				
			||||||
                Log.e(TAG, e.toString())
 | 
					 | 
				
			||||||
                listener.onIOError(e)
 | 
					 | 
				
			||||||
            } catch (e: JSONException) {
 | 
					 | 
				
			||||||
                Log.e(TAG, e.toString())
 | 
					 | 
				
			||||||
                listener.onJSONError(e)
 | 
					 | 
				
			||||||
            } catch (e: Exception) {
 | 
					 | 
				
			||||||
                listener.onError(e)
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }).start()
 | 
					        } catch (e: IOException) {
 | 
				
			||||||
 | 
					            Log.e(TAG, e.toString())
 | 
				
			||||||
 | 
					            return e.toString()
 | 
				
			||||||
 | 
					        } catch (e: SocketTimeoutException) {
 | 
				
			||||||
 | 
					            Log.e(TAG, e.toString())
 | 
				
			||||||
 | 
					            return e.toString()
 | 
				
			||||||
 | 
					        } catch (e: JSONException) {
 | 
				
			||||||
 | 
					            Log.e(TAG, e.toString())
 | 
				
			||||||
 | 
					            return e.toString()
 | 
				
			||||||
 | 
					        } catch (e: Exception) {
 | 
				
			||||||
 | 
					            return e.toString()
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private fun getUrl(name: String): String {
 | 
					    private fun getUrl(name: String): String {
 | 
				
			||||||
@@ -185,13 +184,4 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
 | 
				
			|||||||
        Log.d(TAG, fileName)
 | 
					        Log.d(TAG, fileName)
 | 
				
			||||||
        return "$webDavURL/$fileName"
 | 
					        return "$webDavURL/$fileName"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    interface LogbookCreatedListener {
 | 
					 | 
				
			||||||
        fun onLogbookCreated()
 | 
					 | 
				
			||||||
        fun onIOError(error: okio.IOException)
 | 
					 | 
				
			||||||
        fun onWebDAVError(error: SardineException)
 | 
					 | 
				
			||||||
        fun onJSONError(error: JSONException)
 | 
					 | 
				
			||||||
        fun onError(error: Exception)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -66,6 +66,7 @@
 | 
				
			|||||||
    <string name="settings_webdav_creation_ok">Connessione al server WebDAV avvenuta con successo</string>
 | 
					    <string name="settings_webdav_creation_ok">Connessione al server WebDAV avvenuta con successo</string>
 | 
				
			||||||
    <string name="settings_json_error">Sul server esiste un salvataggio, ma è corrotto o illeggibile. Cancellare il file </string>
 | 
					    <string name="settings_json_error">Sul server esiste un salvataggio, ma è corrotto o illeggibile. Cancellare il file </string>
 | 
				
			||||||
    <string name="settings_generic_error">Si è verificato un errore: </string>
 | 
					    <string name="settings_generic_error">Si è verificato un errore: </string>
 | 
				
			||||||
 | 
					    <string name="settings_webdav_upload_error">Errore durante l\'upload del logbook locale %1$s su webdav: %2$s</string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <string name="trim_logbook_dialog_title">Il tuo diario è bello grande!</string>
 | 
					    <string name="trim_logbook_dialog_title">Il tuo diario è bello grande!</string>
 | 
				
			||||||
    <string name="trim_logbook_dialog_message_local">Il file del tuo diario sta crescendo molto. Ti suggeriamo di cancellare gli eventi più vecchi per evitare problemi di memoria.</string>
 | 
					    <string name="trim_logbook_dialog_message_local">Il file del tuo diario sta crescendo molto. Ti suggeriamo di cancellare gli eventi più vecchi per evitare problemi di memoria.</string>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -80,6 +80,7 @@
 | 
				
			|||||||
    <string name="settings_webdav_creation_ok">Successfully connected with WebDAV server</string>
 | 
					    <string name="settings_webdav_creation_ok">Successfully connected with WebDAV server</string>
 | 
				
			||||||
    <string name="settings_json_error">There\'s a save file on the server, but is corrupted or unreadable. Please delete it </string>
 | 
					    <string name="settings_json_error">There\'s a save file on the server, but is corrupted or unreadable. Please delete it </string>
 | 
				
			||||||
    <string name="settings_generic_error">Error: </string>
 | 
					    <string name="settings_generic_error">Error: </string>
 | 
				
			||||||
 | 
					    <string name="settings_webdav_upload_error">Error while uploading local logbook %1$s to webdav: %2$s</string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <string name="trim_logbook_dialog_title">Your logbook is pretty big!</string>
 | 
					    <string name="trim_logbook_dialog_title">Your logbook is pretty big!</string>
 | 
				
			||||||
    <string name="trim_logbook_dialog_message_local">Your logbook file is growing a lot. We suggest trimming the oldest events to avoid crashes.</string>
 | 
					    <string name="trim_logbook_dialog_message_local">Your logbook file is growing a lot. We suggest trimming the oldest events to avoid crashes.</string>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user