From 47099c723b006cb305af4f2785e547a6525a903a Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Sat, 22 May 2021 17:24:49 +0530 Subject: di: add context and preferences module Signed-off-by: Aditya Wasan --- .../aps/injection/context/ContextModule.kt | 19 ++++++++ .../aps/injection/context/FilesDirPath.kt | 7 +++ .../aps/injection/prefs/GitPreferences.kt | 7 +++ .../aps/injection/prefs/PreferenceModule.kt | 52 ++++++++++++++++++++++ .../aps/injection/prefs/ProxyPreferences.kt | 7 +++ .../aps/injection/prefs/SettingsPreferences.kt | 7 +++ 6 files changed, 99 insertions(+) create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/context/ContextModule.kt create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/context/FilesDirPath.kt create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/prefs/GitPreferences.kt create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/prefs/ProxyPreferences.kt create mode 100644 app/src/main/java/dev/msfjarvis/aps/injection/prefs/SettingsPreferences.kt (limited to 'app/src') diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/context/ContextModule.kt b/app/src/main/java/dev/msfjarvis/aps/injection/context/ContextModule.kt new file mode 100644 index 00000000..cdb0f9f1 --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/context/ContextModule.kt @@ -0,0 +1,19 @@ +package dev.msfjarvis.aps.injection.context + +import android.content.Context +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent + +@Module +@InstallIn(SingletonComponent::class) +class ContextModule { + + @Provides + @FilesDirPath + fun providesFilesDirPath(@ApplicationContext context: Context): String { + return context.filesDir.path + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/context/FilesDirPath.kt b/app/src/main/java/dev/msfjarvis/aps/injection/context/FilesDirPath.kt new file mode 100644 index 00000000..8bced295 --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/context/FilesDirPath.kt @@ -0,0 +1,7 @@ +package dev.msfjarvis.aps.injection.context + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class FilesDirPath \ No newline at end of file diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/GitPreferences.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/GitPreferences.kt new file mode 100644 index 00000000..c07bc65e --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/GitPreferences.kt @@ -0,0 +1,7 @@ +package dev.msfjarvis.aps.injection.prefs + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class GitPreferences \ No newline at end of file diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt new file mode 100644 index 00000000..41d2a30b --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt @@ -0,0 +1,52 @@ +package dev.msfjarvis.aps.injection.prefs + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import android.content.SharedPreferences +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKey +import dagger.Module +import dagger.Provides +import dagger.Reusable +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent +import dev.msfjarvis.aps.BuildConfig + +@Module +@InstallIn(SingletonComponent::class) +class PreferenceModule { + + private fun provideBaseEncryptedPreferences(context: Context, fileName: String): SharedPreferences { + val masterKeyAlias = + MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build() + return EncryptedSharedPreferences.create( + context, + fileName, + masterKeyAlias, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + } + + @Provides + @SettingsPreferences + @Reusable + fun provideSettingsPreferences(@ApplicationContext context: Context): SharedPreferences { + return context.getSharedPreferences("${BuildConfig.APPLICATION_ID}_preferences", MODE_PRIVATE) + } + + @Provides + @GitPreferences + @Reusable + fun provideEncryptedPreferences(@ApplicationContext context: Context): SharedPreferences { + return provideBaseEncryptedPreferences(context, "git_operation") + } + + @Provides + @ProxyPreferences + @Reusable + fun provideProxyPreferences(@ApplicationContext context: Context): SharedPreferences { + return provideBaseEncryptedPreferences(context, "http_proxy") + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/ProxyPreferences.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/ProxyPreferences.kt new file mode 100644 index 00000000..c0b43088 --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/ProxyPreferences.kt @@ -0,0 +1,7 @@ +package dev.msfjarvis.aps.injection.prefs + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class ProxyPreferences \ No newline at end of file diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/SettingsPreferences.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/SettingsPreferences.kt new file mode 100644 index 00000000..7bd1f9b9 --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/SettingsPreferences.kt @@ -0,0 +1,7 @@ +package dev.msfjarvis.aps.injection.prefs + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class SettingsPreferences \ No newline at end of file -- cgit v1.2.3