Skip to content

Commit

Permalink
fix: App should work without no data inside custom.properties file
Browse files Browse the repository at this point in the history
ALS-1884
  • Loading branch information
shah272728 committed Nov 8, 2024
1 parent ddfc221 commit 8e3e5ae
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 41 deletions.
76 changes: 69 additions & 7 deletions app/src/main/java/com/aws/amazonlocation/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aws.amazonlocation.ui.main

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.pm.ActivityInfo
import android.content.res.Configuration
Expand All @@ -14,7 +15,6 @@ import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -85,14 +85,17 @@ import com.aws.amazonlocation.utils.SIGN_OUT
import com.aws.amazonlocation.utils.Units.checkInternetConnection
import com.aws.amazonlocation.utils.VERSION_FRAGMENT
import com.aws.amazonlocation.utils.analytics.AnalyticsUtils
import com.aws.amazonlocation.utils.analyticsFields
import com.aws.amazonlocation.utils.getLanguageCode
import com.aws.amazonlocation.utils.hide
import com.aws.amazonlocation.utils.hideViews
import com.aws.amazonlocation.utils.invisible
import com.aws.amazonlocation.utils.makeTransparentStatusBar
import com.aws.amazonlocation.utils.requiredFields
import com.aws.amazonlocation.utils.setLocale
import com.aws.amazonlocation.utils.show
import com.aws.amazonlocation.utils.showViews
import com.aws.amazonlocation.utils.simulationFields
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
Expand Down Expand Up @@ -642,6 +645,7 @@ class MainActivity :
var mRegion = mPreferenceManager.getValue(KEY_USER_REGION, "")

if (mRegion.isNullOrEmpty()) {
if (BuildConfig.DEFAULT_REGION == "null") return@launch
mRegion = BuildConfig.DEFAULT_REGION
}
val iotClient =
Expand Down Expand Up @@ -690,6 +694,7 @@ class MainActivity :

private fun setSimulationIotPolicy() {
val identityId = mLocationProvider.getIdentityId()
if (identityId.isNullOrEmpty()) return
CoroutineScope(Dispatchers.IO).launch {
val attachPolicyRequest =
AttachPolicyRequest {
Expand All @@ -699,7 +704,7 @@ class MainActivity :

val iotClient =
IotClient {
region = identityId?.split(":")?.get(0)
region = identityId.split(":")[0]
credentialsProvider =
createCredentialsProviderForPolicy(
mLocationProvider.getCredentials(),
Expand Down Expand Up @@ -934,7 +939,7 @@ class MainActivity :
mBinding.imgAmazonLogo?.setImageResource(logoResId)
}

fun setExplorer() {
private fun setExplorer() {
val fragment = mNavHostFragment.childFragmentManager.fragments[0]
if (fragment !is ExploreFragment) {
mNavController.navigate(R.id.explore_fragment)
Expand Down Expand Up @@ -998,7 +1003,6 @@ class MainActivity :
mTrackingUtils?.hideTrackingBottomSheet()
mSimulationUtils?.showSimulationBottomSheet()
if (mNavHostFragment.childFragmentManager.fragments.isNotEmpty()) {
val fragment = mNavHostFragment.childFragmentManager.fragments[0]
if (fragment is ExploreFragment) {
if (isTablet) {
fragment.hideDirectionAndCurrentLocationIcon()
Expand Down Expand Up @@ -1200,7 +1204,64 @@ class MainActivity :
}
}

fun setWelcomeToExplorer() {
fun checkPropertiesData() {
val missingRequiredFields = requiredFields.filter { it.value == "null" }.keys
val simulationMissingFields = simulationFields.filter { it.value == "null" }.keys
val analyticsMissingFields = analyticsFields.filter { it.value == "null" }.keys

if (missingRequiredFields.isNotEmpty() || simulationMissingFields.isNotEmpty() || analyticsMissingFields.isNotEmpty()) {
val dialogMessage = buildString {
when {
missingRequiredFields.isNotEmpty() -> {
append(getString(R.string.label_required_fields_missing))
append("\n")
missingRequiredFields.forEach { append("$it\n") }
simulationMissingFields.forEach { append("$it\n") }
analyticsMissingFields.forEach { append("$it\n") }
}
simulationMissingFields.isNotEmpty() && analyticsMissingFields.isNotEmpty() -> {
append(getString(R.string.label_some_fields_missing))
append("\n")
simulationMissingFields.forEach { append("$it\n") }
analyticsMissingFields.forEach { append("$it\n") }
}
simulationMissingFields.isNotEmpty() -> {
append(getString(R.string.label_simulation_fields_missing))
append("\n")
simulationMissingFields.forEach { append("$it\n") }
}
analyticsMissingFields.isNotEmpty() -> {
append(getString(R.string.label_analytics_fields_missing))
append("\n")
analyticsMissingFields.forEach { append("$it\n") }
}
}
}

val dialogTitle = getString(R.string.title_configuration_incomplete)
val positiveButtonText = if (missingRequiredFields.isNotEmpty()) {
getString(R.string.ok)
} else {
getString(R.string.label_continue)
}

AlertDialog
.Builder(this)
.setTitle(dialogTitle)
.setMessage(dialogMessage)
.setPositiveButton(positiveButtonText) { _, _ ->
if (missingRequiredFields.isNotEmpty()) {
finish()
} else {
setWelcomeToExplorer()
}
}.setCancelable(false).show()
} else {
setWelcomeToExplorer()
}
}

private fun setWelcomeToExplorer() {
mPreferenceManager.setValue(IS_APP_FIRST_TIME_OPENED, true)
isAppNotFirstOpened = true
val fragment =
Expand Down Expand Up @@ -1232,11 +1293,12 @@ class MainActivity :
}
}

fun initClient(isAfterSignOut:Boolean = false){
fun initClient(isAfterSignOut: Boolean = false) {
if (!isAfterSignOut) {
try {
mLocationProvider.clearCredentials()
} catch (_: Exception) { }
} catch (_: Exception) {
}
}
CoroutineScope(Dispatchers.IO).launch {
async { initMobileClient() }.await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.aws.amazonlocation.ui.main.simulation

import android.Manifest.permission.POST_NOTIFICATIONS
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
Expand All @@ -27,6 +28,7 @@ import com.aws.amazonlocation.utils.AnalyticsAttributeValue
import com.aws.amazonlocation.utils.EventType
import com.aws.amazonlocation.utils.NotificationDialogInterface
import com.aws.amazonlocation.utils.notificationPermission
import com.aws.amazonlocation.utils.simulationFields
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
Expand Down Expand Up @@ -99,36 +101,28 @@ class SimulationBottomSheetFragment : BottomSheetDialogFragment() {
private fun clickListener() {
mBinding.apply {
btnStartSimulation.setOnClickListener {
when {
ContextCompat.checkSelfPermission(
requireContext(),
POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED -> {
openSimulation()
}
shouldShowRequestPermissionRationale(POST_NOTIFICATIONS) -> {
if (!NotificationManagerCompat.from(requireContext()).areNotificationsEnabled()) {
requireContext().notificationPermission(object : NotificationDialogInterface {
override fun onOkClick(dialog: DialogInterface) {
openAppNotificationSettings(requireContext())
}

override fun onCancelClick(dialog: DialogInterface) {
openSimulation()
}
})
}
}
else -> {
// The registered ActivityResultCallback gets the result of this request
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(
POST_NOTIFICATIONS
)
} else {
openSimulation()
val simulationMissingFields = simulationFields.filter { it.value == "null" }.keys

if (simulationMissingFields.isNotEmpty()) {
val dialogMessage =
buildString {
append(getString(R.string.label_simulation_fields_missing))
append("\n")
simulationMissingFields.forEach { append("$it\n") }
}
}

val dialogTitle: String = getString(R.string.title_configuration_incomplete)
val positiveButtonText: String = getString(R.string.ok)

AlertDialog
.Builder(activity)
.setTitle(dialogTitle)
.setMessage(dialogMessage)
.setPositiveButton(positiveButtonText) { dialog, _ ->
dialog.dismiss()
}.setCancelable(false).show()
} else {
startSimulation()
}
}
tvMaybeLater.setOnClickListener {
Expand All @@ -140,6 +134,41 @@ class SimulationBottomSheetFragment : BottomSheetDialogFragment() {
}
}

private fun startSimulation() {
when {
ContextCompat.checkSelfPermission(
requireContext(),
POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED -> {
openSimulation()
}

shouldShowRequestPermissionRationale(POST_NOTIFICATIONS) -> {
if (!NotificationManagerCompat.from(requireContext()).areNotificationsEnabled()) {
requireContext().notificationPermission(object : NotificationDialogInterface {
override fun onOkClick(dialog: DialogInterface) {
openAppNotificationSettings(requireContext())
}

override fun onCancelClick(dialog: DialogInterface) {
openSimulation()
}
})
}
}

else -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(
POST_NOTIFICATIONS
)
} else {
openSimulation()
}
}
}
}

@SuppressLint("ObsoleteSdkInt")
private fun openAppNotificationSettings(context: Context) {
val intent = Intent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,11 @@ class SimulationUtils(
),
mPreferenceManager?.getValue(KEY_NEAREST_REGION, "")
)

if (defaultIdentityPoolId == "null") return
val credentials = createCredentialsProvider(mLocationProvider.getCredentials())
mqttClient = AWSIotMqttClient(getSimulationWebSocketUrl(defaultIdentityPoolId), identityId, credentials, defaultIdentityPoolId.split(":")[0])
val webSocketUrl = getSimulationWebSocketUrl(defaultIdentityPoolId)
if (webSocketUrl == "null") return
mqttClient = AWSIotMqttClient(webSocketUrl, identityId, credentials, defaultIdentityPoolId.split(":")[0])

try {
mqttClient?.connect(MQTT_CONNECT_TIME_OUT, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class WelcomeBottomSheetFragment : BottomSheetDialogFragment() {
private fun clickListener() {
mBinding.apply {
btnContinueToApp.setOnClickListener {
(activity as MainActivity).setWelcomeToExplorer()
(activity as MainActivity).checkPropertiesData()
dialog.dismiss()
}
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/aws/amazonlocation/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ val notificationData = arrayListOf(
NotificationData("Bus 05 UBC", false)
)

val requiredFields = mapOf(
"API_KEY_EU_CENTRAL" to BuildConfig.API_KEY_EU_CENTRAL,
"API_KEY_US_EAST" to BuildConfig.API_KEY_US_EAST
)

val simulationFields = mapOf(
"DEFAULT_IDENTITY_POOL_ID" to BuildConfig.DEFAULT_IDENTITY_POOL_ID,
"DEFAULT_IDENTITY_POOL_ID_EU" to BuildConfig.DEFAULT_IDENTITY_POOL_ID_EU,
"DEFAULT_REGION" to BuildConfig.DEFAULT_REGION,
"SIMULATION_WEB_SOCKET_URL" to BuildConfig.SIMULATION_WEB_SOCKET_URL,
"SIMULATION_WEB_SOCKET_URL_EU" to BuildConfig.SIMULATION_WEB_SOCKET_URL_EU,
)

val analyticsFields = mapOf(
"ANALYTICS_IDENTITY_POOL_ID" to BuildConfig.ANALYTICS_IDENTITY_POOL_ID,
"ANALYTICS_APP_ID" to BuildConfig.ANALYTICS_APP_ID
)

object EventType {
const val SCREEN_OPEN = "SCREEN_OPEN"
const val SCREEN_CLOSE = "SCREEN_CLOSE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class MapHelper(
),
mPreferenceManager?.getValue(KEY_NEAREST_REGION, ""),
)
if (defaultIdentityPoolId == "null") return
region = defaultIdentityPoolId.split(":")[0]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ class AnalyticsUtils(

suspend fun initAnalytics() {
credentialProvider = mLocationProvider.getAnalyticsCredentialProvider()
credentialProvider?.let {
if (BuildConfig.ANALYTICS_IDENTITY_POOL_ID != "null" || credentialProvider != null) {
val region = BuildConfig.ANALYTICS_IDENTITY_POOL_ID.split(":")[0]
pinpointClient =
PinpointClient {
this.region = region
credentialsProvider = it
credentialsProvider = credentialProvider
}
if (endpointId.isEmpty()) {
endpointId = UUID.randomUUID().toString()
Expand Down Expand Up @@ -89,6 +89,7 @@ class AnalyticsUtils(
event: String,
properties: List<Pair<String, String>> = emptyList(),
) {
if (BuildConfig.ANALYTICS_APP_ID == "null") return
CoroutineScope(Dispatchers.IO).launch {
if (!mLocationProvider.isUnAuthCredentialsValid(true)) {
runBlocking { initAnalytics() }
Expand Down Expand Up @@ -211,6 +212,7 @@ class AnalyticsUtils(
}

suspend fun startSession() {
if (BuildConfig.ANALYTICS_APP_ID == "null") return
session.creationStatus = AnalyticsSessionStatus.IN_PROGRESS
runBlocking { createOrUpdateEndpoint() }
session.id = UUID.randomUUID().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class LocationProvider(
suspend fun initPlaceRoutesClients(baseActivity: BaseActivity) {
val mRegion = Units.getRegion(mPreferenceManager)
val apiKey = Units.getApiKey(mPreferenceManager)
if (apiKey == "null") return
val credentialProvider =
CoroutineScope(Dispatchers.Main)
.async {
Expand Down Expand Up @@ -113,6 +114,7 @@ class LocationProvider(
"",
).toString()
}
if (defaultIdentityPoolId == "null") return
val defaultRegion = defaultIdentityPoolId.split(":")[0]
region = defaultRegion
region?.let {
Expand Down Expand Up @@ -232,6 +234,7 @@ class LocationProvider(
}
}
suspend fun getAnalyticsCredentialProvider(): CredentialsProvider? {
if (BuildConfig.ANALYTICS_IDENTITY_POOL_ID == "null") return null
val defaultIdentityPoolId = BuildConfig.ANALYTICS_IDENTITY_POOL_ID
val defaultRegion = BuildConfig.ANALYTICS_IDENTITY_POOL_ID.split(":")[0]
return generateUnAuthCredentials(defaultRegion, defaultIdentityPoolId, true)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-ar/string.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,9 @@
<string name="description_tza">تنزانيا منظر في بحيرة مالاوي</string>
<string name="description_ury">الأوروغواي منظر في رينكون دي أرتيجاس</string>
<string name="description_vnm">فيتنام منظر في ال باراسيل الجزر و سبراتلي الجزر</string>
<string name="label_required_fields_missing">مطلوب التكوين مفاتيح يكونوا في عداد المفقودين، و ال تطبيق غير قادر وظيفة بدون هم. ال التالي مفاتيح يكونوا مفقود:\n</string>
<string name="label_some_fields_missing">بعض التكوين مفاتيح يكونوا مفقود. خريطة، ابحث، الأماكن، و المسار يكونوا متاح، لكن محاكاة و تحليلات يكونوا معاق حتى ال التالي مفاتيح يكونوا وأضاف:\n</string>
<string name="label_simulation_fields_missing">بعض التكوين مفاتيح يكونوا مفقود. خريطة، ابحث، الأماكن، و المسار يكونوا متاح، لكن محاكاة هو معاق حتى ال التالي مفاتيح يكونوا وأضاف:\n</string>
<string name="label_analytics_fields_missing">تحليلات تتبع هو معاق بسبب ال التالي التكوين مفاتيح يكونوا مفقود:\n</string>
<string name="title_configuration_incomplete">التكوين غير مكتمل</string>
</resources>
Loading

0 comments on commit 8e3e5ae

Please sign in to comment.