From 3cc9a94eb78281013d31209a47fb9ac2b4e3b30c Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 12 Oct 2023 15:00:05 +0300 Subject: [PATCH] Add default settings functionality into the app --- .github/workflows/blank.yml | 2 +- app/build.gradle.kts | 5 ++ .../fpjs_pro_demo/base/BaseActivity.kt | 6 -- .../dialogs/ClientSettingsDialog.kt | 86 ------------------- .../input_screen/InputPresenter.kt | 20 ++++- .../fpjs_pro_demo/input_screen/InputView.kt | 45 ++++++++++ .../persistence/ApplicationPreferences.kt | 11 +++ .../results_screen/ResultsPresenter.kt | 13 ++- app/src/main/res/color/checkbox_color.xml | 5 ++ .../main/res/color/edittext_text_colors.xml | 5 ++ .../res/color/edittext_text_hint_colors.xml | 5 ++ app/src/main/res/drawable/bg_run_btn.xml | 2 +- .../main/res/drawable/ic_settings_48px.xml | 10 --- app/src/main/res/layout/activity_input.xml | 28 +++++- app/src/main/res/layout/dialog_settings.xml | 30 ------- app/src/main/res/menu/menu.xml | 5 -- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 2 + 18 files changed, 133 insertions(+), 148 deletions(-) delete mode 100644 app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/dialogs/ClientSettingsDialog.kt create mode 100644 app/src/main/res/color/checkbox_color.xml create mode 100644 app/src/main/res/color/edittext_text_colors.xml create mode 100644 app/src/main/res/color/edittext_text_hint_colors.xml delete mode 100644 app/src/main/res/drawable/ic_settings_48px.xml delete mode 100644 app/src/main/res/layout/dialog_settings.xml diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 31e3359..8b97fd5 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -33,7 +33,7 @@ jobs: run: ./gradlew app:lint - name: Build App - run: ./gradlew app:assembleRelease + run: ./gradlew app:assembleRelease -PdefaultApiKey=${{ secrets.DEFAULT_PUBLIC_API_KEY }} -PdefaultEndpointUrl=https://ap.api.fpjs.io - name: Save application to artifacts uses: actions/upload-artifact@v3 diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 2a0002e..1167a76 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,6 +22,11 @@ android { versionName = VERSION_NAME testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + buildConfigField("String", "DEFAULT_API_KEY", + (project.properties.get("defaultApiKey") as? String)?.let { "\"$it\"" } ?: "null") + buildConfigField("String", "DEFAULT_ENDPOINT_URL", + (project.properties.get("defaultEndpointUrl") as? String)?.let { "\"$it\"" } ?: "null") } signingConfigs { diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/base/BaseActivity.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/base/BaseActivity.kt index 49075f7..84b9252 100644 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/base/BaseActivity.kt +++ b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/base/BaseActivity.kt @@ -12,7 +12,6 @@ import com.fingerprintjs.android.fpjs_pro_demo.BuildConfig import com.fingerprintjs.android.fpjs_pro_demo.R import com.fingerprintjs.android.fpjs_pro_demo.dialogs.IdentificationRequestParams import com.fingerprintjs.android.fpjs_pro_demo.dialogs.IdentificationRequestSettingsDialog -import com.fingerprintjs.android.fpjs_pro_demo.dialogs.ClientSettingsDialog import com.fingerprintjs.android.fpjs_pro_demo.persistence.ApplicationPreferences @@ -57,7 +56,6 @@ abstract class BaseActivity( override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_repository -> openLink(Uri.parse(GITHUB_REPO_URL)) - R.id.menu_settings -> openSettings() } return super.onOptionsItemSelected(item) } @@ -77,10 +75,6 @@ abstract class BaseActivity( IdentificationRequestSettingsDialog(this, identificationRequestParams).show(onSettingsAppliedListener) } - private fun openSettings() { - ClientSettingsDialog(this, preferences).showSettings() - } - private fun saveState(outState: Bundle) { outState.putParcelable(parcelableStateKey, presenter.onSaveState()) } diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/dialogs/ClientSettingsDialog.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/dialogs/ClientSettingsDialog.kt deleted file mode 100644 index 5e132f5..0000000 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/dialogs/ClientSettingsDialog.kt +++ /dev/null @@ -1,86 +0,0 @@ -package com.fingerprintjs.android.fpjs_pro_demo.dialogs - - -import android.annotation.SuppressLint -import android.app.Activity -import android.app.AlertDialog -import android.content.DialogInterface -import android.text.SpannableStringBuilder -import android.view.LayoutInflater -import android.widget.CheckBox -import android.widget.EditText -import androidx.core.content.ContextCompat -import com.fingerprintjs.android.fpjs_pro_demo.R -import com.fingerprintjs.android.fpjs_pro_demo.persistence.ApplicationPreferences - - -class ClientSettingsDialog( - private val activity: Activity, - private val preferences: ApplicationPreferences -) { - - private var dialog: AlertDialog? = null - - private val accentColor = ContextCompat.getColor(activity, R.color.orange) - - @SuppressLint("InflateParams") - fun showSettings() { - val builder = activity.let { - AlertDialog.Builder(it) - } - - val view = LayoutInflater.from(activity).inflate(R.layout.dialog_settings, null) - - val endpointUrlEditText = view.findViewById(R.id.edit_text_endpoint_url) - val publicApiKeyEditText = view.findViewById(R.id.edit_text_public_api_key) - val extendedResultCheckBox = view.findViewById(R.id.checkbox_extended_result) - - endpointUrlEditText.text = SpannableStringBuilder(preferences.getEndpointUrl()) - publicApiKeyEditText.text = SpannableStringBuilder(preferences.getPublicApiKey()) - extendedResultCheckBox.isChecked = preferences.getExtendedResult() - - dialog = builder - .setTitle("Client Settings") - .setPositiveButton( - "Apply" - ) { _, _ -> - applyPreferences( - endpointUrlEditText.text.toString(), - publicApiKeyEditText.text.toString(), - extendedResultCheckBox.isChecked - ) - dismiss() - } - .setNegativeButton( - "Cancel" - ) { _, _ -> - dismiss() - } - .setView(view) - - .create() - - dialog?.show() - dialog?.window?.decorView?.setBackgroundResource(R.drawable.bg_main) - - val negativeButton = dialog?.getButton(DialogInterface.BUTTON_NEGATIVE) - negativeButton?.setTextColor(accentColor) - - val positiveButton = dialog?.getButton(DialogInterface.BUTTON_POSITIVE) - positiveButton?.setTextColor(accentColor) - } - - fun dismiss() { - dialog?.dismiss() - } - - private fun applyPreferences( - endpointUrl: String, - apiToken: String, - extendedResult: Boolean - ) { - preferences.setEndpointUrl(endpointUrl) - preferences.setPublicApiKey(apiToken) - preferences.setExtendedResult(extendedResult) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputPresenter.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputPresenter.kt index 1655c28..e3f614d 100644 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputPresenter.kt +++ b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputPresenter.kt @@ -16,6 +16,8 @@ data class InputScreenState( val selectedRegion: Configuration.Region?, val endpointUrl: String?, val apiKey: String?, + val isDefaultApiKeyUsed: Boolean, + val isExtendedResult: Boolean, val identificationRequestParams: IdentificationRequestParams? ) : Parcelable @@ -28,6 +30,8 @@ class InputPresenter( state?.selectedRegion ?: applicationPreferences.getRegion() private var endpointUrl: String = state?.endpointUrl ?: applicationPreferences.getEndpointUrl() private var apiKey: String = state?.apiKey ?: applicationPreferences.getPublicApiKey() + private var isDefaultApiKeyUsed: Boolean = state?.isDefaultApiKeyUsed ?: applicationPreferences.getIsDefaultApiKeyUsed() + private var isExtendedResult: Boolean = state?.isExtendedResult ?: applicationPreferences.getExtendedResult() private var identificationRequestParams: IdentificationRequestParams? = state?.identificationRequestParams private var view: InputView? = null @@ -41,6 +45,8 @@ class InputPresenter( setRegionText(selectedRegion.name) setEndpointUrl(endpointUrl) setPublicApiKey(apiKey) + setIsDefaultApiKeyUsed(isDefaultApiKeyUsed) + setExtendedResult(isExtendedResult) } } @@ -58,10 +64,12 @@ class InputPresenter( override fun onSaveState(): InputScreenState { return InputScreenState( - selectedRegion, - view?.getEndpointUrl(), - view?.getPublicApiKey(), - identificationRequestParams + selectedRegion = selectedRegion, + endpointUrl = view?.getEndpointUrl(), + apiKey = view?.getPublicApiKey(), + isDefaultApiKeyUsed = isDefaultApiKeyUsed, + isExtendedResult = isExtendedResult, + identificationRequestParams = identificationRequestParams, ) } @@ -71,11 +79,15 @@ class InputPresenter( view?.apply { apiKey = getPublicApiKey() endpointUrl = getEndpointUrl() + isDefaultApiKeyUsed = getIsDefaultApiKeyUsed() + isExtendedResult = getExtendedResult() } applicationPreferences.setPublicApiKey(apiKey) applicationPreferences.setEndpointUrl(endpointUrl) applicationPreferences.setRegion(selectedRegion) + applicationPreferences.setIsDefaultApiKeyUsed(isDefaultApiKeyUsed) + applicationPreferences.setExtendedResult(isExtendedResult) router?.openFingerprintResultScreen(identificationRequestParams) } diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputView.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputView.kt index 058e07d..d0c6fc9 100644 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputView.kt +++ b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/input_screen/InputView.kt @@ -5,9 +5,11 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.CheckBox import android.widget.EditText import android.widget.TextView import com.fingerprintjs.android.fpjs_pro.Configuration +import com.fingerprintjs.android.fpjs_pro_demo.BuildConfig import com.fingerprintjs.android.fpjs_pro_demo.R import com.fingerprintjs.android.fpjs_pro_demo.base.BaseActivity import com.fingerprintjs.android.fpjs_pro_demo.base.BaseView @@ -30,6 +32,12 @@ interface InputView { fun setPublicApiKey(key: String) fun getPublicApiKey(): String + + fun getIsDefaultApiKeyUsed(): Boolean + fun setIsDefaultApiKeyUsed(used: Boolean) + + fun getExtendedResult(): Boolean + fun setExtendedResult(extendedResult: Boolean) } @@ -41,8 +49,22 @@ class InputViewImpl(private val activity: BaseActivity<*>) : BaseView(activity), private val getVisitorIdButton: View = activity.findViewById(R.id.get_visitor_id_button) private val openRequestSettingsDialogBtn: View = activity.findViewById(R.id.request_settings_btn) + private val isDefaultApiKeyUsedCheckbox: CheckBox = + activity.findViewById(R.id.checkbox_is_default_api_key_used) + private val isExtendedResultCheckbox: CheckBox = + activity.findViewById(R.id.checkbox_is_extended_result) private val fragmentManager = activity.supportFragmentManager + init { + if (BuildConfig.DEFAULT_API_KEY == null || BuildConfig.DEFAULT_ENDPOINT_URL == null) { + isDefaultApiKeyUsedCheckbox.text = isDefaultApiKeyUsedCheckbox.text.toString() + " (empty in this build)" + } + + isDefaultApiKeyUsedCheckbox.setOnCheckedChangeListener { _, isChecked -> + setIsDefaultApiKeyUsed(isChecked) + } + } + override fun setOnGetVisitorIdBtnClickedListener(listener: () -> Unit) { activity.runOnUiThread { getVisitorIdButton.setOnClickListener { listener.invoke() } @@ -92,6 +114,29 @@ class InputViewImpl(private val activity: BaseActivity<*>) : BaseView(activity), regionSelectButton.text = text } } + + override fun getIsDefaultApiKeyUsed(): Boolean { + return isDefaultApiKeyUsedCheckbox.isChecked + } + + override fun setIsDefaultApiKeyUsed(used: Boolean) { + isDefaultApiKeyUsedCheckbox.isChecked = used + listOf( + regionSelectButton, + apiKeyInput, + endpointUrlInput + ).forEach { + it.isEnabled = !used + } + } + + override fun getExtendedResult(): Boolean { + return isExtendedResultCheckbox.isChecked + } + + override fun setExtendedResult(extendedResult: Boolean) { + isExtendedResultCheckbox.isChecked = extendedResult + } } diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/persistence/ApplicationPreferences.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/persistence/ApplicationPreferences.kt index 275ad23..2f5821f 100644 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/persistence/ApplicationPreferences.kt +++ b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/persistence/ApplicationPreferences.kt @@ -19,6 +19,8 @@ interface ApplicationPreferences { fun setPublicApiKey(apiToken: String): Boolean fun setExtendedResult(extendedResult: Boolean): Boolean fun setRegion(region: Configuration.Region) + fun getIsDefaultApiKeyUsed(): Boolean + fun setIsDefaultApiKeyUsed(used: Boolean) class Factory(private val context: Context) { @@ -51,6 +53,7 @@ class ApplicationPreferencesImpl(context: Context) : ApplicationPreferences { private val ENDPOINT_URL_KEY = context.getString(R.string.prefs_endpoint_url_key) private val EXTENDED_RESULT_SUPPORT_KEY = context.getString(R.string.prefs_extended_result_key) private val REGION_KEY = context.getString(R.string.prefs_region_key) + private val IS_DEFAULT_API_KEY_USED_KEY = context.getString(R.string.prefs_is_default_api_key_used_key) override fun getEndpointUrl() = preferences.getString(ENDPOINT_URL_KEY, null) ?: defaultEndpointUrl @@ -69,6 +72,10 @@ class ApplicationPreferencesImpl(context: Context) : ApplicationPreferences { } } + override fun getIsDefaultApiKeyUsed(): Boolean { + return preferences.getBoolean(IS_DEFAULT_API_KEY_USED_KEY, true) + } + override fun setEndpointUrl(endpointUrl: String) = preferences.edit().putString(ENDPOINT_URL_KEY, endpointUrl).commit() @@ -82,6 +89,10 @@ class ApplicationPreferencesImpl(context: Context) : ApplicationPreferences { preferences.edit().putString(REGION_KEY, region.name).apply() } + override fun setIsDefaultApiKeyUsed(used: Boolean) { + preferences.edit().putBoolean(IS_DEFAULT_API_KEY_USED_KEY, used).apply() + } + private fun createPreferences(context: Context) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { EncryptedSharedPreferences.create( diff --git a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/results_screen/ResultsPresenter.kt b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/results_screen/ResultsPresenter.kt index 61c1844..bda2a77 100644 --- a/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/results_screen/ResultsPresenter.kt +++ b/app/src/main/java/com/fingerprintjs/android/fpjs_pro_demo/results_screen/ResultsPresenter.kt @@ -3,6 +3,7 @@ package com.fingerprintjs.android.fpjs_pro_demo.results_screen import android.os.Parcelable import com.fingerprintjs.android.fpjs_pro.* +import com.fingerprintjs.android.fpjs_pro_demo.BuildConfig import com.fingerprintjs.android.fpjs_pro_demo.base.BasePresenter import com.fingerprintjs.android.fpjs_pro_demo.base.BaseRouter import com.fingerprintjs.android.fpjs_pro_demo.base.BaseView @@ -136,9 +137,17 @@ class ResultsPresenter( } private fun initFPJSClient() { + val apiKey = when(applicationPreferences.getIsDefaultApiKeyUsed()) { + true -> BuildConfig.DEFAULT_API_KEY.orEmpty() + false -> applicationPreferences.getPublicApiKey() + } + val endpointUrl = when(applicationPreferences.getIsDefaultApiKeyUsed()) { + true -> BuildConfig.DEFAULT_ENDPOINT_URL.orEmpty() + false -> applicationPreferences.getEndpointUrl() + } val configuration = Configuration( - apiKey = applicationPreferences.getPublicApiKey(), - endpointUrl = applicationPreferences.getEndpointUrl(), + apiKey = apiKey, + endpointUrl = endpointUrl, extendedResponseFormat = applicationPreferences.getExtendedResult() ) fpjsClient = fpjsProFactory.createInstance( diff --git a/app/src/main/res/color/checkbox_color.xml b/app/src/main/res/color/checkbox_color.xml new file mode 100644 index 0000000..6ec9247 --- /dev/null +++ b/app/src/main/res/color/checkbox_color.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/color/edittext_text_colors.xml b/app/src/main/res/color/edittext_text_colors.xml new file mode 100644 index 0000000..8e1732b --- /dev/null +++ b/app/src/main/res/color/edittext_text_colors.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/color/edittext_text_hint_colors.xml b/app/src/main/res/color/edittext_text_hint_colors.xml new file mode 100644 index 0000000..4678226 --- /dev/null +++ b/app/src/main/res/color/edittext_text_hint_colors.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_run_btn.xml b/app/src/main/res/drawable/bg_run_btn.xml index 05fa685..1fb18d3 100644 --- a/app/src/main/res/drawable/bg_run_btn.xml +++ b/app/src/main/res/drawable/bg_run_btn.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_settings_48px.xml b/app/src/main/res/drawable/ic_settings_48px.xml deleted file mode 100644 index 9bfb30d..0000000 --- a/app/src/main/res/drawable/ic_settings_48px.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - diff --git a/app/src/main/res/layout/activity_input.xml b/app/src/main/res/layout/activity_input.xml index b3e8b5e..454abdb 100644 --- a/app/src/main/res/layout/activity_input.xml +++ b/app/src/main/res/layout/activity_input.xml @@ -68,20 +68,42 @@ android:minHeight="48dp" android:padding="16dp" android:text="@string/default_endpoint_url" - android:textColor="#000000" /> + android:textColorHint="@color/edittext_text_hint_colors" + android:textColor="@color/edittext_text_colors" /> + android:textColorHint="@color/edittext_text_hint_colors" + android:textColor="@color/edittext_text_colors" /> + + + + - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 4378661..48a41f8 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -6,9 +6,4 @@ android:icon="@drawable/ic_github_logo_small" android:title="@string/menu_repository_page_title" app:showAsAction="ifRoom" /> - \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 7b169f4..6c4d61a 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -9,4 +9,5 @@ #0E1029 #444872 #FF5D22 + #F43316 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 77ffbcb..41bca31 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ publicToken extendedResultKey regionKey + isDefaultApiKeyUsedKey IP: OS: Location: @@ -25,6 +26,7 @@ Failed to get visitor ID Go back Application preferences + Use default settings Enable extended result Linked ID Enable tags