-
-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathWifiConfigurationAdapter.kt
More file actions
39 lines (31 loc) · 1.31 KB
/
WifiConfigurationAdapter.kt
File metadata and controls
39 lines (31 loc) · 1.31 KB
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
package com.xayah.databackup.adapter
import android.net.wifi.WifiConfiguration
import com.google.gson.ExclusionStrategy
import com.google.gson.FieldAttributes
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
class WifiConfigurationAdapter {
private val mType = object : TypeToken<WifiConfiguration>() {}.type
private val mGson: Gson = GsonBuilder().addDeserializationExclusionStrategy(NetworkStrategy()).create()
private class NetworkStrategy : ExclusionStrategy {
private val mSkipFields: List<String> = listOf(
"mNetworkSeclectionDisableCounter" // We need to skip this filed to make it work for restoring
)
override fun shouldSkipField(f: FieldAttributes): Boolean {
return mSkipFields.contains(f.name)
}
override fun shouldSkipClass(clazz: Class<*>?): Boolean {
return false
}
}
@ToJson
fun toJson(wifiConfiguration: WifiConfiguration): String = mGson.toJson(wifiConfiguration)
@FromJson
fun fromJson(json: String): WifiConfiguration {
val config: WifiConfiguration? = mGson.fromJson(json, mType)
return requireNotNull(config) { "Invalid WifiConfiguration JSON: $json" }
}
}