├── .gitignore ├── .idea ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── dbnavigator.xml ├── encodings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── WIKIPAGE.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zcy │ │ └── pudding │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zcy │ │ │ └── pudding │ │ │ └── demo │ │ │ ├── JavaActivity.java │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zcy │ └── pudding │ └── ExampleUnitTest.kt ├── build.gradle ├── demogif ├── first3.gif ├── startActivity.gif └── withDialog.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pudding ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zcy │ │ └── pudding │ │ └── ExampleInstrumentedTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zcy │ │ └── pudding │ │ ├── Choco.kt │ │ ├── Pudding.kt │ │ └── SwipeDismissTouchListener.kt │ └── res │ ├── anim │ └── alerter_pulse.xml │ ├── drawable │ ├── alerter_ic_notifications.xml │ └── ic_event_available_black_24dp.xml │ ├── layout │ └── layout_choco.xml │ └── values │ ├── color.xml │ ├── dimens.xml │ ├── strings.xml │ └── style.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 14 | 15 | 16 | 17 | 18 | 24 | 25 | 29 | 30 | 31 | 33 | 34 | 35 | 36 |
37 | 38 | 39 | 40 | xmlns:android 41 | 42 | ^$ 43 | 44 | 45 | 46 |
47 |
48 | 49 | 50 | 51 | xmlns:.* 52 | 53 | ^$ 54 | 55 | 56 | BY_NAME 57 | 58 |
59 |
60 | 61 | 62 | 63 | .*:id 64 | 65 | http://schemas.android.com/apk/res/android 66 | 67 | 68 | 69 |
70 |
71 | 72 | 73 | 74 | .*:name 75 | 76 | http://schemas.android.com/apk/res/android 77 | 78 | 79 | 80 |
81 |
82 | 83 | 84 | 85 | name 86 | 87 | ^$ 88 | 89 | 90 | 91 |
92 |
93 | 94 | 95 | 96 | style 97 | 98 | ^$ 99 | 100 | 101 | 102 |
103 |
104 | 105 | 106 | 107 | .* 108 | 109 | ^$ 110 | 111 | 112 | BY_NAME 113 | 114 |
115 |
116 | 117 | 118 | 119 | .* 120 | 121 | http://schemas.android.com/apk/res/android 122 | 123 | 124 | ANDROID_ATTRIBUTE_ORDER 125 | 126 |
127 |
128 | 129 | 130 | 131 | .* 132 | 133 | .* 134 | 135 | 136 | BY_NAME 137 | 138 |
139 |
140 |
141 |
142 | 143 | 145 |
146 |
-------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/dbnavigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 41 | 42 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 94 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 zhaocy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pudding 2 | [![](https://jitpack.io/v/o0o0oo00/Pudding.svg)](https://jitpack.io/#o0o0oo00/Pudding) 3 | 4 | 5 | 代替Toast/SnackBar新方式,使用WindowManager 无需申请权限 6 | 7 | ### WIKI 8 | 9 | [中文说明](https://github.com/o0o0oo00/Pudding/blob/master/WIKIPAGE.md) 10 | 11 | ### Usage 12 | 13 | ```groovy 14 | allprojects { 15 | repositories { 16 | ... 17 | maven { url 'https://jitpack.io' } 18 | ``` 19 | 20 | ```groovy 21 | implementation 'com.github.o0o0oo00:Pudding:1.2.1' 22 | ``` 23 | 24 | 25 | 26 | GIF可能要等会就顺畅了,一开始可能会比较迟钝 (●゚ω゚●) 27 | 28 | 常见的可配置的几种形式 29 | 30 | Common configurable Pudding 31 | 32 | 33 | 34 | ```kotlin 35 | Pudding.create(this) { 36 | setTitle("This is Title") 37 | setText("this is text") 38 | }.show() 39 | ``` 40 | 41 | ```kotlin 42 | Pudding.create(this) { 43 | setChocoBackgroundColor(resources.getColor(R.color.colorAccent)) 44 | setTitleTypeface(Typeface.DEFAULT_BOLD) 45 | }.show() 46 | ``` 47 | 48 | ```kotlin 49 | Pudding.create(this) { 50 | setTitle("Choco Title") 51 | setText("this is text") 52 | setIcon(R.drawable.ic_event_available_black_24dp) 53 | }.show() 54 | ``` 55 | 56 | ```kotlin 57 | Pudding.create(this) { 58 | setTitle("Choco Title") 59 | setText("This is Text , it's very short and I don't like short \n This is Text , it's very short and I don't like short") 60 | onShow { 61 | Toast.makeText(this@MainActivity, "onShowListener", Toast.LENGTH_SHORT).show() 62 | } 63 | onDismiss { 64 | Toast.makeText(this@MainActivity, "onDismissListener", Toast.LENGTH_SHORT).show() 65 | } 66 | }.show() 67 | ``` 68 | 69 | 70 | 配合[FancyDialog](https://github.com/o0o0oo00/FancyDialog)可实现**Pudding**显示在**Dialog**的阴影之上 71 | 72 | With [FancyDialog](https://github.com/o0o0oo00/FancyDialog), Pudding can be displayed above the shadow of Dialog. 73 | 74 | 75 | 76 | 77 | 78 | 每个Activity拥有各自的Pudding,互相之间不影响 79 | 80 | Each Activity has its own Pudding, which does not affect each other. 81 | 82 | 83 | 84 | 85 | 86 | **Imitate [Alerter](https://github.com/Tapadoo/Alerter) and ☆⌒(*^-゜)v THX!! a lot** 87 | 88 | ### what's different 89 | 90 | Use activity decorView we can show a view on top of Activity。but when dialog is showing , its black background will cover the view , that is not cool. so i create this repository to solve this problem , and I hope it's useful to you 91 | 92 | ### Something new 93 | 94 | * Use DSL style to config `Choco` 95 | * Show Queue 96 | * Cover Dialog/PopWindow 97 | * Don't need to request permission 98 | 99 | 100 | #### TODO 101 | 102 | 1. 两种addView方式 103 | - [x] activity decorView 104 | - [x] windowManager 105 | - [x] 动画显示 106 | - [x] 生命周期控制 107 | - [x] 有权限/无权限情况 (考虑到这个需求不是很大,而且每个项目的权限适配都不一致,而且国产手机权限这一块参差不齐,所以决定不做这一块了,对功能也没影响) 108 | - [x] 各个版本的适配 109 | 110 | 2. 内部显示队列(考虑可以使用kotlin通道来实现) 111 | - [x] 上一条未消失时,后来一条等待 112 | - [x] 直接顶掉上一条,上一条渐变消失 113 | 114 | 3. 左右/上下滑动消失动画 115 | 116 | - [x] 上下消失 117 | - [x] 左右滑动消失 118 | 119 | 4. Pudding cover dialog 120 | 121 | - [x] 配合[FancyDialog](https://github.com/o0o0oo00/FancyDialog)实现 122 | 123 | 124 | 125 | #### 可能会有的疑问: 126 | 127 | - [x] **Pudding**使用的是WindowManager 难道不需要申请权限吗? 128 | 129 | - [x] **Pudding**使用`WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL`的层级低于系统级层级`TYPE_SYSTEM_ALERT`也就是在2000之下,是不需要申请权限的。 130 | 131 | - [x] 不同的Activity对应的WindowManager是否相同 132 | 133 | - [x] 不同的window,不同的WindowManager,相同的WindowManagerGlobal(最终实现view的添加、删除、更新) 134 | 135 | - [x] 不同的WindowManager为什么可以移除对方的View 136 | 137 | - [x] 因为所有的Activity共同只有一个WindowManagerGlobal(其内部持有所有添加的View的集合引用) 138 | 139 | - [X] 如何去维护一个Pudding队列 140 | 141 | - [x] 用一个`Map` 142 | - [X] 什么时间 去clear这个map (●゚ω゚●)挠头? 143 | when activity destory clean the key which is activity toString() 144 | -------------------------------------------------------------------------------- /WIKIPAGE.md: -------------------------------------------------------------------------------- 1 | # Window & WindowManager 2 | 3 | ## 手写一个Alert弹窗控件 4 | 5 | #### Window的添加与删除 6 | 7 | 每一个Window都对应着一个View和一个ViewRootImpl。Window和View通过ViewRootImpl来建立联系(ViewRootImpl是Window的实际展现形式,它是视图层级结构的最顶部。) 8 | 9 | 通过WindowManager的addView来实现在屏幕上添加一个View,但它只是一个接口,真正的实现是WindowManagerImpl 10 | 11 | WindowManagerImpl这种工作模式是典型的桥接模式,将所有的操作全部委托给WindowManagerGlobal这个**单例类**来实现,而其内部则是通过ViewRootImpl来更新界面并完成Window的添加过程。 12 | 13 | ##### 添加过程 14 | 15 | WindowManagerGlobal.java/addView 16 | 17 | ```kotlin 18 | root = new ViewRootImpl(view.getContext(), display); 19 | mViews.add(view); 20 | mRoots.add(root); 21 | root.setView(view, wparams, panelParentView); 22 | ``` 23 | 24 | 这里的这个root,是新创建出的`ViewRootImpl(view.getContext(), display);`前面说了,它其实是Window的实际展现形式,暂且理解为window本身,这样我们将view添加到window本身上了。 25 | 26 | 真正的调用顺序:**windowManager -> WindowManagerImpl -> WindowManagerGlobal -> ViewRootImpl** 27 | 28 | ##### 删除过程 29 | 30 | WindowManagerGlobal.java 31 | 32 | ```kotlin 33 | removeView(View view, boolean immediate) 34 | ``` 35 | 36 | 它有一个**immediate**参数表示是否是立即移除,其中**removeView**是异步方法**removeViewImmediate**为同步方法。方法内部拿到了view的index之后去`ArrayList mRoots`中寻找对应的**Index**。然后调用`removeViewLocked(index, immediate);`此时将View添加到了`ArraySet mDyingViews`中去,并没有立即执行删除,而是发送一个消息 37 | 38 | ``` 39 | boolean die(boolean immediate) { 40 | ... 41 | if (immediate && !mIsInTraversal) { 42 | doDie(); 43 | return false; 44 | } 45 | mHandler.sendEmptyMessage(MSG_DIE); 46 | } 47 | ``` 48 | 49 | 如果是同步删除,会直接移除,并刷新**mRoots**等属性列表 50 | 51 | 此时我们就会收到`onDetachedFromWindow()`这个方法的调用。 52 | 53 | ```kotlin 54 | void doDie() { 55 | ... 56 | if (mAdded) { 57 | dispatchDetachedFromWindow(); 58 | } 59 | 60 | WindowManagerGlobal.getInstance().doRemoveView(this); 61 | } 62 | ``` 63 | 64 | 65 | 66 | #### Window与Activity 67 | 68 | Activity的启动过程很复杂,最终会由**ActivityThread**中的`performLaunchActivity()`来完成整个启动过程,在这个方法内部会通过类加载器创建Activity的实例对象,并调用其`attach`方法为其关联运行过程中所依赖的一系列上下文环境变量 69 | 70 | 在**Activity**`attach()`中,新建一个`Window`实例作为自己的成员变量,它的类型为`PhoneWindow`,这是抽象类`Window`的一个子类。然后设置`mWindow`的`WindowManager`。 71 | 72 | ##### Activity的布局是如何加载到Window上的 73 | 74 | Activity中的window就是phoneWindow,而Activity中的setContentView就是调用的phoneWindow的setContentView。 75 | 76 | ````kotlin 77 | public void setContentView(@LayoutRes int layoutResID) { 78 | getWindow().setContentView(layoutResID); 79 | initWindowDecorActionBar(); 80 | } 81 | ```` 82 | 83 | PhoneWindow.java/setContentView 84 | 85 | ``` 86 | if (mContentParent == null) { 87 | installDecor(); 88 | } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) { 89 | mContentParent.removeAllViews(); 90 | } 91 | ``` 92 | 93 | PhoneWindow中 包含一个DecorView, 94 | 95 | 如果为null就新建一个,通过`installDecor()`,内部调用`generateDecor(-1);`创建mDecor。 96 | 97 | 如果mDecor不为null那么直接关联上phoneWindow`mDecor.setWindow(this);` 98 | 99 | 100 | 101 | 拿到了mDecor后去生成布局`mContentParent = generateLayout(mDecor);` 102 | 103 | 在`generateLayout`中,将Activity的布局addView到decorView上`mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);` 104 | 105 | `onResourcesLoaded`内部实现最终的`addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));` 106 | 107 | 108 | 109 | ##### DecorView添加到WindowManager上 110 | 111 | 在ActivityThread的handleResumeActivity方法中,首先会调用Activity的onResume方法,接着会调用Activity的`makeVisible()` 112 | 113 | 只有在Activity的`makeVisible()`被调用的时候,DecorView才会被addview进Windowmanager 114 | 115 | ```java 116 | void makeVisible() { 117 | if (!mWindowAdded) { 118 | ViewManager wm = getWindowManager(); 119 | wm.addView(mDecor, getWindow().getAttributes()); 120 | mWindowAdded = true; 121 | } 122 | mDecor.setVisibility(View.VISIBLE); 123 | } 124 | 125 | ``` 126 | 127 | 128 | 129 | ##### 不同的Activity对应的WindowManager是否相同 130 | 131 | - **不同**的`WindowManager`对象,**相同**的`WindowManagerGlobal` 132 | - `WindowManager` 只是一个`interface`,它真正的对象是`WindowManagerImpl`,而`WindowManagerImpl`中的addView,removeView等方法,又是直接调用了`WindowManagerGlobal`的相应方法。 133 | - `WindowManagerImpl`中依赖的`WindowManagerGlobal`也是单例模式创建的,所以app范围内,`WindowManagerGlobal`实例也只有一个 134 | 135 | 136 | 137 | #### Window 层级关系 138 | 139 | Dialog的弹窗需要Context,而这个Context必须是Activity,其内的Window包含一个token。如果没有这个Token就会报错。 140 | 141 | ``` 142 | “Caused by: android.view.WindowManager$BadToken-Exception: Unable to add window --token null is not for an application” 143 | ``` 144 | 145 | 而系统级的Window就不需要token。因此我们可以 146 | 147 | ```kotlin 148 | dialog.getWindow().setType(LayoutParams.TYPE_SYSTEM_OVERLAY) 149 | ``` 150 | 151 | 来实现一个系统级别的Dialog。But运行时权限如果不给的话,就不会显示出来,显然这是不被接受的。 152 | 153 | 我们知道View在window上的显示是有层级顺序的,层级高的覆盖在层级低的View之上。 154 | Activity的层级为 `TYPE_APPLICATION` 155 | 156 | PopupWindow的层级为`TYPE_APPLICATION_PANEL` 157 | 158 | Dialog的层级为`TYPE_SYSTEM_OVERLAY` 159 | 160 | 而我们只需要添加一个层级为`TYPE_APPLICATION_SUB_PANEL`的View到Window上面即可 161 | 162 | ##### 为什么不把View的层级设置的高一点呢? 163 | 164 | ```kotlin 165 | android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@308ebe3 -- permission denied for window type 2000 166 | ``` 167 | 168 | 我们在这里验证了View的层级为2000以下的时候是不需要申请权限的 169 | 170 | 那么我们要实现这个WindowManager添加的View显示在Dialog之上,就只剩剩下一个东西了,就是降低**Dialog**的背景等级 或者 改为使用**PopUpWindow**来代替**Dialog**。 171 | 172 | 我采用的是,改Dialog的背景阴影为Activity的透明度 173 | 174 | #### Window Leak 175 | 176 | 这里的处理方式是将传入的Activity绑定lifecycle,然后在onDestroy的时候,将**Pudding**`dismiss`掉 177 | 178 | **Pudding**类继承`LifecycleObserver` 179 | 180 | ```kotlin 181 | class Pudding : LifecycleObserver 182 | ``` 183 | 184 | 绑定生命周期,并且Activity的每个生命周期都可以通知到**Pudding** 185 | 186 | ```kotlin 187 | activity.lifecycle.addObserver(this) 188 | ``` 189 | 190 | 监听状态 191 | 192 | ``` 193 | // window manager must associate activity's lifecycle 194 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 195 | fun onDestroy(owner: LifecycleOwner) { 196 | choco.hide(windowManager ?: return, true) 197 | owner.lifecycle.removeObserver(this) // 移除监听事件 198 | } 199 | ``` 200 | 201 | [代码详情](https://github.com/o0o0oo00/Pudding) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 28 9 | defaultConfig { 10 | applicationId "com.zcy.pudding" 11 | minSdkVersion 21 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation project(':pudding') 27 | implementation 'com.github.o0o0oo00:FancyDialog:1.0.0' 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation 'androidx.appcompat:appcompat:1.1.0-alpha03' 31 | implementation 'androidx.core:core-ktx:1.1.0-alpha05' 32 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'androidx.test:runner:1.1.2-alpha02' 35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02' 36 | } 37 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zcy/pudding/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.zcy.pudding 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.zcy.pudding", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/zcy/pudding/demo/JavaActivity.java: -------------------------------------------------------------------------------- 1 | package com.zcy.pudding.demo; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import androidx.annotation.Nullable; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import com.zcy.pudding.Choco; 9 | import com.zcy.pudding.Pudding; 10 | import kotlin.Unit; 11 | import kotlin.jvm.functions.Function1; 12 | 13 | /** 14 | * @author: zhaochunyu 15 | * @description: ${DESP} 16 | * @date: 2019-07-01 17 | */ 18 | public class JavaActivity extends AppCompatActivity { 19 | 20 | @Override 21 | protected void onCreate(@Nullable Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | } 25 | 26 | public void click1(View view){ 27 | Pudding.create(this, new Function1() { 28 | @Override 29 | public Unit invoke(Choco choco) { 30 | choco.setText("Java Call"); 31 | return null; 32 | } 33 | }).show(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/zcy/pudding/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.zcy.pudding.demo 2 | 3 | import android.content.Intent 4 | import android.graphics.Typeface 5 | import android.os.Bundle 6 | import android.text.Editable 7 | import android.text.TextWatcher 8 | import android.view.View 9 | import android.widget.Toast 10 | import androidx.appcompat.app.AlertDialog 11 | import androidx.appcompat.app.AppCompatActivity 12 | import com.zcy.fancydialog.askDialog 13 | import com.zcy.pudding.Pudding 14 | import kotlinx.android.synthetic.main.activity_main.* 15 | 16 | class MainActivity : AppCompatActivity() { 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(R.layout.activity_main) 21 | 22 | editText.addTextChangedListener(object : TextWatcher { 23 | override fun afterTextChanged(s: Editable?) { 24 | Pudding.create(this@MainActivity) { 25 | setTitle("This is Title") 26 | }.show() 27 | } 28 | 29 | override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { 30 | } 31 | 32 | override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { 33 | } 34 | 35 | }) 36 | } 37 | 38 | // 默认形式 39 | fun click1(view: View) { 40 | Pudding.create(this) { 41 | setTitle("This is Title") 42 | setText("this is text") 43 | } 44 | .show() 45 | } 46 | 47 | // 设置背景颜色、字体 48 | fun click2(view: View) { 49 | Pudding.create(this) { 50 | setChocoBackgroundColor(resources.getColor(R.color.colorAccent)) 51 | setTitleTypeface(Typeface.DEFAULT_BOLD) 52 | }.show() 53 | } 54 | 55 | // 更改icon 56 | fun click3(view: View) { 57 | Pudding.create(this) { 58 | setTitle("Choco Title") 59 | setText("this is text") 60 | setIcon(R.drawable.ic_event_available_black_24dp) 61 | }.show() 62 | } 63 | 64 | // 长说明 文字形式 65 | fun click4(view: View) { 66 | Pudding.create(this) { 67 | setText(R.string.verbose_text_text) 68 | setIcon(R.drawable.ic_event_available_black_24dp) 69 | }.show() 70 | } 71 | 72 | // 永久显示 73 | fun click5(view: View) { 74 | Pudding.create(this) { 75 | setTitle("Choco Title") 76 | enableInfiniteDuration = true 77 | }.show() 78 | } 79 | 80 | // loading 形式 81 | fun click6(view: View) { 82 | Pudding.create(this) { 83 | setTitle("Choco Title") 84 | setEnableProgress(true) 85 | }.show() 86 | } 87 | 88 | // loading 形式 89 | fun click8(view: View) { 90 | Pudding.create(this) { 91 | setTitle("Choco Title") 92 | setText("This is Text , it's very short and I don't like short \n This is Text , it's very short and I don't like short") 93 | enableInfiniteDuration = true 94 | enableSwipeToDismiss() 95 | }.show() 96 | } 97 | 98 | // loading 形式 99 | fun click9(view: View) { 100 | Pudding.create(this) { 101 | setTitle("Choco Title") 102 | setText("This is Text , it's very short and I don't like short \n This is Text , it's very short and I don't like short") 103 | onShow { 104 | Toast.makeText(this@MainActivity, "onShowListener", Toast.LENGTH_SHORT).show() 105 | } 106 | onDismiss { 107 | Toast.makeText(this@MainActivity, "onDismissListener", Toast.LENGTH_SHORT).show() 108 | } 109 | }.show() 110 | } 111 | 112 | // loading 形式 113 | fun click7(view: View) { 114 | Pudding.create(this) { 115 | setTitle("Choco Title") 116 | setText("This is Text , it's very short and I don't like short \n This is Text , it's very short and I don't like short") 117 | setChocoBackgroundColor(resources.getColor(R.color.color_FFCC00)) 118 | enableInfiniteDuration = true 119 | addButton("OK", R.style.PuddingButton, View.OnClickListener { 120 | hide() 121 | Toast.makeText(this@MainActivity, "click ok", Toast.LENGTH_SHORT).show() 122 | }) 123 | addButton("NO", R.style.PuddingButton, View.OnClickListener { 124 | Toast.makeText(this@MainActivity, "click no", Toast.LENGTH_SHORT).show() 125 | }) 126 | 127 | }.show() 128 | } 129 | 130 | fun showAlertDialog(view: View) { 131 | AlertDialog.Builder(this) 132 | .setTitle("AlertDialog") 133 | .setMessage("normal AlertDialog will cover Pudding") 134 | .create().show() 135 | } 136 | 137 | fun showFancyDialog(view: View) { 138 | askDialog(supportFragmentManager) { 139 | mTitle = "FancyDialog" 140 | mMessage = "Use DSL it's so Awesome" 141 | onlySure = true 142 | lowerBackground = true 143 | } 144 | } 145 | 146 | // 启动一个Activity ,验证是否存在lack window exception 147 | fun startAnActivity(view: View) { 148 | startActivity(Intent(this, MainActivity::class.java)) 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 |