@@ -28,9 +28,9 @@ internal interface SecureStorage {
2828@Singleton
2929internal class ArmadilloSecureStorage @Inject constructor(
3030 @Named(Constants .DI .STANDARD_STORAGE ) private val legacyStandardStorage : SharedPreferences ,
31- @Named(Constants .DI .STANDARD_SECURE_STORAGE ) private val secureStandardStorage : SharedPreferences ,
31+ @Named(Constants .DI .STANDARD_SECURE_STORAGE ) private val secureStandardStorage : SharedPreferences ? ,
3232 @Named(Constants .DI .DRM_DOWNLOAD_STORAGE ) private val legacyDrmStorage : SharedPreferences ,
33- @Named(Constants .DI .DRM_SECURE_STORAGE ) private val secureDrmStorage : SharedPreferences
33+ @Named(Constants .DI .DRM_SECURE_STORAGE ) private val secureDrmStorage : SharedPreferences ?
3434) : SecureStorage {
3535 companion object {
3636 const val DOWNLOAD_KEY = " download_key"
@@ -41,26 +41,31 @@ internal class ArmadilloSecureStorage @Inject constructor(
4141 }
4242
4343 override fun downloadSecretKey (context : Context ): ByteArray {
44- return if (secureStandardStorage.contains(DOWNLOAD_KEY )) {
44+ return if (secureStandardStorage? .contains(DOWNLOAD_KEY ) == true ) {
4545 val storedKey = secureStandardStorage.getString(DOWNLOAD_KEY , DEFAULT ) ? : DEFAULT
4646 if (storedKey == DEFAULT ) {
4747 Log .e(TAG , " Storage Is Out of Alignment" )
4848 }
4949 storedKey.toSecretByteArray
50- } else if (legacyStandardStorage.contains(DOWNLOAD_KEY )) {
50+ } else if (legacyStandardStorage.contains(DOWNLOAD_KEY )) {
5151 // migrate to secured version
5252 val storedKey = legacyStandardStorage.getString(DOWNLOAD_KEY , DEFAULT ) ? : DEFAULT
5353 if (storedKey == DEFAULT ) {
5454 Log .e(TAG , " Storage Is Out of Alignment" )
5555 }
56- secureStandardStorage.edit().putString(DOWNLOAD_KEY , storedKey).apply ()
57- legacyStandardStorage.edit().remove(DOWNLOAD_KEY ).apply ()
56+ if (secureStandardStorage != null ) {
57+ secureStandardStorage.edit().putString(DOWNLOAD_KEY , storedKey).apply ()
58+ legacyStandardStorage.edit().remove(DOWNLOAD_KEY ).apply ()
59+ }
5860 storedKey.toSecretByteArray
59- } else {
61+ } else if (secureStandardStorage != null ) {
6062 // no key exists anywhere yet
6163 createRandomString().also {
6264 secureStandardStorage.edit().putString(DOWNLOAD_KEY , it).apply ()
6365 }.toSecretByteArray
66+ } else {
67+ " " .toSecretByteArray
68+ // we've attempted to create 2 sharedPrefs by this point, so this shouldn't happen. Let exoplayer fail to decrypt
6469 }
6570 }
6671
@@ -73,28 +78,30 @@ internal class ArmadilloSecureStorage @Inject constructor(
7378 override fun saveDrmDownload (context : Context , id : String , drmDownload : DrmDownload ) {
7479 val alias = getDrmDownloadAlias(id, drmDownload.drmType)
7580 val value = Base64 .encodeToString(Json .encodeToString(drmDownload).toByteArray(StandardCharsets .UTF_8 ), Base64 .NO_WRAP )
76- secureDrmStorage.edit().putString(alias, value).apply ()
81+ secureDrmStorage? .edit()? .putString(alias, value)? .apply ()
7782 }
7883
7984 override fun getDrmDownload (context : Context , id : String , drmType : DrmType ): DrmDownload ? {
8085 val alias = getDrmDownloadAlias(id, drmType)
81- var download = secureDrmStorage.getString(alias, null )?.decodeToDrmDownload()
86+ var download = secureDrmStorage? .getString(alias, null )?.decodeToDrmDownload()
8287 if (download == null && legacyDrmStorage.contains(alias)) {
8388 // migrate old storage to secure storage
8489 val downloadValue = legacyDrmStorage.getString(alias, null )
8590 download = downloadValue?.decodeToDrmDownload()
86- secureDrmStorage.edit().putString(alias, downloadValue).apply ()
87- legacyDrmStorage.edit().remove(alias).apply ()
91+ if (secureDrmStorage != null ) {
92+ secureDrmStorage.edit().putString(alias, downloadValue).apply ()
93+ legacyDrmStorage.edit().remove(alias).apply ()
94+ }
8895 }
8996 return download
9097 }
9198
9299 override fun getAllDrmDownloads (context : Context ): Map <String , DrmDownload > {
93- val drmDownloads = secureDrmStorage.all.keys.mapNotNull { alias ->
100+ val drmDownloads = secureDrmStorage? .all? .keys? .mapNotNull { alias ->
94101 secureDrmStorage.getString(alias, null )?.let { drmResult ->
95102 alias to drmResult.decodeToDrmDownload()
96103 }
97- }.toMap()
104+ }? .toMap() ? : emptyMap ()
98105 val legacyDownloads = legacyDrmStorage.all.keys.mapNotNull { alias ->
99106 legacyDrmStorage.getString(alias, null )?.let { drmResult ->
100107 alias to drmResult.decodeToDrmDownload()
@@ -107,12 +114,12 @@ internal class ArmadilloSecureStorage @Inject constructor(
107114 override fun removeDrmDownload (context : Context , id : String , drmType : DrmType ) {
108115 val alias = getDrmDownloadAlias(id, drmType)
109116 legacyDrmStorage.edit().remove(alias).apply ()
110- secureDrmStorage.edit().remove(alias).apply ()
117+ secureDrmStorage? .edit()? .remove(alias)? .apply ()
111118 }
112119
113120 override fun removeDrmDownload (context : Context , key : String ) {
114121 legacyDrmStorage.edit().remove(key).apply ()
115- secureDrmStorage.edit().remove(key).apply ()
122+ secureDrmStorage? .edit()? .remove(key)? .apply ()
116123 }
117124
118125 private val String .toSecretByteArray: ByteArray
0 commit comments