Skip to content

Commit c52c26a

Browse files
eyosenGenkzsz11
authored andcommitted
Show icon of package associated with Toast
For all those times you have some random app or background service that posts a Toast and you have no idea who's posting it. This adds an icon badge to the top left corner of the Toast to show the app's icon the Toast belongs to Change-Id: I82bf23664eea134f3b1f89ad5a99f6be73906ba8 Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
1 parent 531f7b7 commit c52c26a

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

core/java/android/widget/Toast.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import android.compat.annotation.EnabledAfter;
3232
import android.compat.annotation.UnsupportedAppUsage;
3333
import android.content.Context;
34+
import android.content.pm.PackageManager;
3435
import android.content.res.Resources;
36+
import android.graphics.drawable.Drawable;
3537
import android.os.Binder;
3638
import android.os.Build;
3739
import android.os.Handler;
@@ -44,6 +46,7 @@
4446
import android.view.View;
4547
import android.view.WindowManager;
4648
import android.view.accessibility.IAccessibilityManager;
49+
import android.widget.ImageView;
4750

4851
import com.android.internal.annotations.GuardedBy;
4952

@@ -496,6 +499,17 @@ public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
496499
} else {
497500
Toast result = new Toast(context, looper);
498501
View v = ToastPresenter.getTextToastView(context, text);
502+
ImageView appIcon = (ImageView) v.findViewById(android.R.id.icon);
503+
if (appIcon != null) {
504+
PackageManager pm = context.getPackageManager();
505+
Drawable icon = null;
506+
try {
507+
icon = pm.getApplicationIcon(context.getPackageName());
508+
} catch (PackageManager.NameNotFoundException e) {
509+
// nothing to do
510+
}
511+
appIcon.setImageDrawable(icon);
512+
}
499513
result.mNextView = v;
500514
result.mDuration = duration;
501515

core/res/res/layout/transient_notification.xml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@
1818
*/
1919
-->
2020

21-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
21+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2222
android:layout_width="match_parent"
2323
android:layout_height="match_parent"
2424
android:orientation="vertical"
25-
android:background="?android:attr/toastFrameBackground">
25+
android:clipChildren="false">
2626

2727
<TextView
2828
android:id="@android:id/message"
2929
android:layout_width="wrap_content"
3030
android:layout_height="wrap_content"
31-
android:layout_weight="1"
32-
android:layout_marginHorizontal="24dp"
33-
android:layout_marginVertical="15dp"
31+
android:paddingHorizontal="24dp"
32+
android:paddingVertical="15dp"
3433
android:layout_gravity="center_horizontal"
34+
android:layout_marginTop="-16dp"
35+
android:layout_marginStart="-16dp"
36+
android:layout_toRightOf="@android:id/icon"
37+
android:layout_below="@android:id/icon"
3538
android:textAppearance="@style/TextAppearance.Toast"
3639
android:textColor="@color/primary_text_default_material_light"
40+
android:background="?android:attr/toastFrameBackground"
3741
/>
3842

39-
</LinearLayout>
40-
43+
<ImageView
44+
android:id="@android:id/icon"
45+
android:layout_width="24dp"
46+
android:layout_height="24dp"
47+
android:layout_alignParentTop="true"
48+
android:layout_alignParentStart="true"/>
4149

50+
</RelativeLayout>

0 commit comments

Comments
 (0)