implementing the secure storage using ndk

This commit is contained in:
sathwik
2025-07-01 16:27:55 +05:30
parent 5c56ef7630
commit 28b8409207
2 changed files with 88 additions and 57 deletions

View File

@@ -15,7 +15,6 @@ package com.example.hpostesting
import android.app.Application import android.app.Application
import com.example.hpostesting.firebase.FirebaseManager import com.example.hpostesting.firebase.FirebaseManager
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings import com.google.firebase.firestore.FirebaseFirestoreSettings
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -28,10 +27,10 @@ import javax.inject.Inject
class HPOSTestingApplication : Application() { class HPOSTestingApplication : Application() {
companion object { companion object {
init { init {
System.loadLibrary("native-lib") // or whatever you named the lib in CMakeLists System.loadLibrary("native-server")
} }
} }
external fun startNativeServer(): String
@Inject @Inject
lateinit var firebaseManager: FirebaseManager lateinit var firebaseManager: FirebaseManager

View File

@@ -14,6 +14,7 @@ package com.example.hpostesting.firebase
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Process import android.os.Process
import android.util.Log
import androidx.core.content.edit import androidx.core.content.edit
import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.Constants
import com.google.firebase.FirebaseApp import com.google.firebase.FirebaseApp
@@ -28,7 +29,15 @@ import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
class FirebaseManager @Inject constructor(private val context: Context) { class FirebaseManager @Inject constructor(private val context: Context) {
private var defaultServers = mutableListOf<FirebaseConfig>()
external fun getFirebaseDataMap(): Map<String, Map<String, String>>
private val map: Map<String, Map<String, String>> = getFirebaseDataMap()
companion object {
init {
System.loadLibrary("native-server") //use NDK ro secure somethings
}
}
private val sharedPreferencesServerConfig = context.getSharedPreferences("FirebaseConfigPrefs", Context.MODE_PRIVATE) private val sharedPreferencesServerConfig = context.getSharedPreferences("FirebaseConfigPrefs", Context.MODE_PRIVATE)
private val sharedPreferencesDev = context.getSharedPreferences("DevMode",Context.MODE_PRIVATE) private val sharedPreferencesDev = context.getSharedPreferences("DevMode",Context.MODE_PRIVATE)
@@ -36,13 +45,37 @@ class FirebaseManager @Inject constructor(private val context: Context) {
private var currentStorage: FirebaseStorage = FirebaseStorage.getInstance() private var currentStorage: FirebaseStorage = FirebaseStorage.getInstance()
private var updateStatus = false private var updateStatus = false
private val defaultServers = listOf(
// i have it in different place look down private fun Map<String, String>.toFirebaseConfig(server: String): FirebaseConfig? {
firebaseConfigProd, firebaseConfigDev , firebaseConfigQaQc, firebaseConfigPreProd, firebaseConfigSmiRandD, val apiKey = this["apiKey"] ?: return null
configMolbio //not a firebase config val appId = this["appId"] ?: return null
) val projectId = this["projectId"] ?: return null
val storageBucket = this["storageBucket"] ?: return null
return FirebaseConfig(
serverName = server,
projectId = projectId,
appId = appId,
apiKey = apiKey,
storageBucket = storageBucket
)
}
fun populate() {
defaultServers.clear()
map.forEach { (server, config) ->
config.toFirebaseConfig(server)?.let {
defaultServers.add(it)
} ?: Log.w("Firebase", "Skipping $server due to missing keys")
}
}
init { init {
populate()
val lastSelectedServer = getLastSelectedServer() val lastSelectedServer = getLastSelectedServer()
switchServer(lastSelectedServer) switchServer(lastSelectedServer)
} }
@@ -158,52 +191,51 @@ fun delayedRestart(context: Context) {
} }
} }
//
//
private val firebaseConfigDev = FirebaseConfig( //private val firebaseConfigDev = FirebaseConfig(
serverName = "dev", // serverName = "dev",
apiKey = "AIzaSyAVdNGCev_AFX0qmuZJF6kxzRzXUTeAW5I", // apiKey = "AIzaSyAVdNGCev_AFX0qmuZJF6kxzRzXUTeAW5I",
appId = "1:650071678820:android:a53292637abb7c0d6c6471", // appId = "1:650071678820:android:a53292637abb7c0d6c6471",
projectId = "hpos-af3cc", // projectId = "hpos-af3cc",
storageBucket = "hpos-af3cc.appspot.com" // storageBucket = "hpos-af3cc.appspot.com"
) //)
//private val firebaseConfigQaQc = FirebaseConfig(
private val firebaseConfigQaQc = FirebaseConfig( // serverName = "qc-qa",
serverName = "qc-qa", // apiKey = "AIzaSyC4d4GhWHr_NMJAeBvnztQ_yQ3Qe9MAnLs",
apiKey = "AIzaSyC4d4GhWHr_NMJAeBvnztQ_yQ3Qe9MAnLs", // appId = "1:1004619739289:android:3dbcefb10ea99654e5c808",
appId = "1:1004619739289:android:3dbcefb10ea99654e5c808", // projectId = "hpos-qa",
projectId = "hpos-qa", // storageBucket = "hpos-qa.appspot.com"
storageBucket = "hpos-qa.appspot.com" //)
) //
//private val firebaseConfigProd = FirebaseConfig(
private val firebaseConfigProd = FirebaseConfig( // serverName = "prod",
serverName = "prod", // apiKey = "AIzaSyA_O0oZGzy3Kiw9fiLQ3OFPME-xQJP88vs",
apiKey = "AIzaSyA_O0oZGzy3Kiw9fiLQ3OFPME-xQJP88vs", // appId = "1:1012719870714:android:d8d7c962d96f4097de5f43",
appId = "1:1012719870714:android:d8d7c962d96f4097de5f43", // projectId = "hpos-prod",
projectId = "hpos-prod", // storageBucket = "hpos-prod.appspot.com"
storageBucket = "hpos-prod.appspot.com" //)
) //
//private val firebaseConfigPreProd = FirebaseConfig(
private val firebaseConfigPreProd = FirebaseConfig( // serverName = "pre prod",
serverName = "pre prod", // apiKey = "AIzaSyDYySi27LioZGNisP1NfnNU5inJX_0FT38",
apiKey = "AIzaSyDYySi27LioZGNisP1NfnNU5inJX_0FT38", // appId = "1:121176529204:android:e6841fdac57bdc95bbed61",
appId = "1:121176529204:android:e6841fdac57bdc95bbed61", // projectId = "hpos-preprod",
projectId = "hpos-preprod", // storageBucket = "hpos-preprod.appspot.com"
storageBucket = "hpos-preprod.appspot.com" //)
) //
//private val firebaseConfigSmiRandD= FirebaseConfig(
private val firebaseConfigSmiRandD= FirebaseConfig( // serverName = "SMI R&D",
serverName = "SMI R&D", // apiKey = "AIzaSyAVxpilbB804iRCCpMBDjjmdZhQguctGHM",
apiKey = "AIzaSyAVxpilbB804iRCCpMBDjjmdZhQguctGHM", // appId = "1:327771387914:android:a3b4ed2e42ea6e38dc87f3",
appId = "1:327771387914:android:a3b4ed2e42ea6e38dc87f3", // projectId = "hposrandd",
projectId = "hposrandd", // storageBucket = "hposrandd.firebasestorage.app"
storageBucket = "hposrandd.firebasestorage.app" //)
) //
//private val configMolbio = FirebaseConfig(
private val configMolbio = FirebaseConfig( // serverName = "molbio",
serverName = "molbio", // apiKey = "molbio",
apiKey = "molbio", // appId = "molbio",
appId = "molbio", // projectId = "molbio",
projectId = "molbio", // storageBucket = "molbio"
storageBucket = "molbio" //)
)