Commit 70457b42 authored by Mygod's avatar Mygod

Fix unchecked cast

parent 072a11d2
...@@ -80,7 +80,7 @@ object Core { ...@@ -80,7 +80,7 @@ object Core {
val activeProfileIds get() = ProfileManager.getProfile(DataStore.profileId).let { val activeProfileIds get() = ProfileManager.getProfile(DataStore.profileId).let {
if (it == null) emptyList() else listOfNotNull(it.id, it.udpFallback) if (it == null) emptyList() else listOfNotNull(it.id, it.udpFallback)
} }
val currentProfile: Pair<Profile, Profile?>? get() { val currentProfile: ProfileManager.ExpandedProfile? get() {
if (DataStore.directBootAware) DirectBoot.getDeviceProfile()?.apply { return this } if (DataStore.directBootAware) DirectBoot.getDeviceProfile()?.apply { return this }
return ProfileManager.expand(ProfileManager.getProfile(DataStore.profileId) ?: return null) return ProfileManager.expand(ProfileManager.getProfile(DataStore.profileId) ?: return null)
} }
......
...@@ -67,7 +67,7 @@ class UrlImportActivity : AppCompatActivity() { ...@@ -67,7 +67,7 @@ class UrlImportActivity : AppCompatActivity() {
} }
private fun handleShareIntent() = intent.data?.toString()?.let { sharedStr -> private fun handleShareIntent() = intent.data?.toString()?.let { sharedStr ->
val profiles = Profile.findAllUrls(sharedStr, Core.currentProfile?.first).toList() val profiles = Profile.findAllUrls(sharedStr, Core.currentProfile?.main).toList()
if (profiles.isEmpty()) null else ImportProfilesDialogFragment().withArg(ProfilesArg(profiles)) if (profiles.isEmpty()) null else ImportProfilesDialogFragment().withArg(ProfilesArg(profiles))
} }
} }
...@@ -321,15 +321,15 @@ object BaseService { ...@@ -321,15 +321,15 @@ object BaseService {
fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val data = data val data = data
if (data.state != State.Stopped) return Service.START_NOT_STICKY if (data.state != State.Stopped) return Service.START_NOT_STICKY
val profilePair = Core.currentProfile val expanded = Core.currentProfile
this as Context this as Context
if (profilePair == null) { if (expanded == null) {
// gracefully shutdown: https://stackoverflow.com/q/47337857/2245107 // gracefully shutdown: https://stackoverflow.com/q/47337857/2245107
data.notification = createNotification("") data.notification = createNotification("")
stopRunner(false, getString(R.string.profile_empty)) stopRunner(false, getString(R.string.profile_empty))
return Service.START_NOT_STICKY return Service.START_NOT_STICKY
} }
val (profile, fallback) = profilePair val (profile, fallback) = expanded
profile.name = profile.formattedName // save name for later queries profile.name = profile.formattedName // save name for later queries
val proxy = ProxyInstance(profile) val proxy = ProxyInstance(profile)
data.proxy = proxy data.proxy = proxy
......
...@@ -96,7 +96,7 @@ class TrafficMonitor(statFile: File) { ...@@ -96,7 +96,7 @@ class TrafficMonitor(statFile: File) {
ProfileManager.updateProfile(profile) ProfileManager.updateProfile(profile)
} catch (e: IOException) { } catch (e: IOException) {
if (!DataStore.directBootAware) throw e // we should only reach here because we're in direct boot if (!DataStore.directBootAware) throw e // we should only reach here because we're in direct boot
val profile = DirectBoot.getDeviceProfile()!!.toList().filterNotNull().single { it.id == id } val profile = DirectBoot.getDeviceProfile()!!.toList().single { it.id == id }
profile.tx += current.txTotal profile.tx += current.txTotal
profile.rx += current.rxTotal profile.rx += current.rxTotal
profile.dirty = true profile.dirty = true
......
...@@ -46,6 +46,10 @@ object ProfileManager { ...@@ -46,6 +46,10 @@ object ProfileManager {
} }
var listener: Listener? = null var listener: Listener? = null
data class ExpandedProfile(val main: Profile, val udpFallback: Profile?) {
fun toList() = listOfNotNull(main, udpFallback)
}
@Throws(SQLException::class) @Throws(SQLException::class)
fun createProfile(profile: Profile = Profile()): Profile { fun createProfile(profile: Profile = Profile()): Profile {
profile.id = 0 profile.id = 0
...@@ -59,7 +63,7 @@ object ProfileManager { ...@@ -59,7 +63,7 @@ object ProfileManager {
val profiles = if (replace) getAllProfiles()?.associateBy { it.formattedAddress } else null val profiles = if (replace) getAllProfiles()?.associateBy { it.formattedAddress } else null
val feature = if (replace) { val feature = if (replace) {
profiles?.values?.singleOrNull { it.id == DataStore.profileId } profiles?.values?.singleOrNull { it.id == DataStore.profileId }
} else Core.currentProfile?.first } else Core.currentProfile?.main
val lazyClear = lazy { clear() } val lazyClear = lazy { clear() }
jsons.asIterable().forEachTry { json -> jsons.asIterable().forEachTry { json ->
Profile.parseJson(JsonStreamParser(json.bufferedReader()).asSequence().single(), feature) { Profile.parseJson(JsonStreamParser(json.bufferedReader()).asSequence().single(), feature) {
...@@ -99,7 +103,7 @@ object ProfileManager { ...@@ -99,7 +103,7 @@ object ProfileManager {
} }
@Throws(IOException::class) @Throws(IOException::class)
fun expand(profile: Profile): Pair<Profile, Profile?> = Pair(profile, profile.udpFallback?.let { getProfile(it) }) fun expand(profile: Profile) = ExpandedProfile(profile, profile.udpFallback?.let { getProfile(it) })
@Throws(SQLException::class) @Throws(SQLException::class)
fun delProfile(id: Long) { fun delProfile(id: Long) {
......
...@@ -21,8 +21,8 @@ object DirectBoot : BroadcastReceiver() { ...@@ -21,8 +21,8 @@ object DirectBoot : BroadcastReceiver() {
private val file = File(Core.deviceStorage.noBackupFilesDir, "directBootProfile") private val file = File(Core.deviceStorage.noBackupFilesDir, "directBootProfile")
private var registered = false private var registered = false
fun getDeviceProfile(): Pair<Profile, Profile?>? = try { fun getDeviceProfile(): ProfileManager.ExpandedProfile? = try {
ObjectInputStream(file.inputStream()).use { it.readObject() as? Pair<Profile, Profile?> } ObjectInputStream(file.inputStream()).use { it.readObject() as? ProfileManager.ExpandedProfile }
} catch (_: IOException) { null } } catch (_: IOException) { null }
fun clean() { fun clean() {
......
...@@ -502,7 +502,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ...@@ -502,7 +502,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
try { try {
val profiles = Profile.findAllUrls( val profiles = Profile.findAllUrls(
Core.clipboard.primaryClip!!.getItemAt(0).text, Core.clipboard.primaryClip!!.getItemAt(0).text,
Core.currentProfile?.first Core.currentProfile?.main
).toList() ).toList()
if (profiles.isNotEmpty()) { if (profiles.isNotEmpty()) {
profiles.forEach { ProfileManager.createProfile(it) } profiles.forEach { ProfileManager.createProfile(it) }
...@@ -533,7 +533,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ...@@ -533,7 +533,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
} }
R.id.action_manual_settings -> { R.id.action_manual_settings -> {
startConfig(ProfileManager.createProfile( startConfig(ProfileManager.createProfile(
Profile().also { Core.currentProfile?.first?.copyFeatureSettingsTo(it) })) Profile().also { Core.currentProfile?.main?.copyFeatureSettingsTo(it) }))
true true
} }
R.id.action_export_clipboard -> { R.id.action_export_clipboard -> {
......
...@@ -105,7 +105,7 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever { ...@@ -105,7 +105,7 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever {
} }
override fun onRetrieved(barcode: Barcode) = runOnUiThread { override fun onRetrieved(barcode: Barcode) = runOnUiThread {
Profile.findAllUrls(barcode.rawValue, Core.currentProfile?.first).forEach { ProfileManager.createProfile(it) } Profile.findAllUrls(barcode.rawValue, Core.currentProfile?.main).forEach { ProfileManager.createProfile(it) }
onSupportNavigateUp() onSupportNavigateUp()
} }
override fun onRetrievedMultiple(closetToClick: Barcode?, barcode: MutableList<BarcodeGraphic>?) = check(false) override fun onRetrievedMultiple(closetToClick: Barcode?, barcode: MutableList<BarcodeGraphic>?) = check(false)
...@@ -141,7 +141,7 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever { ...@@ -141,7 +141,7 @@ class ScannerActivity : AppCompatActivity(), BarcodeRetriever {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) { when (requestCode) {
REQUEST_IMPORT, REQUEST_IMPORT_OR_FINISH -> if (resultCode == Activity.RESULT_OK) { REQUEST_IMPORT, REQUEST_IMPORT_OR_FINISH -> if (resultCode == Activity.RESULT_OK) {
val feature = Core.currentProfile?.first val feature = Core.currentProfile?.main
try { try {
var success = false var success = false
data!!.datas.forEachTry { uri -> data!!.datas.forEachTry { uri ->
......
...@@ -362,7 +362,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, ...@@ -362,7 +362,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener,
} }
private val isEnabled get() = (activity as? MainActivity)?.state == BaseService.State.Stopped || private val isEnabled get() = (activity as? MainActivity)?.state == BaseService.State.Stopped ||
Core.currentProfile?.first?.route != Acl.CUSTOM_RULES Core.currentProfile?.main?.route != Acl.CUSTOM_RULES
private val selectedItems = HashSet<Any>() private val selectedItems = HashSet<Any>()
private val adapter by lazy { AclRulesAdapter() } private val adapter by lazy { AclRulesAdapter() }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment