Skip to content

Commit

Permalink
优化 ApplicationToast 类命名
Browse files Browse the repository at this point in the history
优化 ToastLogInterceptor 类筛除类的规则
修复在 Android 13 上面会出现 Toast 布局被半透明化的问题
  • Loading branch information
getActivity committed Jun 3, 2023
1 parent 87a97e9 commit e6dee89
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion HelpDoc-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class ToastStrategy {

#### What should I do if the framework cannot meet the scene I am currently using

* The Toaster framework is intended to solve some toast requirements. If Toaster cannot meet your needs, you can consider using the [XToast](https://github.com/getActivity/XToast) floating window framework to achieve it.
* The Toaster framework is intended to solve some toast requirements. If Toaster cannot meet your needs, you can consider using the [EasyWindow](https://github.com/getActivity/EasyWindow) floating window framework to achieve it.

#### Why the framework prefers to use window manager to implement toast

Expand Down
2 changes: 1 addition & 1 deletion HelpDoc-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public class ToastStrategy {

#### 框架无法满足我当前使用的场景怎么办

* Toaster 框架意在解决一些的 Toast 需求,如果 Toaster 无法满足你的需求,你可以考虑使用 [XToast](https://github.com/getActivity/XToast) 悬浮窗框架来实现。
* Toaster 框架意在解决一些的 Toast 需求,如果 Toaster 无法满足你的需求,你可以考虑使用 [EasyWindow](https://github.com/getActivity/EasyWindow) 悬浮窗框架来实现。

#### 为什么框架优先使用 WindowManager 来实现 Toast

Expand Down
8 changes: 4 additions & 4 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* Project address: [Github](https://github.com/getActivity/Toaster)

* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.2/Toaster.apk)
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.3/Toaster.apk)

![](picture/en/demo_toast_activity.jpg) ![](picture/en/demo_toast_style_white.jpg) ![](picture/en/demo_toast_style_black.jpg)

Expand Down Expand Up @@ -49,7 +49,7 @@ android {
dependencies {
// Toast framework:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.2'
implementation 'com.github.getActivity:Toaster:12.3'
}
```

Expand Down Expand Up @@ -134,7 +134,7 @@ Toaster.getInterceptor();

| Function or detail | [Toaster](https://github.com/getActivity/Toaster) |[ AndroidUtilCode-ToastUtils ](https://github.com/Blankj/AndroidUtilCode)| [Toasty](https://github.com/GrenderG/Toasty) |
| :----: | :------: | :-----: | :-----: |
| Corresponding version | 12.2 | 1.30.6 | 1.5.0 |
| Corresponding version | 12.3 | 1.30.6 | 1.5.0 |
| Number of issues | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) |[![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues)| [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
| Framework pack size | 31 KB | 500 KB | 50 KB |
| Framework maintenance status| 维护中 | 停止维护 | 停止维护 |
Expand Down Expand Up @@ -234,7 +234,7 @@ new Toast

* Title bar framework: [TitleBar](https://github.com/getActivity/TitleBar)![](https://img.shields.io/github/stars/getActivity/TitleBar.svg)![](https://img.shields.io/github/forks/getActivity/TitleBar.svg)

* Floating window framework: [XToast](https://github.com/getActivity/XToast)![](https://img.shields.io/github/stars/getActivity/XToast.svg)![](https://img.shields.io/github/forks/getActivity/XToast.svg)
* Floating window framework: [EasyWindow](https://github.com/getActivity/EasyWindow)![](https://img.shields.io/github/stars/getActivity/EasyWindow.svg)![](https://img.shields.io/github/forks/getActivity/EasyWindow.svg)

* Shape view framework: [ShapeView](https://github.com/getActivity/ShapeView)![](https://img.shields.io/github/stars/getActivity/ShapeView.svg)![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)

Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* 博客地址:[只需体验三分钟,你就会跟我一样,爱上这款 Toast](https://www.jianshu.com/p/9b174ee2c571)

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

![](picture/zh/download_demo_apk_qr_code.png)

Expand Down Expand Up @@ -53,7 +53,7 @@ android {
dependencies {
// 吐司框架:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.2'
implementation 'com.github.getActivity:Toaster:12.3'
}
```

Expand Down Expand Up @@ -138,7 +138,7 @@ Toaster.getInterceptor();

| 功能或细节 | [Toaster](https://github.com/getActivity/Toaster) | [AndroidUtilCode-ToastUtils](https://github.com/Blankj/AndroidUtilCode) | [Toasty](https://github.com/GrenderG/Toasty) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 12.2 | 1.30.6 | 1.5.0 |
| 对应版本 | 12.3 | 1.30.6 | 1.5.0 |
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) | [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
| 框架体积 | 32 KB | 500 KB | 50 KB |
| 框架维护状态 | 维护中 | 停止维护 | 停止维护 |
Expand Down Expand Up @@ -242,7 +242,7 @@ new Toast

* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar) ![](https://img.shields.io/github/stars/getActivity/TitleBar.svg) ![](https://img.shields.io/github/forks/getActivity/TitleBar.svg)

* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast) ![](https://img.shields.io/github/stars/getActivity/XToast.svg) ![](https://img.shields.io/github/forks/getActivity/XToast.svg)
* 悬浮窗框架:[EasyWindow](https://github.com/getActivity/EasyWindow) ![](https://img.shields.io/github/stars/getActivity/EasyWindow.svg) ![](https://img.shields.io/github/forks/getActivity/EasyWindow.svg)

* Shape 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)

Expand Down Expand Up @@ -282,6 +282,18 @@ new Toast

#### [点击查看捐赠列表](https://github.com/getActivity/Donate)

#### 广告区

* 我现在任腾讯云服务器推广大使,大家如果有购买服务器的需求,可以通过下面的链接购买

[![](https://upload-dianshi-1255598498.file.myqcloud.com/upload/nodir/345X200-9ae456f58874df499adf7c331c02cb0fed12b81d.jpg)](https://curl.qcloud.com/A6cYskvv)

[【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中](https://curl.qcloud.com/A6cYskvv)

[![](https://upload-dianshi-1255598498.file.myqcloud.com/345-200-b28f7dee9552f4241ea6a543f15a9798049701d4.jpg)](https://curl.qcloud.com/up4fQsdn)

[【腾讯云】中小企业福利专场,多款刚需产品,满足企业通用场景需求](https://curl.qcloud.com/up4fQsdn)

## License

```text
Expand Down
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 31
compileSdkVersion 33

defaultConfig {
applicationId "com.hjq.toast.demo"
minSdkVersion 16
targetSdkVersion 31
versionCode 1202
versionName "12.2"
targetSdkVersion 33
versionCode 1203
versionName "12.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -62,13 +62,13 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:10.0'
implementation 'com.github.getActivity:TitleBar:10.3'

// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:16.6'
implementation 'com.github.getActivity:XXPermissions:18.0'

// 悬浮窗框架:https://github.com/getActivity/XToast
implementation 'com.github.getActivity:XToast:8.9'
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:10.0'

// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/hjq/toast/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.hjq.toast.style.BlackToastStyle;
import com.hjq.toast.style.CustomToastStyle;
import com.hjq.toast.style.WhiteToastStyle;
import com.hjq.xtoast.XToast;
import com.hjq.window.EasyWindow;

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -169,13 +169,13 @@ public void run() {
}, 5000);
}

public void combinationXToastShow(View v) {
new XToast<>(this)
public void combinationEasyWindowShow(View v) {
new EasyWindow<>(this)
.setDuration(1000)
// 将 Toaster 中的 View 转移给 XToast 来显示
// 将 Toaster 中的 View 转移给 EasyWindow 来显示
.setContentView(Toaster.getStyle().createView(getApplication()))
.setAnimStyle(android.R.style.Animation_Translucent)
.setText(android.R.id.message, R.string.demo_combining_xtoast_use_result)
.setText(android.R.id.message, R.string.demo_combining_window_framework_use_result)
.setGravity(Gravity.BOTTOM)
.setYOffset((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()))
.show();
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:onClick="combinationXToastShow"
android:text="@string/demo_combining_xtoast_use" />
android:onClick="combinationEasyWindowShow"
android:text="@string/demo_combining_window_framework_use" />

</LinearLayout>

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<string name="demo_switch_to_toast_queuing_strategy">切换成 Toast 排队显示策略</string>
<string name="demo_click_show_three_toast">点我连续显示 3 个 Toast</string>
<string name="demo_show_toast_in_background_state">在后台状态下显示 Toast</string>
<string name="demo_combining_xtoast_use">搭配 XToast 悬浮窗框架</string>
<string name="demo_combining_window_framework_use">搭配 EasyWindow 悬浮窗框架</string>

<string name="demo_show_toast_result">我是普通的 Toast</string>
<string name="demo_show_toast_result">我是普通的 Toast我是普通的 Toast我是普通的 Toast我是普通的 Toast我是普通的 Toast</string>
<string name="demo_show_short_toast_result">我是一个短 Toast</string>
<string name="demo_show_long_toast_result">我是一个长 Toast</string>
<string name="demo_show_toast_with_two_second_delay_result">我是延迟 2 秒显示的 Toast</string>
Expand All @@ -32,10 +32,10 @@
<string name="demo_custom_toast_layout_result">我是自定义布局的 Toast(全局生效)</string>
<string name="demo_switch_to_toast_queuing_strategy_result">切换到排队显示策略成功,请点击下方文本来体验效果</string>
<string name="demo_show_three_toast_copywriting">我是第 %d 个 Toast</string>
<string name="demo_show_toast_in_background_state_hint">系好安全带,即将跳转到手机桌面</string>
<string name="demo_show_toast_in_background_state_hint">请系好安全带,即将跳转到手机桌面</string>
<string name="demo_show_toast_in_background_state_snack_bar">温馨提示:安卓 10 在后台显示 Toast 需要有通知栏权限或者悬浮窗权限的情况下才可以显示</string>
<string name="demo_show_toast_in_background_state_result_1">我是在后台显示的 Toast(有悬浮窗权限真的可以为所欲为)</string>
<string name="demo_show_toast_in_background_state_result_2">我是在后台显示的 Toast(安卓 11 及以上在后台显示只能使用系统样式)</string>
<string name="demo_show_toast_in_background_state_result_3">我是在后台显示的 Toast</string>
<string name="demo_combining_xtoast_use_result">就问你溜不溜</string>
<string name="demo_combining_window_framework_use_result">就问你溜不溜</string>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<string name="demo_switch_to_toast_queuing_strategy">Switch to toast queuing display strategy</string>
<string name="demo_click_show_three_toast">Click and I show 3 toast in a row</string>
<string name="demo_show_toast_in_background_state">Show toast in background state</string>
<string name="demo_combining_xtoast_use">Combining xtoast suspension window frames</string>
<string name="demo_combining_window_framework_use">Combining EasyWindow suspension window frames</string>

<string name="demo_show_toast_result">I am an normal toast</string>
<string name="demo_show_short_toast_result">I am a short toast</string>
Expand All @@ -38,5 +38,5 @@
<string name="demo_show_toast_in_background_state_result_1">I display the toast in the background (you can really do whatever you want with the floating window permission)</string>
<string name="demo_show_toast_in_background_state_result_2">I am displaying the toast in the background (Android 11 and above can only use the system style in the background display)</string>
<string name="demo_show_toast_in_background_state_result_3">I am displaying toast in the background</string>
<string name="demo_combining_xtoast_use_result">Hello Word</string>
<string name="demo_combining_window_framework_use_result">Hello Word</string>
</resources>
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 31
compileSdkVersion 33

defaultConfig {
minSdkVersion 14
versionCode 1202
versionName "12.2"
versionCode 1203
versionName "12.3"
}

// 使用 JDK 1.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
* time : 2021/11/30
* desc : 利用悬浮窗权限弹出全局 Toast
*/
public class ApplicationToast extends CustomToast {
public class GlobalToast extends CustomToast {

/** Toast 实现类 */
private final ToastImpl mToastImpl;

public ApplicationToast(Application application) {
public GlobalToast(Application application) {
mToastImpl = new ToastImpl(application, this);
}

Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/hjq/toast/ToastImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public void run() {
if (mGlobalShow) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
// 在 type 等于 TYPE_APPLICATION_OVERLAY 的时候
// 不能添加 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 标记
// 否则会导致在 Android 13 上面会出现 Toast 布局被半透明化的效果
// Github issue 地址:https://github.com/getActivity/Toaster/issues/108
params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
} else {
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
Expand Down
8 changes: 5 additions & 3 deletions library/src/main/java/com/hjq/toast/ToastLogInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ protected void printLog(String msg) {

protected boolean filterClass(Class<?> clazz) {
// 排查以下几种情况:
// 1. 排除自身
// 1. 排除自身及其子类
// 2. 排除 Toaster 类
// 3. 排除接口类
// 4. 排除抽象类
return this.getClass().equals(clazz) || Toaster.class.equals(clazz) ||
clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers());
return IToastInterceptor.class.isAssignableFrom(clazz) ||
Toaster.class.equals(clazz) ||
clazz.isInterface() ||
Modifier.isAbstract(clazz.getModifiers());
}
}
2 changes: 1 addition & 1 deletion library/src/main/java/com/hjq/toast/ToastStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public IToast createToast(IToastStyle<?> style) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
Settings.canDrawOverlays(mApplication)) {
// 如果有悬浮窗权限,就开启全局的 Toast
toast = new ApplicationToast(mApplication);
toast = new GlobalToast(mApplication);
} else if (foregroundActivity != null) {
// 如果没有悬浮窗权限,就开启一个依附于 Activity 的 Toast
toast = new ActivityToast(foregroundActivity);
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/hjq/toast/Toaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static IToastStrategy getStrategy() {

/**
* 设置 Toast 拦截器(可以根据显示的内容决定是否拦截这个Toast)
* 场景:打印 Toast 内容日志、根据 Toast 内容是否包含敏感字来动态切换其他方式显示(这里可以使用我的另外一套框架 XToast
* 场景:打印 Toast 内容日志、根据 Toast 内容是否包含敏感字来动态切换其他方式显示(这里可以使用我的另外一套框架 EasyWindow
*/
public static void setInterceptor(IToastInterceptor interceptor) {
sToastInterceptor = interceptor;
Expand Down

0 comments on commit e6dee89

Please sign in to comment.