Skip to content

Commit c8274ae

Browse files
committed
see 12/28 log
1 parent 63c4dae commit c8274ae

File tree

8 files changed

+96
-13
lines changed

8 files changed

+96
-13
lines changed

utilcode/lib/src/main/java/com/blankj/utilcode/util/ToastUtils.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.Activity;
5+
import android.app.AlertDialog;
6+
import android.app.Dialog;
57
import android.content.Context;
8+
import android.content.DialogInterface;
69
import android.content.res.Configuration;
710
import android.content.res.Resources;
811
import android.graphics.Color;
@@ -24,10 +27,13 @@
2427
import android.view.Gravity;
2528
import android.view.LayoutInflater;
2629
import android.view.View;
30+
import android.view.Window;
2731
import android.view.WindowManager;
2832
import android.widget.TextView;
2933
import android.widget.Toast;
3034

35+
import com.blankj.utilcode.R;
36+
3137
import java.lang.reflect.Field;
3238

3339
/**
@@ -415,6 +421,7 @@ public void dispatchMessage(Message msg) {
415421

416422
static class ToastWithoutNotification extends AbsToast {
417423

424+
private Dialog mDialog;
418425
private WindowManager mWM;
419426
private View mView;
420427

@@ -443,13 +450,26 @@ public void show() {
443450
mParams.y = mToast.getYOffset();
444451
} else {
445452
Context topActivityOrApp = Utils.getTopActivityOrApp();
446-
if (topActivityOrApp instanceof Activity) {
447-
Activity topActivity = (Activity) topActivityOrApp;
453+
if (!(topActivityOrApp instanceof Activity)) {
454+
Log.e("ToastUtils", "Couldn't get top Activity.");
455+
return;
456+
}
457+
Activity topActivity = (Activity) topActivityOrApp;
458+
if (topActivity.isFinishing() || topActivity.isDestroyed()) {
459+
Log.e("ToastUtils", topActivity + " is useless");
460+
return;
461+
}
462+
if (topActivity.hasWindowFocus()) {
448463
mWM = topActivity.getWindowManager();
449-
Utils.getActivityLifecycle().addOnActivityDestroyedListener(topActivity, listener);
464+
mParams.type = WindowManager.LayoutParams.LAST_SUB_WINDOW;
465+
mParams.y = mToast.getYOffset() + getNavBarHeight();
466+
} else {
467+
mDialog = new Dialog(topActivity, R.style.DialogTransparent);
468+
mDialog.setContentView(mView);
469+
Window window = mDialog.getWindow();
470+
mParams.y = mToast.getYOffset();
450471
}
451-
mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
452-
mParams.y = mToast.getYOffset() + getNavBarHeight();
472+
Utils.getActivityLifecycle().addOnActivityDestroyedListener(topActivity, listener);
453473
}
454474

455475
final Configuration config = context.getResources().getConfiguration();
@@ -479,9 +499,17 @@ public void show() {
479499
mParams.x = mToast.getXOffset();
480500
mParams.packageName = Utils.getApp().getPackageName();
481501

482-
try {
483-
mWM.addView(mView, mParams);
484-
} catch (Exception ignored) { /**/ }
502+
if (mDialog != null) {
503+
mDialog.getWindow().setAttributes(mParams);
504+
mDialog.show();
505+
} else {
506+
try {
507+
if (mWM != null) {
508+
mWM.addView(mView, mParams);
509+
}
510+
} catch (Exception ignored) { /**/ }
511+
}
512+
485513

486514
HANDLER.postDelayed(new Runnable() {
487515
@Override
@@ -498,6 +526,10 @@ public void cancel() {
498526
mWM.removeViewImmediate(mView);
499527
}
500528
} catch (Exception ignored) { /**/ }
529+
if (mDialog != null) {
530+
mDialog.dismiss();
531+
mDialog = null;
532+
}
501533
mView = null;
502534
mWM = null;
503535
mToast = null;

utilcode/lib/src/main/res/values-v21/styles.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414
<item name="android:windowAnimationStyle">@null</item>
1515
<item name="android:windowDisablePreview">true</item>
1616
</style>
17+
18+
<style name="DialogTransparent" parent="android:style/Theme.Dialog">
19+
<item name="android:background">@android:color/transparent</item>
20+
<item name="android:windowBackground">@android:color/transparent</item>
21+
<item name="android:windowNoTitle">true</item>
22+
<item name="android:backgroundDimEnabled">false</item>
23+
</style>
1724
</resources>

utilcode/pkg/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
android:launchMode="singleTop" />
9595
<activity
9696
android:name=".feature.blur.TransparentActivity"
97-
android:theme="@style/ActivityTranslucent"
98-
android:launchMode="singleTop" />
97+
android:launchMode="singleTop"
98+
android:theme="@style/ActivityTranslucent" />
9999
<activity
100100
android:name=".feature.clean.CleanActivity"
101101
android:launchMode="singleTop" />

utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/toast/ToastActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.blankj.utilcode.pkg.R;
1313
import com.blankj.lib.base.BaseBackActivity;
14+
import com.blankj.utilcode.pkg.helper.DialogHelper;
1415
import com.blankj.utilcode.util.SpanUtils;
1516
import com.blankj.utilcode.util.ToastUtils;
1617

@@ -24,8 +25,6 @@
2425
*/
2526
public class ToastActivity extends BaseBackActivity {
2627

27-
View toastView;
28-
2928
public static void start(Context context) {
3029
Intent starter = new Intent(context, ToastActivity.class);
3130
context.startActivity(starter);
@@ -54,7 +53,7 @@ public void initView(Bundle savedInstanceState, View contentView) {
5453
findViewById(R.id.btn_show_custom_view).setOnClickListener(this);
5554
findViewById(R.id.btn_show_middle).setOnClickListener(this);
5655
findViewById(R.id.btn_cancel_toast).setOnClickListener(this);
57-
toastView = findViewById(R.id.btn_cancel_toast);
56+
findViewById(R.id.btn_show_toast_dialog).setOnClickListener(this);
5857
}
5958

6059
@Override
@@ -117,6 +116,9 @@ public void run() {
117116
} else if (i == R.id.btn_cancel_toast) {
118117
ToastUtils.cancel();
119118

119+
} else if (i == R.id.btn_show_toast_dialog) {
120+
DialogHelper.showToastDialog();
121+
120122
}
121123
}
122124

utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/helper/DialogHelper.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import android.support.v7.app.AlertDialog;
66
import android.view.LayoutInflater;
77
import android.view.View;
8+
import android.widget.Button;
89
import android.widget.EditText;
910

1011
import com.blankj.utilcode.pkg.R;
1112
import com.blankj.utilcode.util.ActivityUtils;
1213
import com.blankj.utilcode.util.KeyboardUtils;
1314
import com.blankj.utilcode.util.PermissionUtils;
1415
import com.blankj.utilcode.util.PermissionUtils.OnRationaleListener.ShouldRequest;
16+
import com.blankj.utilcode.util.ToastUtils;
1517

1618
/**
1719
* <pre>
@@ -126,4 +128,20 @@ public void onClick(View v) {
126128
dialogView.findViewById(R.id.btn_close_dialog).setOnClickListener(listener);
127129
dialog.show();
128130
}
131+
132+
public static void showToastDialog() {
133+
Activity topActivity = ActivityUtils.getTopActivity();
134+
if (topActivity == null) return;
135+
final View dialogView = LayoutInflater.from(topActivity).inflate(R.layout.dialog_toast, null);
136+
dialogView.findViewById(R.id.btn_show_short_toast)
137+
.setOnClickListener(new View.OnClickListener() {
138+
@Override
139+
public void onClick(View v) {
140+
ToastUtils.showShort("Short");
141+
}
142+
});
143+
final AlertDialog dialog = new AlertDialog.Builder(topActivity).setView(dialogView).create();
144+
dialog.setCanceledOnTouchOutside(false);
145+
dialog.show();
146+
}
129147
}

utilcode/pkg/src/main/res/layout/activity_toast.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@
6969
android:layout_width="match_parent"
7070
android:layout_height="wrap_content"
7171
android:text="@string/toast_cancel" />
72+
73+
<Button
74+
android:id="@+id/btn_show_toast_dialog"
75+
style="@style/WideBtnStyle"
76+
android:layout_width="match_parent"
77+
android:layout_height="wrap_content"
78+
android:text="@string/toast_show_toast_dialog" />
7279
</LinearLayout>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:gravity="center_horizontal"
6+
android:orientation="vertical"
7+
android:padding="@dimen/spacing_16">
8+
9+
<Button
10+
android:id="@+id/btn_show_short_toast"
11+
style="@style/WideBtnStyle"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
android:text="@string/toast_show_short" />
15+
16+
</LinearLayout>

utilcode/pkg/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
<string name="toast_custom_view">Custom View</string>
283283
<string name="toast_show_middle">Show Middle</string>
284284
<string name="toast_cancel">Cancel</string>
285+
<string name="toast_show_toast_dialog">Show Toast Dialog</string>
285286

286287
<string name="toast_short">Short</string>
287288
<string name="toast_long">Long</string>

0 commit comments

Comments
 (0)