-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for non-hilt Applications (#37)
* Adding support for non-hilt Applications * Update README.md
- Loading branch information
Showing
10 changed files
with
207 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/network/RetrofitServiceCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.connect.chat.sdk.network | ||
|
||
import com.amazon.connect.chat.sdk.network.api.ApiUrl | ||
import retrofit2.Retrofit | ||
|
||
object RetrofitServiceCreator { | ||
private const val defaultApiUrl = "https://www.example.com/v1/" | ||
|
||
/** | ||
* Creates a Retrofit service for the specified class. | ||
* | ||
* @param T The type of the service. | ||
* @param clazz The class of the service. | ||
* @param retrofitBuilder The Retrofit.Builder instance for creating the service. | ||
* @return An instance of the specified service class. | ||
*/ | ||
fun <T> createService(clazz: Class<T>, retrofitBuilder: Retrofit.Builder, url: String? = null): T { | ||
// Check if the service has an @ApiUrl annotation | ||
val apiUrlAnnotation = clazz.annotations.find { it is ApiUrl } as ApiUrl? | ||
// Take the URL value, otherwise use the default | ||
val apiUrl = url ?: (apiUrlAnnotation?.url ?: defaultApiUrl) | ||
// Create the service using the extracted URL | ||
return retrofitBuilder.baseUrl(apiUrl).build().create(clazz) | ||
} | ||
} |
136 changes: 115 additions & 21 deletions
136
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/provider/ChatSessionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,127 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.amazon.connect.chat.sdk.provider | ||
|
||
import android.content.Context | ||
import com.amazon.connect.chat.sdk.ChatSession | ||
import dagger.hilt.EntryPoint | ||
import dagger.hilt.InstallIn | ||
import com.amazon.connect.chat.sdk.ChatSessionImpl | ||
import com.amazon.connect.chat.sdk.di.ChatModule | ||
import com.amazon.connect.chat.sdk.network.AWSClientImpl | ||
import com.amazon.connect.chat.sdk.network.NetworkConnectionManager | ||
import com.amazon.connect.chat.sdk.network.RetrofitServiceCreator.createService | ||
import com.amazon.connect.chat.sdk.network.WebSocketManagerImpl | ||
import com.amazon.connect.chat.sdk.network.api.APIClient | ||
import com.amazon.connect.chat.sdk.network.api.AttachmentsInterface | ||
import com.amazon.connect.chat.sdk.network.api.MetricsInterface | ||
import com.amazon.connect.chat.sdk.provider.ConnectionDetailsProviderImpl | ||
import com.amazon.connect.chat.sdk.repository.AttachmentsManager | ||
import com.amazon.connect.chat.sdk.repository.ChatServiceImpl | ||
import com.amazon.connect.chat.sdk.repository.MessageReceiptsManagerImpl | ||
import com.amazon.connect.chat.sdk.repository.MetricsManager | ||
import com.amazon.connect.chat.sdk.utils.MetricsUtils.getMetricsEndpoint | ||
import com.amazon.connect.chat.sdk.utils.logger.SDKLogger | ||
import dagger.hilt.android.EntryPointAccessors | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@EntryPoint | ||
@InstallIn(SingletonComponent::class) | ||
interface ChatSessionEntryPoint { | ||
fun getChatSession(): ChatSession | ||
} | ||
import kotlinx.coroutines.Dispatchers | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
object ChatSessionProvider { | ||
@Volatile | ||
private var chatSession: ChatSession? = null | ||
|
||
// Public method for customers to get ChatSession | ||
fun getChatSession(context: Context): ChatSession { | ||
if (chatSession == null) { | ||
val appContext = context.applicationContext | ||
return chatSession ?: synchronized(this) { | ||
chatSession ?: initializeChatSession(context).also { | ||
chatSession = it | ||
} | ||
} | ||
} | ||
|
||
// Private method to initialize ChatSession using Hilt or manual fallback | ||
private fun initializeChatSession(context: Context): ChatSession { | ||
return if (isHiltAvailable()) { | ||
// Use Hilt's EntryPoint mechanism if Hilt is available | ||
val entryPoint = EntryPointAccessors.fromApplication( | ||
appContext, | ||
ChatSessionEntryPoint::class.java | ||
context.applicationContext, | ||
ChatModule.ChatSessionEntryPoint::class.java | ||
) | ||
chatSession = entryPoint.getChatSession() | ||
entryPoint.getChatSession() | ||
} else { | ||
// Fallback to manual initialization | ||
createChatSessionManually(context) | ||
} | ||
return chatSession!! | ||
} | ||
} | ||
|
||
// Method to check if Hilt is available | ||
private fun isHiltAvailable(): Boolean { | ||
return try { | ||
Class.forName("dagger.hilt.EntryPoints") | ||
true | ||
} catch (e: ClassNotFoundException) { | ||
SDKLogger.logger.logDebug {"Hilt is not available"} | ||
false | ||
} | ||
} | ||
|
||
// Manual initialization of ChatSession for non-Hilt users | ||
private fun createChatSessionManually(context: Context): ChatSession { | ||
val appContext = context.applicationContext | ||
|
||
// Step 1: Create AWS Client | ||
val awsClient = AWSClientImpl.create() | ||
|
||
// Step 2: Create Network Connection Manager | ||
val networkConnectionManager = NetworkConnectionManager(appContext) | ||
|
||
// Step 3: Create WebSocket Manager | ||
val webSocketManager = WebSocketManagerImpl( | ||
dispatcher = Dispatchers.IO, | ||
networkConnectionManager = networkConnectionManager | ||
) | ||
|
||
// Step 4: Create Retrofit Builder | ||
val retrofitBuilder = createRetrofitBuilder() | ||
|
||
// Step 5: Create API Client | ||
val apiClient = createAPIClient(retrofitBuilder) | ||
|
||
// Step 6: Create Other Dependencies | ||
val metricsManager = MetricsManager(apiClient) | ||
val attachmentsManager = AttachmentsManager(appContext, awsClient, apiClient) | ||
val messageReceiptsManager = MessageReceiptsManagerImpl() | ||
val connectionDetailsProvider = ConnectionDetailsProviderImpl() | ||
|
||
// Step 7: Create ChatService and return ChatSessionImpl | ||
val chatService = ChatServiceImpl( | ||
appContext, | ||
awsClient, | ||
connectionDetailsProvider, | ||
webSocketManager, | ||
metricsManager, | ||
attachmentsManager, | ||
messageReceiptsManager | ||
) | ||
|
||
return ChatSessionImpl(chatService) | ||
} | ||
|
||
// Helper method to create Retrofit Builder | ||
private fun createRetrofitBuilder(): Retrofit.Builder { | ||
val okHttpClient = OkHttpClient.Builder().build() | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
} | ||
|
||
// Helper method to create APIClient | ||
private fun createAPIClient(retrofitBuilder: Retrofit.Builder): APIClient { | ||
val metricsInterface: MetricsInterface = createService( | ||
MetricsInterface::class.java, | ||
retrofitBuilder, | ||
url = getMetricsEndpoint() | ||
) | ||
val attachmentsInterface: AttachmentsInterface = createService( | ||
AttachmentsInterface::class.java, | ||
retrofitBuilder | ||
) | ||
return APIClient(metricsInterface, attachmentsInterface) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
sdkVersion=0.0.1-alpha | ||
sdkVersion=1.0.1 | ||
groupId=software.aws.connect | ||
artifactId=amazon-connect-chat-android |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters