Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchangfeng committed Sep 8, 2018
0 parents commit 732e0db
Show file tree
Hide file tree
Showing 54 changed files with 1,303 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
33 changes: 33 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
defaultConfig {
applicationId "com.itscoder.allenwu.eventbuskt"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.itscoder.allenwu.eventbuskt

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("com.itscoder.allenwu.eventbuskt", appContext.packageName)
}
}
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itscoder.allenwu.eventbuskt">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
43 changes: 43 additions & 0 deletions app/src/main/java/com/itscoder/allenwu/eventbuskt/EventBus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.itscoder.allenwu.eventbuskt

import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

object EventBus {
private val executorService: ExecutorService = Executors.newCachedThreadPool()
private const val DEFAULT_TAG = ""
// 根据事件类型,找到所有对应的注解方法
private val subscriberMap = mutableMapOf<EventType,CopyOnWriteArrayList<Subscription>>()
private val methodFinder = SubscriberMethodFinder(subscriberMap)

/**
* 注册观察者,类似查找出当前 Activity 中被@Subscriber关键字来标记方法
*/
fun register(obj: Any) = executorService.execute(){
methodFinder.findSubscribeMethods(obj)
}

/**
* 分发事件
*/
@JvmOverloads
fun post(event: IEvent,tag: String = DEFAULT_TAG){
val eventType = EventType(event.javaClass,tag)
// 找出所有该事件的订阅者
val list = methodFinder.getMatchEventType(eventType)
// 分发
list?.let{
EventDispatcher.dispatchEvent(event, it)
}
}

/**
* 注销观察者
*/
fun unregister(obj: Any) = executorService.execute {
methodFinder.removeSubscriberMethod(obj)
}

fun getExecutorService() = executorService
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.itscoder.allenwu.eventbuskt

import android.os.Looper
import com.itscoder.allenwu.eventbuskt.handler.*
import java.util.concurrent.CopyOnWriteArrayList

object EventDispatcher {

private val postHandler = PostEventHandler()
private val mainHandler = MainEventHandler(Looper.getMainLooper())
private val asyncHandler = AsyncEventHandler()
private val bgHandler = BackgroundHandler()

fun dispatchEvent(event: IEvent, list: CopyOnWriteArrayList<Subscription>) =
list.forEach { it: Subscription ->
it.let {
val subscriber = it.subscriber.get()
subscriber?.let { subscriber: Any ->
val eventHandler = getEventHandler(it.threadMode)
eventHandler.handleEvent(it, event)
}
}
}

// 很据ThreadMode获取对应的事件处理器
private fun getEventHandler(mode: ThreadMode): EventHandler = when (mode) {
ThreadMode.POSTING -> postHandler
ThreadMode.ASYNC -> asyncHandler
ThreadMode.MAIN -> mainHandler
ThreadMode.BACKGROUND -> bgHandler
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/com/itscoder/allenwu/eventbuskt/EventType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.itscoder.allenwu.eventbuskt

/**
* 确定唯一Key,记录对应的事件执行方法
*/
internal class EventType(private val eventClass: Class<*>, private val tag: String){
override fun equals(other: Any?): Boolean {

if (this === other){
return true
}
// 判断是否为空,是否属于同一种类型
if (other == null || (other.javaClass.name !== this.javaClass.name)) {
return false
}

// 能执行到这里,说明 obj 和 this 同类且非 null
val eventType = other as EventType
val tagJudge = tag == eventType.tag
val eventJudge = eventClass.name == eventType.eventClass.name
// EventType是同一个类型
return tagJudge && eventJudge
}

override fun hashCode(): Int {
var hash = 7
hash = hash * 31 + eventClass.hashCode()
hash = hash * 31 + tag.hashCode()
return hash
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/itscoder/allenwu/eventbuskt/IEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.itscoder.allenwu.eventbuskt

/**
* 事件实现接口
*/
abstract class IEvent
31 changes: 31 additions & 0 deletions app/src/main/java/com/itscoder/allenwu/eventbuskt/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.itscoder.allenwu.eventbuskt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
EventBus.register(this)

findViewById<Button>(R.id.btn).setOnClickListener(View.OnClickListener {
EventBus.post(TestEvent("Hello,EventBus"))
})
}

@Subscriber("")
fun test(event: TestEvent){
Log.i("MainActivity",event.toString())
}

override fun onDestroy() {
super.onDestroy()
EventBus.unregister(this)
}
}
Loading

0 comments on commit 732e0db

Please sign in to comment.