-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from KieronQuinn/release/2.3
2.3
- Loading branch information
Showing
23 changed files
with
780 additions
and
13 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
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/kieronquinn/app/ambientmusicmod/receivers/ExternalAccessReceiver.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,36 @@ | ||
package com.kieronquinn.app.ambientmusicmod.receivers | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import com.kieronquinn.app.ambientmusicmod.BuildConfig | ||
import com.kieronquinn.app.ambientmusicmod.repositories.ExternalAccessRepository | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class ExternalAccessReceiver: BroadcastReceiver(), KoinComponent { | ||
|
||
companion object { | ||
private const val ACTION_ENABLE = "${BuildConfig.APPLICATION_ID}.action.ENABLE" | ||
private const val ACTION_DISABLE = "${BuildConfig.APPLICATION_ID}.action.DISABLE" | ||
private const val ACTION_TOGGLE = "${BuildConfig.APPLICATION_ID}.action.TOGGLE" | ||
|
||
private const val ACTION_RUN_RECOGNITION = | ||
"${BuildConfig.APPLICATION_ID}.action.RUN_RECOGNITION" | ||
private const val ACTION_RUN_ONLINE_RECOGNITION = | ||
"${BuildConfig.APPLICATION_ID}.action.RUN_ONLINE_RECOGNITION" | ||
} | ||
|
||
private val externalAccess by inject<ExternalAccessRepository>() | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
when(intent.action) { | ||
ACTION_ENABLE -> externalAccess.onEnable(intent) | ||
ACTION_DISABLE -> externalAccess.onDisable(intent) | ||
ACTION_TOGGLE -> externalAccess.onToggle(intent) | ||
ACTION_RUN_RECOGNITION -> externalAccess.onRecognise(intent, false) | ||
ACTION_RUN_ONLINE_RECOGNITION -> externalAccess.onRecognise(intent, true) | ||
} | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
...main/java/com/kieronquinn/app/ambientmusicmod/repositories/EncryptedSettingsRepository.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,110 @@ | ||
package com.kieronquinn.app.ambientmusicmod.repositories | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKeys | ||
import com.kieronquinn.app.ambientmusicmod.BuildConfig | ||
import com.kieronquinn.app.ambientmusicmod.repositories.BaseSettingsRepository.AmbientMusicModSetting | ||
import com.kieronquinn.app.ambientmusicmod.utils.extensions.randomSecureString | ||
import java.security.KeyStore | ||
|
||
interface EncryptedSettingsRepository { | ||
|
||
val encryptionAvailable: Boolean | ||
|
||
val externalAccessEnabled: AmbientMusicModSetting<Boolean> | ||
val externalAccessRequireToken: AmbientMusicModSetting<Boolean> | ||
val externalAccessToken: AmbientMusicModSetting<String> | ||
val externalAccessToggleEnabled: AmbientMusicModSetting<Boolean> | ||
val externalAccessRecognitionEnabled: AmbientMusicModSetting<Boolean> | ||
|
||
} | ||
|
||
class EncryptedSettingsRepositoryImpl( | ||
context: Context | ||
): BaseSettingsRepositoryImpl(), EncryptedSettingsRepository { | ||
|
||
companion object { | ||
const val EXTERNAL_ACCESS_TOKEN_LENGTH = 16 | ||
|
||
private const val EXTERNAL_ACCESS_ENABLED = "external_access_enabled" | ||
private const val DEFAULT_EXTERNAL_ACCESS_ENABLED = false | ||
|
||
private const val EXTERNAL_ACCESS_REQUIRE_TOKEN = "external_access_require_token" | ||
private const val DEFAULT_EXTERNAL_ACCESS_REQUIRE_TOKEN = true | ||
|
||
private const val EXTERNAL_ACCESS_TOKEN = "external_access_token" | ||
private val DEFAULT_EXTERNAL_ACCESS_TOKEN = randomSecureString(EXTERNAL_ACCESS_TOKEN_LENGTH) | ||
|
||
private const val EXTERNAL_ACCESS_TOGGLE_ENABLED = "external_access_toggle_enabled" | ||
private const val DEFAULT_EXTERNAL_ACCESS_TOGGLE_ENABLED = true | ||
|
||
private const val EXTERNAL_ACCESS_RECOGNITION_ENABLED = "external_access_recognition_enabled" | ||
private const val DEFAULT_ACCESS_EXTERNAL_RECOGNITION_ENABLED = true | ||
|
||
private fun tryLoadSharedPreferences(context: Context): SharedPreferences? { | ||
//Regular load, should work 99% of the time | ||
getSharedPreferences(context)?.let { | ||
return it | ||
} | ||
//If failed, delete the key and start again | ||
deleteMasterKeyEntry() | ||
//If it still fails, nothing we can do | ||
return getSharedPreferences(context) | ||
} | ||
|
||
private fun getSharedPreferences(context: Context): SharedPreferences? { | ||
return try { | ||
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC | ||
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec) | ||
EncryptedSharedPreferences.create( | ||
"${BuildConfig.APPLICATION_ID}_encrypted_prefs", | ||
mainKeyAlias, | ||
context, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
}catch (e: Exception) { | ||
//Failed to load shared prefs | ||
null | ||
} | ||
} | ||
|
||
private fun deleteMasterKeyEntry() { | ||
try { | ||
KeyStore.getInstance("AndroidKeyStore").apply { | ||
load(null) | ||
deleteEntry("_androidx_security_master_key_") | ||
} | ||
}catch (e: Exception){ | ||
//Failed to delete key | ||
} | ||
} | ||
} | ||
|
||
private val encryptedSharedPreferences by lazy { | ||
tryLoadSharedPreferences(context) | ||
} | ||
|
||
override val sharedPreferences | ||
get() = encryptedSharedPreferences ?: throw RuntimeException("Encrypted prefs failed to load") | ||
|
||
override val encryptionAvailable = encryptedSharedPreferences != null | ||
|
||
override val externalAccessEnabled = | ||
boolean(EXTERNAL_ACCESS_ENABLED, DEFAULT_EXTERNAL_ACCESS_ENABLED) | ||
|
||
override val externalAccessRequireToken = | ||
boolean(EXTERNAL_ACCESS_REQUIRE_TOKEN, DEFAULT_EXTERNAL_ACCESS_REQUIRE_TOKEN) | ||
|
||
override val externalAccessToken = | ||
string(EXTERNAL_ACCESS_TOKEN, DEFAULT_EXTERNAL_ACCESS_TOKEN) | ||
|
||
override val externalAccessToggleEnabled = | ||
boolean(EXTERNAL_ACCESS_TOGGLE_ENABLED, DEFAULT_EXTERNAL_ACCESS_TOGGLE_ENABLED) | ||
|
||
override val externalAccessRecognitionEnabled = | ||
boolean(EXTERNAL_ACCESS_RECOGNITION_ENABLED, DEFAULT_ACCESS_EXTERNAL_RECOGNITION_ENABLED) | ||
|
||
} |
Oops, something went wrong.