Repository: KaiXuan666/WindowTree
Branch: master
Commit: 3eb6054c0979
Files: 79
Total size: 116.4 KB
Directory structure:
gitextract_oo05fiuo/
├── .gitignore
├── .idea/
│ ├── codeStyles/
│ │ ├── Project.xml
│ │ └── codeStyleConfig.xml
│ ├── gradle.xml
│ ├── misc.xml
│ ├── runConfigurations.xml
│ └── vcs.xml
├── README.md
├── app/
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src/
│ ├── androidTest/
│ │ └── java/
│ │ └── com/
│ │ └── kaixuan/
│ │ └── windowtree/
│ │ └── ExampleInstrumentedTest.kt
│ ├── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── kaixuan/
│ │ │ └── windowtree/
│ │ │ ├── KotlinCommon.kt
│ │ │ ├── MainActivity.kt
│ │ │ ├── MyApp.kt
│ │ │ ├── WindowTag.kt
│ │ │ ├── activity/
│ │ │ │ ├── BaseActivity.kt
│ │ │ │ ├── EmptyActivity.kt
│ │ │ │ ├── NewsDetailActivity.kt
│ │ │ │ └── NewsListActivity.kt
│ │ │ └── fragment/
│ │ │ ├── ContactsFragment.kt
│ │ │ ├── DynamicFragment.kt
│ │ │ ├── MainFragment.kt
│ │ │ ├── VipFragment.kt
│ │ │ └── dynamic/
│ │ │ └── GoodFriendDynamicFragment.kt
│ │ └── res/
│ │ ├── drawable/
│ │ │ └── ic_launcher_background.xml
│ │ ├── drawable-v24/
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── layout/
│ │ │ ├── activity_main.xml
│ │ │ ├── activity_news_detail.xml
│ │ │ ├── activity_news_list.xml
│ │ │ ├── fragment_msg.xml
│ │ │ └── fragment_test.xml
│ │ ├── mipmap-anydpi-v26/
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ └── values/
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test/
│ └── java/
│ └── com/
│ └── kaixuan/
│ └── windowtree/
│ └── ExampleUnitTest.kt
├── build.gradle
├── gradle/
│ └── wrapper/
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── windowtree_annotation/
│ ├── .gitignore
│ ├── build.gradle
│ └── src/
│ └── main/
│ └── java/
│ └── com/
│ └── kaixuan/
│ └── windowtree_annotation/
│ ├── MyClass.java
│ ├── annotation/
│ │ ├── Window.java
│ │ └── WindowTypeAnnotation.java
│ ├── enums/
│ │ └── WindowType.java
│ └── model/
│ ├── WindowData.java
│ └── WindowMeta.java
├── windowtree_compiler/
│ ├── .gitignore
│ ├── build.gradle
│ └── src/
│ └── main/
│ └── java/
│ └── com/
│ └── kaixuan/
│ └── compiler/
│ └── WindowProcessor.java
└── windowtree_library/
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src/
├── androidTest/
│ └── java/
│ └── com/
│ └── kaixuan/
│ └── windowtreelibrary/
│ └── ExampleInstrumentedTest.java
├── main/
│ ├── AndroidManifest.xml
│ ├── java/
│ │ └── com/
│ │ └── kaixuan/
│ │ └── windowtreelibrary/
│ │ ├── WindowInfo.kt
│ │ ├── WindowTree.kt
│ │ ├── adapter/
│ │ │ └── DefaultJumpAdapter.kt
│ │ ├── model/
│ │ │ └── UnReadCountEvent.kt
│ │ ├── template/
│ │ │ ├── IJumpAdapter.kt
│ │ │ ├── ILogger.java
│ │ │ ├── IMain.java
│ │ │ └── IWindowTreeLoad.java
│ │ ├── thread/
│ │ │ ├── DefaultPoolExecutor.java
│ │ │ └── DefaultThreadFactory.java
│ │ └── util/
│ │ ├── ClassUtils.java
│ │ ├── Consts.kt
│ │ ├── DefaultLogger.java
│ │ ├── TextUtils.java
│ │ └── WindowTreeUtil.kt
│ └── res/
│ └── values/
│ └── strings.xml
└── test/
└── java/
└── com/
└── kaixuan/
└── windowtreelibrary/
└── ExampleUnitTest.java
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
*.iml
.gradle
/local.properties
/.idea/caches/build_file_checksums.ser
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
================================================
FILE: .idea/codeStyles/Project.xml
================================================
================================================
FILE: .idea/codeStyles/codeStyleConfig.xml
================================================
================================================
FILE: .idea/gradle.xml
================================================
================================================
FILE: .idea/misc.xml
================================================
================================================
FILE: .idea/runConfigurations.xml
================================================
================================================
FILE: .idea/vcs.xml
================================================
================================================
FILE: README.md
================================================
# WindowTree
只需使用注解,帮助你轻松维护树形界面的层级关系,管理你的界面结构,当你处于界面的任何位置时,都可以知道,我在哪里,我的父界面是谁,我的子界面是谁。甚至能够自动构建你的界面结构。
## 我们经常会遇一些这样的问题:
home界面下面有三个子界面A,B,C,他们三个业务功能内都会出现一个或多个未读消息或者通知,那么我们的home界面需要展示的未读消息数就应该是三个子界面未读消息数之和。
刚看到这个问题的时候会感觉很简单,我只需要在每次触发未读消息增减变化的时候,将本级界面和上一级界面的未读数量重新统计即可。当然,简单的需求是可以这么做,但是你有没有考虑过:
1. 当子界面A下级又有三个子界面A1,A2,A3的时候该怎么办?
2. 当用户没有一些界面的查看权限时,缺少了B和A2界面的浏览权限时,该怎么办?
3. 在界面构建时,如果发现用户没有A1,A2,A3的权限,那么其实A界面也不需要被构建,这样的工作,框架可以帮助我完成吗?
考虑到以上问题,如果用面向过程的思考方式出现一个解决一个,必然是事倍功半的,所以WindowTree就这样诞生了。
# WindowTree结构

windowTree将应用内的所有界面都视为一个Window,每个window都拥有与之关联的WindowInfo,借助WindowInfo,你可以完成灵活、强大的工作。
目前支持的功能:
1. 维护了父子界面的层级关系,你在应用的任意位置,都可以知道我的父界面是谁,我的子界面有几个等
2. 灵活的消息通讯,你可以在任意位置发送消息给任意界面,支持双向通讯,你可以知道接收者处理该消息的结果
3. 支持贯穿全局的未读消息小红点,一个父节点能够智能计算出他所拥有的所有子节点的未读消息之和,借助Kotlin的委托属性实现了数量变化实时通知更新!
4. 智能的权限管理,当用户未拥有a的子界面a1,a2的权限时,也将失去a的界面权限
5. 界面自动化搭建,你只需要使用注解定义好界面的层级关系,便可以调用api获取所有子界面,一个循环全部建立
6. 统一管理界面跳转,便于管理界面和埋点统计,减少重复代码
7. 支持Activity、Fragment、View、Dialog、PopupWindow...
# WindowTree使用
添加依赖项:
```
apply plugin: 'kotlin-kapt'
...
dependencies {
kapt 'com.kaixuan:windowtree-compiler:1.0.0'
implementation 'com.kaixuan:windowtree-library:1.0.0'
...
}
```
1. 在你应用的所有界面添加注解@Window,参数parentClass指定该界面的父节点(如当前是顶级节点,则不需要设置该属性),可选添加其他属性,index表示当前界面是父界面的第几个同类界面,name表示当前节点名字
```
@Window(parentClass = MainActivity::class,index = 2,name = "联系人")
class ContactsFragment : Fragment()
```
2. 在应用启动时,初始化WindowTree
```
WindowTree.init(MyApp.instance)
```
3.
- 使用kotlin时,可直接在界面类(Activity、Fragment等)中使用扩展属性mWindowInfo拿到当前界面对应的WindowInfo
- 使用java时,使用WindowTree.with(this)获取当前界面对应的WindowInfo
4. 如需接收消息,需要给当前节点设置setEventListener
```
mWindowInfo.setEventListener { sender, sendData ->
if (sendData is UnReadCountEvent){
tv_log.append("未读消息:${sender.name}的未读消息发生变化,数量变化=${sendData.change}\n")
updateUnReadCount()
return@setEventListener "ok 我已收到并处理完毕" // 支持返回给消息发送者一个回信
}
return@setEventListener null
}
```
windowTree支持消息接收者返回一个结果的应答,告知事件处理结果
5. 拿到我的父节点或子节点对象,使用当前的WindowInfo对象可以轻松找到父节点和子节点,如:
```
mWindowInfo.parent
mWindowInfo.child
```
6. 界面自动构建,如果你使用TabLayout管理你的界面,只需要以下代码即可将当前界面的所有子界面加入到Tab
```
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
mWindowInfo.jump(tab!!.position,WindowType.FRAGMENTV4)
}
})
mWindowInfo.filterChildByWindowType(WindowType.FRAGMENTV4).forEach { window ->
tabLayout.addTab(tabLayout.newTab().setText(window.name).setTag(window))
}
```
7. 界面跳转控制
```
mWindowInfo.jump(0,WindowType.FRAGMENTV4)
```
这段代码表示,跳转到我的第1个Fragment类型的子界面
DefaultJumpAdapter中默认实现了一些跳转逻辑,你可以继承它实现自己的特殊逻辑
如果你有更好的建议欢迎与我联系!thank you!kaixuanapp@163.com
github:https://github.com/KaiXuan666/WindowTree
csdn:https://blog.csdn.net/kaixuan_dashen
================================================
FILE: app/.gitignore
================================================
/build
================================================
FILE: app/build.gradle
================================================
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.kaixuan.windowtree"
minSdkVersion 15
targetSdkVersion 28
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(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
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'
implementation project(path: ':windowtree_annotation')
kapt project(':windowtree_compiler')
implementation project(':windowtree_library')
implementation 'com.android.support:design:28.0.0'
// kapt 'com.kaixuan:windowtree-compiler:1.0.0'
// implementation 'com.kaixuan:windowtree-library:1.0.0'
}
================================================
FILE: app/proguard-rules.pro
================================================
# 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
================================================
FILE: app/src/androidTest/java/com/kaixuan/windowtree/ExampleInstrumentedTest.kt
================================================
package com.kaixuan.windowtree
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.kaixuan.windowtree", appContext.packageName)
}
}
================================================
FILE: app/src/main/AndroidManifest.xml
================================================
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/KotlinCommon.kt
================================================
package com.kaixuan.windowtree
import android.widget.Toast
fun showToast(msg : String){
Toast.makeText(MyApp.instance,msg,Toast.LENGTH_SHORT).show()
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/MainActivity.kt
================================================
package com.kaixuan.windowtree
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.kaixuan.windowtree.activity.BaseActivity
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtree_annotation.enums.WindowType
import com.kaixuan.windowtreelibrary.WindowInfo
import com.kaixuan.windowtreelibrary.WindowTree
import com.kaixuan.windowtreelibrary.mWindowInfo
import com.kaixuan.windowtreelibrary.model.UnReadCountEvent
import kotlinx.android.synthetic.main.activity_main.*
@Window
class MainActivity : BaseActivity() {
lateinit var with: WindowInfo<*>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_log.movementMethod = ScrollingMovementMethod.getInstance();
btnInit.setOnClickListener {
init()
}
btnCreateLayout.setOnClickListener { _ ->
WindowTree.instance.hasPageAuthorityFun = { it == -1L }
if (WindowTree.hasInit) {
bindTabLayout()
initActivityButton()
} else {
showToast("请先初始化")
}
}
btnCreateLayout.setOnLongClickListener { _ ->
// 用户的页面权限,一般通过登录接口获取
val userAuthority = listOf(-1L, 1L, 2L, 3L, 4L)
WindowTree.instance.hasPageAuthorityFun = { userAuthority.contains(it) }
if (WindowTree.hasInit) {
bindTabLayout()
initActivityButton()
} else {
showToast("请先初始化")
}
true
}
btnGc.setOnClickListener { System.gc() }
btnDestroy.setOnClickListener {
release()
}
}
fun init() {
if (WindowTree.hasInit) {
showToast("已经初始化过")
return
}
WindowTree.init(MyApp.instance)
with = mWindowInfo
mWindowInfo.frameLayoutId = frameLayout.id
initEventListener()
}
fun initEventListener() {
with.setEventListener { sender, sendData ->
if (sendData is UnReadCountEvent) {
tv_log.append("未读消息:${sender.name}的未读消息发生变化,数量变化=${sendData.change}\n")
updateUnReadCount()
return@setEventListener null
}
when (sender) {
// 1、判断是自己的孩子发来的消息
in with.child -> {
when (sendData) {
// 2、判断发来消息的数据类型,你也可以定义msgCode或其他数据类型来进行判断,此处我为了偷懒
is String -> {
tv_log.append("子模块${sender.name}发来了消息,内容=${sendData}\n")
}
is Int -> {
}
}
}
}
return@setEventListener "ok 我已收到并处理完毕" // 支持返回给消息发送者一个回信
}
}
fun bindTabLayout() {
if (tabLayout.tabCount != 0) {
showToast("不能重复添加tab")
return
}
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
Log.e("onTabSelected", tab!!.tag.toString())
mWindowInfo.jump(tab!!.position, WindowType.FRAGMENTV4)
}
})
// 自动布局到TabLayout,两种方式都可以实现
// with.child.forEach {
// if (it.windowType == WindowType.FRAGMENTV4){
// tabLayout.addTab(tabLayout.newTab().setText(it.name))
// }
// }
mWindowInfo.filterChildByWindowType(WindowType.FRAGMENTV4).filter { WindowTree.hasAuthority(it.pageAuthority) }
.forEach { window ->
tabLayout.addTab(tabLayout.newTab().setText(window.name).setTag(window))
}
}
fun initActivityButton() {
llActivity.visibility = View.VISIBLE
if (llActivity.childCount == 0) {
// 过滤子Window自动进行布局
mWindowInfo.filterChildByWindowType(WindowType.ACTIVITY).forEach { window ->
llActivity.addView(Button(this).apply {
text = "打开 ${window.name}"
setOnClickListener { with.jump(window) } // 注:此处不能使用mWindowInfo获取当前windowInfo对象,因为此处的this指代的是View Button
}, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
}
/**
* 更新未读消息显示
*/
fun updateUnReadCount() {
val calcChildUnReadCount = mWindowInfo.calcChildUnReadCount()
supportActionBar!!.title = if (calcChildUnReadCount != 0) "新消息:$calcChildUnReadCount" else "WindowTree"
(0 until tabLayout.tabCount).forEach {
val windowInfo = tabLayout.getTabAt(it)!!.tag as WindowInfo<*>
val count = windowInfo.calcChildUnReadCount()
tabLayout.getTabAt(it)!!.text = windowInfo.name + if (count == 0) "" else "($count)"
}
(0 until llActivity.childCount).forEach {
val findChildByIndex = mWindowInfo.findChildByIndex(it, WindowType.ACTIVITY)!!
(llActivity.getChildAt(it) as Button).apply {
text = "打开 ${findChildByIndex.name}"
val count = findChildByIndex.calcChildUnReadCount()
if (count != 0) {
append("($count)")
}
}
}
}
override fun onBackPressed() {
super.onBackPressed()
}
fun release() {
supportActionBar!!.title = "WindowTree"
WindowTree.destroy()
llActivity.visibility = View.GONE
llActivity.removeAllViews()
tabLayout.removeAllTabs()
tabLayout.clearOnTabSelectedListeners()
frameLayout.removeAllViews()
System.gc()
tv_log.text = ""
}
override fun onDestroy() {
release()
super.onDestroy()
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/MyApp.kt
================================================
package com.kaixuan.windowtree
import android.app.Application
class MyApp : Application() {
companion object {
lateinit var instance : MyApp;
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/WindowTag.kt
================================================
package com.kaixuan.windowtree
class WindowTag(
var unReadMsgCount : Int = 0,
var pageAuthority : Long = 0
){
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/activity/BaseActivity.kt
================================================
package com.kaixuan.windowtree.activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
open class BaseActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(this.javaClass.simpleName,"onCreate")
}
override fun onStart() {
super.onStart()
Log.i(this.javaClass.simpleName,"onStart")
}
override fun onResume() {
super.onResume()
Log.i(this.javaClass.simpleName,"onResume")
}
override fun onPause() {
super.onPause()
Log.i(this.javaClass.simpleName,"onPause")
}
override fun onStop() {
super.onStop()
Log.i(this.javaClass.simpleName,"onStop")
}
override fun onDestroy() {
super.onDestroy()
Log.i(this.javaClass.simpleName,"onDestroy")
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/activity/EmptyActivity.kt
================================================
package com.kaixuan.windowtree.activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
@Window(parentClassName = "com.kaixuan.windowtree.MainActivity",name = "空白界面",index = 2)
class EmptyActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_test)
supportActionBar!!.title = mWindowInfo.name
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/activity/NewsDetailActivity.kt
================================================
package com.kaixuan.windowtree.activity
import android.os.Bundle
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
import com.kaixuan.windowtreelibrary.model.UnReadCountEvent
import kotlinx.android.synthetic.main.activity_news_detail.*
@Window(parentClass = NewsListActivity::class,name = "新闻详情",index = 3)
class NewsDetailActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_news_detail)
supportActionBar!!.title = mWindowInfo.name + mWindowInfo.getTag()
tv_title.text = mWindowInfo.getTag() as String
mWindowInfo.unReadMsgCount = mWindowInfo.bundle.getInt("unReadCount")
tv_unReadCount.text = "未读数量:${mWindowInfo.unReadMsgCount}"
mWindowInfo.setEventListener { sender, sendData ->
if (sendData is UnReadCountEvent){
tv_unReadCount.text = "未读数量:${mWindowInfo.unReadMsgCount}"
}
}
btn_unReadAdd.setOnClickListener { mWindowInfo.unReadMsgCount ++ }
btn_dealUnRead.setOnClickListener { mWindowInfo.unReadMsgCount = 0 }
}
override fun onDestroy() {
mWindowInfo.release()
super.onDestroy()
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/activity/NewsListActivity.kt
================================================
package com.kaixuan.windowtree.activity
import android.os.Bundle
import android.support.v7.recyclerview.extensions.ListAdapter
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import com.kaixuan.windowtree.MainActivity
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.activity_news_list.*
@Window(parentClass = MainActivity::class,name = "新闻列表",index = 1)
class NewsListActivity : BaseActivity() {
val newsList = 0..200
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_news_list)
supportActionBar!!.title = mWindowInfo.name
recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
recyclerView.adapter = object : RecyclerView.Adapter(){
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(LayoutInflater.from(this@NewsListActivity).inflate(R.layout.support_simple_spinner_dropdown_item,p0,false)){}
}
override fun getItemCount(): Int = newsList.count()
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
(p0.itemView as TextView).let {textView ->
textView.text = "Hello,这里是新闻啊啊啊啊啊啊啊啊 ${p1}"
textView.setOnClickListener {
val childByIndex = mWindowInfo.findChildByIndex(0)!!
childByIndex.setTag(textView.text.toString())
childByIndex.bundle.putInt("unReadCount",p1)
mWindowInfo.jump(childByIndex) }
}
}
}
}
override fun onDestroy() {
mWindowInfo.release()
super.onDestroy()
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/fragment/ContactsFragment.kt
================================================
package com.kaixuan.windowtree.fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kaixuan.windowtree.MainActivity
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.fragment_test.*
@Window(parentClass = MainActivity::class,index = 2,name = "联系人")
class ContactsFragment : Fragment() {
var mView : View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
mView ?: inflater.inflate(R.layout.fragment_test,container,false).apply { mView = this }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tv_title.text = "我是" + mWindowInfo.getClazz()!!.simpleName
btn_send.setOnClickListener {
mWindowInfo.unReadMsgCount ++
val response = mWindowInfo.sendData("hello,我是${javaClass.simpleName}", mWindowInfo.parent!!)
tv_log.append("收到了回信:${mWindowInfo.parent!!.getClazz()!!.simpleName}:$response\n")
}
btn_resetUnReadCount.setOnClickListener { mWindowInfo.unReadMsgCount = 0 }
}
override fun onDestroyView() {
mWindowInfo.release()
super.onDestroyView()
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/fragment/DynamicFragment.kt
================================================
package com.kaixuan.windowtree.fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kaixuan.windowtree.MainActivity
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.fragment_test.*
@Window(parentClass = MainActivity::class,index = 3,name = "动态")
class DynamicFragment : Fragment() {
var mView : View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
mView ?: inflater.inflate(R.layout.fragment_test,container,false).apply { mView = this }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
tv_log.movementMethod = ScrollingMovementMethod.getInstance();
tv_title.text = "我是" + mWindowInfo.getClazz()!!.simpleName
btn_send.setOnClickListener {
mWindowInfo.unReadMsgCount ++
val response = mWindowInfo.sendData("hello,我是${javaClass.simpleName}", mWindowInfo.parent!!)
tv_log.append("收到了回信:${mWindowInfo.parent!!.getClazz()!!.simpleName}:$response\n")
}
btn_resetUnReadCount.setOnClickListener { mWindowInfo.unReadMsgCount = 0 }
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/fragment/MainFragment.kt
================================================
package com.kaixuan.windowtree.fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kaixuan.windowtree.MainActivity
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.WindowInfo
import com.kaixuan.windowtreelibrary.WindowTree
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.fragment_test.*
@Window(parentClass = MainActivity::class,index = 1,name = "主页")
class MainFragment : Fragment() {
lateinit var with : WindowInfo
var mView : View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
mView ?: inflater.inflate(R.layout.fragment_test,container,false).apply { mView = this }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
with = WindowTree.with(this)!!
val t = with.setTag("")
tv_log.movementMethod = ScrollingMovementMethod.getInstance();
tv_title.text = "我是" + mWindowInfo!!.getClazz()!!.simpleName
btn_send.setOnClickListener {
mWindowInfo.unReadMsgCount ++
val response = with.sendData("hello,我是${javaClass.simpleName}", with.parent!!)
tv_log.append("收到了回信:${with.parent!!.getClazz()!!.simpleName}:${response}\n")
}
btn_resetUnReadCount.setOnClickListener { mWindowInfo.unReadMsgCount = 0 }
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/fragment/VipFragment.kt
================================================
package com.kaixuan.windowtree.fragment
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kaixuan.windowtree.MainActivity
import com.kaixuan.windowtree.R
import com.kaixuan.windowtree_annotation.annotation.Window
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.fragment_test.*
@Window(parentClass = MainActivity::class,index = 3,name = "Vip隐藏界面",pageAuthority = 1)
class VipFragment : Fragment() {
var mView : View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
mView ?: inflater.inflate(R.layout.fragment_test,container,false).apply { mView = this }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
tv_log.movementMethod = ScrollingMovementMethod.getInstance();
tv_title.text = "我是Vip界面,一般人没有权限打开我"
btn_send.setOnClickListener {
mWindowInfo.unReadMsgCount ++
val response = mWindowInfo.sendData("hello,我是${javaClass.simpleName},pageAuthority=${mWindowInfo.pageAuthority}", mWindowInfo.parent!!)
tv_log.append( "收到了回信:${mWindowInfo.parent!!.getClazz()!!.simpleName}:$response\n")
}
btn_resetUnReadCount.setOnClickListener { mWindowInfo.unReadMsgCount = 0 }
}
}
================================================
FILE: app/src/main/java/com/kaixuan/windowtree/fragment/dynamic/GoodFriendDynamicFragment.kt
================================================
package com.kaixuan.windowtree.fragment.dynamic
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kaixuan.windowtree.R
import com.kaixuan.windowtreelibrary.mWindowInfo
import kotlinx.android.synthetic.main.fragment_msg.*
class GoodFriendDynamicFragment : Fragment() {
var mView : View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
mView ?: inflater.inflate(R.layout.fragment_msg,container,false).apply { mView = this }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
updateMsgCount()
mWindowInfo.setEventListener { sender, sendData ->
mWindowInfo.unReadMsgCount++
updateMsgCount()
}
}
fun updateMsgCount(){
tv_msg_tips.text = "我有${mWindowInfo.unReadMsgCount}条未读消息"
}
}
================================================
FILE: app/src/main/res/drawable/ic_launcher_background.xml
================================================
================================================
FILE: app/src/main/res/drawable-v24/ic_launcher_foreground.xml
================================================
================================================
FILE: app/src/main/res/layout/activity_main.xml
================================================
================================================
FILE: app/src/main/res/layout/activity_news_detail.xml
================================================
================================================
FILE: app/src/main/res/layout/activity_news_list.xml
================================================
================================================
FILE: app/src/main/res/layout/fragment_msg.xml
================================================
================================================
FILE: app/src/main/res/layout/fragment_test.xml
================================================
================================================
FILE: app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
================================================
================================================
FILE: app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
================================================
================================================
FILE: app/src/main/res/values/colors.xml
================================================
#008577
#00574B
#D81B60
================================================
FILE: app/src/main/res/values/strings.xml
================================================
WindowTree
================================================
FILE: app/src/main/res/values/styles.xml
================================================
================================================
FILE: app/src/test/java/com/kaixuan/windowtree/ExampleUnitTest.kt
================================================
package com.kaixuan.windowtree
import org.junit.Test
import org.junit.Assert.*
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
================================================
FILE: build.gradle
================================================
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.novoda:bintray-release:0.9'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
// maven { url "https://dl.bintray.com/kaixuan666/maven" }
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//统一声明配置
ext {
repoName = 'maven'
userOrg = 'kaixuan666'
groupId = 'com.kaixuan'
uploadName = 'windowtree'
publishVersion = '1.0.0'
desc = 'WindowTree'
website = 'https://github.com/KaiXuan666/WindowTree'
licences = ['Apache-2.0']
}
================================================
FILE: gradle/wrapper/gradle-wrapper.properties
================================================
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
================================================
FILE: gradle.properties
================================================
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
================================================
FILE: gradlew
================================================
#!/usr/bin/env sh
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"
================================================
FILE: gradlew.bat
================================================
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
================================================
FILE: settings.gradle
================================================
include ':app', ':windowtree_library', ':windowtree_compiler', ':windowtree_annotation'
================================================
FILE: windowtree_annotation/.gitignore
================================================
/build
================================================
FILE: windowtree_annotation/build.gradle
================================================
apply plugin: 'java-library'
apply plugin: 'com.novoda.bintray-release'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
sourceCompatibility = "7"
targetCompatibility = "7"
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
//添加
publish {
repoName = rootProject.repoName
artifactId = 'windowtree-annotation'
userOrg = rootProject.userOrg
groupId = rootProject.groupId
uploadName = rootProject.uploadName
publishVersion = rootProject.publishVersion
desc = rootProject.desc
website = rootProject.website
licences = rootProject.licences
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/MyClass.java
================================================
package com.kaixuan.windowtree_annotation;
public class MyClass {
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/annotation/Window.java
================================================
package com.kaixuan.windowtree_annotation.annotation;
import com.kaixuan.windowtree_annotation.model.WindowData;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface Window {
/**
*
* @return 父class
*/
Class parentClass() default Window.class;
/**
*
* @return 父class完整类名,该属性和parentClass只需要任选实现一个即可,该属性便于模块化场景下无法直接引用目标类的情况
*/
String parentClassName() default "";
/**
* 功能名称 可空
* @return
*/
String name() default "";
/**
* 该窗口是父节点的第几个子窗口
* @return
*/
int index() default 0;
/**
* 该窗口在你的项目中,页面权限id是多少
* 该值默认为-1时,即该页面无权限控制,任何人都允许加载
* @return
*/
long pageAuthority() default -1;
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/annotation/WindowTypeAnnotation.java
================================================
package com.kaixuan.windowtree_annotation.annotation;
public @interface WindowTypeAnnotation {
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/enums/WindowType.java
================================================
package com.kaixuan.windowtree_annotation.enums;
public enum WindowType {
ACTIVITY(0, "android.app.Activity"),
FRAGMENT(1, "android.app.Fragment"),
FRAGMENTV4(2, "android.support.v4.app.Fragment"),
VIEW(3, "android.view.View"),
DIALOG(4, "android.app.Dialog"),
POPUPWINDOW(5, "android.widget.PopupWindow"),
UNKNOWN(-1, "Unknown route type");
int id;
String className;
WindowType(int id, String className) {
this.id = id;
this.className = className;
}
public int getId() {
return id;
}
public WindowType setId(int id) {
this.id = id;
return this;
}
public String getClassName() {
return className;
}
public WindowType setClassName(String className) {
this.className = className;
return this;
}
public static WindowType parse(String name) {
for (WindowType windowType : WindowType.values()) {
if (windowType.getClassName().equals(name)) {
return windowType;
}
}
return UNKNOWN;
}
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/model/WindowData.java
================================================
package com.kaixuan.windowtree_annotation.model;
public class WindowData {
}
================================================
FILE: windowtree_annotation/src/main/java/com/kaixuan/windowtree_annotation/model/WindowMeta.java
================================================
package com.kaixuan.windowtree_annotation.model;
import com.kaixuan.windowtree_annotation.enums.WindowType;
import java.util.ArrayList;
import java.util.List;
public class WindowMeta {
private Class> clazz = null;
private String clazzName = "";
public WindowMeta parent = null;
public List child = new ArrayList<>();
public String name;
public int index;
private WindowType windowType;
public long pageAuthority;
public WindowMeta(Class> clazz, String clazzName, WindowMeta parent, String name, int index) {
this(clazz,clazzName,parent,name,index,WindowType.UNKNOWN);
}
public WindowMeta(Class> clazz, String clazzName, WindowMeta parent, String name, int index, WindowType windowType) {
this(clazz,clazzName,parent,name,index,WindowType.UNKNOWN,-1);
}
public WindowMeta(Class> clazz, String clazzName, WindowMeta parent, String name, int index, WindowType windowType,long pageAuthority) {
this.index = index;
this.name = name;
this.clazz = clazz;
this.clazzName = clazzName;
this.parent = parent;
this.windowType = windowType;
this.pageAuthority = pageAuthority;
}
public void addChild(String clazzName,String name,int index,WindowType windowType){
try {
child.add(new WindowMeta(Class.forName(clazzName),clazzName,this,name,index,windowType));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Class> getClazz() {
return clazz;
}
public WindowMeta setClazz(Class> clazz) {
this.clazz = clazz;
return this;
}
public String getClazzName() {
return clazzName;
}
public WindowMeta setClazzName(String clazzName) {
this.clazzName = clazzName;
return this;
}
public WindowType getWindowType() {
return windowType;
}
public WindowMeta setWindowType(WindowType windowType) {
this.windowType = windowType;
return this;
}
@Override
public String toString() {
return "WindowMeta{" +
"clazz=" + clazz +
", clazzName='" + clazzName + '\'' +
", name='" + name + '\'' +
", index=" + index +
", windowType=" + windowType +
", pageAuthority=" + pageAuthority +
'}';
}
}
================================================
FILE: windowtree_compiler/.gitignore
================================================
/build
================================================
FILE: windowtree_compiler/build.gradle
================================================
apply plugin: 'java-library'
apply plugin:'java'
apply plugin: 'com.novoda.bintray-release'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.auto.service:auto-service:1.0-rc3'
implementation project(path: ':windowtree_annotation')
implementation 'com.squareup:javapoet:1.8.0'
}
sourceCompatibility = "7"
targetCompatibility = "7"
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
//添加
publish {
repoName = rootProject.repoName
artifactId = 'windowtree-compiler'
userOrg = rootProject.userOrg
groupId = rootProject.groupId
uploadName = rootProject.uploadName
publishVersion = rootProject.publishVersion
desc = rootProject.desc
website = rootProject.website
licences = rootProject.licences
}
================================================
FILE: windowtree_compiler/src/main/java/com/kaixuan/compiler/WindowProcessor.java
================================================
package com.kaixuan.compiler;
import com.google.auto.service.AutoService;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import com.kaixuan.windowtree_annotation.enums.WindowType;
import com.kaixuan.windowtree_annotation.model.WindowMeta;
import com.kaixuan.windowtree_annotation.annotation.Window;
import com.squareup.javapoet.*;
import java.io.IOException;
import java.util.*;
@AutoService(Processor.class)
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes("com.kaixuan.windowtree_annotation.annotation.Window")
public class WindowProcessor extends AbstractProcessor {
/**
* 文件相关的辅助类
*/
private Filer mFiler;
/**
* 元素相关的辅助类
*/
private Elements mElementUtils;
/**
* 日志相关的辅助类
*/
private Messager mMessager;
/**
* 日志相关的辅助类
*/
private Types types;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
mFiler = processingEnvironment.getFiler();
types = processingEnvironment.getTypeUtils();
mElementUtils = processingEnvironment.getElementUtils();
mMessager = processingEnvironment.getMessager();
mMessager.printMessage(Diagnostic.Kind.WARNING, "init : ");
}
@Override
public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {
Map> stringListMap = parseWindows(roundEnvironment.getElementsAnnotatedWith(Window.class));
List main = new ArrayList<>();
for (String s : stringListMap.keySet()) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("loadWindowTree")//定义方面名
.addModifiers(Modifier.PUBLIC)//定义修饰符
.addAnnotation(Override.class)
.returns(void.class)//定义返回结果
.addParameter(ClassName.get("com.kaixuan.windowtreelibrary","WindowInfo"), "currentWindowMeta");//添加方法参数
List windowMetas = stringListMap.get(s);
windowMetas.sort(new Comparator() {
@Override
public int compare(WindowMeta windowMeta, WindowMeta t1) {
return windowMeta.index - t1.index;
}
});
mMessager.printMessage(Diagnostic.Kind.WARNING, "windowMetas windowMetas : " + windowMetas.toString());
for (WindowMeta meta : windowMetas) {
mMessager.printMessage(Diagnostic.Kind.WARNING, "windowMetas meta.index : " + meta.index);
builder.addStatement("currentWindowMeta.addChild($S,$S,$L,$L,$L)", meta.getClazzName(),meta.name,meta.index,meta.getWindowType(),meta.pageAuthority);//添加方法内容
}
MethodSpec methodSpec = builder.addException(ClassName.get(ClassNotFoundException.class))
.build();
String tempClass = s.substring(s.lastIndexOf(".") + 1) + "$Gen";
TypeSpec finderClass = TypeSpec.classBuilder(tempClass)
.addSuperinterface(ClassName.get(mElementUtils.getTypeElement("com.kaixuan.windowtreelibrary.template.IWindowTreeLoad")))
.addModifiers(Modifier.PUBLIC)
// .addSuperinterface(ParameterizedTypeName.get(TypeUtil.INJECTOR, TypeName.get(mClassElement.asType())))
.addMethod(methodSpec)
.build();
// 创建Java文件
main.add("com.kaixuan.windowtree.windows." + tempClass );
JavaFile javaFile = JavaFile.builder("com.kaixuan.windowtree.windows", finderClass)
.addStaticImport(WindowType.class, "*").build();
try {
javaFile.writeTo(mFiler);
} catch (IOException e) {
e.printStackTrace();
}
}
ParameterizedTypeName map = ParameterizedTypeName.get(ClassName.get(Map.class)
, ClassName.get(String.class)
, ParameterizedTypeName.get(
ClassName.get(Class.class),
WildcardTypeName.subtypeOf(ClassName.get(mElementUtils.getTypeElement("com.kaixuan.windowtreelibrary.template.IWindowTreeLoad")))
));
MethodSpec.Builder builder = MethodSpec.methodBuilder("getAllGeneratedFile")
.addModifiers(Modifier.PUBLIC)//定义修饰符
.addAnnotation(Override.class)
.returns(ParameterizedTypeName.get(ClassName.get(Map.class)
, ClassName.get(String.class)
, ParameterizedTypeName.get(
ClassName.get(Class.class),
WildcardTypeName.subtypeOf(ClassName.get(mElementUtils.getTypeElement("com.kaixuan.windowtreelibrary.template.IWindowTreeLoad")))
)));//定义返回结果
ClassName mapNew = ClassName.get("java.util", "HashMap");
builder.addStatement("$T map = new $T()",map,mapNew);
for (String s : main) {
builder.addStatement("map.put($S,$L.class)",s,s);
}
builder.addStatement("return map");
TypeSpec mainClass = TypeSpec.classBuilder( "Main$Gen")
.addSuperinterface(ClassName.get(mElementUtils.getTypeElement("com.kaixuan.windowtreelibrary.template.IMain")))
.addModifiers(Modifier.PUBLIC)
// .addSuperinterface(ParameterizedTypeName.get(TypeUtil.INJECTOR, TypeName.get(mClassElement.asType())))
.addMethod(builder.build())
.build();
JavaFile javaFile = JavaFile.builder("com.kaixuan.windowtree.windows", mainClass).build();
try {
javaFile.writeTo(mFiler);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
private Map> parseWindows(Set extends Element> routeElements) {
// 以父节点类名为key平铺存储所有WindowMeta
Map> map = new HashMap<>();
TypeMirror type_Activity = mElementUtils.getTypeElement(WindowType.ACTIVITY.getClassName()).asType();
TypeMirror type_Fragment = mElementUtils.getTypeElement(WindowType.FRAGMENT.getClassName()).asType();
TypeMirror type_Fragmentv4 = mElementUtils.getTypeElement(WindowType.FRAGMENTV4.getClassName()).asType();
TypeMirror type_View = mElementUtils.getTypeElement(WindowType.VIEW.getClassName()).asType();
TypeMirror type_Dialog = mElementUtils.getTypeElement(WindowType.DIALOG.getClassName()).asType();
TypeMirror type_PopupWindow = mElementUtils.getTypeElement(WindowType.POPUPWINDOW.getClassName()).asType();
for (Element routeElement : routeElements) {
Window annotation = routeElement.getAnnotation(Window.class);
if (routeElement.getKind() == ElementKind.CLASS) {
mMessager.printMessage(Diagnostic.Kind.WARNING, "annotation : " + annotation.toString());
mMessager.printMessage(Diagnostic.Kind.WARNING, "annotation info : " + annotation.name() + annotation.index());
String parentName = annotation.parentClassName();
if (parentName.isEmpty()) {
try {
mMessager.printMessage(Diagnostic.Kind.WARNING, "parentClass : " + annotation.parentClass());
} catch (MirroredTypeException e) {
parentName = e.getTypeMirror().toString();
// mMessager.printMessage(Diagnostic.Kind.WARNING,"e.getTypeMirror() : " + e.getTypeMirror().toString());
}
}
List windowMetas = map.get(parentName);
if (windowMetas == null) {
windowMetas = new ArrayList<>();
map.put(parentName, windowMetas);
}
// routeElement.toString()
WindowMeta windowMeta = new WindowMeta