From 3963bc22cf2c856a1e79c0823ca4a320797e143e Mon Sep 17 00:00:00 2001 From: vahid torkaman Date: Wed, 26 Jul 2023 15:45:34 +0200 Subject: [PATCH] move the client secret to the local.properties file --- ConfidenceDemoApp/build.gradle.kts | 14 +++++++++++++ .../src/main/AndroidManifest.xml | 3 --- .../confidencedemoapp/ClientSecretProvider.kt | 5 +++++ .../com/example/confidencedemoapp/MainVm.kt | 21 +++++++++---------- .../src/main/res/values/strings.xml | 1 - 5 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/ClientSecretProvider.kt diff --git a/ConfidenceDemoApp/build.gradle.kts b/ConfidenceDemoApp/build.gradle.kts index 5a486d25..708dcedd 100644 --- a/ConfidenceDemoApp/build.gradle.kts +++ b/ConfidenceDemoApp/build.gradle.kts @@ -1,3 +1,6 @@ +import java.io.File +import java.util.Properties + plugins { id("com.android.application") id("org.jetbrains.kotlin.android") @@ -20,11 +23,22 @@ object Versions { const val uiTestManifest = "1.2.0" } +val localPropertiesFile = File(rootProject.projectDir, "local.properties") +val localProperties = Properties() + +// Load the properties from local.properties file +if (localPropertiesFile.exists()) { + localProperties.load(localPropertiesFile.inputStream()) +} + +val clientSecret: String = localProperties.getProperty("CLIENT_SECRET")?: "CLIENT_SECRET" + android { namespace = "com.example.confidencedemoapp" compileSdk = 33 defaultConfig { + buildConfigField("String","CLIENT_SECRET", "\"$clientSecret\"") applicationId = "com.example.confidencedemoapp" minSdk = 21 targetSdk = 33 diff --git a/ConfidenceDemoApp/src/main/AndroidManifest.xml b/ConfidenceDemoApp/src/main/AndroidManifest.xml index 437531db..faeba390 100644 --- a/ConfidenceDemoApp/src/main/AndroidManifest.xml +++ b/ConfidenceDemoApp/src/main/AndroidManifest.xml @@ -21,9 +21,6 @@ - \ No newline at end of file diff --git a/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/ClientSecretProvider.kt b/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/ClientSecretProvider.kt new file mode 100644 index 00000000..fcb50d75 --- /dev/null +++ b/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/ClientSecretProvider.kt @@ -0,0 +1,5 @@ +package com.example.confidencedemoapp + +object ClientSecretProvider { + fun clientSecret(): String = BuildConfig.CLIENT_SECRET +} \ No newline at end of file diff --git a/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/MainVm.kt b/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/MainVm.kt index c2862db3..993e6834 100644 --- a/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/MainVm.kt +++ b/ConfidenceDemoApp/src/main/java/com/example/confidencedemoapp/MainVm.kt @@ -1,7 +1,6 @@ package com.example.confidencedemoapp import android.app.Application -import android.content.pm.PackageManager import android.util.Log import androidx.compose.ui.graphics.Color import androidx.lifecycle.AndroidViewModel @@ -9,9 +8,14 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import dev.openfeature.contrib.providers.ConfidenceFeatureProvider -import dev.openfeature.sdk.* +import dev.openfeature.sdk.Client +import dev.openfeature.sdk.EvaluationContext +import dev.openfeature.sdk.FlagEvaluationDetails +import dev.openfeature.sdk.ImmutableContext +import dev.openfeature.sdk.OpenFeatureAPI +import dev.openfeature.sdk.Value import kotlinx.coroutines.launch -import java.util.* +import java.util.UUID class MainVm(app: Application) : AndroidViewModel(app) { @@ -28,12 +32,7 @@ class MainVm(app: Application) : AndroidViewModel(app) { init { val start = System.currentTimeMillis() - val applicationContext = app.applicationContext - val metadataBundle = applicationContext.packageManager.getApplicationInfo( - applicationContext.packageName, - PackageManager.GET_META_DATA - ).metaData - val clientSecret = metadataBundle.getString("com.example.confidencedemoapp.clientSecret")!! + val clientSecret = ClientSecretProvider.clientSecret() viewModelScope.launch { OpenFeatureAPI.setProvider( ConfidenceFeatureProvider.create( @@ -52,10 +51,10 @@ class MainVm(app: Application) : AndroidViewModel(app) { fun refreshUi() { Log.d(TAG, "refreshing UI") - val flagMessageKey = "myFlag.message" + val flagMessageKey = "hawkflag.message" val flagMessageDefault = "default" val messageValue = client.getStringValue(flagMessageKey, flagMessageDefault) - val flagColorKey = "myFlag.color" + val flagColorKey = "hawkflag.color" val flagColorDefault = "Gray" val colorFlag = client.getStringDetails(flagColorKey, flagColorDefault).apply { Log.d(TAG, "reason=$reason") diff --git a/ConfidenceDemoApp/src/main/res/values/strings.xml b/ConfidenceDemoApp/src/main/res/values/strings.xml index 357d1588..132417da 100644 --- a/ConfidenceDemoApp/src/main/res/values/strings.xml +++ b/ConfidenceDemoApp/src/main/res/values/strings.xml @@ -1,4 +1,3 @@ ConfidenceDemoApp - Some client secret \ No newline at end of file