aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt
blob: 41d2a30b31ab60b867458894db885af389e13f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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")
  }
}