Skip to content

Commit

Permalink
Add shortcut for demo data in debug config
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Feb 7, 2023
1 parent e650dd1 commit 8f6e38a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ internal object CryptoManager {
}

@Throws(EnDecryptionException::class)
fun appendString(file: File, newString: String) {
fun appendStrings(file: File, vararg newStrings: String) {
@Suppress("UNCHECKED_CAST")
var data = ObjectSerializer.deserialize(decrypt(file)) as Set<String>
data = data.plus(newString)
newStrings.forEach { data = data.plus(it) }
encrypt(ObjectSerializer.serialize(data), file)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.cyb3rko.pincredible.fragments

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
Expand All @@ -41,6 +42,7 @@ import com.cyb3rko.pincredible.crypto.CryptoManager.EnDecryptionException
import com.cyb3rko.pincredible.databinding.FragmentHomeBinding
import com.cyb3rko.pincredible.modals.ErrorDialog
import com.cyb3rko.pincredible.recycler.PinAdapter
import com.cyb3rko.pincredible.utils.DebugUtils
import com.cyb3rko.pincredible.utils.ObjectSerializer
import com.cyb3rko.pincredible.utils.Vibration
import com.cyb3rko.pincredible.utils.openUrl
Expand Down Expand Up @@ -103,6 +105,14 @@ class HomeFragment : Fragment() {
hideSubtitle()
findNavController().navigate(HomeFragmentDirections.homeToPincreator())
}
if (BuildConfig.DEBUG) {
binding.fab.setOnLongClickListener {
DebugUtils.demoData(myContext)
requireActivity().finish()
startActivity(Intent(myContext, MainActivity::class.java))
true
}
}
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class PinCreatorFragment : Fragment() {
pinsFile
)
} else {
CryptoManager.appendString(pinsFile, name)
CryptoManager.appendStrings(pinsFile, name)
}
}

Expand Down
57 changes: 57 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/utils/DebugUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 Cyb3rKo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyb3rko.pincredible.utils

import android.content.Context
import com.cyb3rko.pincredible.crypto.CryptoManager
import com.cyb3rko.pincredible.data.PinTable
import java.io.File
import kotlin.random.Random

internal object DebugUtils {
fun demoData(context: Context) {
val names = setOf(
"American Express",
"Backup Number Alarm System",
"Girlfriend's Banking Card",
"SIM Card"
)
names.forEach {
val newPinFile = File(context.filesDir, "p${CryptoManager.hash(it)}")
if (!newPinFile.exists()) {
newPinFile.createNewFile()
val pinTable = PinTable().apply { fill() }
repeat(PinTable.ROW_COUNT) { row ->
repeat(PinTable.COLUMN_COUNT) { column ->
pinTable.putBackground(row, column, Random.nextInt(5))
}
}
val bytes = ObjectSerializer.serialize(pinTable)
val version = CryptoManager.PIN_CRYPTO_ITERATION.toByte()
CryptoManager.encrypt(bytes.plus(version), newPinFile)
}
}

val pinsFile = File(context.filesDir, "pins")
if (!pinsFile.exists()) {
pinsFile.createNewFile()
CryptoManager.encrypt(ObjectSerializer.serialize(names), pinsFile)
} else {
CryptoManager.appendStrings(pinsFile, *names.toTypedArray())
}
}
}

0 comments on commit 8f6e38a

Please sign in to comment.