├── .gitignore
├── LICENSE.txt
├── README.md
├── build.gradle
├── build.sh
├── clean.sh
├── cleanCache.sh
├── floatwindow
├── .classpath
├── .project
├── AndroidManifest.xml
├── build.gradle
├── lint.xml
├── maven.gradle
├── proguard-rules.pro
├── project.properties
└── src
│ └── com
│ └── yhao
│ └── floatwindow
│ ├── FloatActivity.java
│ ├── FloatWindow.java
│ ├── enums
│ ├── EMoveType.java
│ ├── EScreen.java
│ └── ETypeRotateChange.java
│ ├── impl
│ ├── FloatLifecycleReceiver.java
│ ├── FloatPhone.java
│ ├── FloatToast.java
│ └── IFloatWindowImpl.java
│ ├── interfaces
│ ├── BaseFloatView.java
│ ├── BaseFloatWindow.java
│ ├── IConfigChanged.java
│ ├── ISensorRotateChanged.java
│ ├── LifecycleListener.java
│ ├── ResumedListener.java
│ ├── ViewStateListener.java
│ └── ViewStateListenerAdapter.java
│ ├── permission
│ ├── PermissionListener.java
│ └── PermissionUtil.java
│ └── utils
│ ├── FwContent.java
│ ├── L.java
│ ├── Miui.java
│ ├── RefInvoke.java
│ ├── Rom.java
│ ├── RotateUtil.java
│ └── ViewUtils.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── sample
├── .classpath
├── .gitignore
├── .project
├── AndroidManifest.xml
├── build.gradle
├── lint.xml
├── proguard-rules.pro
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── drawable
│ │ ├── abc_btn_check_to_on_mtrl_000.png
│ │ ├── abc_btn_check_to_on_mtrl_015.png
│ │ ├── abc_btn_radio_to_on_mtrl_000.png
│ │ ├── abc_btn_radio_to_on_mtrl_015.png
│ │ ├── abc_ic_star_black_16dp.png
│ │ ├── abc_ic_star_black_36dp.png
│ │ ├── abc_ic_star_black_48dp.png
│ │ ├── abc_ic_star_half_black_16dp.png
│ │ ├── abc_ic_star_half_black_36dp.png
│ │ ├── abc_ic_star_half_black_48dp.png
│ │ ├── abc_list_divider_mtrl_alpha.9.png
│ │ ├── c_outline_add_circle_outline_black_48dp.png
│ │ ├── c_outline_close_black_48dp.png
│ │ ├── c_outline_drag_handle_black_48dp.png
│ │ ├── c_outline_pause_circle_outline_black_48dp.png
│ │ ├── c_outline_remove_circle_outline_black_48dp.png
│ │ ├── c_outline_settings_black_48dp.png
│ │ ├── c_outline_visibility_black_48dp.png
│ │ ├── c_outline_visibility_off_black_48dp.png
│ │ └── icon.png
│ ├── layout
│ │ ├── activity_b.xml
│ │ ├── activity_c.xml
│ │ ├── activity_d.xml
│ │ └── activity_main.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── com
│ └── example
│ └── fixedfloatwindow
│ ├── ActivityB.java
│ ├── ActivityC.java
│ ├── BaseActivity.java
│ ├── BaseApplication.java
│ └── MainActivity.java
├── settings.gradle
└── version.md
/.gitignore:
--------------------------------------------------------------------------------
1 | captures
2 | .externalNativeBuild
3 | import-summary.txt
4 |
5 | #java files
6 | *.class
7 | *.dex
8 |
9 | #for idea temp file
10 | *.iws
11 | *.ipr
12 | *.iml
13 |
14 | #mac temp file
15 | *.idea/
16 | __MACOSX
17 | *.DS_Store
18 |
19 | #test android studio module
20 | *androidTest/
21 | */out/
22 | *.gradle/
23 | */release/
24 | */build/
25 | build/
26 | release/
27 | */bin/
28 | */gen/
29 |
30 | #for eclipse
31 | .settings/
32 | local.properties
33 |
34 | #temp file
35 | *.bak
36 |
37 | *.pmd
38 | *.wiki/
39 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2017 Wang YingHao
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FloatWindowUpdate
2 |
3 | > 致歉:
4 | > 近期因为公司业务,一直在帮客户解决问题。一直没大面积维护,欢迎大家PR,一起加入维护
5 | >> 近期工作要点:
6 | >> 1. 权限判断和验证部分,兼容更多的平台和设备
7 | >> 2. 权限、悬浮窗、管理三部分解耦
8 | >> 如有想加入维护的可以私聊
9 |
10 |
11 | ## 特性:
12 | ------
13 |
14 | 1. 支持拖动,提供自动贴边等动画
15 |
16 | 2. 内部自动进行权限申请操作
17 |
18 | 3. 可自由指定要显示悬浮窗的界面
19 |
20 | 4. 应用退到后台时,悬浮窗会自动隐藏
21 |
22 | 5. 除小米外,4.4~7.0 无需权限申请
23 |
24 | 6. 位置及宽高可设置百分比值,轻松适配各分辨率
25 |
26 | 7. 链式调用,简洁清爽
27 |
28 |
29 | ## 开发工具集成指南:
30 | ------
31 |
32 | ### 1. 编译
33 |
34 | 根据平台执行编译指令即可
35 |
36 | * Windows: gradlew.bat release
37 | * Linux/Mac: gradlew release
38 |
39 | 推荐使用shell编译(linux/mac终端,window建议cmder):
40 | `sh build.sh`
41 | 输出在跟目录下的release目录中
42 |
43 | ### 2. 使用
44 |
45 | * 依赖集成,
46 |
47 | 1. 添加maven仓库:(后面会申请发布到公有仓库)
48 | ```
49 | repositories {
50 | maven { url 'https://dl.bintray.com/miqingtang/maven' }
51 | }
52 | ```
53 | 2. 添加依赖
54 | ```
55 | dependencies {
56 | implementation 'org.sdker.floatwindow:floatwindow:1.1.1'
57 | }
58 | ```
59 |
60 |
61 | * `Android studio`集成,直接使用编译成的aar包即可.AAR包路径 `floatwindow/build/outputs/aar/`
62 |
63 | * `eclipse`集成相对麻烦些: 将AAR解压开,将`classes.jar`修改名字拷贝到项目`libs`中;并在权限配置中集成权限和申请权限的页面即可
64 |
65 | ``` xml
66 |
67 |
68 | <....../>
69 |
70 | <....../>
71 |
76 |
77 |
78 |
79 | ```
80 |
81 |
82 |
83 | ## 具体使用方法
84 | ======
85 |
86 |
87 | **0.声明权限**
88 |
89 | > 非必要,如果想兼容更多使用场景,建议集成.
90 |
91 | ``` xml
92 |
93 | ```
94 |
95 |
96 | **1.创建悬浮控件**
97 |
98 | ``` java
99 | FloatWindow
100 | .with(getApplicationContext())
101 | .setView(view)
102 | .build();
103 |
104 | ```
105 |
106 | setView 方法可设置 View 子类或 xml 布局。
107 |
108 | **2.设置宽高及显示位置**
109 |
110 | ``` java
111 | FloatWindow
112 | .with(getApplicationContext())
113 | .setView(view)
114 | .setWidth(100) //100px
115 | .setHeight(Screen.width,0.2f) //屏幕宽度的 20%
116 | .setX(100) //100px
117 | .setY(Screen.height,0.3f) //屏幕高度的 30%
118 | .build();
119 | ```
120 |
121 | 可设置具体数值或屏幕宽/高百分比,默认宽高为 wrap_content;默认位置为屏幕左上角,x、y 为偏移量。
122 |
123 |
124 | **3.指定界面显示**
125 |
126 | ``` java
127 | FloatWindow
128 | .with(getApplicationContext())
129 | .setView(view)
130 | .setFilter(true, A_Activity.class, C_Activity.class)
131 | .build();
132 |
133 | ```
134 | 此方法表示 A_Activity、C_Activity 显示悬浮窗,其他界面隐藏。
135 |
136 | ``` java
137 | .setFilter(false, B_Activity.class)
138 | ```
139 | 此方法表示 B_Activity 隐藏悬浮窗,其他界面显示。
140 |
141 | 注意:setFilter 方法参数可以识别该 Activity 的子类
142 |
143 | 也就是说,如果 A_Activity、C_Activity 继承自 BaseActivity,你可以这样设置:
144 |
145 | ``` java
146 | .setFilter(true, BaseActivity.class)
147 | ```
148 |
149 | **4.桌面显示**
150 |
151 | ``` java
152 | FloatWindow
153 | .with(getApplicationContext())
154 | .setView(view)
155 | .setDesktopShow(true) //默认 false
156 | .build();
157 |
158 | ```
159 |
160 | **5.可拖动悬浮窗**
161 |
162 | ``` java
163 | FloatWindow
164 | .with(getApplicationContext())
165 | .setView(view)
166 | .setMoveType(MoveType.slide) //可拖动,释放后自动贴边
167 | .build();
168 |
169 | ```
170 |
171 | 共提供 4 种 MoveType :
172 |
173 | MoveType.SLIDE : 可拖动,释放后自动贴边 (默认)
174 |
175 | MoveType.BACK : 可拖动,释放后自动回到原位置
176 |
177 | MoveType.ACTIVE : 可拖动
178 |
179 | MoveType.INACTIVE : 不可拖动
180 |
181 |
182 | **6.悬浮窗动画**
183 |
184 | ``` java
185 | FloatWindow
186 | .with(getApplicationContext())
187 | .setView(view)
188 | .setMoveType(MoveType.slide)
189 | .setMoveStyle(500, new AccelerateInterpolator()) //贴边动画时长为500ms,加速插值器
190 | .build();
191 |
192 | ```
193 |
194 | 自定义动画效果,只在 MoveType.slide 或 MoveType.back 模式下设置此项才有意义。默认减速插值器,默认动画时长为 300ms。
195 |
196 |
197 | **7.后续操作**
198 |
199 | ``` java
200 | //手动控制
201 | FloatWindow.get().show();
202 | FloatWindow.get().hide();
203 |
204 | //修改显示位置
205 | FloatWindow.get().updateX(100);
206 | FloatWindow.get().updateY(100);
207 |
208 | //销毁
209 | FloatWindow.destroy();
210 |
211 | ```
212 |
213 | 以上操作应待悬浮窗初始化后进行。
214 |
215 |
216 | **8.多个悬浮窗**
217 |
218 | ``` java
219 |
220 | FloatWindow
221 | .with(getApplicationContext())
222 | .setView(imageView)
223 | .build();
224 |
225 | FloatWindow
226 | .with(getApplicationContext())
227 | .setView(button)
228 | .setTag("new")
229 | .build();
230 |
231 |
232 | FloatWindow.get("new").show();
233 | FloatWindow.get("new").hide();
234 | FloatWindow.destroy("new");
235 |
236 | ```
237 |
238 | 创建第一个悬浮窗不需加 tag,之后再创建就需指定唯一 tag ,以此区分,方便进行后续操作。
239 |
240 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 |
3 | repositories {
4 | maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
5 | maven { url 'https://dl.bintray.com/miqingtang/maven' }
6 | jcenter { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
7 | google()
8 | jcenter()
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.5.3'
12 | classpath 'com.novoda:bintray-release:0.9.2'
13 | }
14 | }
15 |
16 | allprojects {
17 | gradle.projectsEvaluated {
18 | tasks.withType(JavaCompile) {
19 | options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
20 | }
21 | }
22 | repositories {
23 | maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
24 | jcenter { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
25 | google()
26 | }
27 | }
28 |
29 | task clean(type: Delete) {
30 | delete rootProject.buildDir
31 | }
32 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 |
4 | dir=("floatwindow" "sample" )
5 |
6 | # clean cache
7 | function clean()
8 | {
9 | for element in ${dir[@]}
10 | do
11 | # clean sub dir
12 | rm -rf $element/build/
13 | rm -rf $element/bin/
14 | rm -rf $element/gen/
15 | rm -rf $element/.externalNativeBuild
16 | done
17 | # clean root dir
18 | rm -rf build/
19 | rm -rf release/
20 | }
21 |
22 |
23 | # gradlew build
24 | function build_gradlew()
25 | {
26 | ./gradlew release
27 | }
28 |
29 | # gradle build
30 | function build_gradle()
31 | {
32 | gradle release
33 | }
34 |
35 |
36 | : '
37 | 编译程序的入口
38 | '
39 |
40 | # 1. 清除缓存
41 | clean
42 |
43 | # 2. 编译并处理异常情况
44 | build_gradlew
45 |
46 | if [ $# == 0 ]; then
47 | echo "gradlew build success"
48 | pwd=$(pwd)
49 | echo "gradlew build success. path: $(pwd)/release/"
50 | else
51 | echo "gradlew build failed"
52 | build_gradle
53 | if [ $# == 0 ]; then
54 | echo "gradle build success"
55 | pwd=$(pwd)
56 | echo "gradle build success. path: $(pwd)/release/"
57 | fi
58 | fi
--------------------------------------------------------------------------------
/clean.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | : '
4 | 清除程序入口
5 | '
6 | echo ">>>>clean project<<<<"
7 | dir=("floatwindow" "sample" )
8 | for element in ${dir[@]}
9 | do
10 | #clean task
11 | rm -rf $element/build/
12 | rm -rf $element/bin/
13 | rm -rf $element/gen/
14 | rm -rf $element/.externalNativeBuild
15 | done
16 |
17 | rm -rf build/
18 | rm -rf release/
19 |
20 |
21 | if [ $# == 0 ]; then
22 | echo " clean project success. "
23 | else
24 | echo ">>clean project Failed!<<"
25 | fi
26 |
--------------------------------------------------------------------------------
/cleanCache.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 |
4 | : '
5 | 清除程序入口
6 | '
7 | echo "clean android studio cache! "
8 | echo ">>>>you must close android studio<<<<"
9 |
10 | dir=("floatwindow" "sample" )
11 | for element in ${dir[@]}
12 | do
13 | #clean task
14 | rm -rf $element/build/
15 | rm -rf $element/bin/
16 | rm -rf $element/gen/
17 | rm -rf $element/.settings/
18 | rm -rf $element/.externalNativeBuild
19 | rm -rf $element/$element.iml
20 | done
21 |
22 | rm -rf build/
23 | rm -rf release/
24 | rm -rf *.iml
25 | rm -rf .gradle/
26 | rm -rf .idea/
27 |
28 | if [ $# == 0 ]; then
29 | echo " clean project success. "
30 | else
31 | echo ">>clean project Failed!<<"
32 | fi
33 |
--------------------------------------------------------------------------------
/floatwindow/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/floatwindow/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | floatwindow-sdk
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/floatwindow/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/floatwindow/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 |
4 | def version = "v1.1.0"
5 | android {
6 | compileSdkVersion 28
7 | buildToolsVersion "28.0.3"
8 | defaultConfig {
9 | minSdkVersion 14
10 | targetSdkVersion 22
11 | }
12 | buildTypes {
13 | release {
14 | minifyEnabled false
15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
16 | }
17 | }
18 | sourceSets {
19 | main {
20 | manifest.srcFile 'AndroidManifest.xml'
21 | java.srcDirs = ['src']
22 | resources.srcDirs = ['src']
23 | aidl.srcDirs = ['src']
24 | renderscript.srcDirs = ['src']
25 | res.srcDirs = ['res']
26 | assets.srcDirs = ['assets']
27 | jniLibs.srcDirs = ['libs']
28 | }
29 | debug.setRoot('build-types/debug')
30 | release.setRoot('build-types/release')
31 | }
32 | dexOptions {
33 | preDexLibraries false
34 | maxProcessCount 8
35 | javaMaxHeapSize "4g"
36 | }
37 | compileOptions {
38 | sourceCompatibility JavaVersion.VERSION_1_7
39 | targetCompatibility JavaVersion.VERSION_1_7
40 | }
41 | aaptOptions {
42 | cruncherEnabled = false
43 | useNewCruncher = false
44 | }
45 | lintOptions {
46 | checkReleaseBuilds false
47 | abortOnError false
48 | warningsAsErrors false
49 | disable "UnusedResources"
50 | textOutput "stdout"
51 | textReport false
52 | }
53 | }
54 | // 忽略文档编译错误. 设置编码
55 | tasks.withType(Javadoc) {
56 | options.addStringOption('Xdoclint:none', '-quiet')
57 | options.addStringOption('encoding', 'UTF-8')
58 | options.addStringOption('charSet', 'UTF-8')
59 | }
60 |
61 | dependencies {
62 | // implementation fileTree(include: ['*.jar'], dir: 'libs')
63 | }
64 |
65 |
66 | /** ****************************************************************/
67 | /**************************** 编译 ****************************/
68 | /** ****************************************************************/
69 |
70 | //task releaseJar(type: Copy,dependsOn: [copyWithApp, copyWithOutApp]) {
71 | task releaseJar(type: Copy) {
72 | from("build/intermediates/bundles/release")
73 | into "../release"
74 | include("classes.jar")
75 | rename("classes.jar", "floatwindow_" + version + ".jar")
76 | }
77 |
78 | task release(type: Copy, dependsOn: [releaseJar]) {
79 | from("build/outputs/aar")
80 | into "../release"
81 | include("floatwindow-release.aar")
82 | rename("floatwindow-release.aar", "floatwindow_" + version + ".aar")
83 | }
84 |
85 | release.dependsOn build
86 |
87 |
88 | apply from: "maven.gradle"
--------------------------------------------------------------------------------
/floatwindow/lint.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/floatwindow/maven.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven'
2 | apply plugin: 'com.novoda.bintray-release'
3 |
4 |
5 | def maven_version = "1.1.1"
6 | def maven_groupId = 'org.sdker.floatwindow'
7 |
8 | //下面的version和group必须有,gradle打包的pom文件依赖这个版本号和group
9 | version = maven_version
10 | group = maven_groupId
11 |
12 | def maven_artifactId = project.name
13 |
14 | uploadArchives {
15 | repositories {
16 | mavenDeployer {
17 |
18 | repository(url: uri('../../repo'))
19 |
20 | pom.project {
21 | name maven_artifactId
22 | groupId maven_groupId
23 | artifactId maven_artifactId
24 | version maven_version
25 | packaging 'aar'
26 | }
27 | }
28 | }
29 | }
30 |
31 |
32 | //添加
33 | publish {
34 | userOrg = 'miqingtang'//bintray.com用户名
35 | groupId = maven_groupId//jcenter上的路径
36 | artifactId = maven_artifactId//项目名称
37 | publishVersion = maven_version//版本号
38 | desc = 'https://github.com/SDKers/FloatWindow/readme.md'
39 | website = 'https://github.com/SDKers/FloatWindow'//网站,不重要
40 | }
--------------------------------------------------------------------------------
/floatwindow/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 |
--------------------------------------------------------------------------------
/floatwindow/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 | # Project target.
13 | target=android-23
14 | android.library=true
15 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/FloatActivity.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.net.Uri;
8 | import android.os.Build;
9 | import android.os.Bundle;
10 |
11 | import com.yhao.floatwindow.permission.PermissionListener;
12 | import com.yhao.floatwindow.permission.PermissionUtil;
13 |
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | /**
18 | * @Copyright © 2019 Analysys Inc. All rights reserved.
19 | * @Description: 用于在内部自动申请权限
20 | * @Version: 1.0.9
21 | * @Create: 2017/01/22 17:08:22
22 | * @Author: yhao
23 | */
24 | public class FloatActivity extends Activity {
25 |
26 | private static List mPermissionListenerList;
27 | private static PermissionListener mPermissionListener;
28 |
29 | public static synchronized void request(Context context, PermissionListener permissionListener) {
30 | if (PermissionUtil.hasPermission(context)) {
31 | permissionListener.onSuccess();
32 | return;
33 | }
34 | if (mPermissionListenerList == null) {
35 | mPermissionListenerList = new ArrayList();
36 | mPermissionListener = new PermissionListener() {
37 | @Override
38 | public void onSuccess() {
39 | for (PermissionListener listener : mPermissionListenerList) {
40 | listener.onSuccess();
41 | }
42 | mPermissionListenerList.clear();
43 | }
44 |
45 | @Override
46 | public void onFail() {
47 | for (PermissionListener listener : mPermissionListenerList) {
48 | listener.onFail();
49 | }
50 | mPermissionListenerList.clear();
51 | }
52 | };
53 | Intent intent = new Intent(context, FloatActivity.class);
54 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
55 | context.startActivity(intent);
56 | }
57 | mPermissionListenerList.add(permissionListener);
58 | }
59 |
60 | @Override
61 | protected void onCreate(Bundle savedInstanceState) {
62 | super.onCreate(savedInstanceState);
63 | if (Build.VERSION.SDK_INT >= 23) {
64 | requestAlertWindowPermission();
65 | }
66 | }
67 |
68 | @TargetApi(23)
69 | private void requestAlertWindowPermission() {
70 | // Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
71 | Intent intent = new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION");
72 | intent.setData(Uri.parse("package:" + getPackageName()));
73 | startActivityForResult(intent, 756232212);
74 | }
75 |
76 | @Override
77 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
78 | super.onActivityResult(requestCode, resultCode, data);
79 | if (requestCode == 756232212) {
80 | if (PermissionUtil.hasPermissionOnActivityResult(this)) {
81 | mPermissionListener.onSuccess();
82 | } else {
83 | mPermissionListener.onFail();
84 | }
85 | }
86 | finish();
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/FloatWindow.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.animation.TimeInterpolator;
4 | import android.content.Context;
5 | import android.util.Log;
6 | import android.view.Gravity;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.yhao.floatwindow.enums.EMoveType;
11 | import com.yhao.floatwindow.enums.EScreen;
12 | import com.yhao.floatwindow.impl.IFloatWindowImpl;
13 | import com.yhao.floatwindow.interfaces.BaseFloatWindow;
14 | import com.yhao.floatwindow.interfaces.ViewStateListener;
15 | import com.yhao.floatwindow.permission.PermissionListener;
16 | import com.yhao.floatwindow.utils.FwContent;
17 | import com.yhao.floatwindow.utils.L;
18 | import com.yhao.floatwindow.utils.ViewUtils;
19 |
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | /**
24 | * @Copyright © 2017 Analysys Inc. All rights reserved.
25 | * @Description: API接口
26 | * @Version: 1.0.9
27 | * @Create: 2017/12/29 17:15:35
28 | * @Author: yhao
29 | */
30 | public class FloatWindow {
31 |
32 |
33 | private static Map mFloatWindowMap;
34 | @SuppressWarnings("unused")
35 | private static Builder mBuilder = null;
36 |
37 | private FloatWindow() {
38 |
39 | }
40 |
41 | public static BaseFloatWindow get() {
42 | return get(FwContent.DEFAULT_TAG);
43 | }
44 |
45 | public static BaseFloatWindow get(String tag) {
46 | return mFloatWindowMap == null ? null : mFloatWindowMap.get(tag);
47 | }
48 |
49 | public static Builder with(Context context) {
50 | return mBuilder = new Builder(context);
51 | }
52 |
53 | /**
54 | * 销毁默认tag的
55 | */
56 | public static void destroy() {
57 | destroy(FwContent.DEFAULT_TAG);
58 | }
59 |
60 | /**
61 | * 销毁指定tag的
62 | *
63 | * @param tag
64 | */
65 | public static void destroy(String tag) {
66 | if (mFloatWindowMap == null || !mFloatWindowMap.containsKey(tag)) {
67 | return;
68 | }
69 | mFloatWindowMap.get(tag).dismiss();
70 | mFloatWindowMap.get(tag).destory();
71 | mFloatWindowMap.remove(tag);
72 | }
73 |
74 | /**
75 | * 销毁全部
76 | */
77 | @SuppressWarnings("unlikely-arg-type")
78 | public static void destroyAll() {
79 | if (mFloatWindowMap != null) {
80 | for (BaseFloatWindow iFloatWindow : mFloatWindowMap.values()) {
81 | try {
82 | iFloatWindow.dismiss();
83 | iFloatWindow.destory();
84 | mFloatWindowMap.remove(iFloatWindow);
85 | } catch (Throwable e) {
86 | L.e(Log.getStackTraceString(e));
87 | }
88 | }
89 | mFloatWindowMap = null;
90 | }
91 | }
92 |
93 | /**
94 | * 支持链式调用的Builder类
95 | */
96 | public static class Builder {
97 | public Context mApplicationContext;
98 | public View mView;
99 | public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
100 | public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
101 | public int gravity = Gravity.TOP | Gravity.START;
102 | public int xOffset;
103 | public int yOffset;
104 | public boolean mShow = true;
105 | //自动旋转屏幕. 默认旋转
106 | public boolean isAutoRotate = true;
107 | public Class>[] mActivities;
108 | public EMoveType mMoveType = EMoveType.SLIDE;
109 | public int mSlideLeftMargin;
110 | public int mSlideRightMargin;
111 | public long mDuration = 300;
112 | public TimeInterpolator mInterpolator;
113 | public boolean mDesktopShow;
114 | public PermissionListener mPermissionListener;
115 | public ViewStateListener mViewStateListener;
116 | public int mLayoutId;
117 | private String mTag = FwContent.DEFAULT_TAG;
118 |
119 | @SuppressWarnings("unused")
120 | private Builder() {
121 | }
122 |
123 | Builder(Context applicationContext) {
124 | mApplicationContext = applicationContext;
125 | }
126 |
127 | public Builder setView(View view) {
128 | mView = view;
129 | return this;
130 | }
131 |
132 | public Builder setView(int layoutId) {
133 | mLayoutId = layoutId;
134 | return this;
135 | }
136 |
137 | public Builder setWidth(int width) {
138 | mWidth = width;
139 | return this;
140 | }
141 |
142 | public Builder setHeight(int height) {
143 | mHeight = height;
144 | return this;
145 | }
146 |
147 | public Builder setWidth(EScreen screenType, float ratio) {
148 | mWidth = (int) ((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mApplicationContext)
149 | : ViewUtils.getScreenHeight(mApplicationContext)) * ratio);
150 | return this;
151 | }
152 |
153 | public Builder setHeight(EScreen screenType, float ratio) {
154 | mHeight = (int) ((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mApplicationContext)
155 | : ViewUtils.getScreenHeight(mApplicationContext)) * ratio);
156 | return this;
157 | }
158 |
159 | public Builder setX(int x) {
160 | xOffset = x;
161 | return this;
162 | }
163 |
164 | public Builder setY(int y) {
165 | yOffset = y;
166 | return this;
167 | }
168 |
169 | public Builder setAutoRotate(boolean autoRotate) {
170 | isAutoRotate = autoRotate;
171 | return this;
172 | }
173 |
174 | public Builder setX(EScreen screenType, float ratio) {
175 | xOffset = (int) ((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mApplicationContext)
176 | : ViewUtils.getScreenHeight(mApplicationContext)) * ratio);
177 | return this;
178 | }
179 |
180 | public Builder setY(EScreen screenType, float ratio) {
181 | yOffset = (int) ((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mApplicationContext)
182 | : ViewUtils.getScreenHeight(mApplicationContext)) * ratio);
183 | return this;
184 | }
185 |
186 | /**
187 | * 设置 Activity 过滤器,用于指定在哪些界面显示悬浮窗,默认全部界面都显示
188 | *
189 | * @param show 过滤类型,子类类型也会生效
190 | * @param activities 过滤界面
191 | */
192 | public Builder setFilter(boolean show, Class>... activities) {
193 | mShow = show;
194 | mActivities = activities;
195 | return this;
196 | }
197 |
198 | public Builder setMoveType(EMoveType moveType) {
199 | return setMoveType(moveType, 0, 0);
200 | }
201 |
202 | /**
203 | * 设置带边距的贴边动画,只有 moveType 为 EMoveType.SLIDE,设置边距才有意义,这个方法不标准,后面调整
204 | *
205 | * @param moveType 贴边动画 EMoveType.SLIDE
206 | * @param slideLeftMargin 贴边动画左边距,默认为 0
207 | * @param slideRightMargin 贴边动画右边距,默认为 0
208 | */
209 | public Builder setMoveType(EMoveType moveType, int slideLeftMargin, int slideRightMargin) {
210 | mMoveType = moveType;
211 | mSlideLeftMargin = slideLeftMargin;
212 | mSlideRightMargin = slideRightMargin;
213 | return this;
214 | }
215 |
216 | public Builder setMoveStyle(long duration, TimeInterpolator interpolator) {
217 | mDuration = duration;
218 | mInterpolator = interpolator;
219 | return this;
220 | }
221 |
222 | public Builder setTag(String tag) {
223 | mTag = tag;
224 | return this;
225 | }
226 |
227 | public Builder setDesktopShow(boolean show) {
228 | mDesktopShow = show;
229 | return this;
230 | }
231 |
232 | public Builder setPermissionListener(PermissionListener listener) {
233 | mPermissionListener = listener;
234 | return this;
235 | }
236 |
237 | public Builder setViewStateListener(ViewStateListener listener) {
238 | mViewStateListener = listener;
239 | return this;
240 | }
241 |
242 | public void build() {
243 | if (mFloatWindowMap == null) {
244 | mFloatWindowMap = new HashMap(16);
245 | }
246 | if (mFloatWindowMap.containsKey(mTag)) {
247 | throw new IllegalArgumentException(
248 | "FloatWindow of this tag has been added, Please set a new tag for the new FloatWindow");
249 | }
250 | if (mView == null && mLayoutId == 0) {
251 | throw new IllegalArgumentException("View has not been set!");
252 | }
253 | if (mView == null) {
254 | mView = ViewUtils.inflate(mApplicationContext, mLayoutId);
255 | }
256 | BaseFloatWindow floatWindowImpl = new IFloatWindowImpl(this);
257 | mFloatWindowMap.put(mTag, floatWindowImpl);
258 |
259 | L.i("build [" + mTag + "] success. sdk version:" + FwContent.VERSION);
260 | }
261 | }
262 | }
263 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/enums/EMoveType.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.enums;
2 |
3 | /**
4 | * @Copyright © 2019 Analysys Inc. All rights reserved.
5 | * @Description: 移动类型, 去除V4依赖
6 | *
7 | * SLIDE : 可拖动,释放后自动贴边 (默认)
8 | *
9 | * BACK : 可拖动,释放后自动回到原位置
10 | *
11 | * ACTIVE : 可拖动
12 | *
13 | * INACTIVE : 不可拖动
14 | * @Version: 1.0
15 | * @Create: Feb 19, 2019 11:32:21 AM
16 | * @Author: sanbo
17 | */
18 | public enum EMoveType {
19 | FIXED,
20 | INACTIVE,
21 | ACTIVE,
22 | SLIDE,
23 | BACK
24 | }
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/enums/EScreen.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.enums;
2 |
3 | /**
4 | * @Copyright © 2019 Analysys Inc. All rights reserved.
5 | * @Description: 屏幕宽高。去除V4依赖
6 | * @Version: 1.0
7 | * @Create: Feb 19, 2019 11:25:37 AM
8 | * @Author: sanbo
9 | */
10 | public enum EScreen {
11 | WIDTH,
12 | HEIGHT
13 | }
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/enums/ETypeRotateChange.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.enums;
2 |
3 | /**
4 | * 手机旋转类型.
5 | *
6 | * T_ACTIVITY_ROTATE: activity lifecycle 监听的页面屏幕旋转
7 | * T_SENSOR_ROTATE: 传感器监听的手机旋转
8 | */
9 | public enum ETypeRotateChange {
10 | // 页面回调,监听的页面旋转(此时手机旋转无效)
11 | T_ACTIVITY_ROTATE,
12 | // 跳出app后,监听的手机旋转(页面旋转失效)
13 | T_SENSOR_ROTATE
14 | }
15 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/impl/FloatLifecycleReceiver.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.impl;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.content.BroadcastReceiver;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.IntentFilter;
9 | import android.os.Bundle;
10 | import android.os.Handler;
11 |
12 | import com.yhao.floatwindow.interfaces.IConfigChanged;
13 | import com.yhao.floatwindow.interfaces.LifecycleListener;
14 | import com.yhao.floatwindow.interfaces.ResumedListener;
15 | import com.yhao.floatwindow.utils.FwContent;
16 | import com.yhao.floatwindow.utils.L;
17 | import com.yhao.floatwindow.utils.RotateUtil;
18 | import com.yhao.floatwindow.utils.ViewUtils;
19 |
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | /**
24 | * @Copyright © 2017 Analysys Inc. All rights reserved.
25 | * @Description:
26 | * 用于控制悬浮窗显示周期 使用了三种方法针对返回桌面时隐藏悬浮按钮
27 | * 1. startCount计数,针对back到桌面可以及时隐藏
28 | * 2.监听home键,从而及时隐藏
29 | * 3.resumeCount计时,针对一些只执行onPause不执行onStop的奇葩情况
30 | *
31 | * @Version: 1.0.9
32 | * @Create: 2017-12-1 17:04:11
33 | * @Author: yhao
34 | */
35 | public class FloatLifecycleReceiver extends BroadcastReceiver implements Application.ActivityLifecycleCallbacks {
36 |
37 | private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
38 | private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
39 | private static final long DELAY = 300;
40 | private static ResumedListener sResumedListener;
41 | private static int num = 0;
42 | private Handler mHandler;
43 | private Class>[] activities;
44 | private boolean showFlag;
45 | private int startCount;
46 | private int resumeCount;
47 | private boolean appBackground;
48 | private LifecycleListener mLifecycleListener = null;
49 | private IConfigChanged mConfigChange = null;
50 | // 存储当前页面的信息,为切换页面时作为上个页面进行旋转屏幕比较
51 | private Map mActivityAndLandscape = new HashMap();
52 |
53 | private FloatLifecycleReceiver() {
54 | }
55 |
56 | public FloatLifecycleReceiver(Context applicationContext, boolean showFlag, Class>[] activities,
57 | LifecycleListener lifecycleListener) {
58 | this.showFlag = showFlag;
59 | this.activities = activities;
60 | num++;
61 | mLifecycleListener = lifecycleListener;
62 | mHandler = new Handler();
63 | ((Application) applicationContext).registerActivityLifecycleCallbacks(this);
64 | applicationContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
65 | }
66 |
67 | public static void setResumedListener(ResumedListener resumedListener) {
68 | sResumedListener = resumedListener;
69 | }
70 |
71 | private boolean needShow(Activity activity) {
72 | if (activities == null) {
73 | return true;
74 | }
75 | for (Class> a : activities) {
76 | if (a.isInstance(activity)) {
77 | return showFlag;
78 | }
79 | }
80 | return !showFlag;
81 | }
82 |
83 |
84 | public void setConfigChanged(IConfigChanged configChanged) {
85 | if (configChanged != null) {
86 | mConfigChange = configChanged;
87 | }
88 | }
89 |
90 |
91 | @Override
92 | public void onActivityResumed(Activity activity) {
93 | if (mConfigChange != null) {
94 | mConfigChange.onBackToDesktop(false);
95 | }
96 | checkConfigChangeWhenResume(activity);
97 |
98 | if (sResumedListener != null) {
99 | num--;
100 | if (num == 0) {
101 | sResumedListener.onResumed();
102 | sResumedListener = null;
103 | }
104 | }
105 | resumeCount++;
106 | if (needShow(activity)) {
107 | mLifecycleListener.onShow();
108 | } else {
109 | mLifecycleListener.onHide();
110 | }
111 | if (appBackground) {
112 | appBackground = false;
113 | }
114 | }
115 |
116 |
117 | @Override
118 | public void onActivityPaused(final Activity activity) {
119 | if (mConfigChange != null) {
120 | mConfigChange.onBackToDesktop(false);
121 | }
122 | checkConfigChangeWhenPause(activity);
123 |
124 | resumeCount--;
125 | mHandler.postDelayed(new Runnable() {
126 | @Override
127 | public void run() {
128 | if (resumeCount == 0) {
129 | appBackground = true;
130 | mLifecycleListener.onBackToDesktop();
131 | if (mConfigChange != null) {
132 | mConfigChange.onBackToDesktop(true);
133 | }
134 | }
135 | }
136 | }, DELAY);
137 |
138 | }
139 |
140 |
141 | @Override
142 | public void onActivityStarted(Activity activity) {
143 | startCount++;
144 | }
145 |
146 | @Override
147 | public void onActivityStopped(Activity activity) {
148 | if (mConfigChange != null) {
149 | mConfigChange.onBackToDesktop(false);
150 | }
151 | startCount--;
152 | if (startCount == 0) {
153 | mLifecycleListener.onBackToDesktop();
154 | if (mConfigChange != null) {
155 | mConfigChange.onBackToDesktop(true);
156 | }
157 | }
158 | }
159 |
160 | @Override
161 | public void onReceive(Context context, Intent intent) {
162 | String action = intent.getAction();
163 | if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
164 | String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
165 | if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
166 | mLifecycleListener.onBackToDesktop();
167 | if (mConfigChange != null) {
168 | mConfigChange.onBackToDesktop(true);
169 | }
170 | }
171 | }
172 | }
173 |
174 | @Override
175 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
176 | }
177 |
178 | @Override
179 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
180 | }
181 |
182 | @Override
183 | public void onActivityDestroyed(Activity activity) {
184 | }
185 |
186 | public void unRegisterReceiver(Context context) {
187 | ((Application) context).unregisterActivityLifecycleCallbacks(this);
188 | context.unregisterReceiver(this);
189 | }
190 |
191 |
192 | /**
193 | * 页面展示时检查是否页面旋转。逻辑: 和上个页面对比
194 | *
195 | * @param activity
196 | */
197 | private void checkConfigChangeWhenResume(Activity activity) {
198 | if (mActivityAndLandscape.size() > 0) {
199 | mActivityAndLandscape.clear();
200 | }
201 | if (FwContent.isDebug) {
202 | L.i("------checkConfigChangeWhenResume----mActivityAndLandscape: " + mActivityAndLandscape.size());
203 | }
204 | RotateUtil.getInstance().start(activity);
205 | String activityName = activity.getPackageName() + "." + activity.getLocalClassName();
206 | if (mActivityAndLandscape.size() < 1) {
207 | // 有悬浮窗后的,首次切换页面
208 | mActivityAndLandscape.put(activityName, ViewUtils.isActivityLandscape(activity));
209 | if (mConfigChange != null) {
210 | mConfigChange.onActivityConfigChanged();
211 | }
212 | } else {
213 | if (mActivityAndLandscape.containsKey(activityName)) {
214 | boolean isLandscape = ViewUtils.isActivityLandscape(activity);
215 | if (mActivityAndLandscape.get(activityName) != isLandscape) {
216 | mActivityAndLandscape.put(activityName, isLandscape);
217 | if (mConfigChange != null) {
218 | mConfigChange.onActivityConfigChanged();
219 | }
220 | } else {
221 | // 页面方向未变
222 | }
223 | } else {
224 | mActivityAndLandscape.clear();
225 | // 有悬浮窗后的,首次切换页面
226 | mActivityAndLandscape.put(activityName, ViewUtils.isActivityLandscape(activity));
227 | if (mConfigChange != null) {
228 | mConfigChange.onActivityConfigChanged();
229 | }
230 | }
231 | }
232 | }
233 |
234 | /**
235 | * 页面关闭时检查页面监听监视
236 | *
237 | * @param activity
238 | */
239 | private void checkConfigChangeWhenPause(Activity activity) {
240 | RotateUtil.getInstance().start(activity);
241 |
242 | if (mActivityAndLandscape.size() < 1) {
243 | // 有悬浮窗后的,首次关闭页面-(兼容页面打开,才展示悬浮窗,此时map无此页面信息)
244 | String activityName = activity.getPackageName() + "." + activity.getLocalClassName();
245 | mActivityAndLandscape.put(activityName, ViewUtils.isActivityLandscape(activity));
246 | }
247 | }
248 | }
249 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/impl/FloatPhone.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.impl;
2 |
3 | import android.content.Context;
4 | import android.os.Build;
5 | import android.view.View;
6 | import android.view.WindowManager;
7 |
8 | import com.yhao.floatwindow.FloatActivity;
9 | import com.yhao.floatwindow.interfaces.BaseFloatView;
10 | import com.yhao.floatwindow.permission.PermissionListener;
11 | import com.yhao.floatwindow.utils.L;
12 | import com.yhao.floatwindow.utils.Miui;
13 |
14 | /**
15 | * @Copyright © 2017 Analysys Inc. All rights reserved.
16 | * @Description:
17 | * @Version: 1.0.9
18 | * @Create: 2017-11-14 17:15:35
19 | * @Author: yhao
20 | */
21 | public class FloatPhone extends BaseFloatView {
22 | private final Context mContext;
23 |
24 | private final WindowManager mWindowManager;
25 | private final WindowManager.LayoutParams mLayoutParams;
26 | private View mView;
27 | private int mX, mY;
28 | private boolean isRemove = false;
29 | private PermissionListener mPermissionListener;
30 |
31 | public FloatPhone(Context applicationContext, PermissionListener permissionListener) {
32 | mContext = applicationContext;
33 | mPermissionListener = permissionListener;
34 | mWindowManager = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE);
35 | mLayoutParams = new WindowManager.LayoutParams();
36 | // mLayoutParams.format = PixelFormat.RGBA_8888;
37 | // mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
38 | // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
39 |
40 | //PixelFormat.RGBA_8888
41 | mLayoutParams.format = 1;
42 | // WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL: 0x00000020
43 | // WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE: 0x00000008
44 | // WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS: 0x00000200
45 | // WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN: 0x00000100
46 | // WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR: 0x00010000
47 | mLayoutParams.flags = 0x00000020 | 0x00000008 | 0x00000200 | 0x00000100 | 0x00010000;
48 | mLayoutParams.windowAnimations = 0;
49 | }
50 |
51 | @Override
52 | public void setSize(int width, int height) {
53 | mLayoutParams.width = width;
54 | mLayoutParams.height = height;
55 | }
56 |
57 | @Override
58 | public void setView(View view) {
59 | mView = view;
60 | }
61 |
62 | @Override
63 | public void setGravity(int gravity, int xOffset, int yOffset) {
64 | mLayoutParams.gravity = gravity;
65 | mLayoutParams.x = mX = xOffset;
66 | mLayoutParams.y = mY = yOffset;
67 | }
68 |
69 | @Override
70 | public void init() {
71 | if (Build.VERSION.SDK_INT >= 25) {
72 | req();
73 | } else if (Miui.rom()) {
74 | if (Build.VERSION.SDK_INT >= 23) {
75 | req();
76 | } else {
77 | // 避免方法警告
78 | // WindowManager.LayoutParams.TYPE_PHONE: 2002
79 | mLayoutParams.type = 2002;
80 | Miui.requestPermission(mContext, new PermissionListener() {
81 | @Override
82 | public void onSuccess() {
83 | mWindowManager.addView(mView, mLayoutParams);
84 | if (mPermissionListener != null) {
85 | mPermissionListener.onSuccess();
86 | }
87 | }
88 |
89 | @Override
90 | public void onFail() {
91 | if (mPermissionListener != null) {
92 | mPermissionListener.onFail();
93 | }
94 | }
95 | });
96 | }
97 | } else {
98 | try {
99 | // 避免过时方法警告
100 | // WindowManager.LayoutParams.TYPE_TOAST:2005
101 | mLayoutParams.type = 2005;
102 | mWindowManager.addView(mView, mLayoutParams);
103 | } catch (Exception e) {
104 | mWindowManager.removeView(mView);
105 | L.e("TYPE_TOAST 失败");
106 | req();
107 | }
108 | }
109 | }
110 |
111 | private void req() {
112 | //Build.VERSION_CODES.O: 26
113 | if (Build.VERSION.SDK_INT >= 26) {
114 | //WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY: 2038
115 | mLayoutParams.type = 2038;
116 | } else {
117 | //WindowManager.LayoutParams.TYPE_PHONE: 2002
118 | mLayoutParams.type = 2002;
119 | }
120 | FloatActivity.request(mContext, new PermissionListener() {
121 | @Override
122 | public void onSuccess() {
123 | mWindowManager.addView(mView, mLayoutParams);
124 | if (mPermissionListener != null) {
125 | mPermissionListener.onSuccess();
126 | }
127 | }
128 |
129 | @Override
130 | public void onFail() {
131 | if (mPermissionListener != null) {
132 | mPermissionListener.onFail();
133 | }
134 | }
135 | });
136 | }
137 |
138 | @Override
139 | public void dismiss() {
140 | isRemove = true;
141 | mWindowManager.removeView(mView);
142 | }
143 |
144 | @Override
145 | public void updateXY(int x, int y) {
146 | if (isRemove) {
147 | return;
148 | }
149 | mLayoutParams.x = mX = x;
150 | mLayoutParams.y = mY = y;
151 | mWindowManager.updateViewLayout(mView, mLayoutParams);
152 | }
153 |
154 | @Override
155 | public void updateX(int x) {
156 | if (isRemove) {
157 | return;
158 | }
159 | mLayoutParams.x = mX = x;
160 | if (mView == null || mView.getParent() == null) {
161 | mWindowManager.addView(mView, mLayoutParams);
162 | } else {
163 | mWindowManager.updateViewLayout(mView, mLayoutParams);
164 | }
165 | }
166 |
167 | @Override
168 | public void updateY(int y) {
169 | if (isRemove) {
170 | return;
171 | }
172 | mLayoutParams.y = mY = y;
173 | if (mView == null || mView.getParent() == null) {
174 | mWindowManager.addView(mView, mLayoutParams);
175 | } else {
176 | mWindowManager.updateViewLayout(mView, mLayoutParams);
177 | }
178 | }
179 |
180 | @Override
181 | public int getX() {
182 | return mX;
183 | }
184 |
185 | @Override
186 | public int getY() {
187 | return mY;
188 | }
189 |
190 | }
191 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/impl/FloatToast.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.impl;
2 |
3 | import android.content.Context;
4 | import android.os.Handler;
5 | import android.os.IBinder;
6 | import android.os.Message;
7 | import android.view.View;
8 | import android.view.WindowManager;
9 | import android.widget.Toast;
10 |
11 | import com.yhao.floatwindow.interfaces.BaseFloatView;
12 | import com.yhao.floatwindow.utils.RefInvoke;
13 |
14 | import java.lang.reflect.Method;
15 |
16 | /**
17 | * @Copyright © 2017 Analysys Inc. All rights reserved.
18 | * @Description: 自定义 toast 方式,无需申请权限 当前版本暂时用 TYPE_TOAST 代替,后续版本可能会再融入此方式
19 | * @Version: 1.0.9
20 | * @Create: 2017-11-14 17:15:35
21 | * @Author: yhao
22 | */
23 | public class FloatToast extends BaseFloatView {
24 |
25 | private Toast toast;
26 |
27 | private Object mTN;
28 | private Method show;
29 | private Method handleShow;
30 | private Method hide;
31 |
32 | private int mWidth;
33 | private int mHeight;
34 |
35 | public FloatToast(Context applicationContext) {
36 | toast = new Toast(applicationContext);
37 | }
38 |
39 | @Override
40 | public void setSize(int width, int height) {
41 | mWidth = width;
42 | mHeight = height;
43 | }
44 |
45 | @Override
46 | public void setView(View view) {
47 | toast.setView(view);
48 | initTN();
49 | }
50 |
51 | @Override
52 | public void setGravity(int gravity, int xOffset, int yOffset) {
53 | toast.setGravity(gravity, xOffset, yOffset);
54 | }
55 |
56 | @Override
57 | public void init() {
58 | try {
59 | if (show != null) {
60 | show.invoke(mTN);
61 | } else {
62 | toast.show();
63 | }
64 | } catch (Exception e) {
65 | e.printStackTrace();
66 | }
67 | }
68 |
69 | @Override
70 | public void dismiss() {
71 | try {
72 | hide.invoke(mTN);
73 | } catch (Exception e) {
74 | e.printStackTrace();
75 | }
76 | }
77 |
78 | private void initTN() {
79 | try {
80 |
81 | mTN = RefInvoke.getFieldObject(toast, "mTN");
82 | final Handler originHandler = (Handler) RefInvoke.getFieldObject(mTN, "mHandler");
83 | show = RefInvoke.getMethod(mTN, "show");
84 | if (show == null) {
85 | handleShow = RefInvoke.getMethod(mTN, "handleShow", new Class[]{IBinder.class});
86 | RefInvoke.setFieldObject(mTN, "mHandler", new Handler() {
87 | @Override
88 | public void dispatchMessage(Message msg) {
89 | try {
90 | if (msg.what == 0) {
91 | IBinder token = (IBinder) msg.obj;
92 | // mShow.invoke()
93 | handleShow.invoke(mTN, token);
94 | return;
95 | }
96 | originHandler.dispatchMessage(msg);
97 | } catch (Exception e) {
98 | // ignore
99 | }
100 | }
101 |
102 | @Override
103 | public void handleMessage(Message msg) {
104 | try {
105 | if (msg.what == 0) {
106 | IBinder token = (IBinder) msg.obj;
107 | handleShow.invoke(mTN, token);
108 | return;
109 | }
110 | originHandler.handleMessage(msg);
111 | } catch (Exception igone) {
112 | }
113 | }
114 | });
115 | }
116 | hide = RefInvoke.getMethod(mTN, "hide");
117 | // 更新无效,可以显示,不可以长时间显示
118 | // RefInvoke.getInstance().setFieldValue(mTN, "mDuration", 999999);
119 | // RefInvoke.getInstance().setFieldValue(mTN, "SHORT_DURATION_TIMEOUT", 999999);
120 | // RefInvoke.getInstance().setFieldValue(mTN, "SHORT_DURATION_TIMEOUT", 999999);
121 |
122 | WindowManager.LayoutParams params = (WindowManager.LayoutParams) RefInvoke.getFieldObject(mTN, "mParams");
123 | params.flags = 0x00000020 | 0x00000008 | 0x00000100 | 0x00010000;
124 | params.width = mWidth;
125 | params.height = mHeight;
126 | params.windowAnimations = 0;
127 | RefInvoke.setFieldObject(mTN, "mNextView", toast.getView());
128 | } catch (Exception e) {
129 | e.printStackTrace();
130 | }
131 | }
132 |
133 | }
134 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/impl/IFloatWindowImpl.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.impl;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.animation.PropertyValuesHolder;
7 | import android.animation.TimeInterpolator;
8 | import android.animation.ValueAnimator;
9 | import android.annotation.SuppressLint;
10 | import android.os.Build;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewConfiguration;
14 | import android.view.animation.DecelerateInterpolator;
15 |
16 | import com.yhao.floatwindow.FloatWindow;
17 | import com.yhao.floatwindow.enums.EMoveType;
18 | import com.yhao.floatwindow.enums.EScreen;
19 | import com.yhao.floatwindow.enums.ETypeRotateChange;
20 | import com.yhao.floatwindow.interfaces.BaseFloatView;
21 | import com.yhao.floatwindow.interfaces.BaseFloatWindow;
22 | import com.yhao.floatwindow.interfaces.IConfigChanged;
23 | import com.yhao.floatwindow.interfaces.ISensorRotateChanged;
24 | import com.yhao.floatwindow.interfaces.LifecycleListener;
25 | import com.yhao.floatwindow.utils.FwContent;
26 | import com.yhao.floatwindow.utils.L;
27 | import com.yhao.floatwindow.utils.RotateUtil;
28 | import com.yhao.floatwindow.utils.ViewUtils;
29 |
30 | /**
31 | * @Copyright © 2017 Analysys Inc. All rights reserved.
32 | * @Description:
33 | * @Version: 1.0.9
34 | * @Create: 2017/12/29 17:15:35
35 | * @Author: yhao
36 | * @Modify: sanbo
37 | */
38 | public class IFloatWindowImpl extends BaseFloatWindow {
39 |
40 | private FloatWindow.Builder mBuilder;
41 | private BaseFloatView mFloatView;
42 | private FloatLifecycleReceiver mFloatLifecycle;
43 | private boolean isShow;
44 | private boolean once = true;
45 | private ValueAnimator mAnimator;
46 | private TimeInterpolator mDecelerateInterpolator;
47 | private float downX;
48 | private float downY;
49 | private float upX;
50 | private float upY;
51 | private boolean mClick = false;
52 | private int mSlop;
53 | private int screenWidth, screenHeight;
54 | // 返回桌面
55 | private boolean isBackToDesktop = false;
56 |
57 | @SuppressWarnings("unused")
58 | private IFloatWindowImpl() {
59 | }
60 |
61 | public IFloatWindowImpl(FloatWindow.Builder b) {
62 | if (b == null) {
63 | return;
64 | }
65 | mBuilder = b;
66 | checkScreenSize();
67 |
68 | if (mBuilder.mMoveType == EMoveType.FIXED) {
69 | if (Build.VERSION.SDK_INT >= 25) {
70 | mFloatView = new FloatPhone(b.mApplicationContext, mBuilder.mPermissionListener);
71 | } else {
72 | mFloatView = new FloatToast(b.mApplicationContext);
73 | }
74 | } else {
75 | mFloatView = new FloatPhone(b.mApplicationContext, mBuilder.mPermissionListener);
76 | initTouchEvent();
77 | }
78 | mFloatView.setSize(mBuilder.mWidth, mBuilder.mHeight);
79 | mFloatView.setGravity(mBuilder.gravity, mBuilder.xOffset, mBuilder.yOffset);
80 | mFloatView.setView(mBuilder.mView);
81 | mFloatLifecycle = new FloatLifecycleReceiver(mBuilder.mApplicationContext, mBuilder.mShow, mBuilder.mActivities,
82 | new LifecycleListener() {
83 | // new FloatLifecycleReceiver(mBuilder.mApplicationContext, mBuilder.mShow, mBuilder.mActivities,
84 | // new LifecycleListener() {
85 | @Override
86 | public void onShow() {
87 | // show();
88 | }
89 |
90 | @Override
91 | public void onHide() {
92 | // hide();
93 | }
94 |
95 | @Override
96 | public void onBackToDesktop() {
97 | if (!mBuilder.mDesktopShow) {
98 | hide();
99 | }
100 | if (mBuilder.mViewStateListener != null) {
101 | mBuilder.mViewStateListener.onBackToDesktop();
102 | }
103 | }
104 | });
105 |
106 | if (mBuilder != null && mBuilder.isAutoRotate) {
107 | if (mFloatLifecycle != null) {
108 | mFloatLifecycle.setConfigChanged(new IConfigChanged() {
109 | @Override
110 | public void onActivityConfigChanged() {
111 | onPhoneConfigChanged(ETypeRotateChange.T_ACTIVITY_ROTATE);
112 | }
113 |
114 | @Override
115 | public void onBackToDesktop(boolean isBack) {
116 | isBackToDesktop = isBack;
117 | }
118 | });
119 | }
120 |
121 | RotateUtil.getInstance().setRotateChanged(new ISensorRotateChanged() {
122 | @Override
123 | public void onRotateChanged() {
124 | onPhoneConfigChanged(ETypeRotateChange.T_SENSOR_ROTATE);
125 | }
126 | });
127 | }
128 |
129 | }
130 |
131 |
132 | /**
133 | * 旋转时操作,处理方式:
134 | * 1. 更新页面的尺寸
135 | * 2. 矫正组件到可见区域.暂时不会按照屏幕等比例移动(如竖屏右下,旋转后移动到横屏右下)
136 | *
137 | * @param rotateType 旋转类型
138 | */
139 | private void onPhoneConfigChanged(ETypeRotateChange rotateType) {
140 | checkScreenSize();
141 | if (FwContent.isDebug) {
142 | L.i(rotateType + " [" + screenWidth + "*" + screenHeight + "]------> " + getX() + " * " + getY() + "------" + isBackToDesktop);
143 | }
144 | // // 应用内,不使用手机旋转(即传感器方向)
145 | // if (!isBackToDesktop && rotateType == ETypeRotateChange.T_SENSOR_ROTATE) {
146 | // return;
147 | // }
148 | //超出屏幕 移动到屏幕中间,确保超出屏幕的case能兼容处理
149 | if (Math.abs(getX() - screenWidth) > screenWidth / 3 || Math.abs(getY() - screenHeight) > screenWidth / 3
150 | || screenHeight == getY()
151 | ) {
152 | if (FwContent.isDebug) {
153 | L.w("超出屏幕...即将自动校正....");
154 | }
155 | updateX(screenWidth - 200);
156 | updateY(screenHeight * 2 / 3);
157 | } else {
158 | if (FwContent.isDebug) {
159 | L.d("未超出屏幕....");
160 | }
161 | }
162 |
163 |
164 | }
165 |
166 | @Override
167 | public void show() {
168 | if (once) {
169 | mFloatView.init();
170 | once = false;
171 | isShow = true;
172 | } else {
173 | if (isShow) {
174 | return;
175 | }
176 | getView().setVisibility(View.VISIBLE);
177 | isShow = true;
178 | }
179 | if (mBuilder.mViewStateListener != null) {
180 | mBuilder.mViewStateListener.onShow();
181 | }
182 | }
183 |
184 | @Override
185 | public void hide() {
186 | if (once || !isShow) {
187 | return;
188 | }
189 | getView().setVisibility(View.INVISIBLE);
190 | isShow = false;
191 | if (mBuilder.mViewStateListener != null) {
192 | mBuilder.mViewStateListener.onHide();
193 | }
194 | }
195 |
196 | @Override
197 | public boolean isShowing() {
198 | return isShow;
199 | }
200 |
201 | @Override
202 | public void dismiss() {
203 | mFloatView.dismiss();
204 | isShow = false;
205 | if (mBuilder.mViewStateListener != null) {
206 | mBuilder.mViewStateListener.onDismiss();
207 | }
208 | }
209 |
210 | @Override
211 | public void destory() {
212 | if (mFloatLifecycle != null) {
213 | mFloatLifecycle.unRegisterReceiver(mBuilder.mApplicationContext);
214 |
215 | }
216 | if (mAnimator != null && mAnimator.isRunning()) {
217 | mAnimator.cancel();
218 | }
219 | }
220 |
221 | @Override
222 | public void updateX(int x) {
223 | checkMoveType();
224 | mBuilder.xOffset = x;
225 | mFloatView.updateX(x);
226 | }
227 |
228 | @Override
229 | public void updateY(int y) {
230 | checkMoveType();
231 | mBuilder.yOffset = y;
232 | mFloatView.updateY(y);
233 | }
234 |
235 | @Override
236 | public void updateX(EScreen screenType, float ratio) {
237 | checkMoveType();
238 |
239 | // mBuilder.xOffset = (int)((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mBuilder.mApplicationContext)
240 | // : ViewUtils.getScreenHeight(mBuilder.mApplicationContext)) * ratio);
241 | mBuilder.xOffset = (int) ((screenType == EScreen.WIDTH ? screenWidth : screenHeight) * ratio);
242 | mFloatView.updateX(mBuilder.xOffset);
243 |
244 | }
245 |
246 | @Override
247 | public void updateY(EScreen screenType, float ratio) {
248 | checkMoveType();
249 | // mBuilder.yOffset = (int)((screenType == EScreen.WIDTH ? ViewUtils.getScreenWidth(mBuilder.mApplicationContext)
250 | // : ViewUtils.getScreenHeight(mBuilder.mApplicationContext)) * ratio);
251 | mBuilder.yOffset = (int) ((screenType == EScreen.WIDTH ? screenWidth : screenHeight) * ratio);
252 | mFloatView.updateY(mBuilder.yOffset);
253 |
254 | }
255 |
256 | @Override
257 | public int getX() {
258 | return mFloatView.getX();
259 | }
260 |
261 | @Override
262 | public int getY() {
263 | return mFloatView.getY();
264 | }
265 |
266 | @Override
267 | public View getView() {
268 | mSlop = ViewConfiguration.get(mBuilder.mApplicationContext).getScaledTouchSlop();
269 | return mBuilder.mView;
270 | }
271 |
272 | private void checkMoveType() {
273 | if (mBuilder.mMoveType == EMoveType.FIXED) {
274 | throw new IllegalArgumentException("FloatWindow of this tag is not allowed to move!");
275 | }
276 | }
277 |
278 | private void initTouchEvent() {
279 | checkScreenSize();
280 | if (mBuilder.mMoveType != EMoveType.INACTIVE) {
281 | getView().setOnTouchListener(new View.OnTouchListener() {
282 | float lastX, lastY, changeX, changeY;
283 | int newX, newY;
284 |
285 | @SuppressLint("ClickableViewAccessibility")
286 | @Override
287 | public boolean onTouch(View v, MotionEvent event) {
288 |
289 | try {
290 | switch (event.getAction()) {
291 | case MotionEvent.ACTION_DOWN:
292 | onActionDown(event);
293 | break;
294 | case MotionEvent.ACTION_MOVE:
295 | onActionMove(event);
296 | break;
297 | case MotionEvent.ACTION_UP:
298 | onActionUp(event, v);
299 | break;
300 | default:
301 | break;
302 | }
303 | } catch (Throwable e) {
304 | L.e(e);
305 | }
306 | return mClick;
307 | }
308 |
309 | private void onActionDown(MotionEvent event) {
310 | mClick = false;
311 | downX = event.getRawX();
312 | downY = event.getRawY();
313 | lastX = event.getRawX();
314 | lastY = event.getRawY();
315 | cancelAnimator();
316 | }
317 |
318 | private void onActionMove(MotionEvent event) {
319 | // if (!isOutOfRange(event.getRawX(), event.getRawY())) {
320 | changeX = event.getRawX() - lastX;
321 | changeY = event.getRawY() - lastY;
322 | newX = (int) (mFloatView.getX() + changeX);
323 | newY = (int) (mFloatView.getY() + changeY);
324 | mFloatView.updateXY(newX, newY);
325 | if (mBuilder.mViewStateListener != null) {
326 | mBuilder.mViewStateListener.onPositionUpdate(newX, newY);
327 | }
328 | lastX = event.getRawX();
329 | lastY = event.getRawY();
330 | // }
331 | }
332 |
333 | private void onActionUp(MotionEvent event, View v) {
334 | upX = event.getRawX();
335 | upY = event.getRawY();
336 | mClick = (Math.abs(upX - downX) > mSlop) || (Math.abs(upY - downY) > mSlop);
337 | // L.i("Raw [%f x %f] -- FloatView[%d x %d] 分辨率(%d---%d) ", upX, upY, mFloatView.getX(),
338 | // mFloatView.getY(), screenWidth, screenHeight);
339 | if (mBuilder.mMoveType == EMoveType.SLIDE) {
340 | int startX = mFloatView.getX();
341 | int startY = mFloatView.getY();
342 | // 如果图标滑动的坐标是在哪部分,根据坐标和边缘距离计算,相应的坐标
343 | int endX = (startX * 2 + v.getWidth() > screenWidth)
344 | ? screenWidth - v.getWidth() - mBuilder.mSlideRightMargin : mBuilder.mSlideLeftMargin;
345 | // 是否需要校正Y坐标
346 | if (isNeedMoveY(startY, upY)) {
347 | int endY = getY(startY, upY, v.getHeight());
348 | PropertyValuesHolder pvhX = PropertyValuesHolder.ofInt("x", mFloatView.getX(), endX);
349 | PropertyValuesHolder pvhY = PropertyValuesHolder.ofInt("y", mFloatView.getY(), endY);
350 | mAnimator = ObjectAnimator.ofPropertyValuesHolder(pvhX, pvhY);
351 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
352 | @Override
353 | public void onAnimationUpdate(ValueAnimator animation) {
354 | int x = (Integer) animation.getAnimatedValue("x");
355 | int y = (Integer) animation.getAnimatedValue("y");
356 | mFloatView.updateXY(x, y);
357 | if (mBuilder.mViewStateListener != null) {
358 | mBuilder.mViewStateListener.onPositionUpdate(x, y);
359 | }
360 | }
361 | });
362 | } else {
363 | mAnimator = ObjectAnimator.ofInt(startX, endX);
364 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
365 | @Override
366 | public void onAnimationUpdate(ValueAnimator animation) {
367 | int x = (Integer) animation.getAnimatedValue();
368 | mFloatView.updateX(x);
369 | if (mBuilder.mViewStateListener != null) {
370 | mBuilder.mViewStateListener.onPositionUpdate(x, (int) upY);
371 | }
372 | }
373 | });
374 | }
375 | startAnimator();
376 | } else if (mBuilder.mMoveType == EMoveType.BACK) {
377 | PropertyValuesHolder pvhX =
378 | PropertyValuesHolder.ofInt("x", mFloatView.getX(), mBuilder.xOffset);
379 | PropertyValuesHolder pvhY =
380 | PropertyValuesHolder.ofInt("y", mFloatView.getY(), mBuilder.yOffset);
381 | mAnimator = ObjectAnimator.ofPropertyValuesHolder(pvhX, pvhY);
382 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
383 | @Override
384 | public void onAnimationUpdate(ValueAnimator animation) {
385 | int x = (Integer) animation.getAnimatedValue("x");
386 | int y = (Integer) animation.getAnimatedValue("y");
387 | mFloatView.updateXY(x, y);
388 | if (mBuilder.mViewStateListener != null) {
389 | mBuilder.mViewStateListener.onPositionUpdate(x, y);
390 | }
391 | }
392 | });
393 | startAnimator();
394 | }
395 | lastX = event.getRawX();
396 | lastY = event.getRawY();
397 | }
398 |
399 | /**
400 | * 是否需要移动Y坐标
401 | *
402 | * @param startY 悬浮窗获取的位置
403 | * @param upY 真实针对屏幕的位置
404 | * @return
405 | */
406 | private boolean isNeedMoveY(int startY, float upY) {
407 | if (startY < 0 || upY > screenHeight) {
408 | return true;
409 | }
410 | return false;
411 | }
412 |
413 | /**
414 | * 获取Y的坐标
415 | *
416 | * @param startY 悬浮窗获取的位置
417 | * @param upY 真实针对屏幕的位置
418 | * @param height 组建高度
419 | * @return
420 | */
421 | private int getY(int startY, float upY, int height) {
422 | // 挨近顶部处理
423 | if (startY < 0) {
424 | // return (int)(upY + (0 - startY));
425 | // return height;
426 | return (int) Math.abs(startY - upY);
427 | }
428 | // 接近底部处理
429 | if (upY > screenHeight) {
430 | return (int) (screenHeight - height - (Math.abs(screenHeight - upY)));
431 | }
432 | return 0;
433 | }
434 | });
435 | }
436 | }
437 |
438 | /**
439 | * 初始化屏幕分辨率
440 | */
441 | private void checkScreenSize() {
442 | // if (screenWidth == 0) {
443 | screenWidth = ViewUtils.getScreenWidth(mBuilder.mApplicationContext);
444 | // }
445 | // if (screenHeight == 0) {
446 | screenHeight = ViewUtils.getScreenHeight(mBuilder.mApplicationContext);
447 | // }
448 | }
449 |
450 | private void cancelAnimator() {
451 | if (mAnimator != null && mAnimator.isRunning()) {
452 | mAnimator.cancel();
453 | }
454 | }
455 |
456 | // /**
457 | // * 判断是否超出范围,根据自己需求设置比例大小,我自己设置的是0.025和0.975
458 | // * 这是合并一个哥们 没有完善。暂时不使用
459 | // * https://github.com/yhaolpz/FloatWindow/pull/89/commits/f48b0ea10351246da43bf7e259855a91e1314dbc
460 | // *
461 | // * @param x event.getRawX()
462 | // * @param y event.getRawY()
463 | // * @return
464 | // */
465 | // private boolean isOutOfRange(float x, float y) {
466 | // boolean b = true;
467 | // // float screenWidth = ViewUtils.getScreenWidth(mBuilder.mApplicationContext);
468 | // // float screenHeight = ViewUtils.getScreenHeight(mBuilder.mApplicationContext);
469 | // float widthRate, heightRate;
470 | // widthRate = (screenWidth - x) / screenWidth;
471 | // heightRate = (screenHeight - y) / screenHeight;
472 | // if (widthRate > 0.025 && widthRate < 0.975 && heightRate > 0.025 && heightRate < 0.975) {
473 | // b = false;
474 | // } else {
475 | // b = true;
476 | // }
477 | // return b;
478 | // }
479 |
480 | private void startAnimator() {
481 | if (mBuilder.mInterpolator == null) {
482 | if (mDecelerateInterpolator == null) {
483 | mDecelerateInterpolator = new DecelerateInterpolator();
484 | }
485 | mBuilder.mInterpolator = mDecelerateInterpolator;
486 | }
487 | mAnimator.setInterpolator(mBuilder.mInterpolator);
488 | mAnimator.addListener(new AnimatorListenerAdapter() {
489 | @Override
490 | public void onAnimationEnd(Animator animation) {
491 | mAnimator.removeAllUpdateListeners();
492 | mAnimator.removeAllListeners();
493 | mAnimator = null;
494 | if (mBuilder.mViewStateListener != null) {
495 | mBuilder.mViewStateListener.onMoveAnimEnd();
496 | }
497 | }
498 | });
499 | mAnimator.setDuration(mBuilder.mDuration).start();
500 | if (mBuilder.mViewStateListener != null) {
501 | mBuilder.mViewStateListener.onMoveAnimStart();
502 | }
503 | }
504 |
505 | }
506 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/BaseFloatView.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * @Copyright © 2017 Analysys Inc. All rights reserved.
7 | * @Description:
8 | * @Version: 1.0.9
9 | * @Create: 2017-11-14 17:15:35
10 | * @Author: yhao
11 | */
12 | public abstract class BaseFloatView {
13 |
14 | public abstract void setSize(int width, int height);
15 |
16 | public abstract void setView(View view);
17 |
18 | public abstract void setGravity(int gravity, int xOffset, int yOffset);
19 |
20 | public abstract void init();
21 |
22 | public abstract void dismiss();
23 |
24 | public void updateXY(int x, int y) {
25 | }
26 |
27 | public void updateX(int x) {
28 | }
29 |
30 | public void updateY(int y) {
31 | }
32 |
33 | public int getX() {
34 | return 0;
35 | }
36 |
37 | public int getY() {
38 | return 0;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/BaseFloatWindow.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | import android.view.View;
4 |
5 | import com.yhao.floatwindow.enums.EScreen;
6 |
7 | /**
8 | * @Copyright © 2017 Analysys Inc. All rights reserved.
9 | * @Description: https://github.com/yhaolpz
10 | * @Version: 1.0
11 | * @Create: 2017/12/22 17:05:41
12 | * @Author: yhao
13 | */
14 | public abstract class BaseFloatWindow {
15 | public abstract void show();
16 |
17 | public abstract void hide();
18 |
19 | public abstract boolean isShowing();
20 |
21 | public abstract int getX();
22 |
23 | public abstract int getY();
24 |
25 | public abstract void updateX(int x);
26 |
27 | public abstract void updateX(EScreen screenType, float ratio);
28 |
29 | public abstract void updateY(int y);
30 |
31 | public abstract void updateY(EScreen screenType, float ratio);
32 |
33 | public abstract View getView();
34 |
35 | public abstract void dismiss();
36 |
37 | public abstract void destory();
38 | }
39 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/IConfigChanged.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | /**
4 | * @Copyright © 2019 sanbo Inc. All rights reserved.
5 | * @Description: 页面旋转回调
6 | * @Version: 1.0
7 | * @Create: 2019-10-15 14:47:56
8 | * @author: sanbo
9 | */
10 | public interface IConfigChanged {
11 | //页面旋转时回调
12 | public abstract void onActivityConfigChanged();
13 |
14 | // 跳出应用、打开应用回调,用于旋转优先级选择。
15 | public abstract void onBackToDesktop(boolean isBack);
16 | }
17 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/ISensorRotateChanged.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | public interface ISensorRotateChanged {
4 | public abstract void onRotateChanged();
5 | }
6 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/LifecycleListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | /**
4 | * @Copyright © 2017 Analysys Inc. All rights reserved.
5 | * @Description:
6 | * @Version: 1.0.9
7 | * @Create: 2017-11-14 17:15:35
8 | * @Author: yhao
9 | */
10 | public interface LifecycleListener {
11 |
12 | public void onShow();
13 |
14 | public void onHide();
15 |
16 | public void onBackToDesktop();
17 | }
18 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/ResumedListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | /**
4 | * @Copyright © 2017 Analysys Inc. All rights reserved.
5 | * @Description:
6 | * @Version: 1.0.9
7 | * @Create: 2017-11-14 17:15:35
8 | * @Author: yhao
9 | */
10 | public interface ResumedListener {
11 | public void onResumed();
12 | }
13 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/ViewStateListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | /**
4 | * @Copyright © 2017 Analysys Inc. All rights reserved.
5 | * @Description:
6 | * @Version: 1.0.9
7 | * @Create: 2017-11-14 17:15:35
8 | * @Author: yhao
9 | */
10 | public interface ViewStateListener {
11 | public void onPositionUpdate(int x, int y);
12 |
13 | public void onShow();
14 |
15 | public void onHide();
16 |
17 | public void onDismiss();
18 |
19 | public void onMoveAnimStart();
20 |
21 | public void onMoveAnimEnd();
22 |
23 | public void onBackToDesktop();
24 | }
25 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/interfaces/ViewStateListenerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.interfaces;
2 |
3 | /**
4 | * @Copyright © 2018 Analysys Inc. All rights reserved.
5 | * @Description:
6 | * @Version: 1.0.9
7 | * @Create: 2018/5/5 17:15:35
8 | * @Author: yhao
9 | */
10 | public class ViewStateListenerAdapter implements ViewStateListener {
11 | @Override
12 | public void onPositionUpdate(int x, int y) {
13 | }
14 |
15 | @Override
16 | public void onShow() {
17 | }
18 |
19 | @Override
20 | public void onHide() {
21 | }
22 |
23 | @Override
24 | public void onDismiss() {
25 | }
26 |
27 | @Override
28 | public void onMoveAnimStart() {
29 | }
30 |
31 | @Override
32 | public void onMoveAnimEnd() {
33 | }
34 |
35 | @Override
36 | public void onBackToDesktop() {
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/permission/PermissionListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.permission;
2 |
3 | /**
4 | * @Copyright © 2017 Analysys Inc. All rights reserved.
5 | * @Description:
6 | * @Version: 1.0.9
7 | * @Create: 2017/11/14 17:15:35
8 | * @Author: yhao
9 | */
10 | public interface PermissionListener {
11 | void onSuccess();
12 |
13 | void onFail();
14 | }
15 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/permission/PermissionUtil.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.permission;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.AppOpsManager;
5 | import android.content.Context;
6 | import android.graphics.PixelFormat;
7 | import android.os.Binder;
8 | import android.os.Build;
9 | import android.provider.Settings;
10 | import android.view.View;
11 | import android.view.WindowManager;
12 |
13 | import com.yhao.floatwindow.utils.L;
14 |
15 | import java.lang.reflect.Method;
16 |
17 | /**
18 | * @Copyright © 2017 Analysys Inc. All rights reserved.
19 | * @Description:
20 | * @Version: 1.0.9
21 | * @Create: 2017/12/29 17:15:35
22 | * @Author: yhao
23 | */
24 | public class PermissionUtil {
25 |
26 | public static boolean hasPermission(Context context) {
27 | if (Build.VERSION.SDK_INT >= 23) {
28 | return Settings.canDrawOverlays(context);
29 | } else {
30 | return hasPermissionBelowMarshmallow(context);
31 | }
32 | }
33 |
34 | public static boolean hasPermissionOnActivityResult(Context context) {
35 | if (Build.VERSION.SDK_INT >= 26) {
36 | return hasPermissionForO(context);
37 | }
38 | if (Build.VERSION.SDK_INT >= 23) {
39 | return Settings.canDrawOverlays(context);
40 | } else {
41 | return hasPermissionBelowMarshmallow(context);
42 | }
43 | }
44 |
45 | /**
46 | * 6.0以下判断是否有权限 理论上6.0以上才需处理权限, 但有的国内rom在6.0以下就添加了权限 其实此方式也可以用于判断6.0以上版本, 只不过有更简单的canDrawOverlays代替
47 | */
48 | public static boolean hasPermissionBelowMarshmallow(Context context) {
49 | try {
50 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
51 | Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int.class, String.class);
52 | // AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
53 | return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke(manager, 24, Binder.getCallingUid(),
54 | context.getApplicationContext().getPackageName());
55 | } catch (Exception e) {
56 | return false;
57 | }
58 | }
59 |
60 | /**
61 | * 用于判断8.0时是否有权限,仅用于OnActivityResult 针对8.0官方bug:在用户授予权限后Settings.canDrawOverlays或checkOp方法判断仍然返回false
62 | */
63 | @TargetApi(23)
64 | private static boolean hasPermissionForO(Context context) {
65 | try {
66 | WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
67 | if (mgr == null) {
68 | return false;
69 | }
70 | View viewToAdd = new View(context);
71 | WindowManager.LayoutParams params = new WindowManager.LayoutParams(0, 0,
72 | // android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O
73 | // ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
74 | // : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
75 | // WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
76 | // PixelFormat.TRANSPARENT);
77 | android.os.Build.VERSION.SDK_INT >= 26 ? 2038 : 2003, 0x00000010 | 0x00000008, PixelFormat.TRANSPARENT);
78 | viewToAdd.setLayoutParams(params);
79 | mgr.addView(viewToAdd, params);
80 | mgr.removeView(viewToAdd);
81 | return true;
82 | } catch (Exception e) {
83 | L.e("hasPermissionForO e:" + e.toString());
84 | }
85 | return false;
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/FwContent.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 | /**
4 | * @Copyright © 2019 sanbo Inc. All rights reserved.
5 | * @Description: 常量
6 | * @Version: 1.0
7 | * @Create: 2019-10-17 15:19:24
8 | * @author: sanbo
9 | */
10 | public class FwContent {
11 |
12 | public static final String VERSION = "v1.10.0";
13 | public static final String DEFAULT_TAG = "default_float_window_tag";
14 | public static final boolean isDebug = false;
15 | }
16 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/L.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Intent;
6 | import android.os.BaseBundle;
7 | import android.os.Build;
8 | import android.os.Bundle;
9 | import android.os.Message;
10 | import android.text.TextUtils;
11 | import android.util.Log;
12 | import android.util.SparseArray;
13 |
14 | import org.json.JSONArray;
15 | import org.json.JSONException;
16 | import org.json.JSONObject;
17 |
18 | import java.io.PrintWriter;
19 | import java.io.StringReader;
20 | import java.io.StringWriter;
21 | import java.lang.ref.Reference;
22 | import java.lang.reflect.Field;
23 | import java.lang.reflect.Modifier;
24 | import java.util.ArrayList;
25 | import java.util.Arrays;
26 | import java.util.Collection;
27 | import java.util.Iterator;
28 | import java.util.List;
29 | import java.util.Locale;
30 | import java.util.Map;
31 | import java.util.Set;
32 | import java.util.regex.Matcher;
33 | import java.util.regex.Pattern;
34 |
35 | import javax.xml.transform.OutputKeys;
36 | import javax.xml.transform.Source;
37 | import javax.xml.transform.Transformer;
38 | import javax.xml.transform.TransformerFactory;
39 | import javax.xml.transform.stream.StreamResult;
40 | import javax.xml.transform.stream.StreamSource;
41 |
42 | /**
43 | * @Copyright © 2015 sanbo Inc. All rights reserved.
44 | * @Description
45 | * Log统一管理类,提供功能:
46 | * 1.log工具类支持全部打印 「支持Log的所有功能.」
47 | * 2.支持类似C的格式化输出或Java的String.format「%个数和参数个数需要一直才能格式化」
48 | * 3.支持Java堆栈打印
49 | * 4.支持键入和不键入TAG 「不键入tag,tag是sanbo,默认第一个参数String为tag」
50 | * 5.支持shell控制log是否打印.
51 | * tag为sanbo的控制命令:setprop log.tag.sanbo log等级.
52 | * log等级:VERBOSE/DEBUG/INFO/WARN/ERROR/ASSERT
53 | * 6.格式化输出.
54 | * 7.支持XML/JSON/Map/Array等更多对象打印
55 | *
56 | * @Version: 6.1
57 | * @Create: 2015年6月18日 下午4:14:01
58 | * @Author: sanbo
59 | */
60 | public class L {
61 |
62 | // 解析属性最大层级
63 | public static final int MAX_CHILD_LEVEL = 3;
64 | // 换行符
65 | public static final String BR = System.getProperty("line.separator");
66 | private static final int JSON_INDENT = 2;
67 | // 是否打印bug.建议在application中调用init接口初始化
68 | public static boolean USER_DEBUG = true;
69 | // 是否接受shell控制打印
70 | private static boolean isShellControl = true;
71 | // 是否打印详细log,详细打印调用的堆栈
72 | private static boolean isNeedCallstackInfo = false;
73 | // 是否按照条形框输出,有包裹域的输出
74 | private static boolean isNeedWrapper = false;
75 | // 是否格式化展示,主要针对JSON
76 | private static boolean isFormat = false;
77 | // 默认tag
78 | private static String DEFAULT_TAG = "FloatWindow";
79 | // 临时tag.用法:调用log中大于1个参数,且第一个参数为字符串,且不是format用法,字符串长度没超过协议值,此时启用临时tag
80 | private static String TEMP_TAG = "";
81 | // 规定每段显示的长度.每行最大日志长度 (Android Studio3.1最多2902字符)
82 | private static int LOG_MAXLENGTH = 2900;
83 | // 类名(getClassName).方法名(getMethodName)[行号(getLineNumber)]
84 | private static String content_simple_callstack = "简易调用堆栈: %s.%s[%d]";
85 | // 格式化时,行首封闭符
86 | private static String CONTENT_LINE = "║ ";
87 | // 空格
88 | private static String CONTENT_SPACE = " ";
89 | private static String CONTENT_LOG_INFO = "log info:";
90 | private static String CONTENT_LOG_EMPTY = "打印的日志信息为空!";
91 | private static String content_title_begin =
92 | "╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════";
93 | private static String content_title_info_callstack =
94 | "╔══════════════════════════════════════════════════════════════调用详情══════════════════════════════════════════════════════════════";
95 | private static String content_title_info_log =
96 | "╔══════════════════════════════════════════════════════════════日志详情══════════════════════════════════════════════════════════════";
97 | private static String content_title_info_error =
98 | "╔══════════════════════════════════════════════════════════════异常详情══════════════════════════════════════════════════════════════";
99 | private static String content_title_info_type = "╔════════════════════════════════════════════════════「%s"
100 | + "」════════════════════════════════════════════════════";
101 | private static String content_title_end =
102 | "╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════";
103 | /**
104 | * 行首为该符号时,不增加行首封闭符
105 | */
106 | private static String CONTENT_A = CONTENT_LINE;
107 | private static String CONTENT_B = "╔";
108 | private static String CONTENT_C = "╚";
109 | private static String CONTENT_D = " ╔";
110 | private static String CONTENT_E = " ╚";
111 | private static String CONTENT_WARNNING_SHELL =
112 | "Wranning....不够打印级别,请在命令行设置指令后重新尝试打印,命令行指令: adb shell setprop log.tag." + DEFAULT_TAG + " ";
113 | private static Character FORMATER = '%';
114 | private static List names = Arrays.asList(new String[]{"$change", "this$0"});
115 |
116 | private L() {
117 | }
118 |
119 | /*********************************************************************************************************/
120 | /**
121 | * 支持可变参数打印,根据不同的结构支持. 可以统一成一个接口
122 | */
123 |
124 | /**
125 | * 初始化接口
126 | *
127 | * @param showLog 是否展示log,默认展示
128 | * @param shellControl 是否使用shell控制log动态打印.默认不使用. shell设置方式:setprop log.tag.sanbo INFO
129 | * 最后一个参数为log等级,可选项目:VERBOSE/DEBUG/INFO/WARN/ERROR/ASSERT
130 | * @param needWarpper 是否需要格式化输出
131 | * @param needCallStackInfo 是否需要打印详细的堆栈调用信息.
132 | * @param format 是否需要格式化.
133 | * @param defaultTag android logcat的tag一个意义,不设置默认的tag为"sanbo"
134 | */
135 | public static void init(boolean showLog, boolean shellControl, boolean needWarpper, boolean needCallStackInfo,
136 | boolean format, String defaultTag) {
137 | USER_DEBUG = showLog;
138 | isShellControl = shellControl;
139 | isNeedWrapper = needWarpper;
140 | isNeedCallstackInfo = needCallStackInfo;
141 | isFormat = format;
142 | if (!TextUtils.isEmpty(defaultTag)) {
143 | DEFAULT_TAG = defaultTag;
144 | }
145 | }
146 |
147 | /*********************************************************************************************************/
148 | public static void v(Object... args) {
149 | if (isShellControl) {
150 | if (!Log.isLoggable(DEFAULT_TAG, Log.VERBOSE)) {
151 | Log.v(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "VERBOSE");
152 | return;
153 | }
154 | }
155 | parserArgsMain(MLEVEL.VERBOSE, args);
156 | }
157 |
158 | public static void d(Object... args) {
159 | if (isShellControl) {
160 | if (!Log.isLoggable(DEFAULT_TAG, Log.DEBUG)) {
161 | Log.d(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "DEBUG");
162 | return;
163 | }
164 | }
165 | parserArgsMain(MLEVEL.DEBUG, args);
166 | }
167 |
168 | public static void i(Object... args) {
169 | if (isShellControl) {
170 | if (!Log.isLoggable(DEFAULT_TAG, Log.INFO)) {
171 | Log.i(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "INFO");
172 | return;
173 | }
174 | }
175 | parserArgsMain(MLEVEL.INFO, args);
176 | }
177 |
178 | public static void w(Object... args) {
179 | if (isShellControl) {
180 | if (!Log.isLoggable(DEFAULT_TAG, Log.WARN)) {
181 | Log.w(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "WARN");
182 | return;
183 | }
184 | }
185 | parserArgsMain(MLEVEL.WARN, args);
186 | }
187 |
188 | public static void e(Object... args) {
189 | if (isShellControl) {
190 | if (!Log.isLoggable(DEFAULT_TAG, Log.ERROR)) {
191 | Log.e(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "ERROR");
192 | return;
193 | }
194 | }
195 | parserArgsMain(MLEVEL.ERROR, args);
196 | }
197 |
198 | public static void wtf(Object... args) {
199 | if (isShellControl) {
200 | if (!Log.isLoggable(DEFAULT_TAG, Log.ASSERT)) {
201 | Log.wtf(DEFAULT_TAG, CONTENT_WARNNING_SHELL + "ASSERT");
202 | return;
203 | }
204 | }
205 | parserArgsMain(MLEVEL.WTF, args);
206 | }
207 |
208 | /**
209 | * 解析参数入口.这步骤开始忽略类型.解析所有参数,参数检查逻辑: 1.是否为String,若为String,则先判断是否格式化输出,不是再进行字符串转换格式尝试 2.对象其他类型判断:
210 | * StringBuffer>StringBuild>Throwable>Intent>List>Map
211 | *
212 | * @param level
213 | * @param args
214 | */
215 | private static void parserArgsMain(int level, Object[] args) {
216 |
217 | /*
218 | * 确认打印
219 | */
220 | if (!USER_DEBUG) {
221 | Log.e(DEFAULT_TAG, "请确认Log工具类已经设置打印!");
222 | return;
223 | }
224 |
225 | StringBuilder sb = new StringBuilder();
226 | // 开始
227 |
228 | if (isFormat) {
229 | sb.append(CONTENT_LOG_INFO).append("\n");
230 | }
231 | String stackinfo = getCallStaceInfo();
232 | if (!TextUtils.isEmpty(stackinfo)) {
233 | sb.append(stackinfo).append("\n");
234 | }
235 |
236 | if (args[0] instanceof String) {
237 | // if (isNeedWrapper) {
238 | // sb.append(content_title_info_log).append("\n");
239 | // }
240 | String one = (String) args[0];
241 | // 解析fromat
242 | if (one.contains(String.valueOf(FORMATER)) && args.length > 1) {
243 |
244 | /*
245 | * 参数解析
246 | */
247 | Object[] temp = new Object[args.length - 1];
248 | for (int i = 1; i < args.length; i++) {
249 | temp[i - 1] = args[i];
250 | }
251 |
252 | // 查找%个数
253 | Pattern p = Pattern.compile("%", Pattern.CASE_INSENSITIVE);
254 | Matcher m = p.matcher(one);
255 | int count = 0;
256 | while (m.find()) {
257 | count++;
258 | }
259 |
260 | /**
261 | * %和后面参数一样,则格式化,否则不进行格式化
262 | */
263 | if (count == temp.length) {
264 | // 格式化操作
265 | String log = String.format(Locale.getDefault(), one, temp);
266 | if (isNeedWrapper) {
267 | sb.append(content_title_info_log).append("\n");
268 | }
269 | sb.append(wrapperString(log)).append("\n");
270 | } else {
271 | if (isNeedWrapper) {
272 | sb.append(content_title_info_log).append("\n");
273 | }
274 | StringBuilder tempSB = new StringBuilder();
275 | for (Object obj : args) {
276 | // 解析成字符串,添加
277 | String tempStr = objectToString(obj);
278 | // Log.i(DEFAULT_TAG, "tempStr:" + tempStr);
279 | if (!TextUtils.isEmpty(tempStr)) {
280 | // sb.append(nativeWrapperString(temp)).append("\n");
281 | tempSB.append(tempStr).append("\t");
282 | }
283 | }
284 | sb.append(wrapperString(tempSB.toString())).append("\n");
285 | }
286 | } else {
287 | // 不符合format规则数据
288 | if (args.length > 1) {
289 | // 大于一次参数,第一个参数是字符串,默认是tag
290 | String log = processTagCase(args);
291 | if (!TextUtils.isEmpty(log)) {
292 | sb.append(wrapperString(log)).append("\n");
293 | } else {
294 | // 需要支持打印""或者null
295 | sb.append(wrapperString("")).append("\n");
296 | }
297 | } else {
298 | if (isNeedWrapper) {
299 | sb.append(content_title_info_log).append("\n");
300 | }
301 | sb.append(wrapperString(one)).append("\n");
302 | }
303 | }
304 | } else {
305 |
306 | for (Object obj : args) {
307 | // 解析成字符串,添加
308 | String temp = processObjectCase(obj);
309 | // Log.i(DEFAULT_TAG, "temp:" + temp);
310 | if (!TextUtils.isEmpty(temp)) {
311 | // sb.append(nativeWrapperString(temp)).append("\n");
312 | sb.append(temp).append("\n");
313 | }
314 | }
315 | }
316 | // 结束,标记结束符
317 | if (isNeedWrapper) {
318 | sb.append(content_title_end);
319 | }
320 | // 打印字符
321 | preparePrint(level, sb.toString());
322 |
323 | }
324 |
325 | /*********************************************************************************************************/
326 | /**
327 | * 基础工具方法
328 | */
329 | /*********************************************************************************************************/
330 |
331 | /**
332 | * 处理对象
333 | *
334 | * @param obj
335 | * @return
336 | */
337 | private static String processObjectCase(Object obj) {
338 |
339 | StringBuilder sb = new StringBuilder();
340 | try {
341 | // 1.解析对象
342 | String result = objectToString(obj);
343 | if (!TextUtils.isEmpty(result)) {
344 | // 2.打印行头
345 | header(obj, sb);
346 | // 3.打印内容
347 | sb.append(wrapperString(result));// .append("\n");
348 | } else {
349 | // 需要支持""或null
350 | if (isNeedWrapper) {
351 | sb.append(content_title_info_log).append("\n");
352 | }
353 | sb.append(wrapperString(""));// .append("\n");
354 | }
355 | } catch (Throwable e) {
356 | e.printStackTrace();
357 | }
358 | return sb.toString();
359 | }
360 |
361 | /**
362 | *
363 | * 只有第一个参数为字符串且不是格式化的情况下才会进入该方法.
364 | * 该方法是负责处理tag或者message的情况. 主要要支持多重格式:
365 | * 1.L.x(TAG,Object);
366 | * 2.L.x(msg,Object);
367 | * 默认第一个参数为字符串且参数大于2个,第一个参数就为tag
368 | *
369 | *
370 | * @param args
371 | * @return
372 | */
373 | private static String processTagCase(Object[] args) {
374 | String one = (String) args[0];
375 | StringBuilder sb = new StringBuilder();
376 | TEMP_TAG = one;
377 | for (int i = 1; i < args.length; i++) {
378 | sb.append(processObjectCase(args[i])).append("\n");
379 | }
380 | return sb.toString();
381 | }
382 |
383 | /**
384 | * 打印行头
385 | *
386 | * @param obj
387 | * @param sb
388 | */
389 | private static void header(Object obj, StringBuilder sb) {
390 | if (isNeedWrapper) {
391 | if (obj instanceof String) {
392 | sb.append(content_title_info_log).append("\n");
393 | } else if (obj instanceof Throwable) {
394 | sb.append(content_title_info_error).append("\n");
395 | } else {
396 | sb.append(String.format(content_title_info_type, obj.getClass().getName())).append("\n");
397 | }
398 | }
399 | }
400 |
401 | /**
402 | * 处理堆栈信息
403 | *
404 | * @return
405 | */
406 | private static String getCallStaceInfo() {
407 | Exception callStack = new Exception("debug_info call stack.");
408 | StringBuilder sb = new StringBuilder();
409 | StackTraceElement stackElement[] = Thread.currentThread().getStackTrace();
410 | // 现在文件
411 | boolean currentFile = false;
412 | // 现在文件多重调用
413 | boolean isKeeping = false;
414 | for (StackTraceElement ste : stackElement) {
415 | if (currentFile && !isKeeping) {
416 | break;
417 | }
418 | if (ste.getClassName().equals(L.class.getName())) {
419 | if (!currentFile) {
420 | currentFile = true;
421 | }
422 | isKeeping = true;
423 | continue;
424 | } else {
425 | if (currentFile) {
426 |
427 | // 堆栈的错误第一行可以不要
428 | String cc = null;
429 | if (isNeedCallstackInfo) {
430 | cc = parseString(callStack);
431 | String[] tempArray = cc.split("\n");
432 | StringBuilder tempSB = new StringBuilder();
433 | for (int i = 1; i < tempArray.length; i++) {
434 | tempSB.append(CONTENT_SPACE).append(CONTENT_SPACE).append(CONTENT_SPACE)
435 | .append(tempArray[i]);
436 | if (i != tempArray.length - 1) {
437 | tempSB.append("\n");
438 | }
439 | }
440 | cc = tempSB.toString();
441 | }
442 |
443 | if (isNeedWrapper) {
444 | if (isNeedCallstackInfo) {
445 |
446 | sb.append("\n").append(content_title_info_callstack).append("\n").append(CONTENT_LINE)
447 | .append(CONTENT_SPACE).append("文件名: " + ste.getFileName()).append("\n")
448 | .append(CONTENT_LINE).append(CONTENT_SPACE).append("类名: " + ste.getClassName())
449 | .append("\n").append(CONTENT_LINE).append(CONTENT_SPACE)
450 | .append("方法名: " + ste.getMethodName()).append("\n").append(CONTENT_LINE)
451 | .append(CONTENT_SPACE).append("行号: " + ste.getLineNumber()).append("\n")
452 | .append(CONTENT_LINE).append(CONTENT_SPACE)
453 | .append("Native方法:" + (!ste.isNativeMethod() ? "不是" : "是")).append("\n")
454 | .append(CONTENT_LINE).append(CONTENT_SPACE).append("调用堆栈详情:").append("\n")
455 | .append(wrapperString(cc));
456 | } else {
457 | sb.append("\n").append(content_title_begin).append("\n").append(CONTENT_LINE)
458 | .append(String.format(content_simple_callstack, ste.getClassName(), ste.getMethodName(),
459 | ste.getLineNumber()));
460 | // 上一层会处理
461 | // .append("\n");
462 | }
463 | } else {
464 | if (isNeedCallstackInfo) {
465 | sb.append("文件名: " + ste.getFileName()).append("\n")
466 | .append("类名: " + ste.getClassName()).append("\n")
467 | .append("方法名: " + ste.getMethodName()).append("\n")
468 | .append("行号: " + ste.getLineNumber()).append("\n")
469 | .append("Native方法:" + (!ste.isNativeMethod() ? "不是" : "是")).append("\n")
470 | .append("调用堆栈详情:").append("\n").append(wrapperString(cc));
471 | } else {
472 | if (isFormat) {
473 | sb.append(String.format(content_simple_callstack, ste.getClassName(),
474 | ste.getMethodName(), ste.getLineNumber()));
475 | }
476 | }
477 | }
478 |
479 | isKeeping = false;
480 | break;
481 | }
482 | }
483 | }
484 | currentFile = false;
485 | isKeeping = false;
486 | callStack = null;
487 | stackElement = null;
488 | return sb.toString();
489 | }
490 |
491 | /*********************************************************************************************************/
492 | /**
493 | * 解析对象成字符串
494 | */
495 |
496 | /*********************************************************************************************************/
497 | private static String objectToString(Object object) {
498 | return objectToString(object, 0);
499 | }
500 |
501 | /**
502 | * 是否为静态内部类
503 | *
504 | * @param cla
505 | * @return
506 | */
507 | private static boolean isStaticInnerClass(Class> cla) {
508 | if (cla != null && cla.isMemberClass()) {
509 | int modifiers = cla.getModifiers();
510 | return (modifiers & Modifier.STATIC) == Modifier.STATIC;
511 | }
512 | return false;
513 | }
514 |
515 | /**
516 | * 根据类型匹配
517 | *
518 | * @param object
519 | * @param childLevel
520 | * @return
521 | */
522 | private static String objectToString(Object object, int childLevel) {
523 | if (object == null) {
524 | return null;
525 | }
526 | if (childLevel > MAX_CHILD_LEVEL) {
527 | return object.toString();
528 | }
529 | // 支持的类型.单独处理
530 | Class> czz = object.getClass();
531 |
532 | if (Build.VERSION.SDK_INT > 20) {
533 | if (BaseBundle.class.isAssignableFrom(czz)) {
534 | BaseBundle bundle = (BaseBundle) object;
535 | return parseString(bundle);
536 | }
537 | } else {
538 | if (Bundle.class.isAssignableFrom(czz)) {
539 | Bundle bundle = (Bundle) object;
540 | return parseString(bundle);
541 | }
542 | }
543 | if (String.class.isAssignableFrom(czz)) {
544 | String obj = (String) object;
545 | return parseString(obj);
546 | } else if (Number.class.isAssignableFrom(czz)) {
547 | Number obj = (Number) object;
548 | return String.valueOf(obj);
549 | } else if (Intent.class.isAssignableFrom(czz)) {
550 | Intent obj = (Intent) object;
551 | return parseString(obj);
552 | } else if (Collection.class.isAssignableFrom(czz)) {
553 | Collection> obj = (Collection>) object;
554 | return parseString(obj);
555 | } else if (Map.class.isAssignableFrom(czz)) {
556 | Map, ?> obj = (Map, ?>) object;
557 | return parseString(obj);
558 | } else if (Throwable.class.isAssignableFrom(czz)) {
559 | Throwable obj = (Throwable) object;
560 | return parseString(obj);
561 | } else if (Reference.class.isAssignableFrom(czz)) {
562 | Reference> obj = (Reference>) object;
563 | return parseString(obj);
564 | } else if (Message.class.isAssignableFrom(czz)) {
565 | Message obj = (Message) object;
566 | return parseString(obj);
567 | // } else if (isSubClass(czz, Activity.class)) {
568 | } else if (Activity.class.isAssignableFrom(czz)) {
569 | Activity obj = (Activity) object;
570 | return parseString(obj);
571 | } else if (JSONArray.class.isAssignableFrom(czz)) {
572 | JSONArray obj = (JSONArray) object;
573 | return format(obj);
574 | } else if (JSONObject.class.isAssignableFrom(czz)) {
575 | JSONObject obj = (JSONObject) object;
576 | return format(obj);
577 | } else if (StringBuilder.class.isAssignableFrom(czz)) {
578 | StringBuilder obj = (StringBuilder) object;
579 | return obj.toString();
580 | } else if (StringBuffer.class.isAssignableFrom(czz)) {
581 | StringBuffer obj = (StringBuffer) object;
582 | return obj.toString();
583 | } else if (Class.class.isAssignableFrom(czz)) {
584 | return parseStringByObject(object, childLevel);
585 | } else if (isArray(object)) {
586 | StringBuilder result = new StringBuilder();
587 | traverseArray(result, object);
588 | return result.toString();
589 | } else {
590 | if (object.toString().startsWith(object.getClass().getName() + "@")) {
591 | return parseStringByObject(object, childLevel);
592 | } else {
593 | // 若对象重写toString()方法默认走toString()
594 | return object.toString();
595 | }
596 | }
597 | }
598 |
599 | /**
600 | * 拼接class的字段和值
601 | *
602 | * @param cla
603 | * @param obj
604 | * @param o 对象
605 | * @param childOffset 递归解析属性的层级
606 | */
607 | private static void getClassFields(Class> cla, JSONObject obj, Object o, int childOffset) {
608 | try {
609 | if (cla.equals(Object.class)) {
610 | return;
611 | }
612 | // if (isSubClass) {
613 | // builder.append(BR + BR + "=> ");
614 | // }
615 | Field[] fields = cla.getDeclaredFields();
616 | for (int i = 0; i < fields.length; ++i) {
617 | Field field = fields[i];
618 | field.setAccessible(true);
619 | if (cla.isMemberClass() && !isStaticInnerClass(cla) && i == 0) {
620 | continue;
621 | }
622 |
623 | if ("$change".equals(field.getName())) {
624 | continue;
625 | }
626 | // 解决Instant Run情况下内部类死循环的问题
627 | // System.out.println(field.getName()+ "***" +subObject.getClass() + "啊啊啊啊啊啊" +
628 | // cla);
629 | if (!isStaticInnerClass(cla)
630 | && ("$change".equals(field.getName()) || "this$0".equalsIgnoreCase(field.getName()))) {
631 | continue;
632 | }
633 | Object subObject = null;
634 | try {
635 | subObject = field.get(o);
636 | } catch (IllegalAccessException e) {
637 | subObject = e;
638 | } finally {
639 | if (subObject != null) {
640 |
641 | if (childOffset < MAX_CHILD_LEVEL) {
642 | if (!Number.class.isAssignableFrom(subObject.getClass())) {
643 | subObject = objectToString(subObject, childOffset + 1);
644 | String s = (String) subObject;
645 | s = s.replaceAll("\n", "").replaceAll("\r", "").replaceAll("\r\n", "");
646 | try {
647 | JSONObject temp = new JSONObject(s);
648 | obj.put(field.getName(), temp);
649 | } catch (Throwable e) {
650 | try {
651 | JSONArray arr = new JSONArray(s);
652 | obj.put(field.getName(), arr);
653 | } catch (Throwable e2) {
654 | obj.put(field.getName(), s);
655 | }
656 | }
657 | } else {
658 | obj.put(field.getName(), subObject);
659 | }
660 | } else {
661 | obj.put(field.getName(), subObject.toString());
662 | }
663 | } else {
664 | obj.put(field.getName(), "null");
665 | }
666 | }
667 | }
668 | } catch (Throwable e) {
669 | e.printStackTrace();
670 | }
671 | }
672 |
673 | /**
674 | * 获取数组的纬度
675 | *
676 | * @param object
677 | * @return
678 | */
679 | private static int getArrayDimension(Object object) {
680 | int dim = 0;
681 | for (int i = 0; i < object.toString().length(); ++i) {
682 | if (object.toString().charAt(i) == '[') {
683 | ++dim;
684 | } else {
685 | break;
686 | }
687 | }
688 | return dim;
689 | }
690 |
691 | /**
692 | * 是否为数组
693 | *
694 | * @param object
695 | * @return
696 | */
697 | private static boolean isArray(Object object) {
698 | return object.getClass().isArray();
699 | }
700 |
701 | /**
702 | * 获取数组类型
703 | *
704 | * @param object 如L为int型
705 | * @return
706 | */
707 | private static char getType(Object object) {
708 | if (isArray(object)) {
709 | String str = object.toString();
710 | return str.substring(str.lastIndexOf("[") + 1, str.lastIndexOf("[") + 2).charAt(0);
711 | }
712 | return 0;
713 | }
714 |
715 | /**
716 | * 遍历数组
717 | *
718 | * @param result
719 | * @param array
720 | */
721 | private static void traverseArray(StringBuilder result, Object array) {
722 | if (isArray(array)) {
723 | if (getArrayDimension(array) == 1) {
724 | switch (getType(array)) {
725 | case 'I':
726 | result.append(Arrays.toString((int[]) array));
727 | break;
728 | case 'D':
729 | result.append(Arrays.toString((double[]) array));
730 | break;
731 | case 'Z':
732 | result.append(Arrays.toString((boolean[]) array));
733 | break;
734 | case 'B':
735 | result.append(Arrays.toString((byte[]) array));
736 | break;
737 | case 'S':
738 | result.append(Arrays.toString((short[]) array));
739 | break;
740 | case 'J':
741 | result.append(Arrays.toString((long[]) array));
742 | break;
743 | case 'F':
744 | result.append(Arrays.toString((float[]) array));
745 | break;
746 | case 'C':
747 | result.append(Arrays.toString((char[]) array));
748 | break;
749 | case 'L':
750 | Object[] objects = (Object[]) array;
751 | result.append("[");
752 | for (int i = 0; i < objects.length; ++i) {
753 | result.append(objectToString(objects[i]));
754 | if (i != objects.length - 1) {
755 | result.append(",");
756 | }
757 | }
758 | result.append("]");
759 | break;
760 | default:
761 | result.append(Arrays.toString((Object[]) array));
762 | break;
763 | }
764 | } else {
765 | result.append("[");
766 | for (int i = 0; i < ((Object[]) array).length; i++) {
767 | traverseArray(result, ((Object[]) array)[i]);
768 | if (i != ((Object[]) array).length - 1) {
769 | result.append(",");
770 | }
771 | }
772 | result.append("]");
773 | }
774 | } else {
775 | result.append("not a array!!");
776 | }
777 | }
778 |
779 | private static String parseStringByObject(Object object, int childLevel) {
780 | try {
781 | JSONObject obj = new JSONObject();
782 | getClassFields(object.getClass(), obj, object, childLevel);
783 | Class> superClass = object.getClass().getSuperclass();
784 | if (superClass != null) {
785 | while (!superClass.equals(Object.class)) {
786 | getClassFields(superClass, obj, object, childLevel);
787 | superClass = superClass.getSuperclass();
788 | }
789 | } else {
790 | obj.put("toString", object.toString());
791 | }
792 | return format(obj);
793 | } catch (Throwable e) {
794 | return object.toString();
795 | }
796 | }
797 |
798 | private static String parseString(Activity activity) {
799 | JSONObject obj = new JSONObject();
800 | // Field[] fields = activity.getClass().getDeclaredFields();
801 | Field[] fields = activity.getClass().getFields();
802 | for (Field f : fields) {
803 | f.setAccessible(true);
804 | if ("org.aspectj.lang.JoinPoint$StaticPart".equals(f.getType().getName())) {
805 | continue;
806 | }
807 | if (names.contains(f.getName())) {
808 | continue;
809 | }
810 | try {
811 | Object fieldValue = f.get(activity);
812 | obj.put(f.getName(), objectToString(fieldValue));
813 | } catch (Throwable e) {
814 | e.printStackTrace();
815 | }
816 | }
817 |
818 | StringBuilder builder = new StringBuilder(activity.getClass().getName());
819 | builder.append(" {");
820 | builder.append(BR);
821 | for (Field field : fields) {
822 | field.setAccessible(true);
823 | if ("org.aspectj.lang.JoinPoint$StaticPart".equals(field.getType().getName())) {
824 | continue;
825 | }
826 | if (names.contains(field.getName())) {
827 | continue;
828 | }
829 | try {
830 | Object fieldValue = field.get(activity);
831 | builder.append(field.getName()).append("=>").append(objectToString(fieldValue)).append(BR);
832 | } catch (IllegalAccessException e) {
833 | e.printStackTrace();
834 | }
835 | }
836 | builder.append("}");
837 | Log.d("www", builder.toString());
838 | return format(obj);
839 | }
840 |
841 | private static String parseString(Message message) {
842 | if (message == null) {
843 | return null;
844 | }
845 | JSONObject obj = new JSONObject();
846 | try {
847 | obj.put("what", message.what);
848 | obj.put("when", message.getWhen());
849 | obj.put("arg1", message.arg1);
850 | obj.put("arg2", message.arg2);
851 | obj.put("data", parseString(message.getData()));
852 | obj.put("obj", objectToString(message.obj));
853 | } catch (Exception e) {
854 | e.printStackTrace();
855 | }
856 | return format(obj);
857 | }
858 |
859 | private static String parseString(Reference> reference) {
860 | Object actual = reference.get();
861 | return objectToString(actual);
862 | }
863 |
864 | private static String parseString(Map, ?> map) {
865 | JSONObject obj = new JSONObject();
866 | Set> keys = map.keySet();
867 | for (Object key : keys) {
868 | try {
869 | Object value = map.get(key);
870 | if (key == null) {
871 | key = "null";
872 | }
873 | if (value != null) {
874 | obj.put(objectToString(key), objectToString(value));
875 | } else {
876 | obj.put(objectToString(key), "null");
877 | }
878 | } catch (Throwable e) {
879 | e.printStackTrace();
880 | }
881 | }
882 | return format(obj);
883 |
884 | }
885 |
886 | private static String parseString(Collection> collection) {
887 |
888 | JSONArray arr = new JSONArray();
889 | Iterator> it = collection.iterator();
890 | while (it.hasNext()) {
891 | Object o = it.next();
892 | arr.put(objectToString(o));
893 | }
894 | return format(arr);
895 | }
896 |
897 | @TargetApi(21)
898 | private static String parseString(BaseBundle bundle) {
899 | if (bundle != null) {
900 | JSONObject bun = new JSONObject();
901 | for (String key : bundle.keySet()) {
902 | try {
903 | bun.put(key, objectToString(bundle.get(key)));
904 | } catch (Throwable e) {
905 | e.printStackTrace();
906 | }
907 | }
908 | return format(bun);
909 | }
910 | return null;
911 | }
912 |
913 | private static String parseString(Bundle bundle) {
914 | if (bundle != null) {
915 | JSONObject bun = new JSONObject();
916 | for (String key : bundle.keySet()) {
917 | try {
918 | bun.put(key, objectToString(bundle.get(key)));
919 | } catch (Throwable e) {
920 | e.printStackTrace();
921 | }
922 | }
923 | return format(bun);
924 | }
925 | return null;
926 | }
927 |
928 | /**
929 | * 处理对象且返回.处理顺序是: JSONObject>JSONArray>XML
930 | *
931 | * @param src
932 | * @return
933 | */
934 | private static String parseString(String src) {
935 | try {
936 | JSONObject oo = new JSONObject(src);
937 | return format(oo);
938 | } catch (JSONException e1) {
939 | // 不是JSONObject
940 | try {
941 | JSONArray arr = new JSONArray(src);
942 | return format(arr);
943 | } catch (JSONException e2) {
944 | // 不是JSONArray
945 |
946 | StringReader reader = null;
947 | try {
948 | reader = new StringReader(src);
949 | Source xmlInput = new StreamSource(reader);
950 | StreamResult xmlOutput = new StreamResult(new StringWriter());
951 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
952 | transformer.setOutputProperty(OutputKeys.INDENT, "yes");
953 | transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
954 | transformer.transform(xmlInput, xmlOutput);
955 | String xml = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
956 | return xml;
957 | } catch (Throwable e3) {
958 | // 不是XML
959 | return src;
960 | } finally {
961 | if (reader != null) {
962 | reader.close();
963 | }
964 | }
965 | }
966 | } catch (Throwable e) {
967 | return src;
968 | }
969 | }
970 |
971 | /**
972 | * 将异常信息打印出来。出来数据是带行前的双竖线(如果设置wrapper是true),不带头
973 | *
974 | * @param e
975 | * @return
976 | */
977 | private static String parseString(Throwable e) {
978 | // 可以使用系统的这个接口
979 | // return Log.getStackTraceString(e);
980 | // 自己实现堆栈,较之官方的增加了两个字符的缩进
981 | StringWriter sw = null;
982 | PrintWriter pw = null;
983 | StringBuilder sb = new StringBuilder();
984 | try {
985 | sw = new StringWriter();
986 | pw = new PrintWriter(sw);
987 | e.printStackTrace(pw);
988 | pw.flush();
989 | sw.flush();
990 | String result = sw.toString();
991 | String[] ss = result.split("\n");
992 | String s = null;
993 | for (int i = 0; i < ss.length; i++) {
994 | s = ss[i];
995 | // 一般首第一个字符不知道是什么东西
996 | if ("at".equalsIgnoreCase(s.substring(1, 3))) {
997 | // 部分堆栈怕其他行缩进失误
998 | // if (i > 0) {
999 | sb.append(CONTENT_SPACE).append(s);
1000 | } else {
1001 | sb.append(s);
1002 | }
1003 | if (i != ss.length - 1) {
1004 | sb.append("\n");
1005 | }
1006 | }
1007 | } catch (Throwable error) {
1008 | error.printStackTrace();
1009 | } finally {
1010 | if (sw != null) {
1011 | try {
1012 | sw.close();
1013 | } catch (Throwable e1) {
1014 | e1.printStackTrace();
1015 | }
1016 | }
1017 | if (pw != null) {
1018 | pw.close();
1019 | }
1020 | }
1021 | return sb.toString();
1022 | }
1023 |
1024 | private static String parseString(Intent intent) {
1025 | JSONObject obj = new JSONObject();
1026 | try {
1027 | if (!TextUtils.isEmpty(intent.getScheme())) {
1028 | obj.put("Scheme", intent.getScheme());
1029 | }
1030 | if (!TextUtils.isEmpty(intent.getAction())) {
1031 | obj.put("Action", intent.getAction());
1032 | }
1033 | if (!TextUtils.isEmpty(intent.getDataString())) {
1034 | obj.put("DataString", intent.getDataString());
1035 | }
1036 | if (!TextUtils.isEmpty(intent.getType())) {
1037 | obj.put("Type", intent.getType());
1038 | }
1039 | if (!TextUtils.isEmpty(intent.getPackage())) {
1040 | obj.put("Package", intent.getPackage());
1041 | }
1042 | if (!TextUtils.isEmpty(intent.getComponent().toString())) {
1043 | obj.put("ComponentInfo", intent.getComponent().toString());
1044 | }
1045 | if (!TextUtils.isEmpty(intent.getCategories().toString())) {
1046 | obj.put("Categories", intent.getCategories().toString());
1047 | }
1048 | String extras = parseString(intent.getExtras());
1049 | if (!TextUtils.isEmpty(extras)) {
1050 | obj.put("Extras", extras);
1051 | }
1052 | String flags = getFlags(intent.getFlags());
1053 | if (!TextUtils.isEmpty(flags)) {
1054 | obj.put("Flags", intent.getType());
1055 | }
1056 | } catch (JSONException e) {
1057 | e.printStackTrace();
1058 | }
1059 |
1060 | return format(obj);
1061 | }
1062 |
1063 | private static String getFlags(int flags) {
1064 | // 获取相应信息
1065 | SparseArray flagMap = new SparseArray();
1066 | Class> cla = Intent.class;
1067 | Field[] fields = cla.getDeclaredFields();
1068 | for (Field field : fields) {
1069 | try {
1070 | field.setAccessible(true);
1071 | if (field.getName().startsWith("FLAG_")) {
1072 | int value = 0;
1073 | Object object = field.get(cla);
1074 | if (object instanceof Integer || "int".equals(object.getClass().getSimpleName())) {
1075 | value = (Integer) object;
1076 | }
1077 |
1078 | if (flagMap.get(value) == null) {
1079 | flagMap.put(value, field.getName());
1080 | }
1081 | }
1082 | } catch (Throwable e) {
1083 | }
1084 | }
1085 |
1086 | StringBuilder builder = new StringBuilder();
1087 | for (int i = 0; i < flagMap.size(); i++) {
1088 | int flagKey = flagMap.keyAt(i);
1089 | if ((flagKey & flags) == flagKey) {
1090 | builder.append(flagMap.get(flagKey));
1091 | builder.append(" | ");
1092 | }
1093 | }
1094 | if (TextUtils.isEmpty(builder.toString())) {
1095 | builder.append(flags);
1096 | } else if (builder.indexOf("|") != -1) {
1097 | builder.delete(builder.length() - 2, builder.length());
1098 | }
1099 | return builder.toString();
1100 | }
1101 |
1102 | /**
1103 | * 格式化输出JSONArray
1104 | *
1105 | * @param arr
1106 | * @return
1107 | */
1108 | private static String format(JSONArray arr) {
1109 | if (arr != null) {
1110 | try {
1111 | return isFormat ? (arr.toString(JSON_INDENT)) : arr.toString();
1112 | } catch (Exception e) {
1113 | e.printStackTrace();
1114 | }
1115 | }
1116 | return "";
1117 | }
1118 |
1119 | /*********************************************************************************************************/
1120 | /**
1121 | * 格式化字符串、异常、JSONArray、JSONObject
1122 | */
1123 | /*********************************************************************************************************/
1124 |
1125 | /**
1126 | * 格式化输出JSONObject
1127 | *
1128 | * @param obj
1129 | * @return
1130 | */
1131 | private static String format(JSONObject obj) {
1132 |
1133 | if (obj != null) {
1134 | try {
1135 | return isFormat ? obj.toString(JSON_INDENT) : obj.toString();
1136 | } catch (Exception e) {
1137 | e.printStackTrace();
1138 | }
1139 | }
1140 | return "";
1141 | }
1142 |
1143 | /**
1144 | * 字符串处理,wrapper选中情况下,行首加封闭符
1145 | *
1146 | * @param log
1147 | * @return
1148 | */
1149 | private static String wrapperString(String log) {
1150 | StringBuilder sb = new StringBuilder();
1151 |
1152 | if (TextUtils.isEmpty(log)) {
1153 | if (isNeedWrapper) {
1154 | sb.append(CONTENT_LINE);
1155 | }
1156 | sb.append(CONTENT_LOG_EMPTY);
1157 | return sb.toString();
1158 | }
1159 | String ss[] = new String[]{};
1160 | String temp = null;
1161 | if (log.contains("\n")) {
1162 | ss = log.split("\n");
1163 | if (ss.length > 0) {
1164 | sb = new StringBuilder();
1165 | for (int i = 0; i < ss.length; i++) {
1166 | temp = ss[i];
1167 | if (isNeedWrapper && !temp.startsWith(CONTENT_A) && !temp.startsWith(CONTENT_B)
1168 | && !temp.startsWith(CONTENT_C) && !temp.startsWith(CONTENT_D)
1169 | && !temp.startsWith(CONTENT_LOG_INFO) && !TextUtils.isEmpty(temp)
1170 | && !temp.startsWith(CONTENT_E)) {
1171 | sb.append(CONTENT_LINE);
1172 | }
1173 | sb.append(temp);
1174 |
1175 | if (i != ss.length - 1) {
1176 | sb.append("\n");
1177 | }
1178 | }
1179 | }
1180 | } else if (log.contains("\r")) {
1181 | ss = log.split("\r");
1182 | if (ss.length > 0) {
1183 | sb = new StringBuilder();
1184 | for (int i = 0; i < ss.length; i++) {
1185 | temp = ss[i];
1186 |
1187 | if (isNeedWrapper && !temp.startsWith(CONTENT_A) && !temp.startsWith(CONTENT_B)
1188 | && !temp.startsWith(CONTENT_D) && !temp.startsWith(CONTENT_E)
1189 | && !temp.startsWith(CONTENT_LOG_INFO) && !TextUtils.isEmpty(temp)
1190 | && !temp.startsWith(CONTENT_C)) {
1191 | sb.append(CONTENT_LINE);
1192 | }
1193 | sb.append(temp);
1194 | if (i != ss.length - 1) {
1195 | sb.append("\r");
1196 | }
1197 | }
1198 | }
1199 | } else if (log.contains("\r\n")) {
1200 | ss = log.split("\r\n");
1201 | if (ss.length > 0) {
1202 | sb = new StringBuilder();
1203 | for (int i = 0; i < ss.length; i++) {
1204 | temp = ss[i];
1205 |
1206 | if (isNeedWrapper && !temp.startsWith(CONTENT_A) && !temp.startsWith(CONTENT_B)
1207 | && !temp.startsWith(CONTENT_D) && !temp.startsWith(CONTENT_E)
1208 | && !temp.startsWith(CONTENT_LOG_INFO) && !TextUtils.isEmpty(temp)
1209 | && !temp.startsWith(CONTENT_C)) {
1210 | sb.append(CONTENT_LINE);
1211 | }
1212 | sb.append(temp);
1213 |
1214 | if (i != ss.length - 1) {
1215 | sb.append("\r\n");
1216 | }
1217 | }
1218 | }
1219 | } else if (log.contains("\n\r")) {
1220 | ss = log.split("\n\r");
1221 | if (ss.length > 0) {
1222 | sb = new StringBuilder();
1223 | for (int i = 0; i < ss.length; i++) {
1224 | temp = ss[i];
1225 | if (isNeedWrapper && !temp.startsWith(CONTENT_A) && !temp.startsWith(CONTENT_B)
1226 | && !temp.startsWith(CONTENT_D) && !temp.startsWith(CONTENT_E)
1227 | && !temp.startsWith(CONTENT_LOG_INFO) && !TextUtils.isEmpty(temp)
1228 | && !temp.startsWith(CONTENT_C)) {
1229 | sb.append(CONTENT_LINE);
1230 | }
1231 | sb.append(temp);
1232 |
1233 | if (i != ss.length - 1) {
1234 | sb.append("\n\r");
1235 | }
1236 | }
1237 | }
1238 | } else {
1239 | if (isNeedWrapper && !log.startsWith(CONTENT_A) && !log.startsWith(CONTENT_B) && !log.startsWith(CONTENT_D)
1240 | && !log.startsWith(CONTENT_LOG_INFO) && !TextUtils.isEmpty(log) && !log.startsWith(CONTENT_E)
1241 | && !log.startsWith(CONTENT_C)) {
1242 | sb.append(CONTENT_LINE);
1243 | }
1244 | sb.append(log);
1245 | }
1246 | return sb.toString();
1247 | }
1248 |
1249 | /*********************************************************************************************************/
1250 | /**
1251 | * 字符串包裹处理
1252 | */
1253 | /*********************************************************************************************************/
1254 |
1255 | /**
1256 | * 动态检查临时.切割大文件
1257 | *
1258 | * @param level
1259 | * @param msg
1260 | */
1261 | private static void preparePrint(int level, String msg) {
1262 | String tag = DEFAULT_TAG;
1263 | if (!TextUtils.isEmpty(TEMP_TAG)) {
1264 | tag = TEMP_TAG;
1265 | }
1266 |
1267 | if (msg.length() > LOG_MAXLENGTH) {
1268 | List splitStr = getStringBysplitLine(msg, LOG_MAXLENGTH);
1269 |
1270 | StringBuilder sb = null;
1271 | for (int i = 0; i < splitStr.size(); i++) {
1272 | String line = splitStr.get(i);
1273 |
1274 | if (sb == null) {
1275 | sb = new StringBuilder();
1276 | }
1277 | if (sb.length() + line.length() >= LOG_MAXLENGTH) {
1278 | realPrint(level, tag, wrapperString(sb.toString()));
1279 | sb = new StringBuilder();
1280 | if (line.length() >= LOG_MAXLENGTH) {
1281 | realPrint(level, tag, wrapperString(line));
1282 | } else {
1283 | sb.append(line);
1284 | }
1285 | if (i != splitStr.size() - 1) {
1286 | sb.append("\n");
1287 | }
1288 | } else {
1289 | sb.append(line);
1290 | if (i != splitStr.size() - 1) {
1291 | sb.append("\n");
1292 | }
1293 | }
1294 | }
1295 | if (sb != null) {
1296 | realPrint(level, tag, wrapperString(sb.toString()));
1297 | sb = null;
1298 | }
1299 | } else {
1300 | realPrint(level, tag, wrapperString(msg));
1301 | }
1302 | TEMP_TAG = "";
1303 | }
1304 |
1305 | /*********************************************************************************************************/
1306 | /**
1307 | * 打印方法
1308 | */
1309 | /*********************************************************************************************************/
1310 |
1311 | /**
1312 | * 真正打印单个信息
1313 | *
1314 | * @param level
1315 | * @param tag
1316 | * @param printStr
1317 | */
1318 | private static void realPrint(int level, String tag, String printStr) {
1319 | switch (level) {
1320 | case MLEVEL.DEBUG:
1321 | Log.d(tag, printStr);
1322 | break;
1323 | case MLEVEL.INFO:
1324 | Log.i(tag, printStr);
1325 | break;
1326 | case MLEVEL.ERROR:
1327 | Log.e(tag, printStr);
1328 | break;
1329 | case MLEVEL.VERBOSE:
1330 | Log.v(tag, printStr);
1331 | break;
1332 | case MLEVEL.WARN:
1333 | Log.w(tag, printStr);
1334 | break;
1335 | case MLEVEL.WTF:
1336 | Log.wtf(tag, printStr);
1337 | break;
1338 | default:
1339 | break;
1340 | }
1341 | }
1342 |
1343 | /**
1344 | * 按行分割字符串
1345 | *
1346 | * @param msg
1347 | * @param maxLen
1348 | * @return
1349 | */
1350 | private static List getStringBysplitLine(String msg, int maxLen) {
1351 | List result = new ArrayList();
1352 | String[] lines = msg.split("\n");
1353 | if (lines.length == 1) {
1354 | lines = msg.split("\r");
1355 | if (lines.length == 1) {
1356 | lines = msg.split("\r\n");
1357 | if (lines.length == 1) {
1358 | lines = msg.split("\n\r");
1359 | }
1360 | }
1361 | }
1362 | if (lines.length > 1) {
1363 |
1364 | for (int i = 0; i < lines.length; i++) {
1365 | String line = lines[i];
1366 | // 单行都超过最大长度,直接按照字符串分别来做
1367 | processLine(maxLen, result, line);
1368 | }
1369 | } else {
1370 | processLine(maxLen, result, msg);
1371 | }
1372 |
1373 | return result;
1374 | }
1375 |
1376 | /**
1377 | * 处理单行超长处理
1378 | *
1379 | * @param maxLen
1380 | * @param result
1381 | * @param line
1382 | */
1383 | private static void processLine(int maxLen, List result, String line) {
1384 | if (line.length() > maxLen) {
1385 | int current = 0;
1386 | String str;
1387 | while (true) {
1388 | try {
1389 | str = line.substring(current, current + maxLen);
1390 | result.add(str);
1391 | current += maxLen;
1392 | } catch (StringIndexOutOfBoundsException e) {
1393 | str = line.substring(current, line.length());
1394 | result.add(str);
1395 | break;
1396 | }
1397 | }
1398 | } else {
1399 | result.add(line);
1400 | }
1401 | }
1402 |
1403 | public static final class MLEVEL {
1404 | public static final int VERBOSE = 0x1;
1405 | public static final int DEBUG = 0x2;
1406 | public static final int INFO = 0x3;
1407 | public static final int WARN = 0x4;
1408 | public static final int ERROR = 0x5;
1409 | public static final int WTF = 0x6;
1410 | }
1411 |
1412 | }
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/Miui.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Build;
7 | import android.provider.Settings;
8 | import android.view.View;
9 | import android.view.WindowManager;
10 |
11 | import com.yhao.floatwindow.impl.FloatLifecycleReceiver;
12 | import com.yhao.floatwindow.interfaces.ResumedListener;
13 | import com.yhao.floatwindow.permission.PermissionListener;
14 | import com.yhao.floatwindow.permission.PermissionUtil;
15 |
16 | import java.lang.reflect.Field;
17 | import java.util.ArrayList;
18 | import java.util.List;
19 |
20 | /**
21 | * @Copyright © 2017 Analysys Inc. All rights reserved.
22 | * @Description:
23 | * 需要清楚:一个MIUI版本对应小米各种机型,基于不同的安卓版本,但是权限设置页跟MIUI版本有关
24 | * 测试TYPE_TOAST类型:
25 | * 7.0:
26 | * 小米 5 MIUI8 -------------------- 失败
27 | * 小米 Note2 MIUI9 -------------------- 失败
28 | * 6.0.1
29 | * 小米 5 -------------------- 失败
30 | * 小米 红米note3 -------------------- 失败
31 | * 6.0:
32 | * 小米 5 -------------------- 成功
33 | * 小米 红米4A MIUI8 -------------------- 成功
34 | * 小米 红米Pro MIUI7 -------------------- 成功
35 | * 小米 红米Note4 MIUI8 -------------------- 失败
36 | * *
37 | * * 经过各种横向纵向测试对比,得出一个结论,就是小米对TYPE_TOAST的处理机制毫无规律可言!
38 | * * 跟Android版本无关,跟MIUI版本无关,addView方法也不报错
39 | * * 所以最后对小米6.0以上的适配方法是:不使用 TYPE_TOAST 类型,统一申请权限
40 | *
41 | * @Version: 1.0.9
42 | * @Create: 2017/12/30 17:11:30
43 | * @Author: yhao
44 | */
45 | public class Miui {
46 |
47 | private static final String MIUI = "ro.MIUI.ui.version.name";
48 | private static final String MIUI5 = "V5";
49 | private static final String MIUI6 = "V6";
50 | private static final String MIUI7 = "V7";
51 | private static final String MIUI8 = "V8";
52 | private static final String MIUI9 = "V9";
53 | private static List mPermissionListenerList;
54 | private static PermissionListener mPermissionListener;
55 |
56 | public static boolean rom() {
57 | L.d(" Miui : " + Miui.getProp());
58 | return "Xiaomi".equals(Build.MANUFACTURER);
59 | }
60 |
61 | private static String getProp() {
62 | return Rom.getProp(MIUI);
63 | }
64 |
65 | /**
66 | * Android6.0以下申请权限
67 | */
68 | public static void requestPermission(final Context context, PermissionListener permissionListener) {
69 | if (PermissionUtil.hasPermission(context)) {
70 | permissionListener.onSuccess();
71 | return;
72 | }
73 | if (mPermissionListenerList == null) {
74 | mPermissionListenerList = new ArrayList();
75 | mPermissionListener = new PermissionListener() {
76 | @Override
77 | public void onSuccess() {
78 | for (PermissionListener listener : mPermissionListenerList) {
79 | listener.onSuccess();
80 | }
81 | mPermissionListenerList.clear();
82 | }
83 |
84 | @Override
85 | public void onFail() {
86 | for (PermissionListener listener : mPermissionListenerList) {
87 | listener.onFail();
88 | }
89 | mPermissionListenerList.clear();
90 | }
91 | };
92 | requestPermission(context);
93 | }
94 | mPermissionListenerList.add(permissionListener);
95 | }
96 |
97 | private static void requestPermission(final Context context) {
98 | String prop = getProp();
99 | if (MIUI5.equals(prop)) {
100 | reqForMiui5(context);
101 | } else if (MIUI6.equals(prop) || MIUI7.equals(prop)) {
102 | reqForMiui67(context);
103 | } else if (MIUI8.equals(prop) || MIUI9.equals(prop)) {
104 | reqForMiui89(context);
105 | }
106 | FloatLifecycleReceiver.setResumedListener(new ResumedListener() {
107 | @Override
108 | public void onResumed() {
109 | if (PermissionUtil.hasPermission(context)) {
110 | mPermissionListener.onSuccess();
111 | } else {
112 | mPermissionListener.onFail();
113 | }
114 | }
115 | });
116 | }
117 |
118 | private static void reqForMiui5(Context context) {
119 | String packageName = context.getPackageName();
120 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
121 | Uri uri = Uri.fromParts("package", packageName, null);
122 | intent.setData(uri);
123 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
124 | if (Rom.isIntentAvailable(intent, context)) {
125 | context.startActivity(intent);
126 | } else {
127 | L.e("intent is not available!");
128 | }
129 | }
130 |
131 | private static void reqForMiui67(Context context) {
132 | Intent intent = new Intent("MIUI.intent.action.APP_PERM_EDITOR");
133 | intent.setClassName("com.MIUI.securitycenter", "com.MIUI.permcenter.permissions.AppPermissionsEditorActivity");
134 | intent.putExtra("extra_pkgname", context.getPackageName());
135 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
136 | if (Rom.isIntentAvailable(intent, context)) {
137 | context.startActivity(intent);
138 | } else {
139 | L.e("intent is not available!");
140 | }
141 | }
142 |
143 | private static void reqForMiui89(Context context) {
144 | Intent intent = new Intent("MIUI.intent.action.APP_PERM_EDITOR");
145 | intent.setClassName("com.MIUI.securitycenter", "com.MIUI.permcenter.permissions.PermissionsEditorActivity");
146 | intent.putExtra("extra_pkgname", context.getPackageName());
147 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
148 | if (Rom.isIntentAvailable(intent, context)) {
149 | context.startActivity(intent);
150 | } else {
151 | intent = new Intent("MIUI.intent.action.APP_PERM_EDITOR");
152 | intent.setPackage("com.MIUI.securitycenter");
153 | intent.putExtra("extra_pkgname", context.getPackageName());
154 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155 | if (Rom.isIntentAvailable(intent, context)) {
156 | context.startActivity(intent);
157 | } else {
158 | L.e("intent is not available!");
159 | }
160 | }
161 | }
162 |
163 | /**
164 | * 有些机型在添加TYPE-TOAST类型时会自动改为TYPE_SYSTEM_ALERT,通过此方法可以屏蔽修改 但是...即使成功显示出悬浮窗,移动的话也会崩溃
165 | */
166 | @SuppressWarnings("unused")
167 | private static void addViewToWindow(WindowManager wm, View view, WindowManager.LayoutParams params) {
168 | setMiUIInternational(true);
169 | wm.addView(view, params);
170 | setMiUIInternational(false);
171 | }
172 |
173 | private static void setMiUIInternational(boolean flag) {
174 | try {
175 | Class> buildClazz = Class.forName("MIUI.os.Build");
176 | Field isInternational = buildClazz.getDeclaredField("IS_INTERNATIONAL_BUILD");
177 | isInternational.setAccessible(true);
178 | isInternational.setBoolean(null, flag);
179 | } catch (Exception e) {
180 | e.printStackTrace();
181 | }
182 | }
183 |
184 | }
185 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/RefInvoke.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 |
4 | import android.os.Build;
5 |
6 | import java.lang.reflect.Constructor;
7 | import java.lang.reflect.Field;
8 | import java.lang.reflect.Method;
9 |
10 | /**
11 | * @Copyright © 2019 sanbo Inc. All rights reserved.
12 | * @Description: TODO
13 | * @Version: 1.0
14 | * @Create: 2019-12-11 15:34
15 | * @author: sanbo
16 | */
17 | public class RefInvoke {
18 | private static Object sVmRuntime;
19 | private static Method setHiddenApiExemptions;
20 |
21 | static {
22 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
23 | try {
24 | Method forName = Class.class.getDeclaredMethod("forName", String.class);
25 | Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
26 |
27 | Class> vmRuntimeClass = (Class>) forName.invoke(null, "dalvik.system.VMRuntime");
28 | Method getRuntime = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null);
29 | setHiddenApiExemptions = (Method) getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", new Class[]{String[].class});
30 | sVmRuntime = getRuntime.invoke(null);
31 | setHiddenApiExemptions.invoke(sVmRuntime, new Object[]{new String[]{"L"}});
32 | } catch (Throwable e) {
33 | }
34 | }
35 |
36 | }
37 |
38 | //无参
39 | public static Object createObject(String className) {
40 | Class[] pareTyples = new Class[]{};
41 | Object[] pareVaules = new Object[]{};
42 |
43 | try {
44 | Class r = Class.forName(className);
45 | return createObject(r, pareTyples, pareVaules);
46 | } catch (ClassNotFoundException e) {
47 | e.printStackTrace();
48 | }
49 |
50 | return null;
51 | }
52 |
53 | //无参
54 | public static Object createObject(Class clazz) {
55 | Class[] pareTyple = new Class[]{};
56 | Object[] pareVaules = new Object[]{};
57 |
58 | return createObject(clazz, pareTyple, pareVaules);
59 | }
60 |
61 | //一个参数
62 | public static Object createObject(String className, Class pareTyple, Object pareVaule) {
63 | Class[] pareTyples = new Class[]{pareTyple};
64 | Object[] pareVaules = new Object[]{pareVaule};
65 |
66 | try {
67 | Class r = Class.forName(className);
68 | return createObject(r, pareTyples, pareVaules);
69 | } catch (ClassNotFoundException e) {
70 | e.printStackTrace();
71 | }
72 |
73 | return null;
74 | }
75 |
76 | //一个参数
77 | public static Object createObject(Class clazz, Class pareTyple, Object pareVaule) {
78 | Class[] pareTyples = new Class[]{pareTyple};
79 | Object[] pareVaules = new Object[]{pareVaule};
80 |
81 | return createObject(clazz, pareTyples, pareVaules);
82 | }
83 |
84 | //多个参数
85 | public static Object createObject(String className, Class[] pareTyples, Object[] pareVaules) {
86 | try {
87 | Class r = Class.forName(className);
88 | return createObject(r, pareTyples, pareVaules);
89 | } catch (ClassNotFoundException e) {
90 | e.printStackTrace();
91 | }
92 |
93 | return null;
94 | }
95 |
96 | //多个参数
97 | public static Object createObject(Class clazz, Class[] pareTyples, Object[] pareVaules) {
98 | try {
99 | Constructor ctor = clazz.getDeclaredConstructor(pareTyples);
100 | ctor.setAccessible(true);
101 | return ctor.newInstance(pareVaules);
102 | } catch (Exception e) {
103 | e.printStackTrace();
104 | }
105 |
106 | return null;
107 | }
108 |
109 |
110 | //多个参数
111 | public static Object invokeInstanceMethod(Object obj, String methodName, Class[] pareTyples, Object[] pareVaules) {
112 | if (obj == null) {
113 | return null;
114 | }
115 |
116 | try {
117 | //调用一个private方法
118 | Method method = obj.getClass().getDeclaredMethod(methodName, pareTyples); //在指定类中获取指定的方法
119 | method.setAccessible(true);
120 | return method.invoke(obj, pareVaules);
121 |
122 | } catch (Exception e) {
123 | e.printStackTrace();
124 | }
125 |
126 | return null;
127 | }
128 |
129 | //一个参数
130 | public static Object invokeInstanceMethod(Object obj, String methodName, Class pareTyple, Object pareVaule) {
131 | Class[] pareTyples = {pareTyple};
132 | Object[] pareVaules = {pareVaule};
133 |
134 | return invokeInstanceMethod(obj, methodName, pareTyples, pareVaules);
135 | }
136 |
137 | //无参
138 | public static Object invokeInstanceMethod(Object obj, String methodName) {
139 | Class[] pareTyples = new Class[]{};
140 | Object[] pareVaules = new Object[]{};
141 |
142 | return invokeInstanceMethod(obj, methodName, pareTyples, pareVaules);
143 | }
144 |
145 |
146 | //无参
147 | public static Object invokeStaticMethod(String className, String method_name) {
148 | Class[] pareTyples = new Class[]{};
149 | Object[] pareVaules = new Object[]{};
150 |
151 | return invokeStaticMethod(className, method_name, pareTyples, pareVaules);
152 | }
153 |
154 | //一个参数
155 | public static Object invokeStaticMethod(String className, String method_name, Class pareTyple, Object pareVaule) {
156 | Class[] pareTyples = new Class[]{pareTyple};
157 | Object[] pareVaules = new Object[]{pareVaule};
158 |
159 | return invokeStaticMethod(className, method_name, pareTyples, pareVaules);
160 | }
161 |
162 | //多个参数
163 | public static Object invokeStaticMethod(String className, String method_name, Class[] pareTyples, Object[] pareVaules) {
164 | try {
165 | Class obj_class = Class.forName(className);
166 | return invokeStaticMethod(obj_class, method_name, pareTyples, pareVaules);
167 | } catch (Exception e) {
168 | e.printStackTrace();
169 | }
170 |
171 | return null;
172 | }
173 |
174 | public static Method getMethod(Object obj, String methodName, Class... pareTyples) {
175 | try {
176 | return getMethod(obj.getClass(), methodName, pareTyples);
177 | } catch (Exception e) {
178 | e.printStackTrace();
179 | }
180 | return null;
181 | }
182 |
183 | public static Method getMethod(Class clazz, String methodName, Class... pareTyples) {
184 | try {
185 | Method method = clazz.getDeclaredMethod(methodName, pareTyples);
186 | if (method == null) {
187 | method = clazz.getMethod(methodName, pareTyples);
188 | }
189 | if (method != null) {
190 | method.setAccessible(true);
191 | return method;
192 | }
193 | } catch (Exception e) {
194 | e.printStackTrace();
195 | }
196 | return null;
197 | }
198 |
199 |
200 | //无参
201 | public static Object invokeStaticMethod(Class clazz, String method_name) {
202 | Class[] pareTyples = new Class[]{};
203 | Object[] pareVaules = new Object[]{};
204 |
205 | return invokeStaticMethod(clazz, method_name, pareTyples, pareVaules);
206 | }
207 |
208 | //一个参数
209 | public static Object invokeStaticMethod(Class clazz, String method_name, Class classType, Object pareVaule) {
210 | Class[] classTypes = new Class[]{classType};
211 | Object[] pareVaules = new Object[]{pareVaule};
212 |
213 | return invokeStaticMethod(clazz, method_name, classTypes, pareVaules);
214 | }
215 |
216 | //多个参数
217 | public static Object invokeStaticMethod(Class clazz, String method_name, Class[] pareTyples, Object[] pareVaules) {
218 | try {
219 | Method method = clazz.getDeclaredMethod(method_name, pareTyples);
220 | method.setAccessible(true);
221 | return method.invoke(null, pareVaules);
222 | } catch (Exception e) {
223 | e.printStackTrace();
224 | }
225 | return null;
226 | }
227 |
228 |
229 | //简写版本
230 | public static Object getFieldObject(Object obj, String filedName) {
231 | return getFieldObject(obj.getClass(), obj, filedName);
232 | }
233 |
234 | public static Object getFieldObject(String className, Object obj, String filedName) {
235 | try {
236 | Class obj_class = Class.forName(className);
237 | return getFieldObject(obj_class, obj, filedName);
238 | } catch (ClassNotFoundException e) {
239 | e.printStackTrace();
240 | }
241 | return null;
242 | }
243 |
244 | public static Object getFieldObject(Class clazz, Object obj, String filedName) {
245 | try {
246 | Field field = clazz.getDeclaredField(filedName);
247 | field.setAccessible(true);
248 | return field.get(obj);
249 | } catch (Exception e) {
250 | e.printStackTrace();
251 | }
252 |
253 | return null;
254 | }
255 |
256 | //简写版本
257 | public static void setFieldObject(Object obj, String filedName, Object filedVaule) {
258 | setFieldObject(obj.getClass(), obj, filedName, filedVaule);
259 | }
260 |
261 | public static void setFieldObject(Class clazz, Object obj, String filedName, Object filedVaule) {
262 | try {
263 | Field field = clazz.getDeclaredField(filedName);
264 | field.setAccessible(true);
265 | field.set(obj, filedVaule);
266 | } catch (Exception e) {
267 | e.printStackTrace();
268 | }
269 | }
270 |
271 | public static void setFieldObject(String className, Object obj, String filedName, Object filedVaule) {
272 | try {
273 | Class obj_class = Class.forName(className);
274 | setFieldObject(obj_class, obj, filedName, filedVaule);
275 | } catch (ClassNotFoundException e) {
276 | e.printStackTrace();
277 | }
278 | }
279 |
280 |
281 | public static Object getStaticFieldObject(String className, String filedName) {
282 | return getFieldObject(className, null, filedName);
283 | }
284 |
285 | public static Object getStaticFieldObject(Class clazz, String filedName) {
286 | return getFieldObject(clazz, null, filedName);
287 | }
288 |
289 | public static void setStaticFieldObject(String classname, String filedName, Object filedVaule) {
290 | setFieldObject(classname, null, filedName, filedVaule);
291 | }
292 |
293 | public static void setStaticFieldObject(Class clazz, String filedName, Object filedVaule) {
294 | setFieldObject(clazz, null, filedName, filedVaule);
295 | }
296 | }
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/Rom.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 |
7 | import java.io.BufferedReader;
8 | import java.io.IOException;
9 | import java.io.InputStreamReader;
10 |
11 | /**
12 | * @Copyright © 2017 Analysys Inc. All rights reserved.
13 | * @Description:
14 | * @Version: 1.0.9
15 | * @Create: 2017/12/29 17:15:35
16 | * @Author: yhao
17 | */
18 | public class Rom {
19 |
20 | public static boolean isIntentAvailable(Intent intent, Context context) {
21 | return intent != null
22 | && context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
23 | }
24 |
25 | public static String getProp(String name) {
26 | BufferedReader input = null;
27 | try {
28 | Process p = Runtime.getRuntime().exec("getprop " + name);
29 | input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
30 | String line = input.readLine();
31 | input.close();
32 | return line;
33 | } catch (IOException ex) {
34 | return null;
35 | } finally {
36 | if (input != null) {
37 | try {
38 | input.close();
39 | } catch (IOException e) {
40 | e.printStackTrace();
41 | }
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/RotateUtil.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 |
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.pm.ActivityInfo;
7 | import android.hardware.Sensor;
8 | import android.hardware.SensorEvent;
9 | import android.hardware.SensorEventListener;
10 | import android.hardware.SensorManager;
11 |
12 | import com.yhao.floatwindow.interfaces.ISensorRotateChanged;
13 |
14 | import java.lang.ref.WeakReference;
15 |
16 | /**
17 | * @Copyright © 2019 sanbo Inc. All rights reserved.
18 | * @Description: 方向感应
19 | * @Version: 1.0
20 | * @Create: 2019-10-16 18:10:38
21 | * @author: sanbo
22 | */
23 | public class RotateUtil {
24 |
25 | private SensorManager mSenserManger = null;
26 | private OrientationSensorListener mSensorListener = null;
27 | private Sensor mSensor = null;
28 | // 多次初始化
29 | private boolean isInit = false;
30 | // 是否中断, 默认不中断
31 | private boolean isInterrupt = false;
32 | // 默认是竖屏
33 | private boolean isLandscape = false;
34 | // 记录点击全屏后屏幕朝向是否改变,默认会自动切换
35 | private boolean isChangeOrientation = true;
36 | // 为了给页面传递方向改变
37 | private WeakReference mActivityRef = null;
38 | // 变动时回调
39 | private ISensorRotateChanged mSensorRotateChanged = null;
40 |
41 |
42 | private RotateUtil() {
43 | }
44 |
45 | public static RotateUtil getInstance() {
46 | return HOLDER.INSTANCE;
47 | }
48 |
49 | public void start(Activity activity) {
50 | initSensorManager(activity.getApplicationContext());
51 | if (!isInit && mSenserManger != null) {
52 | mSenserManger.registerListener(mSensorListener, mSensor, SensorManager.SENSOR_DELAY_UI);
53 | }
54 | if (activity != null || mActivityRef.get() == null) {
55 | mActivityRef = new WeakReference(activity);
56 | }
57 | }
58 |
59 | public void stop() {
60 | if (isInit && mSenserManger != null) {
61 | mSenserManger.unregisterListener(mSensorListener);
62 | }
63 | if (mActivityRef != null || mActivityRef.get() != null) {
64 | mActivityRef = null;
65 | }
66 | }
67 |
68 | /**
69 | * 初始化传感器
70 | *
71 | * @param context
72 | */
73 | private void initSensorManager(Context context) {
74 | // 初始化重力感应器
75 | if (mSenserManger == null) {
76 | mSenserManger = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
77 | }
78 | if (mSensor == null) {
79 | mSensor = mSenserManger.getDefaultSensor(Sensor.TYPE_GRAVITY);
80 | }
81 | if (mSensorListener == null) {
82 | mSensorListener = new OrientationSensorListener();
83 | }
84 | }
85 |
86 | /**
87 | * 设置回调
88 | *
89 | * @param sensorRotateChanged
90 | */
91 | public void setRotateChanged(ISensorRotateChanged sensorRotateChanged) {
92 | if (sensorRotateChanged != null) {
93 | mSensorRotateChanged = sensorRotateChanged;
94 | }
95 | }
96 |
97 | /**
98 | * 当前屏幕朝向是否横屏
99 | *
100 | * @param orientation
101 | * @return
102 | */
103 | private boolean screenIsLandscape(int orientation) {
104 | return ((orientation > 45 && orientation <= 135) || (orientation > 225 && orientation <= 315));
105 | }
106 |
107 | /**
108 | * 当前屏幕朝向是否竖屏
109 | *
110 | * @param orientation
111 | * @return
112 | */
113 | private boolean screenIsPortrait(int orientation) {
114 | return (((orientation > 315 && orientation <= 360) || (orientation >= 0 && orientation <= 45))
115 | || (orientation > 135 && orientation <= 225));
116 | }
117 |
118 | private static class HOLDER {
119 | private static RotateUtil INSTANCE = new RotateUtil();
120 | }
121 |
122 | /**
123 | * 重力感应监听者
124 | */
125 | public class OrientationSensorListener implements SensorEventListener {
126 | public static final int ORIENTATION_UNKNOWN = -1;
127 | private static final int DATA_X = 0;
128 | private static final int DATA_Y = 1;
129 | private static final int DATA_Z = 2;
130 | //上次是否竖屏
131 | private boolean isLastLandscape = false;
132 |
133 | @Override
134 | public void onSensorChanged(SensorEvent event) {
135 |
136 | float[] values = event.values;
137 | int orientation = ORIENTATION_UNKNOWN;
138 | float X = -values[DATA_X];
139 | float Y = -values[DATA_Y];
140 | float Z = -values[DATA_Z];
141 | float magnitude = X * X + Y * Y;
142 | // Don't trust the angle if the magnitude is small compared to the y
143 | // value
144 | if (magnitude * 4 >= Z * Z) {
145 | // 屏幕旋转时
146 | float OneEightyOverPi = 57.29577957855f;
147 | float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
148 | orientation = 90 - Math.round(angle);
149 | // normalize to 0 - 359 range
150 | while (orientation >= 360) {
151 | orientation -= 360;
152 | }
153 | while (orientation < 0) {
154 | orientation += 360;
155 | }
156 | }
157 |
158 | //竖屏
159 | if (screenIsPortrait(orientation)) {
160 | //上次非竖屏,屏幕旋转
161 | if (!isLastLandscape) {
162 | isChangeOrientation = true;
163 | if (FwContent.isDebug) {
164 | L.v("onSensorChanged: 横屏 ----> 竖屏");
165 | }
166 | } else {
167 | isChangeOrientation = false;
168 | if (FwContent.isDebug) {
169 | L.v("onSensorChanged: 竖屏 ----> 竖屏");
170 | }
171 | }
172 | isLastLandscape = true;
173 | // 横屏处理
174 | } else if (screenIsLandscape(orientation)) {
175 | //上次竖屏,屏幕旋转
176 | if (isLastLandscape) {
177 | isChangeOrientation = true;
178 | if (FwContent.isDebug) {
179 | L.v("onSensorChanged: 竖屏 ---->横屏");
180 | }
181 | } else {
182 | isChangeOrientation = false;
183 | if (FwContent.isDebug) {
184 | L.v("onSensorChanged: 横屏 ----> 横屏");
185 | }
186 | }
187 | isLastLandscape = false;
188 | }
189 |
190 | // 页面变化,回调
191 | if (isChangeOrientation) {
192 | mSensorRotateChanged.onRotateChanged();
193 | isChangeOrientation = false;
194 | }
195 |
196 | // 不拦截时,对应页面也可以收到页面的改变
197 |
198 | if (!isInterrupt && mActivityRef != null && mActivityRef.get() != null) {
199 | // 根据手机屏幕的朝向角度,来设置内容的横竖屏,并且记录状态
200 | int requestedOrientation = 0;
201 |
202 | if (orientation > 45 && orientation < 135) {
203 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
204 | } else if (orientation > 135 && orientation < 225) {
205 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
206 | } else if (orientation > 225 && orientation < 315) {
207 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
208 | } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
209 | requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
210 | }
211 | // 接收重力感应监听的结果,来改变屏幕朝向
212 | mActivityRef.get().setRequestedOrientation(requestedOrientation);
213 | }
214 | }
215 |
216 | @Override
217 | public void onAccuracyChanged(Sensor sensor, int accuracy) {
218 | }
219 |
220 | }
221 |
222 | }
223 |
--------------------------------------------------------------------------------
/floatwindow/src/com/yhao/floatwindow/utils/ViewUtils.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow.utils;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.res.Configuration;
6 | import android.graphics.Point;
7 | import android.graphics.Rect;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.WindowManager;
11 |
12 | /**
13 | * @Copyright © 2017 Analysys Inc. All rights reserved.
14 | * @Description:
15 | * @Version: 1.0.9
16 | * @Create: 2017/12/29 17:15:35
17 | * @Author: yhao
18 | */
19 | public class ViewUtils {
20 |
21 | private static Point sPoint;
22 |
23 | public static View inflate(Context applicationContext, int layoutId) {
24 | LayoutInflater inflate = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
25 | return inflate.inflate(layoutId, null);
26 | }
27 |
28 | public static int getScreenWidth(Context context) {
29 | if (sPoint == null) {
30 | sPoint = new Point();
31 | }
32 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
33 | wm.getDefaultDisplay().getSize(sPoint);
34 | return sPoint.x;
35 | }
36 |
37 | public static int getScreenHeight(Context context) {
38 | if (sPoint == null) {
39 | sPoint = new Point();
40 | }
41 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
42 | wm.getDefaultDisplay().getSize(sPoint);
43 | return sPoint.y;
44 | }
45 |
46 | public static boolean isViewVisible(View view) {
47 | return view.getGlobalVisibleRect(new Rect());
48 | }
49 |
50 | /**
51 | * 当前屏幕的朝向
52 | *
53 | * @return 是否是横屏
54 | */
55 | public static boolean isActivityLandscape(Activity activity) {
56 | Configuration c = activity.getResources().getConfiguration();
57 | if (c.orientation == Configuration.ORIENTATION_PORTRAIT) {
58 | //竖屏
59 | return false;
60 | } else {
61 | //横屏
62 | return true;
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | android.injected.testOnly=false
2 | org.gradle.daemon=true
3 | org.gradle.parallel=true
4 | org.gradle.jvmargs=-Xmx2048m -XX\:MaxPermSize\=512m -XX\:+HeapDumpOnOutOfMemoryError -Dfile.encoding\=UTF-8
5 | org.gradle.configureondemand=true
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jul 22 21:37:38 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/sample/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/sample/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | floatwindow-demo
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/sample/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "28.0.3"
6 | defaultConfig {
7 | minSdkVersion 14
8 | targetSdkVersion 22
9 | }
10 | buildTypes {
11 | release {
12 | minifyEnabled false
13 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
14 | }
15 | }
16 | sourceSets {
17 | main {
18 | manifest.srcFile 'AndroidManifest.xml'
19 | java.srcDirs = ['src']
20 | resources.srcDirs = ['src']
21 | aidl.srcDirs = ['src']
22 | renderscript.srcDirs = ['src']
23 | res.srcDirs = ['res']
24 | assets.srcDirs = ['assets']
25 | jniLibs.srcDirs = ['libs']
26 | }
27 | debug.setRoot('build-types/debug')
28 | release.setRoot('build-types/release')
29 | }
30 | dexOptions {
31 | preDexLibraries false
32 | maxProcessCount 8
33 | javaMaxHeapSize "4g"
34 | }
35 | compileOptions {
36 | sourceCompatibility JavaVersion.VERSION_1_7
37 | targetCompatibility JavaVersion.VERSION_1_7
38 | }
39 | aaptOptions {
40 | cruncherEnabled = false
41 | useNewCruncher = false
42 | }
43 | lintOptions {
44 | checkReleaseBuilds false
45 | abortOnError false
46 | warningsAsErrors false
47 | disable "UnusedResources"
48 | textOutput "stdout"
49 | textReport false
50 | }
51 | }
52 | // 忽略文档编译错误. 设置编码
53 | tasks.withType(Javadoc) {
54 | options.addStringOption('Xdoclint:none', '-quiet')
55 | options.addStringOption('encoding', 'UTF-8')
56 | options.addStringOption('charSet', 'UTF-8')
57 | }
58 |
59 | dependencies {
60 | // implementation fileTree(dir: 'libs', include: ['*.jar'])
61 | implementation project(':floatwindow')
62 | }
63 |
--------------------------------------------------------------------------------
/sample/lint.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/sample/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 |
--------------------------------------------------------------------------------
/sample/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 | # Project target.
13 | target=android-23
14 | android.library.reference.1=../floatwindow
15 |
--------------------------------------------------------------------------------
/sample/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_btn_check_to_on_mtrl_000.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_btn_check_to_on_mtrl_000.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_btn_check_to_on_mtrl_015.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_btn_check_to_on_mtrl_015.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_btn_radio_to_on_mtrl_000.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_btn_radio_to_on_mtrl_000.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_btn_radio_to_on_mtrl_015.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_btn_radio_to_on_mtrl_015.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_black_16dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_black_16dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_black_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_black_36dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_half_black_16dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_half_black_16dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_half_black_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_half_black_36dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_ic_star_half_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_ic_star_half_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/abc_list_divider_mtrl_alpha.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/abc_list_divider_mtrl_alpha.9.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_add_circle_outline_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_add_circle_outline_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_close_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_close_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_drag_handle_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_drag_handle_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_pause_circle_outline_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_pause_circle_outline_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_remove_circle_outline_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_remove_circle_outline_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_settings_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_settings_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_visibility_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_visibility_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/c_outline_visibility_off_black_48dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/c_outline_visibility_off_black_48dp.png
--------------------------------------------------------------------------------
/sample/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SDKers/FloatWindow/945924b31e5e4a4ed2caea7b08b6bb344e09b1ad/sample/res/drawable/icon.png
--------------------------------------------------------------------------------
/sample/res/layout/activity_b.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
14 |
15 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/sample/res/layout/activity_c.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/sample/res/layout/activity_d.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/sample/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
18 |
19 |
28 |
29 |
37 |
38 |
46 |
47 |
55 |
56 |
64 |
65 |
73 |
74 |
--------------------------------------------------------------------------------
/sample/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | FloadWindow
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/sample/src/com/example/fixedfloatwindow/ActivityB.java:
--------------------------------------------------------------------------------
1 | package com.example.fixedfloatwindow;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.view.View;
6 |
7 | public class ActivityB extends BaseActivity {
8 |
9 | @Override
10 | protected void onCreate(Bundle savedInstanceState) {
11 | super.onCreate(savedInstanceState);
12 | setContentView(R.layout.activity_b);
13 | setTitle("Builder");
14 | }
15 |
16 | public void change(View view) {
17 | startActivity(new Intent(this, ActivityC.class));
18 | }
19 |
20 | public void back(View view) {
21 | finish();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/sample/src/com/example/fixedfloatwindow/ActivityC.java:
--------------------------------------------------------------------------------
1 | package com.example.fixedfloatwindow;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | public class ActivityC extends BaseActivity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_c);
12 | setTitle("C");
13 |
14 | }
15 |
16 | public void back(View view) {
17 | finish();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/sample/src/com/example/fixedfloatwindow/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.fixedfloatwindow;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 |
6 | public class BaseActivity extends Activity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_d);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/sample/src/com/example/fixedfloatwindow/BaseApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.fixedfloatwindow;
2 |
3 | import android.app.Application;
4 |
5 | /**
6 | * @Copyright © 2017 Analysys Inc. All rights reserved.
7 | * @Description: https://github.com/yhaolpz
8 | * @Version: 1.0
9 | * @Create: 2017/12/18 16:57:40
10 | * @Author: yhao
11 | */
12 | public class BaseApplication extends Application {
13 |
14 | @Override
15 | public void onCreate() {
16 | super.onCreate();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/sample/src/com/example/fixedfloatwindow/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.fixedfloatwindow;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.view.View;
8 | import android.view.animation.BounceInterpolator;
9 | import android.widget.ImageView;
10 | import android.widget.Toast;
11 |
12 | import com.yhao.floatwindow.FloatWindow;
13 | import com.yhao.floatwindow.enums.EMoveType;
14 | import com.yhao.floatwindow.enums.EScreen;
15 | import com.yhao.floatwindow.interfaces.BaseFloatWindow;
16 |
17 | public class MainActivity extends Activity {
18 |
19 | private ImageView mImageView = null;
20 | private ImageView mImageView2 = null;
21 | private FloatWindow.Builder mBuilderA = null;
22 | private BaseFloatWindow mFirstWindow = null;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setTitle("A");
28 | setContentView(R.layout.activity_main);
29 | initUI();
30 | }
31 |
32 | private void initUI() {
33 | mImageView = new ImageView(getApplicationContext());
34 | mImageView2 = new ImageView(getApplicationContext());
35 | mBuilderA = FloatWindow.with(getApplicationContext()).setView(mImageView).setWidth(EScreen.WIDTH, 0.2f)
36 | .setHeight(EScreen.WIDTH, 0.2f).setX(EScreen.WIDTH, 0.8f).setY(EScreen.HEIGHT, 0.3f)
37 | .setMoveType(EMoveType.SLIDE).setMoveStyle(500, new BounceInterpolator()).setDesktopShow(true)
38 | .setTag("mFirstWindow");
39 |
40 | }
41 |
42 | public void onClick(View view) {
43 | mImageView.setImageResource(R.drawable.c_outline_add_circle_outline_black_48dp);
44 | mImageView2.setImageResource(R.drawable.c_outline_settings_black_48dp);
45 | switch (view.getId()) {
46 | case R.id.btnOpenActivityB:
47 | // 打开 B 界面
48 | startActivity(new Intent(this, ActivityB.class));
49 | break;
50 | case R.id.btnReqPermission:
51 | // 申请权限且不构建. 完善中
52 | break;
53 | case R.id.btnOnlyBuild:
54 | // 构建不加载
55 | break;
56 | case R.id.btnInitAndShowA:
57 | // 初始化展示
58 | mFirstWindow = FloatWindow.get("mFirstWindow");
59 | // 效果图1
60 | if (mFirstWindow != null) {
61 | FloatWindow.get("mFirstWindow").show();
62 | } else {
63 | mBuilderA.build();
64 | FloatWindow.get("mFirstWindow").show();
65 | }
66 | break;
67 |
68 | case R.id.btnHideA:
69 | // 隐藏悬浮窗
70 | mFirstWindow = FloatWindow.get("mFirstWindow");
71 | if (mFirstWindow != null) {
72 | mFirstWindow.hide();
73 | } else {
74 | alert("悬浮窗展示状态还没创建~");
75 | }
76 | break;
77 | case R.id.btnDissmissA:
78 | // 销毁悬浮窗
79 | FloatWindow.destroy("mFirstWindow");
80 | break;
81 | case R.id.btnIsVisable1:
82 | // 判断是否可见
83 | // BaseFloatWindow f = FloatWindow.get("mFirstWindow");
84 | // if (f != null) {
85 | // boolean isv = f.isViewVisible();
86 | // alert("悬浮窗展示状态:" + isv);
87 | // } else {
88 | // alert("窗口一未创建");
89 | // }
90 | break;
91 | default:
92 | break;
93 | }
94 | }
95 |
96 | private void alert(String status) {
97 | Toast.makeText(this, status, Toast.LENGTH_LONG).show();
98 | Log.i("FloatWindow", status);
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':floatwindow'
2 |
--------------------------------------------------------------------------------
/version.md:
--------------------------------------------------------------------------------
1 | # 版本变更
2 |
3 | >记录版本变更
4 |
5 | ## 详情
6 | ======
7 |
8 | ### v1.0.9.1
9 |
10 | * 日期: 2019.02.19
11 | * 变更内容:
12 | 1. 增加操作脚本(编译、清除等)
13 | 2. 支持android studio/eclipse工具开发.(不得不吐槽下,as内存占用太猛了,8Gmac开不了几个工程)
14 | 3. 去除V4依赖
15 | 4. 初步合并之前几个合并.(销毁和反注册方法/滑动超出范围的处理)
16 | 5. 调整目录结构
17 |
18 |
19 |
--------------------------------------------------------------------------------