Skip to content

Commit

Permalink
clean and restart
Browse files Browse the repository at this point in the history
Signed-off-by: phuoc <[email protected]>
  • Loading branch information
phuocbitmark committed Oct 14, 2024
1 parent 2b81f83 commit 3a6a98f
Showing 1 changed file with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.bitmark.libauk.storage

import android.app.ActivityManager
import android.os.Process
import android.content.Context
import android.content.Intent
import androidx.security.crypto.EncryptedFile
import androidx.security.crypto.MasterKey
import io.reactivex.Completable
Expand All @@ -10,6 +12,7 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.util.*
import android.util.Log
import android.widget.Toast
import java.security.KeyStore

internal interface SecureFileStorage {
Expand All @@ -21,8 +24,6 @@ internal interface SecureFileStorage {
fun isExistingOnFilesDir(name: String): Boolean

fun deleteOnFilesDir(name: String): Boolean

fun cleanKeyStoreAlias()
}

internal class SecureFileStorageImpl(
Expand Down Expand Up @@ -89,22 +90,40 @@ internal class SecureFileStorageImpl(
override fun deleteOnFilesDir(name: String): Boolean =
delete(File(context.filesDir, getFileName(name)).absolutePath)

override fun cleanKeyStoreAlias() {
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
am.clearApplicationUserData()
// delay 1s to make sure the data is cleared
Thread.sleep(1000)
KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
private fun cleanKeyStoreAlias() {
clearAppDataAndRestart(context)
/*
val keyStore = KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
val alias = keyStore.aliases().toList();
alias.forEach {
Log.d("alias", "alias: $it")
keyStore.deleteEntry(it)
}
val keyStore = KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
val alias = keyStore.aliases().toList()
alias.forEach {
Log.d("alias", "alias: $it")
keyStore.deleteEntry(it)
}
*/
}

private fun clearAppDataAndRestart(context: Context) {
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

val success = am.clearApplicationUserData()

if (success) {
// Show a Toast message before the app restarts
Toast.makeText(context, "App data cleared. Restarting...", Toast.LENGTH_SHORT).show()
val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
intent?.let {
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(it)
}
// Kill the process to simulate a clean restart
Process.killProcess(Process.myPid())
} else {
// Handle failure (optional)
Toast.makeText(context, "Failed to clear app data", Toast.LENGTH_SHORT).show()
}
}

private fun getEncryptedFile(path: String, read: Boolean) = File(path).let { f ->
if (f.isDirectory) throw IllegalArgumentException("do not support directory")
if (read && !f.exists() && !f.createNewFile()) {
Expand Down

0 comments on commit 3a6a98f

Please sign in to comment.