Skip to content

Commit

Permalink
Merge pull request #47 from fingerprintjs/feature/default-settings
Browse files Browse the repository at this point in the history
Add default settings functionality into the app
  • Loading branch information
Sergey-Makarov authored Oct 16, 2023
2 parents 616ba73 + 3cc9a94 commit 9289b3a
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -57,7 +56,6 @@ abstract class BaseActivity<T : Parcelable>(
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)
}
Expand All @@ -77,10 +75,6 @@ abstract class BaseActivity<T : Parcelable>(
IdentificationRequestSettingsDialog(this, identificationRequestParams).show(onSettingsAppliedListener)
}

private fun openSettings() {
ClientSettingsDialog(this, preferences).showSettings()
}

private fun saveState(outState: Bundle) {
outState.putParcelable(parcelableStateKey, presenter.onSaveState())
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -41,6 +45,8 @@ class InputPresenter(
setRegionText(selectedRegion.name)
setEndpointUrl(endpointUrl)
setPublicApiKey(apiKey)
setIsDefaultApiKeyUsed(isDefaultApiKeyUsed)
setExtendedResult(isExtendedResult)
}
}

Expand All @@ -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,
)
}

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}


Expand All @@ -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() }
Expand Down Expand Up @@ -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
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/color/checkbox_color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/orange_primary" />
<item android:state_checked="false" android:color="@color/black" android:alpha="0.7"/>
</selector>
5 changes: 5 additions & 0 deletions app/src/main/res/color/edittext_text_colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="@color/black" />
<item android:state_enabled="false" android:color="@color/black" android:alpha="0.38"/>
</selector>
5 changes: 5 additions & 0 deletions app/src/main/res/color/edittext_text_hint_colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="@color/abc_hint_foreground_material_light" />
<item android:state_enabled="false" android:color="@color/abc_hint_foreground_material_light" android:alpha="0.38"/>
</selector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/bg_run_btn.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#F43316"/>
<solid android:color="@color/orange_primary"/>
<corners android:radius="8dp"/>
</shape>
10 changes: 0 additions & 10 deletions app/src/main/res/drawable/ic_settings_48px.xml

This file was deleted.

Loading

0 comments on commit 9289b3a

Please sign in to comment.