-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from AlanCheen/feature/service-manager
Feature/service manager
- Loading branch information
Showing
10 changed files
with
202 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package me.yifeiyuan.flapdev | ||
|
||
import android.util.Log | ||
import me.yifeiyuan.flap.service.AdapterService | ||
|
||
/** | ||
* Created by 程序亦非猿 on 2022/8/16. | ||
*/ | ||
|
||
private const val TAG = "Services" | ||
|
||
class TestService : AdapterService { | ||
|
||
fun log(message: String){ | ||
Log.d(TAG, "log() called with: message = $message") | ||
} | ||
|
||
fun testResult():String{ | ||
return "TestService.testResult is called!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package me.yifeiyuan.flap.service | ||
|
||
/** | ||
* Created by 程序亦非猿 on 2022/8/16. | ||
* | ||
* @since 3.0.3 | ||
*/ | ||
interface AdapterService { | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
flap/src/main/java/me/yifeiyuan/flap/service/AdapterServiceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.yifeiyuan.flap.service | ||
|
||
/** | ||
* | ||
* 提供注册和发现 AdapterService 的能力 | ||
* | ||
* @see me.yifeiyuan.flap.FlapAdapter.registerAdapterService | ||
* @see me.yifeiyuan.flap.FlapAdapter.getAdapterService | ||
* | ||
* Created by 程序亦非猿 on 2022/8/16. | ||
* @since 3.0.3 | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
internal class AdapterServiceManager : IAdapterServiceManager { | ||
|
||
private val services = mutableMapOf<Class<*>, AdapterService>() | ||
|
||
/** | ||
* 有名字的 Service | ||
*/ | ||
private val namedServices = mutableMapOf<String, AdapterService>() | ||
|
||
override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>) { | ||
try { | ||
val service = serviceClass.getConstructor().newInstance() | ||
services[serviceClass] = service | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>, service: T) { | ||
services[serviceClass] = service | ||
} | ||
|
||
override fun <T : AdapterService> getAdapterService(serviceClass: Class<T>): T? { | ||
return services[serviceClass] as? T | ||
} | ||
|
||
override fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) { | ||
try { | ||
val service = serviceClass.getConstructor().newInstance() | ||
namedServices[serviceName] = service | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
override fun <T : AdapterService> registerAdapterService(serviceName: String, service: T) { | ||
namedServices[serviceName] = service | ||
} | ||
|
||
override fun <T : AdapterService> getAdapterService(serviceName: String): T? { | ||
return namedServices[serviceName] as? T | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
flap/src/main/java/me/yifeiyuan/flap/service/IAdapterServiceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package me.yifeiyuan.flap.service | ||
|
||
/** | ||
* Created by 程序亦非猿 on 2022/8/18. | ||
* | ||
* @since 3.0.3 | ||
*/ | ||
interface IAdapterServiceManager { | ||
|
||
fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>) | ||
|
||
fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>, service: T) | ||
|
||
fun <T : AdapterService> getAdapterService(serviceClass: Class<T>): T? | ||
|
||
fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) | ||
|
||
fun <T : AdapterService> registerAdapterService(serviceName: String, service: T) | ||
|
||
fun <T : AdapterService> getAdapterService(serviceName: String): T? | ||
} |