Skip to content

Commit 6e6760c

Browse files
committed
优化框架中获取 Context 的方式
1 parent 6a2d16c commit 6e6760c

File tree

9 files changed

+53
-26
lines changed

9 files changed

+53
-26
lines changed

app/src/main/java/com/hjq/toast/demo/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void customGlobalToastStyle(View v) {
153153
}
154154

155155
public void switchToastStrategy(View v) {
156-
Toaster.setStrategy(new ToastStrategy(ToastStrategy.SHOW_STRATEGY_TYPE_QUEUE));
156+
Toaster.setStrategy(new ToastStrategy(getApplication(), ToastStrategy.SHOW_STRATEGY_TYPE_QUEUE));
157157
Toaster.show(R.string.demo_switch_to_toast_queuing_strategy_result);
158158
findViewById(R.id.tv_main_thrice_show).setVisibility(View.VISIBLE);
159159
}

library/src/main/java/com/hjq/toast/ActivityToast.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class ActivityToast extends CustomToast {
1414
private final ToastImpl mToastImpl;
1515

1616
public ActivityToast(Activity activity) {
17+
super(activity);
1718
mToastImpl = new ToastImpl(activity, this);
1819
}
1920

library/src/main/java/com/hjq/toast/CustomToast.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hjq.toast;
22

3+
import android.content.Context;
34
import android.view.View;
45
import android.widget.TextView;
56
import com.hjq.toast.config.IToast;
@@ -12,6 +13,8 @@
1213
*/
1314
public abstract class CustomToast implements IToast {
1415

16+
private final Context mContext;
17+
1518
/** Toast 布局 */
1619
private View mView;
1720
/** Toast 消息 View */
@@ -35,6 +38,15 @@ public abstract class CustomToast implements IToast {
3538
/** 长吐司显示的时长,参考至 NotificationManagerService.LONG_DELAY */
3639
private int mLongDuration = 3500;
3740

41+
public CustomToast(Context context) {
42+
mContext = context;
43+
}
44+
45+
@Override
46+
public Context getContext() {
47+
return mContext;
48+
}
49+
3850
@Override
3951
public void setText(int id) {
4052
if (mView == null) {

library/src/main/java/com/hjq/toast/GlobalToast.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class GlobalToast extends CustomToast {
1414
private final ToastImpl mToastImpl;
1515

1616
public GlobalToast(Application application) {
17+
super(application);
1718
mToastImpl = new ToastImpl(application, this);
1819
}
1920

library/src/main/java/com/hjq/toast/SystemToast.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hjq.toast;
22

33
import android.app.Application;
4+
import android.content.Context;
45
import android.view.View;
56
import android.widget.TextView;
67
import android.widget.Toast;
@@ -15,11 +16,19 @@
1516
@SuppressWarnings("deprecation")
1617
public class SystemToast extends Toast implements IToast {
1718

19+
private final Application mApplication;
20+
1821
/** 吐司消息 View */
1922
private TextView mMessageView;
2023

2124
public SystemToast(Application application) {
2225
super(application);
26+
mApplication = application;
27+
}
28+
29+
@Override
30+
public Context getContext() {
31+
return mApplication;
2332
}
2433

2534
@Override

library/src/main/java/com/hjq/toast/ToastStrategy.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class ToastStrategy implements IToastStrategy {
5050
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
5151

5252
/** 应用上下文 */
53-
private Application mApplication;
53+
private final Application mApplication;
5454

5555
/** Toast 对象 */
5656
private WeakReference<IToast> mToastReference;
@@ -61,11 +61,12 @@ public class ToastStrategy implements IToastStrategy {
6161
/** 上一个 Toast 显示的时间 */
6262
private volatile long mLastShowToastMillis;
6363

64-
public ToastStrategy() {
65-
this(ToastStrategy.SHOW_STRATEGY_TYPE_IMMEDIATELY);
64+
public ToastStrategy(Application application) {
65+
this(application, ToastStrategy.SHOW_STRATEGY_TYPE_IMMEDIATELY);
6666
}
6767

68-
public ToastStrategy(int type) {
68+
public ToastStrategy(Application application, int type) {
69+
mApplication = application;
6970
mShowStrategyType = type;
7071
switch (mShowStrategyType) {
7172
case SHOW_STRATEGY_TYPE_IMMEDIATELY:
@@ -76,45 +77,42 @@ public ToastStrategy(int type) {
7677
}
7778
}
7879

79-
@Override
80-
public void registerStrategy(Application application) {
81-
mApplication = application;
82-
}
83-
8480
@Override
8581
public int computeShowDuration(CharSequence text) {
8682
return text.length() > 20 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
8783
}
8884

8985
@Override
9086
public IToast createToast(ToastParams params) {
91-
Activity toastActivity = getToastActivity();
87+
final Application application = getApplication();
88+
final Activity toastActivity = getToastActivity();
89+
9290
IToast toast;
9391
if ((params.priorityType == ToastParams.PRIORITY_TYPE_DEFAULT ||
9492
params.priorityType == ToastParams.PRIORITY_TYPE_GLOBAL) &&
9593
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
96-
Settings.canDrawOverlays(mApplication)) {
94+
Settings.canDrawOverlays(application)) {
9795
// 如果有悬浮窗权限,就开启全局的 Toast
98-
toast = new GlobalToast(mApplication);
96+
toast = new GlobalToast(application);
9997
} else if (toastActivity != null &&
10098
(params.priorityType == ToastParams.PRIORITY_TYPE_DEFAULT ||
10199
params.priorityType == ToastParams.PRIORITY_TYPE_LOCAL)) {
102100
// 如果没有悬浮窗权限,就开启一个依附于 Activity 的 Toast
103101
toast = new ActivityToast(toastActivity);
104102
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
105103
// 处理 Android 7.1 上 Toast 在主线程被阻塞后会导致报错的问题
106-
toast = new SafeToast(mApplication);
104+
toast = new SafeToast(application);
107105
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q &&
108-
!areNotificationsEnabled(mApplication)) {
106+
!areNotificationsEnabled(application)) {
109107
// 处理 Toast 关闭通知栏权限之后无法弹出的问题
110108
// 通过查看和对比 NotificationManagerService 的源码
111109
// 发现这个问题已经在 Android 10 版本上面修复了
112110
// 但是 Toast 只能在前台显示,没有通知栏权限后台 Toast 仍然无法显示
113111
// 并且 Android 10 刚好禁止了 Hook 通知服务
114112
// 已经有通知栏权限,不需要 Hook 系统通知服务也能正常显示系统 Toast
115-
toast = new NotificationToast(mApplication);
113+
toast = new NotificationToast(application);
116114
} else {
117-
toast = new SystemToast(mApplication);
115+
toast = new SystemToast(application);
118116
}
119117
if (areSupportCustomToastStyle(toast) || !onlyShowSystemToastStyle()) {
120118
diyToastStyle(toast, params.style);
@@ -182,7 +180,7 @@ protected boolean areSupportCustomToastStyle(IToast toast) {
182180
* 定制 Toast 的样式
183181
*/
184182
protected void diyToastStyle(IToast toast, IToastStyle<?> style) {
185-
toast.setView(style.createView(mApplication));
183+
toast.setView(style.createView(toast.getContext()));
186184
toast.setGravity(style.getGravity(), style.getXOffset(), style.getYOffset());
187185
toast.setMargin(style.getHorizontalMargin(), style.getVerticalMargin());
188186
}
@@ -349,6 +347,13 @@ protected boolean areNotificationsEnabled(Context context) {
349347
return true;
350348
}
351349

350+
/**
351+
* 获取 Application 对象
352+
*/
353+
protected Application getApplication() {
354+
return mApplication;
355+
}
356+
352357
/**
353358
* 获取显示自定义 Toast 的 Activity 对象
354359
*/

library/src/main/java/com/hjq/toast/Toaster.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void init(Application application, IToastStrategy strategy, IToast
7676

7777
// 初始化 Toast 策略
7878
if (strategy == null) {
79-
strategy = new ToastStrategy();
79+
strategy = new ToastStrategy(application);
8080
}
8181
setStrategy(strategy);
8282

@@ -289,7 +289,6 @@ public static void setStrategy(IToastStrategy strategy) {
289289
return;
290290
}
291291
sToastStrategy = strategy;
292-
sToastStrategy.registerStrategy(sApplication);
293292
}
294293

295294
public static IToastStrategy getStrategy() {

library/src/main/java/com/hjq/toast/config/IToast.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hjq.toast.config;
22

3+
import android.content.Context;
34
import android.view.View;
45
import android.widget.TextView;
56

@@ -12,6 +13,11 @@
1213
@SuppressWarnings("unused")
1314
public interface IToast {
1415

16+
/**
17+
* 获取上下文对象
18+
*/
19+
Context getContext();
20+
1521
/**
1622
* 显示
1723
*/

library/src/main/java/com/hjq/toast/config/IToastStrategy.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.hjq.toast.config;
22

3-
import android.app.Application;
43
import com.hjq.toast.ToastParams;
54

65
/**
@@ -11,11 +10,6 @@
1110
*/
1211
public interface IToastStrategy {
1312

14-
/**
15-
* 注册策略
16-
*/
17-
void registerStrategy(Application application);
18-
1913
/**
2014
* 计算 Toast 显示时长
2115
*/

0 commit comments

Comments
 (0)