di: add context and preferences module

Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
This commit is contained in:
Aditya Wasan 2021-05-22 17:24:49 +05:30
parent 9fcbde2f07
commit 47099c723b
No known key found for this signature in database
GPG key ID: 6D6DF3BF15DE79B5
6 changed files with 99 additions and 0 deletions

View file

@ -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
}
}

View file

@ -0,0 +1,7 @@
package dev.msfjarvis.aps.injection.context
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class FilesDirPath

View file

@ -0,0 +1,7 @@
package dev.msfjarvis.aps.injection.prefs
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class GitPreferences

View file

@ -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")
}
}

View file

@ -0,0 +1,7 @@
package dev.msfjarvis.aps.injection.prefs
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ProxyPreferences

View file

@ -0,0 +1,7 @@
package dev.msfjarvis.aps.injection.prefs
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class SettingsPreferences