Skip to content

Commit dd34a59

Browse files
committed
优化框架中部分代码嵌套逻辑
优化在透明 Activity 展示 Logcat 入口逻辑 升级和适配 EasyWindow 悬浮窗框架 10.6 版本
1 parent 01a3613 commit dd34a59

File tree

8 files changed

+105
-75
lines changed

8 files changed

+105
-75
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* 项目地址:[Github](https://github.com/getActivity/Logcat)
66

7-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.8/Logcat.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Logcat/releases/download/11.82/Logcat.apk)
88

99
![](picture/demo_code.png)
1010

@@ -51,7 +51,7 @@ dependencyResolutionManagement {
5151
```groovy
5252
dependencies {
5353
// 日志调试框架:https://github.com/getActivity/Logcat
54-
debugImplementation 'com.github.getActivity:Logcat:11.8'
54+
debugImplementation 'com.github.getActivity:Logcat:11.82'
5555
}
5656
```
5757

app/build.gradle

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
apply plugin: 'com.android.application'
2+
apply from : '../common.gradle'
23

34
android {
4-
compileSdk 34
55

66
defaultConfig {
77
applicationId "com.hjq.logcat.demo"
8-
minSdk 16
9-
targetSdk 34
10-
versionCode 1180
11-
versionName "11.8"
12-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13-
}
14-
15-
// 使用 JDK 1.8
16-
compileOptions {
17-
targetCompatibility JavaVersion.VERSION_1_8
18-
sourceCompatibility JavaVersion.VERSION_1_8
198
}
209

2110
// Apk 签名的那些事:https://www.jianshu.com/p/a1f8e5896aa2
@@ -48,9 +37,6 @@ android {
4837
outputFileName = rootProject.getName() + '.apk'
4938
}
5039
}
51-
52-
// 将构建文件统一输出到项目根目录下的 build 文件夹
53-
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
5440
}
5541

5642
dependencies {
@@ -68,7 +54,7 @@ dependencies {
6854
implementation 'com.github.getActivity:TitleBar:10.5'
6955

7056
// 吐司框架:https://github.com/getActivity/Toaster
71-
implementation 'com.github.getActivity:Toaster:12.3'
57+
implementation 'com.github.getActivity:Toaster:12.6'
7258

7359
// 内存泄漏捕捉:https://github.com/square/leakcanary
7460
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'

app/src/main/res/values-v23/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<item name="android:windowLightStatusBar">true</item>
1212
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
1313
<item name="android:textAllCaps">false</item>
14+
<!--<item name="android:windowIsTranslucent">true</item>-->
1415
</style>
1516

1617
</resources>

common.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 通用配置
2+
android {
3+
4+
// 编译源码版本
5+
compileSdk 34
6+
defaultConfig {
7+
// 最低安装版本
8+
minSdk 16
9+
// Android 版本适配指南:https://github.com/getActivity/AndroidVersionAdapter
10+
targetSdk 34
11+
versionCode 1182
12+
versionName "11.82"
13+
}
14+
15+
// 支持 Java JDK 8
16+
compileOptions {
17+
targetCompatibility JavaVersion.VERSION_1_8
18+
sourceCompatibility JavaVersion.VERSION_1_8
19+
}
20+
21+
// 读取 local.properties 文件配置
22+
def properties = new Properties()
23+
def localPropertiesFile = rootProject.file("local.properties")
24+
if (localPropertiesFile.exists()) {
25+
localPropertiesFile.withInputStream { inputStream ->
26+
properties.load(inputStream)
27+
}
28+
}
29+
30+
String buildDirPath = properties.getProperty("build.dir")
31+
if (buildDirPath != null && buildDirPath != "") {
32+
// 将构建文件统一输出到指定的目录下
33+
setBuildDir(new File(buildDirPath, rootProject.name + "/build/${path.replaceAll(':', '/')}"))
34+
} else {
35+
// 将构建文件统一输出到项目根目录下的 build 文件夹
36+
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
37+
}
38+
}

library/build.gradle

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
apply plugin: 'com.android.library'
2+
apply from : '../common.gradle'
23

34
android {
45

56
// 资源前缀限制
67
resourcePrefix "logcat_"
78

8-
compileSdk 34
9-
10-
defaultConfig {
11-
minSdkVersion 16
12-
versionCode 1180
13-
versionName "11.8"
14-
}
15-
169
lintOptions {
1710
abortOnError false
1811
}
@@ -23,9 +16,6 @@ android {
2316
outputFileName = "${rootProject.name}-${android.defaultConfig.versionName}.aar"
2417
}
2518
}
26-
27-
// 将构建文件统一输出到项目根目录下的 build 文件夹
28-
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
2919
}
3020

3121
afterEvaluate {
@@ -42,7 +32,7 @@ dependencies {
4232
// noinspection GradleCompatible
4333
implementation 'com.android.support:design:26.0.0'
4434
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
45-
implementation 'com.github.getActivity:EasyWindow:10.3'
35+
implementation 'com.github.getActivity:EasyWindow:10.6'
4636
}
4737

4838
tasks.withType(Javadoc).configureEach {

library/src/main/java/com/hjq/logcat/LogcatActivity.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -537,56 +537,53 @@ private void refreshLayout() {
537537
}
538538

539539
Window window = getWindow();
540-
if (window != null) {
541-
// 沉浸式状态栏
542-
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
540+
if (window == null) {
541+
return;
543542
}
544543

544+
// 沉浸式状态栏
545+
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
546+
545547
mBarView.setPadding(0, 0, 0, 0);
546548
mRootView.setPadding(0, 0, 0, 0);
547549

548550
if (LogcatUtils.isPortrait(this)) {
549-
if (window != null) {
550-
// 在竖屏的状态下显示状态栏和导航栏
551-
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
552-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
553-
// 实现状态栏图标和文字颜色为亮色
554-
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
555-
}
551+
// 在竖屏的状态下显示状态栏和导航栏
552+
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
553+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
554+
// 实现状态栏图标和文字颜色为亮色
555+
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
556556
}
557+
557558
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
558-
if (window != null) {
559-
WindowManager.LayoutParams params = window.getAttributes();
560-
// 会让屏幕到延伸刘海区域中
561-
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
562-
window.setAttributes(params);
563-
}
559+
WindowManager.LayoutParams params = window.getAttributes();
560+
// 会让屏幕到延伸刘海区域中
561+
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
562+
window.setAttributes(params);
564563
}
565564

566565
mBarView.setPadding(0, LogcatUtils.getStatusBarHeight(this), 0, 0);
567-
} else {
568-
if (window != null) {
569-
// 在横屏的状态下隐藏状态栏和导航栏
570-
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
571-
}
566+
return;
567+
}
572568

573-
mBarView.setPadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0,
574-
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);
569+
// 在横屏的状态下隐藏状态栏和导航栏
570+
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
575571

576-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
577-
if (window != null) {
578-
WindowManager.LayoutParams params = window.getAttributes();
579-
// 不会让屏幕到延伸刘海区域中,会留出一片黑色区域
580-
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
581-
window.setAttributes(params);
582-
}
583-
} else {
584-
if (LogcatUtils.isActivityReverse(this)) {
585-
mRootView.setPadding(0, 0, LogcatUtils.getStatusBarHeight(this), 0);
586-
} else {
587-
mRootView.setPadding(LogcatUtils.getStatusBarHeight(this), 0, 0, 0);
588-
}
589-
}
572+
mBarView.setPadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0,
573+
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);
574+
575+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
576+
WindowManager.LayoutParams params = window.getAttributes();
577+
// 不让屏幕内容到延伸刘海区域中,会留出一片黑色区域
578+
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
579+
window.setAttributes(params);
580+
return;
581+
}
582+
583+
if (LogcatUtils.isActivityReverse(this)) {
584+
mRootView.setPadding(0, 0, LogcatUtils.getStatusBarHeight(this), 0);
585+
} else {
586+
mRootView.setPadding(LogcatUtils.getStatusBarHeight(this), 0, 0, 0);
590587
}
591588
}
592589

@@ -600,16 +597,18 @@ public void run() {
600597
LogcatConfig.setSearchKeyConfig(keyword);
601598
mAdapter.setKeyword(keyword);
602599
mLinearLayoutManager.scrollToPosition(mAdapter.getItemCount() - 1);
600+
603601
if (!"".equals(keyword)) {
604602
mIconView.setVisibility(View.VISIBLE);
605603
mIconView.setImageResource(R.drawable.logcat_ic_empty);
604+
return;
605+
}
606+
607+
if (!mSearchKeyword.isEmpty()) {
608+
mIconView.setVisibility(View.VISIBLE);
609+
mIconView.setImageResource(R.drawable.logcat_ic_history);
606610
} else {
607-
if (!mSearchKeyword.isEmpty()) {
608-
mIconView.setVisibility(View.VISIBLE);
609-
mIconView.setImageResource(R.drawable.logcat_ic_history);
610-
} else {
611-
mIconView.setVisibility(View.GONE);
612-
}
611+
mIconView.setVisibility(View.GONE);
613612
}
614613
}
615614
};

library/src/main/java/com/hjq/logcat/LogcatDispatcher.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Activity;
44
import android.app.Application;
55
import android.os.Bundle;
6+
import android.util.TypedValue;
67

78
/**
89
* author : Android 轮子哥
@@ -21,6 +22,10 @@ public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
2122
if (activity instanceof LogcatActivity) {
2223
return;
2324
}
25+
if (isActivityTranslucent(activity)) {
26+
// Activity是半透明的,则不展示悬浮窗
27+
return;
28+
}
2429
// 在每个 Activity 创建的时候派发
2530
new LogcatWindow(activity)
2631
.show();
@@ -43,4 +48,15 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
4348

4449
@Override
4550
public void onActivityDestroyed(Activity activity) {}
51+
52+
/**
53+
* 判断当前 Activity 窗口是否为半透明的
54+
*/
55+
public boolean isActivityTranslucent(Activity activity) {
56+
TypedValue typedValue = new TypedValue();
57+
activity.getTheme().resolveAttribute(android.R.attr.windowIsTranslucent, typedValue, true);
58+
// 当前 android:windowIsTranslucent 为 true 时,typedValue.data 的值为 - 1
59+
// 如果不设置或者设置为 false,则 typedValue.data 的值为 0
60+
return typedValue.data != 0;
61+
}
4662
}

library/src/main/java/com/hjq/logcat/LogcatWindow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import android.view.View;
88
import android.widget.ImageView;
99
import com.hjq.window.EasyWindow;
10-
import com.hjq.window.draggable.SpringDraggable;
10+
import com.hjq.window.draggable.SpringBackDraggable;
1111

1212
/**
1313
* author : Android 轮子哥
@@ -30,7 +30,7 @@ final class LogcatWindow extends EasyWindow<LogcatWindow> implements EasyWindow.
3030
setHeight(size);
3131

3232
setAnimStyle(android.R.style.Animation_Toast);
33-
setDraggable(new SpringDraggable());
33+
setDraggable(new SpringBackDraggable());
3434
setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
3535
setOnClickListener(android.R.id.icon, this);
3636
}

0 commit comments

Comments
 (0)