Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/fragments/BackupImportFragment.kt
- This file was moved from app/src/main/java/it/reyboz/bustorino/fragments/TestSavingFragment.kt.
Show All 12 Lines | |||||
import android.widget.Toast | import android.widget.Toast | ||||
import androidx.activity.result.contract.ActivityResultContracts | import androidx.activity.result.contract.ActivityResultContracts | ||||
import androidx.fragment.app.Fragment | import androidx.fragment.app.Fragment | ||||
import de.siegmar.fastcsv.reader.CsvReader | import de.siegmar.fastcsv.reader.CsvReader | ||||
import de.siegmar.fastcsv.writer.CsvWriter | import de.siegmar.fastcsv.writer.CsvWriter | ||||
import it.reyboz.bustorino.R | import it.reyboz.bustorino.R | ||||
import it.reyboz.bustorino.data.PreferencesHolder | import it.reyboz.bustorino.data.PreferencesHolder | ||||
import it.reyboz.bustorino.data.UserDB | import it.reyboz.bustorino.data.UserDB | ||||
import it.reyboz.bustorino.util.Saving | import it.reyboz.bustorino.util.ImportExport | ||||
import java.io.* | import java.io.* | ||||
import java.text.DateFormat | import java.text.DateFormat | ||||
import java.text.SimpleDateFormat | import java.text.SimpleDateFormat | ||||
import java.util.* | import java.util.* | ||||
import java.util.zip.ZipEntry | import java.util.zip.ZipEntry | ||||
import java.util.zip.ZipInputStream | import java.util.zip.ZipInputStream | ||||
import java.util.zip.ZipOutputStream | import java.util.zip.ZipOutputStream | ||||
/** | /** | ||||
* A simple [Fragment] subclass. | * A simple [Fragment] subclass. | ||||
* Use the [TestSavingFragment.newInstance] factory method to | * Use the [BackupImportFragment.newInstance] factory method to | ||||
* create an instance of this fragment. | * create an instance of this fragment. | ||||
*/ | */ | ||||
class TestSavingFragment : Fragment() { | class BackupImportFragment : Fragment() { | ||||
private val saveFileLauncher = | private val saveFileLauncher = | ||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||||
if (result.resultCode == Activity.RESULT_OK) { | if (result.resultCode == Activity.RESULT_OK) { | ||||
result.data?.data?.also { uri -> | result.data?.data?.also { uri -> | ||||
writeDataZip(uri) | writeDataZip(uri) | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | private fun writeDataZip(uri: Uri){ | ||||
val contentResolver = context.contentResolver | val contentResolver = context.contentResolver | ||||
contentResolver.openOutputStream(uri)?.use {outs -> | contentResolver.openOutputStream(uri)?.use {outs -> | ||||
val bof = BufferedOutputStream(outs) | val bof = BufferedOutputStream(outs) | ||||
val zipOutputStream = ZipOutputStream(bof) | val zipOutputStream = ZipOutputStream(bof) | ||||
//write main preferences | //write main preferences | ||||
zipOutputStream.putNextEntry(ZipEntry(MAIN_PREF_NAME)) | zipOutputStream.putNextEntry(ZipEntry(MAIN_PREF_NAME)) | ||||
var sharedPrefs = PreferencesHolder.getMainSharedPreferences(context) | var sharedPrefs = PreferencesHolder.getMainSharedPreferences(context) | ||||
var jsonPref = Saving.writeSharedPreferencesIntoJSON(sharedPrefs) | var jsonPref = ImportExport.writeSharedPreferencesIntoJSON(sharedPrefs) | ||||
writeStringToZipOS(jsonPref.toString(2), zipOutputStream) | writeStringToZipOS(jsonPref.toString(2), zipOutputStream) | ||||
zipOutputStream.closeEntry() | zipOutputStream.closeEntry() | ||||
zipOutputStream.putNextEntry(ZipEntry(APP_PREF_NAME)) | zipOutputStream.putNextEntry(ZipEntry(APP_PREF_NAME)) | ||||
sharedPrefs = PreferencesHolder.getAppPreferences(context) | sharedPrefs = PreferencesHolder.getAppPreferences(context) | ||||
jsonPref = Saving.writeSharedPreferencesIntoJSON(sharedPrefs) | jsonPref = ImportExport.writeSharedPreferencesIntoJSON(sharedPrefs) | ||||
writeStringToZipOS(jsonPref.toString(2), zipOutputStream) | writeStringToZipOS(jsonPref.toString(2), zipOutputStream) | ||||
zipOutputStream.closeEntry() | zipOutputStream.closeEntry() | ||||
//add CSV | //add CSV | ||||
zipOutputStream.putNextEntry(ZipEntry(FAVORITES_NAME)) | zipOutputStream.putNextEntry(ZipEntry(FAVORITES_NAME)) | ||||
val outWriter = OutputStreamWriter(zipOutputStream) | val outWriter = OutputStreamWriter(zipOutputStream) | ||||
val csvWriter = CsvWriter.builder().build(outWriter) | val csvWriter = CsvWriter.builder().build(outWriter) | ||||
val userDB = UserDB(context) | val userDB = UserDB(context) | ||||
userDB.writeFavoritesToCsv(csvWriter) | userDB.writeFavoritesToCsv(csvWriter) | ||||
Show All 25 Lines | private fun loadZipData(uri: Uri, loadFavorites: Boolean, loadPreferences: Boolean){ | ||||
userDB.close() | userDB.close() | ||||
//csvReader.close() | //csvReader.close() | ||||
} | } | ||||
APP_PREF_NAME -> if(loadPreferences){ | APP_PREF_NAME -> if(loadPreferences){ | ||||
val jsonString = readFileToString(zipstream) | val jsonString = readFileToString(zipstream) | ||||
try { | try { | ||||
val pref = PreferencesHolder.getAppPreferences(context) | val pref = PreferencesHolder.getAppPreferences(context) | ||||
Saving.importJsonToSharedPreferences(pref, jsonString, null, Regex("osmdroid\\.")) | ImportExport.importJsonToSharedPreferences(pref, jsonString, null, Regex("osmdroid\\.")) | ||||
} catch (e: Exception){ | } catch (e: Exception){ | ||||
Log.e(DEBUG_TAG, "Cannot read app preferences from file") | Log.e(DEBUG_TAG, "Cannot read app preferences from file") | ||||
e.printStackTrace() | e.printStackTrace() | ||||
} | } | ||||
} | } | ||||
//Main preferences contains the lines favorites | //Main preferences contains the lines favorites | ||||
MAIN_PREF_NAME -> if(loadFavorites){ | MAIN_PREF_NAME -> if(loadFavorites){ | ||||
val jsonString = readFileToString(zipstream) | val jsonString = readFileToString(zipstream) | ||||
try { | try { | ||||
val pref = PreferencesHolder.getMainSharedPreferences(context) | val pref = PreferencesHolder.getMainSharedPreferences(context) | ||||
//In the future, if we move the favorite lines to a different file, | //In the future, if we move the favorite lines to a different file, | ||||
// We should check here if the key is in the jsonObject, and copy it to the other file | // We should check here if the key is in the jsonObject, and copy it to the other file | ||||
Saving.importJsonToSharedPreferences(pref, jsonString, PreferencesHolder.IGNORE_KEYS_LOAD_MAIN, null) | ImportExport.importJsonToSharedPreferences(pref, jsonString, PreferencesHolder.IGNORE_KEYS_LOAD_MAIN, null, | ||||
PreferencesHolder.KEYS_MERGE_SET) | |||||
} catch (e: Exception){ | } catch (e: Exception){ | ||||
Log.e(DEBUG_TAG, "Cannot read main preferences from file") | Log.e(DEBUG_TAG, "Cannot read main preferences from file") | ||||
e.printStackTrace() | e.printStackTrace() | ||||
} | } | ||||
} | } | ||||
} | } | ||||
//load new entry | //load new entry | ||||
entry = zipstream.nextEntry | entry = zipstream.nextEntry | ||||
Show All 39 Lines | companion object { | ||||
const val DEBUG_TAG ="BusTO-TestSave" | const val DEBUG_TAG ="BusTO-TestSave" | ||||
const val FAVORITES_NAME = "favorites.csv" | const val FAVORITES_NAME = "favorites.csv" | ||||
const val MAIN_PREF_NAME = "preferences_main.json" | const val MAIN_PREF_NAME = "preferences_main.json" | ||||
const val APP_PREF_NAME = "preferences_app.json" | const val APP_PREF_NAME = "preferences_app.json" | ||||
@JvmStatic | @JvmStatic | ||||
fun newInstance() = | fun newInstance() = | ||||
TestSavingFragment().apply { | BackupImportFragment().apply { | ||||
arguments = Bundle() /*.apply { | arguments = Bundle() /*.apply { | ||||
putString(ARG_PARAM1, param1) | putString(ARG_PARAM1, param1) | ||||
putString(ARG_PARAM2, param2) | putString(ARG_PARAM2, param2) | ||||
} | } | ||||
*/ | */ | ||||
} | } | ||||
fun getCurrentDateString(): String{ | fun getCurrentDateString(): String{ | ||||
val dateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.ITALY) | val dateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.ITALY) | ||||
val date = Date() | val date = Date() | ||||
return dateFormat.format(date) | return dateFormat.format(date) | ||||
} | } | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register