Skip to content

Commit

Permalink
🎨 Move checkifSQL to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jan 31, 2020
1 parent 4f9d7b4 commit c235e14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
33 changes: 29 additions & 4 deletions app/src/main/java/com/kyhsgeekcode/dereinfo/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,47 @@ import android.content.Context
import android.content.Intent
import java.io.*

fun launchActivity(context: Context, target:Class<out Activity>){
val intent = Intent(context,target)
fun launchActivity(context: Context, target: Class<out Activity>) {
val intent = Intent(context, target)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
context.startActivity(intent)
}

fun saveObject(file: File, target:Any) {
fun saveObject(file: File, target: Any) {
val outputStream = ObjectOutputStream(FileOutputStream(file))
outputStream.writeObject(target)
outputStream.flush()
outputStream.close()
}

fun loadObject(file: File) : Any {
fun loadObject(file: File): Any {
val inputStream = ObjectInputStream(FileInputStream(file))
val result = inputStream.readObject()
inputStream.close()
return result
}

val sqliteHeader = byteArrayOf(
'S'.toByte(),
'Q'.toByte(),
'L'.toByte(),
'i'.toByte(),
't'.toByte(),
'e'.toByte(),
' '.toByte(),
'f'.toByte(),
'o'.toByte(),
'r'.toByte(),
'm'.toByte(),
'a'.toByte(),
't'.toByte(),
' '.toByte(),
'3'.toByte(),
0
)

fun checkIfDatabase(file: File): Boolean {
val byteArray = ByteArray(16)
file.inputStream().read(byteArray, 0, 16)
return byteArray contentEquals sqliteHeader
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase
import android.util.Log
import android.util.SparseIntArray
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import com.kyhsgeekcode.dereinfo.checkIfDatabase
import com.kyhsgeekcode.dereinfo.loadObject
import com.kyhsgeekcode.dereinfo.model.CircleType.getColor
import com.kyhsgeekcode.dereinfo.saveObject
Expand Down Expand Up @@ -181,30 +182,6 @@ class DereDatabaseHelper(context: Context) {
}
}

val sqliteHeader = byteArrayOf(
'S'.toByte(),
'Q'.toByte(),
'L'.toByte(),
'i'.toByte(),
't'.toByte(),
'e'.toByte(),
' '.toByte(),
'f'.toByte(),
'o'.toByte(),
'r'.toByte(),
'm'.toByte(),
'a'.toByte(),
't'.toByte(),
' '.toByte(),
'3'.toByte(),
0
)

private fun checkIfDatabase(file: File): Boolean {
val byteArray = ByteArray(16)
file.inputStream().read(byteArray, 0, 16)
return byteArray contentEquals sqliteHeader
}

val mainDBFileCachename = "maindb.dat"
val mainDBFileCacheFile = File(context.filesDir, mainDBFileCachename)
Expand Down Expand Up @@ -265,9 +242,9 @@ class DereDatabaseHelper(context: Context) {
//publisher(100,0,null)
parseDatabases(publisher)
indexFumens(publisher)
publisher(100,50,null)
publisher(100, 50, null)
saveToCache(context)
publisher(100,100,null)
publisher(100, 100, null)
}
Log.d(TAG, "size of databases:${musicIDToInfo.size}")
Log.d(TAG, "Number of fumens:${musicNumberToFumenFile.size}")
Expand Down Expand Up @@ -304,7 +281,7 @@ class DereDatabaseHelper(context: Context) {
var name = cursorFumens.getString(0)
if (!name[name.length - 5].isDigit())
continue
Log.d(TAG,"name:${name}")
Log.d(TAG, "name:${name}")
name = name.substring(13)
name = name.substringBefore('.')
// Log.d(TAG,"name:${name}")
Expand Down Expand Up @@ -333,7 +310,7 @@ class DereDatabaseHelper(context: Context) {
name = name.substringBefore('.')
val difficulty = Integer.parseInt(name.substringAfter('_'))
val twDifficulty = TW5Difficulty.valueOf(difficulty)
if(wantedDifficulty!=twDifficulty)
if (wantedDifficulty != twDifficulty)
continue
val fumenStr = cursorFumens.getBlob(1).toString()
val notes = parseDereFumen(fumenStr, info)
Expand Down

0 comments on commit c235e14

Please sign in to comment.