Skip to content

Commit 48cbfce

Browse files
update: choose gatewayclients by region
1 parent bdf9f9a commit 48cbfce

6 files changed

Lines changed: 316 additions & 7 deletions

File tree

app/src/main/assets/gateway_clients.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"https"
1010
],
1111
"reliability": "0",
12-
"isDefault": true
12+
"isDefault": true,
13+
"region": "America"
1314
},
1415
{
1516
"country": "Nigeria",
@@ -23,7 +24,8 @@
2324
"ftp"
2425
],
2526
"reliability": "0",
26-
"isDefault": false
27+
"isDefault": true,
28+
"region": "Africa"
2729
},
2830
{
2931
"country": "Cameroon",
@@ -37,7 +39,8 @@
3739
"ftp"
3840
],
3941
"reliability": "36",
40-
"isDefault": false
42+
"isDefault": false,
43+
"region": "Africa"
4144
},
4245
{
4346
"country": "Cameroon",
@@ -51,6 +54,7 @@
5154
"ftp"
5255
],
5356
"reliability": "0",
54-
"isDefault": false
57+
"isDefault": false,
58+
"region": "Africa"
5559
}
5660
]

app/src/main/assets/regions.json

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
{
2+
"Africa": [
3+
"AO",
4+
"BF",
5+
"BI",
6+
"BJ",
7+
"BW",
8+
"CD",
9+
"CF",
10+
"CG",
11+
"CI",
12+
"CM",
13+
"CV",
14+
"DJ",
15+
"DZ",
16+
"EG",
17+
"EH",
18+
"ER",
19+
"ET",
20+
"GA",
21+
"GH",
22+
"GM",
23+
"GN",
24+
"GQ",
25+
"GW",
26+
"KE",
27+
"KM",
28+
"LS",
29+
"LR",
30+
"LY",
31+
"MA",
32+
"MG",
33+
"ML",
34+
"MR",
35+
"MU",
36+
"MW",
37+
"MZ",
38+
"NA",
39+
"NE",
40+
"NG",
41+
"RE",
42+
"RW",
43+
"SC",
44+
"SD",
45+
"SL",
46+
"SN",
47+
"SO",
48+
"SS",
49+
"ST",
50+
"SZ",
51+
"TD",
52+
"TG",
53+
"TN",
54+
"TZ",
55+
"UG",
56+
"YT",
57+
"ZA",
58+
"ZM",
59+
"ZW"
60+
],
61+
"Antarctica": [
62+
"AQ",
63+
"BV",
64+
"GS",
65+
"HM",
66+
"TF"
67+
],
68+
"Asia": [
69+
"AE",
70+
"AF",
71+
"AM",
72+
"AZ",
73+
"BD",
74+
"BH",
75+
"BN",
76+
"BT",
77+
"CC",
78+
"CN",
79+
"CX",
80+
"CY",
81+
"GE",
82+
"HK",
83+
"ID",
84+
"IL",
85+
"IN",
86+
"IO",
87+
"IQ",
88+
"IR",
89+
"JO",
90+
"JP",
91+
"KG",
92+
"KH",
93+
"KP",
94+
"KR",
95+
"KW",
96+
"KZ",
97+
"LA",
98+
"LB",
99+
"LK",
100+
"MO",
101+
"MV",
102+
"MY",
103+
"NP",
104+
"OM",
105+
"PH",
106+
"PK",
107+
"PS",
108+
"QA",
109+
"SA",
110+
"SG",
111+
"SY",
112+
"TH",
113+
"TJ",
114+
"TL",
115+
"TM",
116+
"TR",
117+
"TW",
118+
"UZ",
119+
"VN",
120+
"YE"
121+
],
122+
"Europe": [
123+
"AD",
124+
"AL",
125+
"AT",
126+
"AX",
127+
"BA",
128+
"BE",
129+
"BG",
130+
"BY",
131+
"CH",
132+
"CZ",
133+
"DE",
134+
"DK",
135+
"EE",
136+
"ES",
137+
"FI",
138+
"FO",
139+
"FR",
140+
"GB",
141+
"GG",
142+
"GI",
143+
"GR",
144+
"HR",
145+
"HU",
146+
"IE",
147+
"IM",
148+
"IS",
149+
"IT",
150+
"JE",
151+
"LI",
152+
"LT",
153+
"LU",
154+
"LV",
155+
"MC",
156+
"MD",
157+
"ME",
158+
"MK",
159+
"MT",
160+
"NL",
161+
"NO",
162+
"PL",
163+
"PT",
164+
"RO",
165+
"RS",
166+
"RU",
167+
"SE",
168+
"SI",
169+
"SJ",
170+
"SK",
171+
"SM",
172+
"UA",
173+
"VA"
174+
],
175+
"North America": [
176+
"AG",
177+
"AI",
178+
"AW",
179+
"BB",
180+
"BL",
181+
"BM",
182+
"BQ",
183+
"BS",
184+
"BZ",
185+
"CA",
186+
"CR",
187+
"CU",
188+
"CW",
189+
"DM",
190+
"DO",
191+
"GD",
192+
"GL",
193+
"GP",
194+
"GT",
195+
"HN",
196+
"HT",
197+
"JM",
198+
"KN",
199+
"KY",
200+
"LC",
201+
"MF",
202+
"MQ",
203+
"MS",
204+
"MX",
205+
"NI",
206+
"PA",
207+
"PM",
208+
"PR",
209+
"SV",
210+
"SX",
211+
"TC",
212+
"TT",
213+
"US",
214+
"VC",
215+
"VG",
216+
"VI"
217+
],
218+
"Oceania": [
219+
"AS",
220+
"AU",
221+
"CK",
222+
"FJ",
223+
"FM",
224+
"GU",
225+
"KI",
226+
"MH",
227+
"MP",
228+
"NC",
229+
"NF",
230+
"NR",
231+
"NU",
232+
"NZ",
233+
"PF",
234+
"PG",
235+
"PN",
236+
"PW",
237+
"SB",
238+
"TK",
239+
"TO",
240+
"TV",
241+
"UM",
242+
"VU",
243+
"WS"
244+
],
245+
"South America": [
246+
"AR",
247+
"BO",
248+
"BR",
249+
"CL",
250+
"CO",
251+
"EC",
252+
"FK",
253+
"GF",
254+
"GY",
255+
"PE",
256+
"PY",
257+
"SR",
258+
"UY",
259+
"VE"
260+
]
261+
}

app/src/main/java/com/example/sw0b_001/data/Datastore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
GatewayClients.class,
4343
StoredPlatformsEntity.class,
4444
EncryptedContent.class,},
45-
version = 23,
45+
version = 24,
4646
autoMigrations = {
4747
@AutoMigration( from = 8, to = 9, spec = Datastore.DatastoreMigrations.class),
4848
@AutoMigration( from = 9, to = 10, spec= Datastore.DatastoreMigrations.class),
@@ -59,6 +59,7 @@
5959
@AutoMigration( from = 20, to = 21),
6060
@AutoMigration( from = 21, to = 22, spec = Datastore.Migrate21To22.class),
6161
@AutoMigration( from = 22, to = 23, spec = Datastore.Migrate22To23.class),
62+
@AutoMigration( from = 23, to = 24),
6263
})
6364

6465
public abstract class Datastore extends RoomDatabase {

app/src/main/java/com/example/sw0b_001/data/GatewayClientsCommunications.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.example.sw0b_001.data
33
import android.content.Context
44
import com.example.sw0b_001.R
55
import com.example.sw0b_001.data.models.GatewayClients
6+
import com.example.sw0b_001.extensions.context.getTelephonyRegion
67
import com.example.sw0b_001.extensions.context.relaySmsDatastore
78
import com.example.sw0b_001.extensions.context.settingsGetDefaultGatewayClients
89
import com.example.sw0b_001.extensions.context.settingsSetDefaultGatewayClient
@@ -32,11 +33,24 @@ object GatewayClientsCommunications {
3233
val inputStream = context.assets.open(GATEWAY_CLIENTS_FILENAME)
3334
val buffer = BufferedReader(InputStreamReader(inputStream))
3435
val rawGatewayClients = buffer.use{ it.readText() }
36+
37+
val region = context.getTelephonyRegion()
3538
val gatewayClients = json
3639
.decodeFromString<ArrayList<GatewayClients>>(rawGatewayClients).apply {
3740
if(context.settingsGetDefaultGatewayClients == null) {
38-
firstOrNull { gwc -> gwc.isDefault }?.let {
39-
context.settingsSetDefaultGatewayClient(Json.encodeToString(it))
41+
when(region) {
42+
"Africa", "Asia" -> {
43+
firstOrNull { gwc -> gwc.region == region && gwc.isDefault }?.let {
44+
context.settingsSetDefaultGatewayClient(
45+
Json.encodeToString(it))
46+
}
47+
}
48+
else -> {
49+
firstOrNull { gwc -> gwc.region != "Africa" && gwc.isDefault }?.let {
50+
context.settingsSetDefaultGatewayClient(
51+
Json.encodeToString(it))
52+
}
53+
}
4054
}
4155
}
4256
}

app/src/main/java/com/example/sw0b_001/data/models/GatewayClients.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.sw0b_001.data.models
22

3+
import androidx.room.ColumnInfo
34
import androidx.room.Entity
45
import androidx.room.Index
56
import androidx.room.PrimaryKey
@@ -20,4 +21,5 @@ data class GatewayClients(
2021
var manuallyAdded: Boolean = false,
2122
var operatorCode: String? = null,
2223
var reliability: Long? = null,
24+
val region: String? = null,
2325
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.sw0b_001.extensions.context
2+
3+
import android.content.Context
4+
import android.telephony.PhoneNumberUtils
5+
import android.telephony.TelephonyManager
6+
import com.example.sw0b_001.data.GatewayClientsCommunications.GATEWAY_CLIENTS_FILENAME
7+
import com.example.sw0b_001.data.GatewayClientsCommunications.json
8+
import kotlinx.serialization.json.Json
9+
import java.io.BufferedReader
10+
import java.io.InputStreamReader
11+
12+
typealias RegionsMap = Map<String, List<String>>
13+
14+
15+
fun Context.getTelephonyRegion() : String? {
16+
17+
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
18+
val region = telephonyManager.simCountryIso.uppercase()
19+
20+
val inputStream = assets.open("regions.json")
21+
val buffer = BufferedReader(InputStreamReader(inputStream))
22+
val rawRegions = buffer.use{ it.readText() }
23+
24+
return json.decodeFromString<RegionsMap>(rawRegions).entries.find { (_, regions) ->
25+
regions.contains(region)
26+
}?.key
27+
}

0 commit comments

Comments
 (0)