@@ -40,6 +40,7 @@ import android.os.PowerManager
40
40
import android.os.SystemClock
41
41
import android.provider.DocumentsContract
42
42
import androidx.core.app.ServiceCompat
43
+ import androidx.core.content.edit
43
44
import androidx.preference.PreferenceManager
44
45
import com.amaze.filemanager.BuildConfig
45
46
import com.amaze.filemanager.R
@@ -52,6 +53,10 @@ import com.amaze.filemanager.ui.notifications.FtpNotification
52
53
import com.amaze.filemanager.ui.notifications.NotificationConstants
53
54
import com.amaze.filemanager.utils.ObtainableServiceBinder
54
55
import com.amaze.filemanager.utils.PasswordUtil
56
+ import kotlinx.coroutines.CoroutineScope
57
+ import kotlinx.coroutines.Dispatchers
58
+ import kotlinx.coroutines.SupervisorJob
59
+ import kotlinx.coroutines.launch
55
60
import org.apache.ftpserver.ConnectionConfigFactory
56
61
import org.apache.ftpserver.FtpServer
57
62
import org.apache.ftpserver.FtpServerFactory
@@ -61,7 +66,6 @@ import org.apache.ftpserver.ssl.ClientAuth
61
66
import org.apache.ftpserver.ssl.impl.DefaultSslConfiguration
62
67
import org.apache.ftpserver.usermanager.impl.BaseUser
63
68
import org.apache.ftpserver.usermanager.impl.WritePermission
64
- import org.greenrobot.eventbus.EventBus
65
69
import org.slf4j.Logger
66
70
import org.slf4j.LoggerFactory
67
71
import java.io.IOException
@@ -79,6 +83,7 @@ import kotlin.concurrent.thread
79
83
* Edited by zent-co on 30-07-2019 Edited by bowiechen on 2019-10-19.
80
84
*/
81
85
class FtpService : Service (), Runnable {
86
+ private val serviceScope = CoroutineScope (Dispatchers .Default + SupervisorJob ())
82
87
private val binder: IBinder = ObtainableServiceBinder (this )
83
88
84
89
// Service will broadcast via event bus when server start/stop
@@ -95,6 +100,12 @@ class FtpService : Service(), Runnable {
95
100
private var isStartedByTile = false
96
101
private lateinit var wakeLock: PowerManager .WakeLock
97
102
103
+ private fun publishEvent (event : FtpReceiverActions ) {
104
+ serviceScope.launch {
105
+ FtpEventBus .emit(event)
106
+ }
107
+ }
108
+
98
109
override fun onStartCommand (
99
110
intent : Intent ? ,
100
111
flags : Int ,
@@ -126,7 +137,7 @@ class FtpService : Service(), Runnable {
126
137
} else {
127
138
startForeground(NotificationConstants .FTP_ID , notification)
128
139
}
129
- return START_NOT_STICKY
140
+ return START_STICKY
130
141
}
131
142
132
143
override fun onCreate () {
@@ -143,6 +154,7 @@ class FtpService : Service(), Runnable {
143
154
144
155
@Suppress(" LongMethod" )
145
156
override fun run () {
157
+ wakeLock.acquire()
146
158
val preferences = PreferenceManager .getDefaultSharedPreferences(this )
147
159
FtpServerFactory ().run {
148
160
val connectionConfigFactory = ConnectionConfigFactory ()
@@ -175,7 +187,7 @@ class FtpService : Service(), Runnable {
175
187
}.onFailure {
176
188
log.warn(" failed to decrypt password in ftp service" , it)
177
189
AppConfig .toast(applicationContext, R .string.error)
178
- preferences.edit(). putString(KEY_PREFERENCE_PASSWORD , " " ). apply ()
190
+ preferences.edit { putString(KEY_PREFERENCE_PASSWORD , " " ) }
179
191
isPasswordProtected = false
180
192
}
181
193
}
@@ -224,9 +236,9 @@ class FtpService : Service(), Runnable {
224
236
)
225
237
fac.isImplicitSsl = true
226
238
} catch (e: GeneralSecurityException ) {
227
- preferences.edit(). putBoolean(KEY_PREFERENCE_SECURE , false ). apply ()
239
+ preferences.edit { putBoolean(KEY_PREFERENCE_SECURE , false ) }
228
240
} catch (e: IOException ) {
229
- preferences.edit(). putBoolean(KEY_PREFERENCE_SECURE , false ). apply ()
241
+ preferences.edit { putBoolean(KEY_PREFERENCE_SECURE , false ) }
230
242
}
231
243
}
232
244
fac.port = getPort(preferences)
@@ -237,23 +249,22 @@ class FtpService : Service(), Runnable {
237
249
server =
238
250
createServer().apply {
239
251
start()
240
- EventBus .getDefault()
241
- .post(
242
- if (isStartedByTile) {
243
- FtpReceiverActions .STARTED_FROM_TILE
244
- } else {
245
- FtpReceiverActions .STARTED
246
- },
247
- )
252
+ publishEvent(
253
+ if (isStartedByTile) {
254
+ FtpReceiverActions .STARTED_FROM_TILE
255
+ } else {
256
+ FtpReceiverActions .STARTED
257
+ },
258
+ )
248
259
}
249
260
}.onFailure {
250
- EventBus .getDefault().post(FtpReceiverActions .FAILED_TO_START )
261
+ wakeLock.release()
262
+ publishEvent(FtpReceiverActions .FAILED_TO_START )
251
263
}
252
264
}
253
265
}
254
266
255
267
override fun onDestroy () {
256
- wakeLock.release()
257
268
serverThread?.let { serverThread ->
258
269
serverThread.interrupt()
259
270
// wait 10 sec for server thread to finish
@@ -263,9 +274,13 @@ class FtpService : Service(), Runnable {
263
274
Companion .serverThread = null
264
275
}
265
276
server?.stop().also {
266
- EventBus .getDefault().post (FtpReceiverActions .STOPPED )
277
+ publishEvent (FtpReceiverActions .STOPPED )
267
278
}
268
279
}
280
+
281
+ if (wakeLock.isHeld) {
282
+ wakeLock.release()
283
+ }
269
284
}
270
285
271
286
// Restart the service if the app is closed from the recent list
@@ -312,39 +327,6 @@ class FtpService : Service(), Runnable {
312
327
const val TAG_STARTED_BY_TILE = " started_by_tile"
313
328
// attribute of action_started, used by notification
314
329
315
- private lateinit var _enabledCipherSuites : Array <String >
316
-
317
- init {
318
- _enabledCipherSuites =
319
- LinkedList <String >().apply {
320
- if (SDK_INT >= Q ) {
321
- add(" TLS_AES_128_GCM_SHA256" )
322
- add(" TLS_AES_256_GCM_SHA384" )
323
- add(" TLS_CHACHA20_POLY1305_SHA256" )
324
- }
325
- if (SDK_INT >= N ) {
326
- add(" TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" )
327
- add(" TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" )
328
- }
329
- if (SDK_INT >= LOLLIPOP ) {
330
- add(" TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" )
331
- add(" TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" )
332
- add(" TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" )
333
- add(" TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" )
334
- add(" TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" )
335
- add(" TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" )
336
- add(" TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" )
337
- add(" TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" )
338
- add(" TLS_RSA_WITH_AES_128_GCM_SHA256" )
339
- add(" TLS_RSA_WITH_AES_256_GCM_SHA384" )
340
- }
341
- if (SDK_INT < LOLLIPOP ) {
342
- add(" TLS_RSA_WITH_AES_128_CBC_SHA" )
343
- add(" TLS_RSA_WITH_AES_256_CBC_SHA" )
344
- }
345
- }.toTypedArray()
346
- }
347
-
348
330
/* *
349
331
* Return a list of available ciphers for ftpserver.
350
332
*
@@ -355,7 +337,34 @@ class FtpService : Service(), Runnable {
355
337
* @see [javax.net.ssl.SSLEngine]
356
338
*/
357
339
@JvmStatic
358
- val enabledCipherSuites = _enabledCipherSuites
340
+ val enabledCipherSuites: Array <String > =
341
+ LinkedList <String >().apply {
342
+ if (SDK_INT >= Q ) {
343
+ add(" TLS_AES_128_GCM_SHA256" )
344
+ add(" TLS_AES_256_GCM_SHA384" )
345
+ add(" TLS_CHACHA20_POLY1305_SHA256" )
346
+ }
347
+ if (SDK_INT >= N ) {
348
+ add(" TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" )
349
+ add(" TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" )
350
+ }
351
+ if (SDK_INT >= LOLLIPOP ) {
352
+ add(" TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" )
353
+ add(" TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" )
354
+ add(" TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" )
355
+ add(" TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" )
356
+ add(" TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" )
357
+ add(" TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" )
358
+ add(" TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" )
359
+ add(" TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" )
360
+ add(" TLS_RSA_WITH_AES_128_GCM_SHA256" )
361
+ add(" TLS_RSA_WITH_AES_256_GCM_SHA384" )
362
+ }
363
+ if (SDK_INT < LOLLIPOP ) {
364
+ add(" TLS_RSA_WITH_AES_128_CBC_SHA" )
365
+ add(" TLS_RSA_WITH_AES_256_CBC_SHA" )
366
+ }
367
+ }.toTypedArray()
359
368
360
369
private var serverThread: Thread ? = null
361
370
private var server: FtpServer ? = null
0 commit comments