Skip to content

Commit

Permalink
🐛 Fixed #3 by adding condition for live_data
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jan 31, 2020
1 parent c235e14 commit 5c51685
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.kyhsgeekcode.dereinfo

import android.util.SparseArray
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

typealias SerializableSparseIntArray = SerializableSparseArray<Int>

class SerializableSparseArray<E> : SparseArray<E>, java.io.Serializable {

constructor() : super()
constructor(capacity: Int) : super(capacity)

/**
* This method is private but it is called using reflection by java
* serialization mechanism. It overwrites the default object serialization.
*
* <br></br><br></br>**IMPORTANT**
* The access modifier for this method MUST be set to **private** otherwise [java.io.StreamCorruptedException]
* will be thrown.
*
* @param oos
* the stream the data is stored into
* @throws IOException
* an exception that might occur during data storing
*/
@Throws(IOException::class)
private fun writeObject(oos: ObjectOutputStream) {
val data = arrayOfNulls<Any>(size())
for (i in data.indices) {
val pair = Pair<Int,E>(keyAt(i), valueAt(i))
data[i] = pair
}
oos.writeObject(data)
}

/**
* This method is private but it is called using reflection by java
* serialization mechanism. It overwrites the default object serialization.
*
* <br></br><br></br>**IMPORTANT**
* The access modifier for this method MUST be set to **private** otherwise [java.io.StreamCorruptedException]
* will be thrown.
*
* @param oos
* the stream the data is read from
* @throws IOException
* an exception that might occur during data reading
* @throws ClassNotFoundException
* this exception will be raised when a class is read that is
* not known to the current ClassLoader
*/
@Throws(IOException::class, ClassNotFoundException::class)
private fun readObject(ois: ObjectInputStream) {
val data = ois.readObject() as Array<Any>
for (i in data.indices) {
val pair = data[i] as Pair<Int,E>
this.append(pair.first, pair.second)
}
return
}

companion object {
private const val serialVersionUID = 824056059663678000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class SongDetailFragment : Fragment() {
activity?.toolbar_layout?.title = item?.name?.replace("\\n", " ")
activity?.toolbar_layout?.setBackgroundColor(item?.getColor() ?: 0xFFDDDDDD.toInt())
val musicNumber = DereDatabaseHelper.theInstance.musicIDTomusicNumber[item!!.id]
oneMusic = DereDatabaseHelper.theInstance.peekFumens(musicNumber)
Log.w(TAG, "Item.id:${item!!.id}, musicNumber:${musicNumber}")
oneMusic = DereDatabaseHelper.theInstance.peekFumens(musicNumber!!)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/kyhsgeekcode/dereinfo/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ val sqliteHeader = byteArrayOf(
)

fun checkIfDatabase(file: File): Boolean {
if(file==null)
return false
if(file.isDirectory)
return false
val byteArray = ByteArray(16)
file.inputStream().read(byteArray, 0, 16)
val fi = file.inputStream()
fi.read(byteArray, 0, 16)
fi.close()
return byteArray contentEquals sqliteHeader
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.database.Cursor
import android.database.SQLException
import android.database.sqlite.SQLiteDatabase
import android.util.Log
import android.util.SparseIntArray
import androidx.core.util.set
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import com.kyhsgeekcode.dereinfo.SerializableSparseIntArray
import com.kyhsgeekcode.dereinfo.checkIfDatabase
import com.kyhsgeekcode.dereinfo.loadObject
import com.kyhsgeekcode.dereinfo.model.CircleType.getColor
Expand All @@ -26,8 +27,8 @@ class DereDatabaseHelper(context: Context) {
val fumenFolder: File

var musicIDToInfo: MutableMap<Int, MusicInfo> = HashMap()
val musicNumberToMusicID: SparseIntArray = SparseIntArray()
val musicIDTomusicNumber = SparseIntArray()
var musicNumberToMusicID = HashMap<Int,Int>() //SerializableSparseIntArray = SerializableSparseIntArray()
var musicIDTomusicNumber = HashMap<Int,Int>() // = SerializableSparseIntArray()

init {
val datadir = context.getExternalFilesDir(null).parentFile.parentFile
Expand Down Expand Up @@ -71,8 +72,8 @@ class DereDatabaseHelper(context: Context) {
fumensDB.query(
"live_data",
arrayOf("id", "music_data_id", "circle_type"),
null,
null,
"end_date=?",
arrayOf(""),
null,
null,
null
Expand Down Expand Up @@ -127,6 +128,7 @@ class DereDatabaseHelper(context: Context) {
val circleType = cursorLiveData.getInt(circleTypeIndex)
musicNumberToMusicID[liveDataId] = musicDataId
musicIDTomusicNumber[musicDataId] = liveDataId
//Log.w(TAG, "musicIDToMusicNumber[${musicDataId}]=${liveDataId}")
val musicInfo = MusicInfo(
musicDataId,
name,
Expand Down Expand Up @@ -168,18 +170,28 @@ class DereDatabaseHelper(context: Context) {
name = name.substring(13)
name = name.substringBefore('.')
val musicIndex = Integer.parseInt(name.split('/')[0])
//Log.d(TAG, "musicIndex ${musicIndex}")
// Log.d(TAG, "musicIndex ${musicIndex}, ${file.name}")
// val difficulty = Integer.parseInt(name.substringAfter('_'))
musicNumberToFumenFile[musicIndex] = file
break
}
fumenDB.close()
} catch (e: SQLException) {
Log.e(TAG, "indexFumen",e)
continue
} finally {
cursorFumens?.close()
}
}

//check validity(debug)
// for(item in musicIDToInfo.values) {
// val musicNumber = musicIDTomusicNumber[item.id]
// //Log.w(TAG, "Item.id:${item.id}, musicNumber:${musicNumber}")
// val file = musicNumberToFumenFile[musicNumber]
// if(file != null)
// Log.w(TAG,"id:${item.id},name:${item.name},file:${file?.name}")
// }
}


Expand All @@ -189,13 +201,20 @@ class DereDatabaseHelper(context: Context) {
val indexToFumenFileFilename = "indexToFumenFile.dat"
val musicInfoFile = File(context.filesDir, musicInfoFilename)
val indexToFumenFileFile = File(context.filesDir, indexToFumenFileFilename)
val musicNumberToMusicIDFileName = "musicNumberToMusicID.dat"
val musicNumberToMusicIDFile = File(context.filesDir, musicNumberToMusicIDFileName)
val musicIDToMusicNumberFileName = "musicIDToMusicNumber.dat"
val musicIDToMusicNumberFile = File(context.filesDir, musicIDToMusicNumberFileName)


private fun saveToCache(context: Context) {
// searchMainDB()
// parseDatabases()
saveObject(mainDBFileCacheFile, fumensDBFile)
saveObject(musicInfoFile, musicIDToInfo)
saveObject(indexToFumenFileFile, musicNumberToFumenFile)
saveObject(musicNumberToMusicIDFile, musicNumberToMusicID)
saveObject(musicIDToMusicNumberFile, musicIDTomusicNumber)
}

private fun loadFromCache(context: Context): Boolean {
Expand All @@ -207,6 +226,7 @@ class DereDatabaseHelper(context: Context) {
return false
return true
} catch (e: Exception) {
Log.e(TAG, "error loading", e)
return false
}
}
Expand All @@ -215,6 +235,8 @@ class DereDatabaseHelper(context: Context) {
loadFumenDBFileFromCache()
musicIDToInfo = loadObject(musicInfoFile) as MutableMap<Int, MusicInfo>
musicNumberToFumenFile = loadObject(indexToFumenFileFile) as MutableMap<Int, File>
musicNumberToMusicID = loadObject(musicNumberToMusicIDFile) as HashMap<Int,Int> //as SerializableSparseIntArray
musicIDTomusicNumber = loadObject(musicIDToMusicNumberFile) as HashMap<Int, Int> //SerializableSparseIntArray
}

private fun loadFumenDBFileFromCache() {
Expand Down Expand Up @@ -260,9 +282,14 @@ class DereDatabaseHelper(context: Context) {
//5개를 파싱해라.

fun peekFumens(musicNumber: Int): OneMusic {
// Log.d(TAG, "musicIndex : ${musicNumber},indexToFumenFile size:${musicNumberToFumenFile.size}")
Log.d(
TAG,
"musicIndex : ${musicNumber},indexToFumenFile size:${musicNumberToFumenFile.size}"
)
val fumenFile = musicNumberToFumenFile[musicNumber]
Log.d(TAG, "fumenFile:${fumenFile?.name}")
if(fumenFile==null)
throw java.lang.RuntimeException()
val fumenDB =
SQLiteDatabase.openDatabase(fumenFile!!.path, null, SQLiteDatabase.OPEN_READONLY)
val cursorFumens =
Expand All @@ -281,12 +308,12 @@ 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}")
val difficulty = Integer.parseInt(name.substringAfter('_'))
Log.d(TAG, "difficulty:${difficulty}")
//Log.d(TAG, "difficulty:${difficulty}")
val twDifficulty = TW5Difficulty.valueOf(difficulty)
difficulties[twDifficulty] = OneDifficulty(twDifficulty, null)
}
Expand Down

0 comments on commit 5c51685

Please sign in to comment.