Skip to content

Commit 104df13

Browse files
committed
[NEW] Custom HTTP timeout
1 parent 3a9aae5 commit 104df13

File tree

5 files changed

+142
-9
lines changed

5 files changed

+142
-9
lines changed

android/app/src/main/kotlin/com/invertedx/sentinelx/api/ApiService.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.invertedx.sentinelx.api
22

33
import android.content.Context
4+
import android.preference.PreferenceManager
45
import android.util.Log
56
import com.invertedx.sentinelx.BuildConfig
67
import com.invertedx.sentinelx.SentinelxApp
78
import com.invertedx.sentinelx.i
89
import com.invertedx.sentinelx.tor.TorManager
910
import com.invertedx.sentinelx.utils.LoggingInterceptor
11+
import com.invertedx.sentinelx.utils.SentinalPrefs
1012
import io.reactivex.Observable
1113
import okhttp3.FormBody
1214
import okhttp3.OkHttpClient
@@ -140,6 +142,9 @@ class ApiService(private val applicationContext: Context) {
140142
}
141143

142144
fun addHDAccount(xpub: String, bip: String): Observable<String> {
145+
146+
makeClient()
147+
143148
val baseAddress = getBaseUrl()
144149
val baseUrl = if (SentinelxApp.accessToken.isNotEmpty()) "${baseAddress}xpub?at=${SentinelxApp.accessToken.trim()}" else "${baseAddress}xpub";
145150

@@ -157,6 +162,8 @@ class ApiService(private val applicationContext: Context) {
157162
.url(baseUrl)
158163
.method("POST", requestBody)
159164
.build()
165+
166+
client.connectTimeoutMillis
160167
val response = client.newCall(request).execute()
161168
val content = response.body!!.string()
162169
Log.i("API", "response -> $content")
@@ -165,7 +172,8 @@ class ApiService(private val applicationContext: Context) {
165172
}
166173
}
167174

168-
private fun makeClient() {
175+
176+
fun makeClient() {
169177
val builder = OkHttpClient.Builder()
170178
if (BuildConfig.DEBUG) {
171179
builder.addInterceptor(LoggingInterceptor())
@@ -174,8 +182,14 @@ class ApiService(private val applicationContext: Context) {
174182
getHostNameVerifier(builder)
175183
builder.proxy(TorManager.getInstance(this.applicationContext)?.getProxy())
176184
}
177-
builder.connectTimeout(60, TimeUnit.SECONDS) // connect timeout
178-
builder.readTimeout(60, TimeUnit.SECONDS)
185+
val prefs = SentinalPrefs(applicationContext)
186+
187+
var timeout = 90L;
188+
if (prefs.timeout != null) {
189+
timeout = prefs.timeout!!.toLong();
190+
}
191+
builder.connectTimeout(timeout, TimeUnit.SECONDS) // connect timeout
192+
builder.readTimeout(timeout, TimeUnit.SECONDS)
179193
client = builder.build()
180194
}
181195

android/app/src/main/kotlin/com/invertedx/sentinelx/channel/SystemChannel.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.core.app.ActivityCompat
1010
import androidx.core.content.ContextCompat
1111
import com.invertedx.sentinelx.MainActivity
1212
import com.invertedx.sentinelx.SentinelxApp
13+
import com.invertedx.sentinelx.api.ApiService
1314
import com.invertedx.sentinelx.utils.SentinalPrefs
1415
import io.flutter.plugin.common.MethodCall
1516
import io.flutter.plugin.common.MethodChannel
@@ -101,6 +102,22 @@ class SystemChannel(private val applicationContext: Context, private val activit
101102
}
102103
}
103104

105+
"setHttpTimeout" -> {
106+
val timeout = methodCall.arguments as Int
107+
SentinalPrefs(applicationContext).timeout = timeout;
108+
ApiService(applicationContext).makeClient()
109+
result.success(true);
110+
}
111+
"getHttpTimeout" -> {
112+
val timeout = SentinalPrefs(applicationContext).timeout
113+
if (timeout != null) {
114+
result.success(timeout)
115+
} else {
116+
result.success(90)
117+
}
118+
}
119+
120+
104121
"getPackageInfo" -> {
105122
val pm = applicationContext.packageManager
106123
val info = pm.getPackageInfo(applicationContext.packageName, 0)
@@ -111,7 +128,7 @@ class SystemChannel(private val applicationContext: Context, private val activit
111128
map["buildNumber"] = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
112129
info.longVersionCode.toString()
113130
} else info.versionCode.toString()
114-
131+
115132
result.success(map)
116133
}
117134

android/app/src/main/kotlin/com/invertedx/sentinelx/utils/PreferenceHelper.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class SentinalPrefs(context: Context) : Preferences(context) {
130130
var isTestNet by booleanPref()
131131
var firstRunComplete by booleanPref()
132132
var locked by booleanPref()
133+
var timeout by intPref()
133134
}
134135

135136

lib/channels/system_channel.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class SystemChannel {
7878
}
7979
}
8080

81-
Future<Map<String,dynamic>> getPackageInfo() async {
81+
Future<Map<String, dynamic>> getPackageInfo() async {
8282
try {
8383
var run = await platform.invokeMapMethod<String, dynamic>("getPackageInfo");
8484
return run;
@@ -87,4 +87,24 @@ class SystemChannel {
8787
return null;
8888
}
8989
}
90+
91+
Future<bool> setCustomHttpTimeouts(num time) async {
92+
try {
93+
var run = await platform.invokeMethod<bool>("setHttpTimeout", time);
94+
return run;
95+
} catch (exception) {
96+
print(exception);
97+
return null;
98+
}
99+
}
100+
101+
Future<num> getHttpTimeouts() async {
102+
try {
103+
var run = await platform.invokeMethod<num>("getHttpTimeout");
104+
return run;
105+
} catch (exception) {
106+
print(exception);
107+
return null;
108+
}
109+
}
90110
}

lib/screens/settings.dart

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ class _SettingsState extends State<Settings> {
154154
showPortChooser();
155155
},
156156
),
157+
ListTile(
158+
leading: Padding(
159+
padding: const EdgeInsets.symmetric(vertical: 12),
160+
child: Icon(Icons.http),
161+
),
162+
title: Text(
163+
"HTTP Timeout ",
164+
style: Theme.of(context).textTheme.subtitle,
165+
),
166+
subtitle: Text("Set custom http timeout"),
167+
onTap: () {
168+
showTimeoutChooser();
169+
},
170+
),
157171
ListTile(
158172
leading: Padding(
159173
padding: const EdgeInsets.symmetric(vertical: 12),
@@ -288,10 +302,6 @@ class _SettingsState extends State<Settings> {
288302
} catch (e) {
289303
print("Error $e");
290304
}
291-
// Navigator.of(context)
292-
// .pushNamedAndRemoveUntil('/', (Route<dynamic> route) => false);
293-
// if (!SentinelState().eventsStream.isClosed)
294-
// SentinelState().eventsStream.sink.add(SessionStates.LOCK);
295305
}
296306

297307
void showThemeChooser(BuildContext context) {
@@ -305,4 +315,75 @@ class _SettingsState extends State<Settings> {
305315
void showPortChooser() {
306316
showModalBottomSheet(context: context, isScrollControlled: true, builder: (context) => PortSelector());
307317
}
318+
319+
void showTimeoutChooser() {
320+
showModalBottomSheet(
321+
context: context,
322+
builder: (context) {
323+
return TimeoutChooser();
324+
});
325+
}
326+
}
327+
328+
class TimeoutChooser extends StatefulWidget {
329+
@override
330+
_TimeoutChooserState createState() => _TimeoutChooserState();
331+
}
332+
333+
class _TimeoutChooserState extends State<TimeoutChooser> {
334+
List<num> timeouts = [60, 0, 90, 0, 120, 0, 150];
335+
336+
num selectedVal = 90;
337+
338+
@override
339+
Widget build(BuildContext context) {
340+
return Padding(
341+
padding: const EdgeInsets.all(8.0),
342+
child: Card(
343+
child: Wrap(
344+
direction: Axis.horizontal,
345+
children: timeouts.map((timeout) {
346+
if (timeout == 0) {
347+
return Divider();
348+
}
349+
return ListTile(
350+
dense: true,
351+
onTap: () {
352+
onSelect(timeout);
353+
},
354+
title: Text('$timeout seconds'),
355+
trailing: Radio(
356+
value: timeout,
357+
groupValue: selectedVal,
358+
onChanged: onSelect,
359+
),
360+
);
361+
}).toList(),
362+
),
363+
),
364+
);
365+
}
366+
367+
@override
368+
void initState() {
369+
super.initState();
370+
SystemChannel().getHttpTimeouts().then((onValue) {
371+
if (onValue == 0) {
372+
return;
373+
}
374+
setState(() {
375+
selectedVal = onValue;
376+
});
377+
});
378+
}
379+
380+
void onSelect(num timeout) {
381+
SystemChannel().setCustomHttpTimeouts(timeout);
382+
setState(() {
383+
selectedVal = timeout;
384+
});
385+
Future.delayed(Duration(milliseconds: 400)).then((v) {
386+
Navigator.pop(context);
387+
});
388+
}
308389
}

0 commit comments

Comments
 (0)