Share
Embed
Transcript
51 slides
-
{ “id”: “@e10dokup”, “name”: “Yoshihiro Wada”, “affiliations”: [ “CyberAgent Inc,
/ Ameba” ], “interested”: [ “camera”, “gadget”, “driving”, “motorsports” ] } -
Android FTP 車 立 一 見 行 手 Foreground Service
Android Developers FTP 日 3 -
Sony Transfer & Tagging add-on OSS 示 The Apache FtpServer
Android FTP ppareit/swiftp 見 1 FTP FTP 方 14 -
Sony Transfer & Tagging add-on OSS 示 The Apache FtpServer
Android FTP ppareit/swiftp 見 1 FTP FTP 方 16 -
Jetpack Compose IP ConnectivityManager IP 用 UI 26 private fun
getCurrentIpAddress(): String { val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val currentNetwork = connectivityManager.activeNetwork val linkProperties = connectivityManager.getLinkProperties(currentNetwork) return linkProperties?.linkAddresses?.first { it.address is Inet4Address }.toString() } -
曰 LinkProperties IP Android API 行 linkProperties 見 27 Ҿ༻:
https://developer.android.com/develop/connectivity/network-ops/reading-network-state https://developer.android.com/reference/android/net/LinkProperties#getLinkAddresses() -
Activity 用 override Service 用 30 class FtpService : Service()
{ override fun onBind(intent: Intent?): IBinder? { return null } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { // ࣮ࡍʹαʔϏεʹ͍ͤͨ͞ॲཧΛ͜͜ʹهड़͢Δ return START_STICKY } override fun onDestroy() { super.onDestroy() } } -
foregroundServiceType Android 14 API 34 色 Data sync Foreground Service
34 Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/service-types#data-sync -
見 Android Manifest Manifest Foreground Service 言 35 <!-- ݖݶͷՃ
--> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/> <application …> <!-- Foreground Serviceͷએݴ --> <service android:name=".service.FtpService" android:enabled="true" android:exported="false" android:foregroundServiceType=“dataSync"/> … </application> -
Service onStartCommand 行 用 ServiceCompat.startForeground 行 用 Foreground Service 37
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/launch#promote-service -
Foreground Service 用 Foreground Service 38 try { ServiceCompat.startForeground( this,
ID_FOREGROUND_SERVICE_FTP, createNotification(), // ௨Λ࡞Δؔʢ࣍ϖʔδʣ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC } else { 0 } // ManifestͰએݴͨ͠Foreground Service TypeΛͯࠐΉ ) // TODO: Foregroundʹग़དྷͨΒ͜͜ͰFTPαʔόΛ࣮ߦ͍ͨ͠ } catch (e: Exception) { Timber.e(e, "Failed to launch foreground service”) } -
用 Foreground Service 39 private fun createNotification(): Notification { val
channel = NotificationChannel( NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT ) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) // ௨νϟωϧͷઃఆ val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID).apply { setContentTitle("FTP Server") setContentText("FTP Server established") setSmallIcon(R.drawable.notification_icon) }.build() // ࣮ࡍͷ௨ͷੜ return notification } -
Activity ForegroundService 行 行 Foreground Service 40 private fun launchFtpService()
{ val intent = Intent(this, FtpService::class.java) startForegroundService(intent) } -
Listener UserManager 行 FTP 44 private fun startFtpServer() { //
Apache FTP ServerͷϑΝΫτϦ val serverFactory = FtpServerFactory() // ϙʔτͳͲΛࢦఆ͢ΔListenerΛ࡞Δ val ftpListener = createFtpListener() // Ϣʔβ໊ɺύεϫʔυΛཧ͢ΔUserManagerΛ࡞Δ val userManager = createUserManager() // ࡞ͬͨListenerɺUserManagerΛࢦఆ͠ɺFTPαʔόΛ࣮ࡍʹ࡞͠ɺ࣮ߦ͢Δ serverFactory.addListener("default", ftpListener) serverFactory.userManager = userManager ftpServer = serverFactory.createServer() ftpServer?.start() } -
Listener UserManager 行 FTP 45 private fun startFtpServer() { //
Apache FTP ServerͷϑΝΫτϦ val serverFactory = FtpServerFactory() // ϙʔτͳͲΛࢦఆ͢ΔListenerΛ࡞Δ val ftpListener = createFtpListener() // Ϣʔβ໊ɺύεϫʔυΛཧ͢ΔUserManagerΛ࡞Δ val userManager = createUserManager() // ࡞ͬͨListenerɺUserManagerΛࢦఆ͠ɺFTPαʔόΛ࣮ࡍʹ࡞͠ɺ࣮ߦ͢Δ serverFactory.addListener("default", ftpListener) serverFactory.userManager = userManager ftpServer = serverFactory.createServer() ftpServer?.start() } -
FTP Listener 自 Listener 46 private fun createFtpListener(): Listener {
val listenerFactory = ListenerFactory() // ࣮ࡍʹҾΛड͚औͬͯࢦఆ͢Δͱྑͦ͞͏ listenerFactory.port = <ϙʔτ൪߸> return listenerFactory.createListener() } -
Listener UserManager 行 FTP 47 private fun startFtpServer() { //
Apache FTP ServerͷϑΝΫτϦ val serverFactory = FtpServerFactory() // ϙʔτͳͲΛࢦఆ͢ΔListenerΛ࡞Δ val ftpListener = createFtpListener() // Ϣʔβ໊ɺύεϫʔυΛཧ͢ΔUserManagerΛ࡞Δ val userManager = createUserManager() // ࡞ͬͨListenerɺUserManagerΛࢦఆ͠ɺFTPαʔόΛ࣮ࡍʹ࡞͠ɺ࣮ߦ͢Δ serverFactory.addListener("default", ftpListener) serverFactory.userManager = userManager ftpServer = serverFactory.createServer() ftpServer?.start() } -
UserManager 48 private fun createUserManager(): UserManager { val userManagerFactory =
PropertiesUserManagerFactory() // ϢʔβใΛอ͢ΔϑΝΠϧͷ࡞ val profileFile = File(getExternalFilesDir(null)?.path + "/users.property") if (!profileFile.exists()) { profileFile.createNewFile() } userManagerFactory.file = profileFile userManagerFactory.passwordEncryptor = SaltedPasswordEncryptor() // Ϣʔβ໊ɺύεϫʔυɺݖݶͳͲͷઃఆ val baseUser = BaseUser() baseUser.name = <Ϣʔβ໊> baseUser.password = <ύεϫʔυ> baseUser.homeDirectory = getExternalFilesDir(Environment.DIRECTORY_DCIM)?.path baseUser.authorities = listOf(WritePermission()) val userManager = userManagerFactory.createUserManager() userManager.save(baseUser) return userManager } -
Listener UserManager 行 FTP 49 private fun startFtpServer() { //
Apache FTP ServerͷϑΝΫτϦ val serverFactory = FtpServerFactory() // ϙʔτͳͲΛࢦఆ͢ΔListenerΛ࡞Δ val ftpListener = createFtpListener() // Ϣʔβ໊ɺύεϫʔυΛཧ͢ΔUserManagerΛ࡞Δ val userManager = createUserManager() // ࡞ͬͨListenerɺUserManagerΛࢦఆ͠ɺFTPαʔόΛ࣮ࡍʹ࡞͠ɺ࣮ߦ͢Δ serverFactory.addListener("default", ftpListener) serverFactory.userManager = userManager ftpServer = serverFactory.createServer() ftpServer?.start() } -
Foreground Service 行 行 50 try { ServiceCompat.startForeground( this, ID_FOREGROUND_SERVICE_FTP,
createNotification(), // ௨Λ࡞Δؔʢ࣍ϖʔδʣ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC } else { 0 } // ManifestͰએݴͨ͠Foreground Service TypeΛͯࠐΉ ) // FTPαʔόͷ࣮ߦ startFtpServer() } catch (e: Exception) { Timber.e(e, "Failed to launch foreground service”) } Cyberduck Android FTP 子 -
Android FTP 立 子 走 FTP 自 大 Android Developers
大 GitHub OSS 51