From 28b84092073810fdbcc01e8293c9b92f22d9ea3e Mon Sep 17 00:00:00 2001 From: sathwik Date: Tue, 1 Jul 2025 16:27:55 +0530 Subject: [PATCH] implementing the secure storage using ndk --- .../hpostesting/HPOSTestingApplication.kt | 5 +- .../hpostesting/firebase/FirebaseManage.kt | 140 +++++++++++------- 2 files changed, 88 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/example/hpostesting/HPOSTestingApplication.kt b/app/src/main/java/com/example/hpostesting/HPOSTestingApplication.kt index fe27691..fe6b09d 100644 --- a/app/src/main/java/com/example/hpostesting/HPOSTestingApplication.kt +++ b/app/src/main/java/com/example/hpostesting/HPOSTestingApplication.kt @@ -15,7 +15,6 @@ package com.example.hpostesting import android.app.Application import com.example.hpostesting.firebase.FirebaseManager -import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestoreSettings import dagger.hilt.android.HiltAndroidApp import kotlinx.coroutines.CoroutineScope @@ -28,10 +27,10 @@ import javax.inject.Inject class HPOSTestingApplication : Application() { companion object { init { - System.loadLibrary("native-lib") // or whatever you named the lib in CMakeLists + System.loadLibrary("native-server") } } - external fun startNativeServer(): String + @Inject lateinit var firebaseManager: FirebaseManager diff --git a/app/src/main/java/com/example/hpostesting/firebase/FirebaseManage.kt b/app/src/main/java/com/example/hpostesting/firebase/FirebaseManage.kt index 139b101..8999a98 100644 --- a/app/src/main/java/com/example/hpostesting/firebase/FirebaseManage.kt +++ b/app/src/main/java/com/example/hpostesting/firebase/FirebaseManage.kt @@ -14,6 +14,7 @@ package com.example.hpostesting.firebase import android.content.Context import android.content.Intent import android.os.Process +import android.util.Log import androidx.core.content.edit import com.example.hpostesting.data.constant.Constants import com.google.firebase.FirebaseApp @@ -28,7 +29,15 @@ import kotlinx.coroutines.launch import javax.inject.Inject class FirebaseManager @Inject constructor(private val context: Context) { + private var defaultServers = mutableListOf() + external fun getFirebaseDataMap(): Map> + private val map: Map> = getFirebaseDataMap() + companion object { + init { + System.loadLibrary("native-server") //use NDK ro secure somethings + } + } private val sharedPreferencesServerConfig = context.getSharedPreferences("FirebaseConfigPrefs", 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 updateStatus = false - private val defaultServers = listOf( - // i have it in different place look down - firebaseConfigProd, firebaseConfigDev , firebaseConfigQaQc, firebaseConfigPreProd, firebaseConfigSmiRandD, - configMolbio //not a firebase config - ) + + private fun Map.toFirebaseConfig(server: String): FirebaseConfig? { + val apiKey = this["apiKey"] ?: return null + 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 { + populate() val lastSelectedServer = getLastSelectedServer() switchServer(lastSelectedServer) } @@ -158,52 +191,51 @@ fun delayedRestart(context: Context) { } } - - -private val firebaseConfigDev = FirebaseConfig( - serverName = "dev", - apiKey = "AIzaSyAVdNGCev_AFX0qmuZJF6kxzRzXUTeAW5I", - appId = "1:650071678820:android:a53292637abb7c0d6c6471", - projectId = "hpos-af3cc", - storageBucket = "hpos-af3cc.appspot.com" -) - -private val firebaseConfigQaQc = FirebaseConfig( - serverName = "qc-qa", - apiKey = "AIzaSyC4d4GhWHr_NMJAeBvnztQ_yQ3Qe9MAnLs", - appId = "1:1004619739289:android:3dbcefb10ea99654e5c808", - projectId = "hpos-qa", - storageBucket = "hpos-qa.appspot.com" -) - -private val firebaseConfigProd = FirebaseConfig( - serverName = "prod", - apiKey = "AIzaSyA_O0oZGzy3Kiw9fiLQ3OFPME-xQJP88vs", - appId = "1:1012719870714:android:d8d7c962d96f4097de5f43", - projectId = "hpos-prod", - storageBucket = "hpos-prod.appspot.com" -) - -private val firebaseConfigPreProd = FirebaseConfig( - serverName = "pre prod", - apiKey = "AIzaSyDYySi27LioZGNisP1NfnNU5inJX_0FT38", - appId = "1:121176529204:android:e6841fdac57bdc95bbed61", - projectId = "hpos-preprod", - storageBucket = "hpos-preprod.appspot.com" -) - -private val firebaseConfigSmiRandD= FirebaseConfig( - serverName = "SMI R&D", - apiKey = "AIzaSyAVxpilbB804iRCCpMBDjjmdZhQguctGHM", - appId = "1:327771387914:android:a3b4ed2e42ea6e38dc87f3", - projectId = "hposrandd", - storageBucket = "hposrandd.firebasestorage.app" -) - -private val configMolbio = FirebaseConfig( - serverName = "molbio", - apiKey = "molbio", - appId = "molbio", - projectId = "molbio", - storageBucket = "molbio" -) \ No newline at end of file +// +// +//private val firebaseConfigDev = FirebaseConfig( +// serverName = "dev", +// apiKey = "AIzaSyAVdNGCev_AFX0qmuZJF6kxzRzXUTeAW5I", +// appId = "1:650071678820:android:a53292637abb7c0d6c6471", +// projectId = "hpos-af3cc", +// storageBucket = "hpos-af3cc.appspot.com" +//) +//private val firebaseConfigQaQc = FirebaseConfig( +// serverName = "qc-qa", +// apiKey = "AIzaSyC4d4GhWHr_NMJAeBvnztQ_yQ3Qe9MAnLs", +// appId = "1:1004619739289:android:3dbcefb10ea99654e5c808", +// projectId = "hpos-qa", +// storageBucket = "hpos-qa.appspot.com" +//) +// +//private val firebaseConfigProd = FirebaseConfig( +// serverName = "prod", +// apiKey = "AIzaSyA_O0oZGzy3Kiw9fiLQ3OFPME-xQJP88vs", +// appId = "1:1012719870714:android:d8d7c962d96f4097de5f43", +// projectId = "hpos-prod", +// storageBucket = "hpos-prod.appspot.com" +//) +// +//private val firebaseConfigPreProd = FirebaseConfig( +// serverName = "pre prod", +// apiKey = "AIzaSyDYySi27LioZGNisP1NfnNU5inJX_0FT38", +// appId = "1:121176529204:android:e6841fdac57bdc95bbed61", +// projectId = "hpos-preprod", +// storageBucket = "hpos-preprod.appspot.com" +//) +// +//private val firebaseConfigSmiRandD= FirebaseConfig( +// serverName = "SMI R&D", +// apiKey = "AIzaSyAVxpilbB804iRCCpMBDjjmdZhQguctGHM", +// appId = "1:327771387914:android:a3b4ed2e42ea6e38dc87f3", +// projectId = "hposrandd", +// storageBucket = "hposrandd.firebasestorage.app" +//) +// +//private val configMolbio = FirebaseConfig( +// serverName = "molbio", +// apiKey = "molbio", +// appId = "molbio", +// projectId = "molbio", +// storageBucket = "molbio" +//) \ No newline at end of file