Skip to content

Commit

Permalink
优化框架中部分代码嵌套逻辑
Browse files Browse the repository at this point in the history
优化在透明 Activity 展示 Logcat 入口逻辑
升级和适配 EasyWindow 悬浮窗框架 10.6 版本
  • Loading branch information
getActivity committed Nov 25, 2023
1 parent 01a3613 commit dd34a59
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -51,7 +51,7 @@ dependencyResolutionManagement {
```groovy
dependencies {
// 日志调试框架:https://github.com/getActivity/Logcat
debugImplementation 'com.github.getActivity:Logcat:11.8'
debugImplementation 'com.github.getActivity:Logcat:11.82'
}
```

Expand Down
18 changes: 2 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
apply plugin: 'com.android.application'
apply from : '../common.gradle'

android {
compileSdk 34

defaultConfig {
applicationId "com.hjq.logcat.demo"
minSdk 16
targetSdk 34
versionCode 1180
versionName "11.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

// 使用 JDK 1.8
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}

// Apk 签名的那些事:https://www.jianshu.com/p/a1f8e5896aa2
Expand Down Expand Up @@ -48,9 +37,6 @@ android {
outputFileName = rootProject.getName() + '.apk'
}
}

// 将构建文件统一输出到项目根目录下的 build 文件夹
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
}

dependencies {
Expand All @@ -68,7 +54,7 @@ dependencies {
implementation 'com.github.getActivity:TitleBar:10.5'

// 吐司框架:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.3'
implementation 'com.github.getActivity:Toaster:12.6'

// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-v23/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<item name="android:windowLightStatusBar">true</item>
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
<item name="android:textAllCaps">false</item>
<!--<item name="android:windowIsTranslucent">true</item>-->
</style>

</resources>
38 changes: 38 additions & 0 deletions common.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 通用配置
android {

// 编译源码版本
compileSdk 34
defaultConfig {
// 最低安装版本
minSdk 16
// Android 版本适配指南:https://github.com/getActivity/AndroidVersionAdapter
targetSdk 34
versionCode 1182
versionName "11.82"
}

// 支持 Java JDK 8
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}

// 读取 local.properties 文件配置
def properties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withInputStream { inputStream ->
properties.load(inputStream)
}
}

String buildDirPath = properties.getProperty("build.dir")
if (buildDirPath != null && buildDirPath != "") {
// 将构建文件统一输出到指定的目录下
setBuildDir(new File(buildDirPath, rootProject.name + "/build/${path.replaceAll(':', '/')}"))
} else {
// 将构建文件统一输出到项目根目录下的 build 文件夹
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
}
}
14 changes: 2 additions & 12 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
apply plugin: 'com.android.library'
apply from : '../common.gradle'

android {

// 资源前缀限制
resourcePrefix "logcat_"

compileSdk 34

defaultConfig {
minSdkVersion 16
versionCode 1180
versionName "11.8"
}

lintOptions {
abortOnError false
}
Expand All @@ -23,9 +16,6 @@ android {
outputFileName = "${rootProject.name}-${android.defaultConfig.versionName}.aar"
}
}

// 将构建文件统一输出到项目根目录下的 build 文件夹
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
}

afterEvaluate {
Expand All @@ -42,7 +32,7 @@ dependencies {
// noinspection GradleCompatible
implementation 'com.android.support:design:26.0.0'
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:10.3'
implementation 'com.github.getActivity:EasyWindow:10.6'
}

tasks.withType(Javadoc).configureEach {
Expand Down
85 changes: 42 additions & 43 deletions library/src/main/java/com/hjq/logcat/LogcatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,56 +537,53 @@ private void refreshLayout() {
}

Window window = getWindow();
if (window != null) {
// 沉浸式状态栏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (window == null) {
return;
}

// 沉浸式状态栏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

mBarView.setPadding(0, 0, 0, 0);
mRootView.setPadding(0, 0, 0, 0);

if (LogcatUtils.isPortrait(this)) {
if (window != null) {
// 在竖屏的状态下显示状态栏和导航栏
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 实现状态栏图标和文字颜色为亮色
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
// 在竖屏的状态下显示状态栏和导航栏
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 实现状态栏图标和文字颜色为亮色
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
// 会让屏幕到延伸刘海区域中
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
}
WindowManager.LayoutParams params = window.getAttributes();
// 会让屏幕到延伸刘海区域中
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
}

mBarView.setPadding(0, LogcatUtils.getStatusBarHeight(this), 0, 0);
} else {
if (window != null) {
// 在横屏的状态下隐藏状态栏和导航栏
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return;
}

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
// 不会让屏幕到延伸刘海区域中,会留出一片黑色区域
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
window.setAttributes(params);
}
} else {
if (LogcatUtils.isActivityReverse(this)) {
mRootView.setPadding(0, 0, LogcatUtils.getStatusBarHeight(this), 0);
} else {
mRootView.setPadding(LogcatUtils.getStatusBarHeight(this), 0, 0, 0);
}
}
mBarView.setPadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams params = window.getAttributes();
// 不让屏幕内容到延伸刘海区域中,会留出一片黑色区域
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
window.setAttributes(params);
return;
}

if (LogcatUtils.isActivityReverse(this)) {
mRootView.setPadding(0, 0, LogcatUtils.getStatusBarHeight(this), 0);
} else {
mRootView.setPadding(LogcatUtils.getStatusBarHeight(this), 0, 0, 0);
}
}

Expand All @@ -600,16 +597,18 @@ public void run() {
LogcatConfig.setSearchKeyConfig(keyword);
mAdapter.setKeyword(keyword);
mLinearLayoutManager.scrollToPosition(mAdapter.getItemCount() - 1);

if (!"".equals(keyword)) {
mIconView.setVisibility(View.VISIBLE);
mIconView.setImageResource(R.drawable.logcat_ic_empty);
return;
}

if (!mSearchKeyword.isEmpty()) {
mIconView.setVisibility(View.VISIBLE);
mIconView.setImageResource(R.drawable.logcat_ic_history);
} else {
if (!mSearchKeyword.isEmpty()) {
mIconView.setVisibility(View.VISIBLE);
mIconView.setImageResource(R.drawable.logcat_ic_history);
} else {
mIconView.setVisibility(View.GONE);
}
mIconView.setVisibility(View.GONE);
}
}
};
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/com/hjq/logcat/LogcatDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.TypedValue;

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

@Override
public void onActivityDestroyed(Activity activity) {}

/**
* 判断当前 Activity 窗口是否为半透明的
*/
public boolean isActivityTranslucent(Activity activity) {
TypedValue typedValue = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.windowIsTranslucent, typedValue, true);
// 当前 android:windowIsTranslucent 为 true 时,typedValue.data 的值为 - 1
// 如果不设置或者设置为 false,则 typedValue.data 的值为 0
return typedValue.data != 0;
}
}
4 changes: 2 additions & 2 deletions library/src/main/java/com/hjq/logcat/LogcatWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.view.View;
import android.widget.ImageView;
import com.hjq.window.EasyWindow;
import com.hjq.window.draggable.SpringDraggable;
import com.hjq.window.draggable.SpringBackDraggable;

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

setAnimStyle(android.R.style.Animation_Toast);
setDraggable(new SpringDraggable());
setDraggable(new SpringBackDraggable());
setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
setOnClickListener(android.R.id.icon, this);
}
Expand Down

0 comments on commit dd34a59

Please sign in to comment.