Skip to content

Commit 0816dec

Browse files
0xD34DPRJosh
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
1 parent 6a4dca1 commit 0816dec

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

core/java/android/widget/Toast.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import android.app.INotificationManager;
2222
import android.app.ITransientNotification;
2323
import android.content.Context;
24+
import android.content.pm.PackageManager;
2425
import android.content.res.Configuration;
2526
import android.content.res.Resources;
2627
import android.graphics.PixelFormat;
28+
import android.graphics.drawable.Drawable;
2729
import android.os.Handler;
2830
import android.os.RemoteException;
2931
import android.os.ServiceManager;
@@ -400,6 +402,17 @@ public void handleShow() {
400402
if (context == null) {
401403
context = mView.getContext();
402404
}
405+
406+
ImageView appIcon = (ImageView) mView.findViewById(android.R.id.icon);
407+
if (appIcon != null) {
408+
PackageManager pm = context.getPackageManager();
409+
try {
410+
Drawable icon = pm.getApplicationIcon(packageName);
411+
appIcon.setImageDrawable(icon);
412+
} catch (PackageManager.NameNotFoundException e) {
413+
// Empty
414+
}
415+
}
403416
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
404417
// We can resolve the Gravity here by using the Locale for getting
405418
// the layout direction

core/res/res/layout/transient_notification.xml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,35 @@
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"
3231
android:layout_gravity="center_horizontal"
32+
android:layout_marginTop="-16dp"
33+
android:layout_marginStart="-16dp"
34+
android:layout_toRightOf="@android:id/icon"
35+
android:layout_below="@android:id/icon"
3336
android:textAppearance="@style/TextAppearance.Toast"
3437
android:textColor="@color/bright_foreground_dark"
3538
android:shadowColor="#BB000000"
3639
android:shadowRadius="2.75"
40+
android:background="?android:attr/toastFrameBackground"
3741
/>
3842

39-
</LinearLayout>
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"/>
49+
50+
</RelativeLayout>
4051

4152

0 commit comments

Comments
 (0)