1
+ package com.glia.exampleapp
2
+
3
+ import android.content.Context
4
+ import android.content.SharedPreferences
5
+ import android.net.Uri
6
+ import androidx.preference.PreferenceManager
7
+ import com.glia.androidsdk.SiteApiKey
8
+ import com.glia.androidsdk.screensharing.ScreenSharing
9
+ import com.glia.widgets.GliaWidgetsConfig
10
+
11
+ /* *
12
+ * Helper class to obtain Glia Config params from deep-link or preferences.
13
+ *
14
+ * for setup with a secret link must be
15
+ * glia://widgets/secret?site_id={site_id}&api_key_secret={api_key_secret}&api_key_id={api_key_id}&queue_id={queue_id}&visitor_context_asset_id={visitor_context_asset_id}
16
+ * where all query params are mandatory except visitor_context_asset_id
17
+ */
18
+ object GliaWidgetsConfigManager {
19
+ private const val SECRET_KEY = " secret"
20
+ private const val SITE_ID_KEY = " site_id"
21
+ private const val API_KEY_SECRET_KEY = " api_key_secret"
22
+ private const val API_KEY_ID_KEY = " api_key_id"
23
+ private const val QUEUE_ID_KEY = " queue_id"
24
+ const val VISITOR_CONTEXT_ASSET_ID_KEY = " visitor_context_asset_id"
25
+ private const val REGION_BETA = " beta"
26
+ private const val REGION_ACCEPTANCE = " acceptance"
27
+ @JvmStatic
28
+ fun obtainConfigFromDeepLink (data : Uri , applicationContext : Context ): GliaWidgetsConfig {
29
+ saveQueueIdToPrefs(data, applicationContext)
30
+ saveVisitorContextAssetIdIfPresent(data, applicationContext)
31
+ saveSiteIdToPrefs(data, applicationContext)
32
+
33
+ // We're not checking availability for every query param separately because this is for acceptance tests and we assume that link would be correct
34
+ if (SECRET_KEY == data.lastPathSegment) {
35
+ saveSiteApiKeyAuthToPrefs(data, applicationContext)
36
+ } else {
37
+ throw RuntimeException (" deep link must start with \" glia://widgets/secret\" " )
38
+ }
39
+
40
+ // By this point all settings from deep link where saved to the shared prefs overriding the
41
+ // defaults combining both together
42
+ return createDefaultConfig(
43
+ context = applicationContext,
44
+ region = REGION_ACCEPTANCE
45
+ )
46
+ }
47
+
48
+ private fun saveVisitorContextAssetIdIfPresent (data : Uri , applicationContext : Context ) {
49
+ val visitorContextAssetId = data.getQueryParameter(VISITOR_CONTEXT_ASSET_ID_KEY ) ? : return
50
+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(applicationContext)
51
+ sharedPreferences.edit().putString(
52
+ applicationContext.getString(R .string.pref_context_asset_id),
53
+ visitorContextAssetId
54
+ ).apply ()
55
+ }
56
+
57
+ private fun saveQueueIdToPrefs (data : Uri , applicationContext : Context ) {
58
+ val queueId = data.getQueryParameter(QUEUE_ID_KEY ) ? : return
59
+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(applicationContext)
60
+ sharedPreferences.edit()
61
+ .putString(applicationContext.getString(R .string.pref_queue_id), queueId)
62
+ .apply ()
63
+ }
64
+
65
+ private fun saveSiteIdToPrefs (data : Uri , applicationContext : Context ) {
66
+ val siteId = data.getQueryParameter(SITE_ID_KEY ) ? : return
67
+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(applicationContext)
68
+ sharedPreferences.edit()
69
+ .putString(applicationContext.getString(R .string.pref_site_id), siteId)
70
+ .apply ()
71
+ }
72
+
73
+ private fun saveSiteApiKeyAuthToPrefs (data : Uri , applicationContext : Context ) {
74
+ val apiKeyId = data.getQueryParameter(API_KEY_ID_KEY )
75
+ val apiKeySecret = data.getQueryParameter(API_KEY_SECRET_KEY )
76
+ if (apiKeyId == null || apiKeySecret == null ) return
77
+ val sharedPreferences = PreferenceManager .getDefaultSharedPreferences(applicationContext)
78
+ sharedPreferences.edit()
79
+ .putString(applicationContext.getString(R .string.pref_api_key_id), apiKeyId)
80
+ .putString(applicationContext.getString(R .string.pref_api_key_secret), apiKeySecret)
81
+ .apply ()
82
+ }
83
+
84
+ @JvmStatic
85
+ @JvmOverloads
86
+ fun createDefaultConfig (
87
+ context : Context ,
88
+ preferences : SharedPreferences = PreferenceManager .getDefaultSharedPreferences(context),
89
+ uiJsonRemoteConfig : String? = null,
90
+ region : String = REGION_BETA
91
+ ): GliaWidgetsConfig {
92
+ val apiKeyId = preferences.getString(
93
+ context.getString(R .string.pref_api_key_id),
94
+ context.getString(R .string.glia_api_key_id)
95
+ )
96
+ val apiKeySecret = preferences.getString(
97
+ context.getString(R .string.pref_api_key_secret),
98
+ context.getString(R .string.glia_api_key_secret)
99
+ )
100
+ val siteId = preferences.getString(
101
+ context.getString(R .string.pref_site_id),
102
+ context.getString(R .string.site_id)
103
+ )
104
+ val companyName = preferences.getString(context.getString(R .string.pref_company_name), " " )
105
+ val useOverlay = preferences.getBoolean(context.getString(R .string.pref_use_overlay), true )
106
+ val bounded = context.getString(R .string.screen_sharing_mode_app_bounded)
107
+ val unbounded = context.getString(R .string.screen_sharing_mode_unbounded)
108
+ val screenSharingMode = if (preferences.getString(
109
+ context.getString(R .string.pref_screen_sharing_mode), unbounded
110
+ ) == bounded
111
+ ) {
112
+ ScreenSharing .Mode .APP_BOUNDED
113
+ } else {
114
+ ScreenSharing .Mode .UNBOUNDED
115
+ }
116
+ return GliaWidgetsConfig .Builder ()
117
+ .setSiteApiKey(SiteApiKey (apiKeyId!! , apiKeySecret!! ))
118
+ .setSiteId(siteId)
119
+ .setRegion(region)
120
+ .setCompanyName(companyName)
121
+ .setUseOverlay(useOverlay)
122
+ .setScreenSharingMode(screenSharingMode)
123
+ .setContext(context)
124
+ .setUiJsonRemoteConfig(uiJsonRemoteConfig)
125
+ .build()
126
+ }
127
+ }
0 commit comments