BasePreferenceHelper.kt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package com.vpn.fastestvpnservice.helpers
  2. import android.app.Activity
  3. import android.content.Context
  4. import android.graphics.drawable.AdaptiveIconDrawable
  5. import android.graphics.drawable.Drawable
  6. import android.util.Log
  7. import com.google.gson.Gson
  8. import com.google.gson.GsonBuilder
  9. import com.google.gson.reflect.TypeToken
  10. import com.vpn.fastestvpnservice.R
  11. import com.vpn.fastestvpnservice.beans.*
  12. import com.vpn.fastestvpnservice.constants.AppEnum
  13. class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
  14. fun clearAllData() {
  15. removePreference(context, KEY_FILENAME, KEY_USER)
  16. removePreference(context, KEY_FILENAME, KEY_USER_PASSWORD)
  17. removePreference(context, KEY_FILENAME, KEY_SERVERS_DATA)
  18. // removePreference(context, KEY_FILENAME, KEY_SERVERS)
  19. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS)
  20. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NO_VPN)
  21. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NOT_ALLOW)
  22. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL)
  23. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_TV)
  24. removePreference(context, KEY_FILENAME, PROTOCOL)
  25. removePreference(context, KEY_FILENAME, X_PLATFORM_TOKEN)
  26. removePreference(context, KEY_FILENAME, KEY_PRODUCT)
  27. removePreference(context, KEY_FILENAME, KEY_AVAILABLE_PROTOCOLS)
  28. removePreference(context, KEY_FILENAME, KEY_ENABLED_PROTOCOLS)
  29. removePreference(context, KEY_FILENAME, KEY_WIREGUARD)
  30. removePreference(context, KEY_FILENAME, KEY_FEATURES_DATA)
  31. removePreference(context, KEY_FILENAME, KEY_SPLIT_ENABLE_TV)
  32. removePreference(context, KEY_FILENAME, KEY_SPLIT_DISABLE_TV)
  33. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_ENTRY)
  34. removePreference(context, KEY_FILENAME, TV_SPLIT_CLASS)
  35. removePreference(context, KEY_FILENAME, KEY_ADBLOCK_SWITCH)
  36. removePreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
  37. }
  38. fun saveUser(user: UserResponse) {
  39. putStringPreference(context, KEY_FILENAME, KEY_USER, Gson().toJson(user))
  40. }
  41. fun getUser(): UserResponse? {
  42. return Gson().fromJson<UserResponse>(
  43. getStringPreference(context, KEY_FILENAME, KEY_USER), UserResponse::class.java
  44. )
  45. }
  46. fun saveProduct(product: Product) {
  47. putStringPreference(context, KEY_FILENAME, KEY_PRODUCT, Gson().toJson(product))
  48. }
  49. fun getProduct(): Product? {
  50. return Gson().fromJson<Product>(
  51. getStringPreference(context, KEY_FILENAME, KEY_PRODUCT), Product::class.java
  52. )
  53. }
  54. fun saveWireGuard(wireGuard: WireGuard) {
  55. putStringPreference(context, KEY_FILENAME, KEY_WIREGUARD, Gson().toJson(wireGuard))
  56. }
  57. fun getWireGuard(): WireGuard? {
  58. return Gson().fromJson<WireGuard>(
  59. getStringPreference(context, KEY_FILENAME, KEY_WIREGUARD), WireGuard::class.java
  60. )
  61. }
  62. fun saveIpInfo(ipInfo: IpInfo) {
  63. putStringPreference(context, KEY_FILENAME, KEY_IPINFO, Gson().toJson(ipInfo))
  64. }
  65. fun getIpinfo(): IpInfo? {
  66. return Gson().fromJson<IpInfo>(
  67. getStringPreference(context, KEY_FILENAME, KEY_IPINFO), IpInfo::class.java
  68. )
  69. }
  70. fun savePassword(pass: String) {
  71. putStringPreference(context, KEY_FILENAME, KEY_USER_PASSWORD, pass)
  72. }
  73. fun getPassword(): String {
  74. return getStringPreference(context, KEY_FILENAME, KEY_USER_PASSWORD)
  75. }
  76. fun saveServerData(list: ArrayList<ServerData>) {
  77. putStringPreference(context, KEY_FILENAME, KEY_SERVERS_DATA, Gson().toJson(list))
  78. }
  79. fun getServerData(): List<ServerData> {
  80. val type = object : TypeToken<List<ServerData?>?>() {}.type
  81. return Gson().fromJson<List<ServerData>>(
  82. getStringPreference(
  83. context, KEY_FILENAME, KEY_SERVERS_DATA
  84. ), type
  85. )
  86. }
  87. fun saveFeaturesData(list: ArrayList<ProductFeatures>) {
  88. putStringPreference(context, KEY_FILENAME, KEY_FEATURES_DATA, Gson().toJson(list))
  89. }
  90. fun getFeaturesData(): List<ProductFeatures> {
  91. val type = object : TypeToken<List<ProductFeatures?>?>() {}.type
  92. return Gson().fromJson<List<ProductFeatures>>(
  93. getStringPreference(
  94. context, KEY_FILENAME, KEY_FEATURES_DATA
  95. ), type
  96. )
  97. }
  98. fun saveEnabledProtocols(list: ArrayList<String>) {
  99. putStringPreference(context, KEY_FILENAME, KEY_ENABLED_PROTOCOLS, Gson().toJson(list))
  100. }
  101. fun getEnabledProtocols(): ArrayList<String> {
  102. val type = object : TypeToken<ArrayList<String?>?>() {}.type
  103. try {
  104. return Gson().fromJson<ArrayList<String>>(
  105. getStringPreference(
  106. context, KEY_FILENAME, KEY_ENABLED_PROTOCOLS
  107. ), type
  108. )
  109. }
  110. catch (e: Exception) {
  111. val tempList = ArrayList<String>()
  112. tempList.add("WG")
  113. tempList.add("IKEV2")
  114. tempList.add("TCP")
  115. tempList.add("UDP")
  116. return tempList
  117. }
  118. }
  119. fun saveAvailableProtocols(list: ArrayList<String>) {
  120. var tempList = ArrayList<String>()
  121. if (list.contains(AppEnum.WG_PROTOCOL.key)) {
  122. tempList.add(AppEnum.WG_PROTOCOL.key)
  123. }
  124. if (list.contains(AppEnum.IKEV2_PROTOCOL.key)) {
  125. tempList.add(AppEnum.IKEV2_PROTOCOL.key)
  126. }
  127. if (list.contains(AppEnum.TCP_PROTOCOL.key)) {
  128. tempList.add(AppEnum.TCP_PROTOCOL.key)
  129. }
  130. if (list.contains(AppEnum.UDP_PROTOCOL.key)) {
  131. tempList.add(AppEnum.UDP_PROTOCOL.key)
  132. }
  133. putStringPreference(context, KEY_FILENAME, KEY_AVAILABLE_PROTOCOLS, Gson().toJson(tempList))
  134. }
  135. fun getAvailableProtocols(): ArrayList<String> {
  136. val type = object : TypeToken<ArrayList<String?>?>() {}.type
  137. return try {
  138. Gson().fromJson<ArrayList<String>>(
  139. getStringPreference(
  140. context, KEY_FILENAME, KEY_AVAILABLE_PROTOCOLS
  141. ), type
  142. )
  143. } catch (e: Exception) {
  144. val tempList = ArrayList<String>()
  145. tempList.add("WG")
  146. tempList.add("IKEV2")
  147. tempList.add("TCP")
  148. tempList.add("UDP")
  149. return tempList
  150. }
  151. }
  152. fun getEnableTvAppsSplit(): ArrayList<TvEnableApps>? {
  153. val type = object : TypeToken<ArrayList<TvEnableApps?>?>() {}.type
  154. return Gson().fromJson<ArrayList<TvEnableApps>>(
  155. getStringPreference(
  156. context, KEY_FILENAME, KEY_SPLIT_ENABLE_TV
  157. ), type
  158. )
  159. }
  160. fun setEnableTvAppsSplit(allSplitTunnelEnabled: ArrayList<TvEnableApps>?) {
  161. if (allSplitTunnelEnabled == null) {
  162. removePreference(context, KEY_FILENAME, KEY_SPLIT_ENABLE_TV)
  163. return
  164. }
  165. putStringPreference(context, KEY_FILENAME, KEY_SPLIT_ENABLE_TV, Gson().toJson(allSplitTunnelEnabled))
  166. }
  167. fun getDisableTvAppsSplit(): ArrayList<TvDisableApps>? {
  168. val type = object : TypeToken<ArrayList<TvDisableApps?>?>() {}.type
  169. return Gson().fromJson<ArrayList<TvDisableApps>>(
  170. getStringPreference(
  171. context, KEY_FILENAME, KEY_SPLIT_DISABLE_TV
  172. ), type
  173. )
  174. }
  175. fun setDisableTvAppsSplit(allSplitTunnelEnabled: ArrayList<TvDisableApps>?) {
  176. if (allSplitTunnelEnabled == null) {
  177. removePreference(context, KEY_FILENAME, KEY_SPLIT_DISABLE_TV)
  178. return
  179. }
  180. putStringPreference(context, KEY_FILENAME, KEY_SPLIT_DISABLE_TV, Gson().toJson(allSplitTunnelEnabled))
  181. }
  182. fun getSplitTunnelTvAppsName(): ArrayList<String>? {
  183. val type = object : TypeToken<ArrayList<String?>?>() {}.type
  184. return Gson().fromJson<ArrayList<String>>(
  185. getStringPreference(
  186. context, KEY_FILENAME, KEY_SPLIT_TUNNEL
  187. ), type
  188. )
  189. }
  190. fun setSplitTunnelTvAppsName(allSplitTunnelEnabled: ArrayList<String>?) {
  191. if (allSplitTunnelEnabled == null) {
  192. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL)
  193. return
  194. }
  195. putStringPreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL, Gson().toJson(allSplitTunnelEnabled))
  196. }
  197. fun getSplitTunnelTvAppsPackageName(): ArrayList<String>? {
  198. val type = object : TypeToken<ArrayList<String?>?>() {}.type
  199. return Gson().fromJson<ArrayList<String>>(
  200. getStringPreference(
  201. context, KEY_FILENAME, KEY_SPLIT_TUNNEL_TV
  202. ), type
  203. )
  204. }
  205. fun setSplitTunnelTvAppsPackageName(allSplitTunnelEnabled: ArrayList<String>?) {
  206. if (allSplitTunnelEnabled == null) {
  207. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_TV)
  208. return
  209. }
  210. putStringPreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_TV, Gson().toJson(allSplitTunnelEnabled))
  211. }
  212. fun getSplitTunnelTvAppsEntry(): ArrayList<String>? {
  213. val type = object : TypeToken<ArrayList<String?>?>() {}.type
  214. return Gson().fromJson<ArrayList<String>>(
  215. getStringPreference(
  216. context, KEY_FILENAME, KEY_SPLIT_TUNNEL_ENTRY
  217. ), type
  218. )
  219. }
  220. fun setSplitTunnelTvAppsEntry(appsEntry: ArrayList<String>?) {
  221. if (appsEntry == null) {
  222. removePreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_ENTRY)
  223. return
  224. }
  225. putStringPreference(context, KEY_FILENAME, KEY_SPLIT_TUNNEL_ENTRY, Gson().toJson(appsEntry))
  226. }
  227. fun setInstanceActivity(activity: Activity){
  228. putStringPreference(context, KEY_FILENAME, KEY_ACTIVITY, Gson().toJson(activity))
  229. }
  230. fun getInstanceActivity(): Activity? {
  231. var activity: Activity? = null
  232. val mainActivity = getStringPreference(context, KEY_FILENAME, KEY_ACTIVITY)
  233. if (mainActivity != null){
  234. val gson = GsonBuilder().create()
  235. activity = gson.fromJson(mainActivity, Activity::class.java)
  236. }
  237. return activity
  238. }
  239. fun getServerObject(): Server? {
  240. var serverObj: Server? = null
  241. val serverObjStr: String = getStringPreference(context, KEY_FILENAME, KEY_SERVERS)
  242. if (serverObjStr != null) {
  243. val gson = GsonBuilder().create()
  244. serverObj = gson.fromJson(serverObjStr, Server::class.java)
  245. }
  246. return serverObj
  247. }
  248. fun setServerObject(serverObj: Server?) {
  249. putStringPreference(context, KEY_FILENAME, KEY_SERVERS, Gson().toJson(serverObj))
  250. }
  251. fun getSplitTunneledApps(): String? {
  252. return getStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS)
  253. }
  254. fun setSplitTunneledApps(selectedApps: String?) {
  255. if (selectedApps == null) {
  256. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS)
  257. return
  258. }
  259. putStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS, selectedApps)
  260. }
  261. fun getSplitTunneledAppsNoVpn(): String? {
  262. return getStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NO_VPN)
  263. }
  264. fun setSplitTunneledAppsNoVpn(selectedApps: String?) {
  265. if (selectedApps == null) {
  266. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NO_VPN)
  267. return
  268. }
  269. putStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NO_VPN, selectedApps)
  270. }
  271. fun getSplitTunneledAppsNotAllow(): String? {
  272. return getStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NOT_ALLOW)
  273. }
  274. fun setSplitTunneledAppsNotAllow(selectedApps: String?) {
  275. if (selectedApps == null) {
  276. removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NOT_ALLOW)
  277. return
  278. }
  279. putStringPreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NOT_ALLOW, selectedApps)
  280. }
  281. fun saveAppDetails(appDetails: String) {
  282. putStringPreference(context, KEY_FILENAME, APP_DETAILS, appDetails)
  283. }
  284. fun getAppDetails(): String {
  285. return getStringPreference(context, KEY_FILENAME, APP_DETAILS)
  286. }
  287. fun saveFileDetails(fileDetails: String) {
  288. putStringPreference(context, KEY_FILENAME, FILE_DETAILS, fileDetails)
  289. }
  290. fun getFileDetails(): String {
  291. return getStringPreference(context, KEY_FILENAME, FILE_DETAILS)
  292. }
  293. fun saveTvSplitTunnel(tvSplitTunneling: ArrayList<TvSplitTunneling>) {
  294. putStringPreference(context, KEY_FILENAME, TV_SPLIT_CLASS, Gson().toJson(tvSplitTunneling))
  295. }
  296. fun getTvSplitTunnel(): ArrayList<TvSplitTunneling> {
  297. val tvSplitTunneling = Gson().fromJson<ArrayList<TvSplitTunneling>>(
  298. getStringPreference(context, KEY_FILENAME, TV_SPLIT_CLASS), TvSplitTunneling::class.java
  299. )
  300. return tvSplitTunneling
  301. }
  302. fun saveProtocol(protocol: Protocol) {
  303. putStringPreference(context, KEY_FILENAME, PROTOCOL, Gson().toJson(protocol))
  304. }
  305. fun getProtocol(): Protocol {
  306. var protocol = Gson().fromJson<Protocol>(
  307. getStringPreference(context, KEY_FILENAME, PROTOCOL), Protocol::class.java
  308. )
  309. protocol?.let {
  310. return protocol
  311. } ?: kotlin.run {
  312. return Protocol(
  313. AppEnum.WG_PROTOCOL.title, AppEnum.WG_PROTOCOL.key, 1
  314. // AppEnum.AUTO_PROTOCOL.title, AppEnum.IKEV2_PROTOCOL.key, 0
  315. ) //By default (AUTO) Protocol is selected (index 0 = AUTO). Checking IKEV2 first then check TCP/UDP
  316. }
  317. }
  318. fun saveRadioBtnSplitPos(position: Int) {
  319. putIntegerPreference(context, KEY_FILENAME, KEY_SPLIT_POS, position)
  320. }
  321. fun getRadioBtnSplitPos(): Int {
  322. return getIntegerPreference(context, KEY_FILENAME, KEY_SPLIT_POS)
  323. }
  324. fun saveAdBlockState(status: Boolean) {
  325. putBooleanPreference(context, KEY_FILENAME, KEY_ADBLOCK_SWITCH, status)
  326. }
  327. fun getAdBlockState(): Boolean {
  328. return getBooleanPreference(context, KEY_FILENAME, KEY_ADBLOCK_SWITCH)
  329. }
  330. fun saveFcmToken(token: String) {
  331. putStringPreference(context, KEY_FILENAME, KEY_FCM, token)
  332. }
  333. fun getLoggedInState(): Boolean {
  334. return getBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN)
  335. }
  336. fun setLoggedInState(state: Boolean) {
  337. putBooleanPreference(context, KEY_FILENAME, KEY_ISLOGGEDIN, state)
  338. }
  339. fun getFcmToken(): String {
  340. return getStringPreference(context, KEY_FILENAME, KEY_FCM)
  341. }
  342. fun saveXPlatformToken(token: String) {
  343. putStringPreference(context, KEY_FILENAME, X_PLATFORM_TOKEN, token)
  344. }
  345. fun getXPlatformToken(): String {
  346. return getStringPreference(context, KEY_FILENAME, X_PLATFORM_TOKEN)
  347. }
  348. companion object {
  349. private const val KEY_FILENAME = "file_fastestvpn"
  350. private const val KEY_USER = "keydata_user"
  351. private const val KEY_PRODUCT = "keydata_product"
  352. private const val KEY_WIREGUARD = "keydata_wireguard"
  353. private const val KEY_IPINFO = "keydata_ipinfo"
  354. private const val KEY_USER_PASSWORD = "keydata_user_password"
  355. private const val KEY_SERVERS_DATA = "keydata_servers_data"
  356. private const val KEY_FEATURES_DATA = "keydata_features_data"
  357. private const val KEY_ENABLED_PROTOCOLS = "keydata_enabled_protocols"
  358. private const val KEY_AVAILABLE_PROTOCOLS = "keydata_available_protocols"
  359. private const val KEY_SERVERS = "keydata_server"
  360. private const val KEY_SELECTED_APPS = "keydata_selectedApps"
  361. private const val KEY_SELECTED_APPS_NO_VPN = "keydata_selectedAppsNoVpn"
  362. private const val KEY_SELECTED_APPS_NOT_ALLOW = "keydata_selectedAppsNotAllow"
  363. private const val KEY_SPLIT_TUNNEL = "keydata_tv_apps_name"
  364. private const val KEY_SPLIT_TUNNEL_TV = "keydata_tv_apps_package_name"
  365. private const val KEY_SPLIT_ENABLE_TV = "keydata_tv_apps_enable"
  366. private const val KEY_SPLIT_DISABLE_TV = "keydata_tv_apps_disable"
  367. private const val KEY_SPLIT_TUNNEL_ENTRY = "keydata_tv_apps_entry"
  368. private const val APP_DETAILS = "keydata_app_detail"
  369. private const val FILE_DETAILS = "keydata_file_details"
  370. private const val PROTOCOL = "keydata_protocol"
  371. private const val TV_SPLIT_CLASS = "keydata_tvsplitclass"
  372. private const val X_PLATFORM_TOKEN = "keydata_x_platform_token"
  373. private const val KEY_FCM = "keydata_fcm"
  374. private const val KEY_SPLIT_POS = "keydata_split_pos"
  375. private const val KEY_ACTIVITY = "keydata_activity"
  376. private const val KEY_ADBLOCK_SWITCH = "keydata_adblock"
  377. private const val KEY_ISLOGGEDIN = "key_isloggedin"
  378. }
  379. }