-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
629 additions
and
37 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
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
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
20 changes: 20 additions & 0 deletions
20
sample/src/main/java/com/lzh/nonview/router/demo/action/ExecutorAction.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,20 @@ | ||
package com.lzh.nonview.router.demo.action | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import com.lzh.nonview.router.anno.RouteExecutor | ||
import com.lzh.nonview.router.anno.RouterRule | ||
import com.lzh.nonview.router.demo.executor.TestExecutor | ||
import com.lzh.nonview.router.demo.tools.SingleCache | ||
import com.lzh.nonview.router.route.ActionSupport | ||
|
||
/** | ||
* @author haoge on 2018/5/10 | ||
*/ | ||
@RouteExecutor(TestExecutor::class) | ||
@RouterRule("executor/switcher") | ||
class ExecutorAction : ActionSupport(){ | ||
override fun onRouteTrigger(context: Context?, bundle: Bundle?) { | ||
SingleCache.toast?.show("线程切换器测试动作路由被启动,启动线程为:${Thread.currentThread().name}" ) | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
sample/src/main/java/com/lzh/nonview/router/demo/executor/TestExecutor.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 com.lzh.nonview.router.demo.executor | ||
|
||
import java.util.concurrent.Executor | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
/** | ||
* @author haoge on 2018/5/10 | ||
*/ | ||
class TestExecutor : Executor{ | ||
|
||
val pool:ExecutorService = Executors.newSingleThreadExecutor { runnable -> | ||
val thread:Thread = Thread(runnable) | ||
thread.name = "action_annotation_executor" | ||
return@newSingleThreadExecutor thread | ||
} | ||
|
||
override fun execute(command: Runnable?) { | ||
pool.execute(command) | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
sample/src/main/java/com/lzh/nonview/router/demo/tools/EasyToast.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,79 @@ | ||
package com.lzh.nonview.router.demo.tools | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Looper | ||
import android.text.TextUtils | ||
import android.view.LayoutInflater | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
|
||
/** | ||
* 一个简单易用的Toast封装类。用于提供易用的、多样式的Toast组件进行使用 | ||
* | ||
* DATE: 2018/5/9 | ||
* | ||
* AUTHOR: haoge | ||
*/ | ||
class EasyToast private constructor(private val toast: Toast, private val tv: TextView?, private val isDefault: Boolean) { | ||
|
||
fun show(resId:Int) { | ||
show(SingleCache.context!!.getString(resId)) | ||
} | ||
|
||
fun show(message:String) { | ||
if (TextUtils.isEmpty(message)) { | ||
return | ||
} | ||
|
||
if (Looper.myLooper() == Looper.getMainLooper()) { | ||
showInternal(message) | ||
} else { | ||
SingleCache.mainHandler.post { showInternal(message) } | ||
} | ||
} | ||
|
||
private fun showInternal(message: String) { | ||
if (isDefault) { | ||
toast.setText(message) | ||
toast.show() | ||
} else { | ||
tv?.text = message | ||
toast.show() | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* 默认提供的Toast实例,在首次使用时进行加载。 | ||
*/ | ||
@JvmStatic | ||
val DEFAULT: EasyToast by lazy { default() } | ||
|
||
@JvmStatic | ||
private fun default(): EasyToast { | ||
checkThread() | ||
@SuppressLint("ShowToast") | ||
val toast = Toast.makeText(SingleCache.context, "", Toast.LENGTH_SHORT) | ||
return EasyToast(toast, null, true) | ||
} | ||
|
||
@JvmStatic | ||
fun create(layoutId: Int, tvId: Int, duration: Int): EasyToast { | ||
checkThread() | ||
val container = LayoutInflater.from(SingleCache.context).inflate(layoutId, null) | ||
val tv:TextView = container.findViewById(tvId) as TextView | ||
val toast = Toast(SingleCache.context) | ||
toast.view = container | ||
toast.duration = duration | ||
return EasyToast(toast, tv, false) | ||
} | ||
|
||
// Toast限制:在执行了Looper.prepare()的线程中才能创建Toast实例 | ||
// 这里加强限制,仅限主线程创建 | ||
private fun checkThread() { | ||
if (Looper.myLooper() != Looper.getMainLooper()) { | ||
throw RuntimeException("the toast-create method must called on main-thread") | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
sample/src/main/java/com/lzh/nonview/router/demo/tools/SingleCache.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,28 @@ | ||
package com.lzh.nonview.router.demo.tools | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
|
||
|
||
/** | ||
* 提供一些全局的常用数据进行使用。 | ||
* | ||
* DATE: 2018/5/9 | ||
* | ||
* AUTHOR: haoge | ||
*/ | ||
@SuppressLint("StaticFieldLeak") | ||
object SingleCache { | ||
internal var toast:EasyToast? = null | ||
internal var context:Context? = null | ||
var mainHandler: Handler = Handler(Looper.getMainLooper()) | ||
|
||
fun init(context: Context) { | ||
if (this.context == null) { | ||
this.context = context.applicationContext | ||
toast = EasyToast.DEFAULT | ||
} | ||
} | ||
} |
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