Skip to content

Commit

Permalink
Show icon of package associated with Toast
Browse files Browse the repository at this point in the history
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

Show icons of only background apps in Toasts

As implemented and discussed in I82bf23664eea134f3b1f89ad5a99f6be73906ba8,
this patch disables the icons in Tosts for already foreground apps. It's
useful, no questions about that but please be aware of:

- Icons in Toasts are also displayed in "tooltips" of ActionBar (when you
press and hold an action). I think that goes agains material design as the
ActionBar component now doesn't even show app icon.
- Toasts from Android OS contain the Green robot. (IMHO: It was never intended
for system Toast to have an icon)
- Toasts are intended "to be as unobtrusive as possible" and this kinds of
break it. (From javadoc)
- Icons should stay as workaround for finding that badly designed/intrusive
app because: "Your app should not create a dialog or toast if it is not
currently on screen." (From notifications design paterns)
- Not that relevant but not all users welcome this aesthetic:
https://www.reddit.com/r/cyanogenmod/comments/3oazh8/does_the_icon_in_the_toast_notification_bother/
https://www.reddit.com/r/cyanogenmod/comments/3jg5c2/icons_on_toasts/
etc.

Change-Id: If748297bd422c7ddbf061da735b8e4ed0a41b425
  • Loading branch information
0xD34D authored and xlxfoxxlx committed Jun 21, 2017
1 parent 81a877f commit 4a1554d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
17 changes: 17 additions & 0 deletions core/java/android/app/ActivityManager.java
Expand Up @@ -1871,6 +1871,23 @@ public List<RunningTaskInfo> getRunningTasks(int maxNum)
} }
} }


/**
* Check whether the current foreground tasks belongs to a given package.
*
* @param packageName Name of the package to check for
*
* @return Whether the current foreground tasks belongs to the given package
* @hide
*/
public boolean isPackageInForeground(String packageName) {
try {
return ActivityManagerNative.getDefault().isPackageInForeground(packageName);
} catch (RemoteException e) {
// System dead, we will be dead too soon!
return false;
}
}

/** /**
* Completely remove the given task. * Completely remove the given task.
* *
Expand Down
23 changes: 23 additions & 0 deletions core/java/android/app/ActivityManagerNative.java
Expand Up @@ -679,6 +679,15 @@ public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
return true; return true;
} }


case IS_PACKAGE_IN_FOREGROUND_TRANSACTION: {
data.enforceInterface(IActivityManager.descriptor);
String packageName = data.readString();
boolean result = isPackageInForeground(packageName);
reply.writeNoException();
reply.writeInt(result ? 1 : 0);
return true;
}

case GET_RECENT_TASKS_TRANSACTION: { case GET_RECENT_TASKS_TRANSACTION: {
data.enforceInterface(IActivityManager.descriptor); data.enforceInterface(IActivityManager.descriptor);
int maxNum = data.readInt(); int maxNum = data.readInt();
Expand Down Expand Up @@ -3784,6 +3793,20 @@ public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum, int flags)
reply.recycle(); reply.recycle();
return list; return list;
} }

public boolean isPackageInForeground(String packageName) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeString(packageName);
mRemote.transact(IS_PACKAGE_IN_FOREGROUND_TRANSACTION, data, reply, 0);
reply.readException();
boolean result = reply.readInt() != 0;
data.recycle();
reply.recycle();
return result;
}

public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
int flags, int userId) throws RemoteException { int flags, int userId) throws RemoteException {
Parcel data = Parcel.obtain(); Parcel data = Parcel.obtain();
Expand Down
2 changes: 2 additions & 0 deletions core/java/android/app/IActivityManager.java
Expand Up @@ -138,6 +138,7 @@ public int addAppTask(IBinder activityToken, Intent intent,
public List<RunningTaskInfo> getTasks(int maxNum, int flags) throws RemoteException; public List<RunningTaskInfo> getTasks(int maxNum, int flags) throws RemoteException;
public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
int flags, int userId) throws RemoteException; int flags, int userId) throws RemoteException;
public boolean isPackageInForeground(String packageName) throws RemoteException;
public ActivityManager.TaskThumbnail getTaskThumbnail(int taskId) throws RemoteException; public ActivityManager.TaskThumbnail getTaskThumbnail(int taskId) throws RemoteException;
public List<RunningServiceInfo> getServices(int maxNum, int flags) throws RemoteException; public List<RunningServiceInfo> getServices(int maxNum, int flags) throws RemoteException;
public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState()
Expand Down Expand Up @@ -1031,6 +1032,7 @@ private WaitResult(Parcel source) {
= IBinder.FIRST_CALL_TRANSACTION+299; = IBinder.FIRST_CALL_TRANSACTION+299;
int SHOW_ASSIST_FROM_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+300; int SHOW_ASSIST_FROM_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+300;
int IS_ROOT_VOICE_INTERACTION_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+301; int IS_ROOT_VOICE_INTERACTION_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+301;
int IS_PACKAGE_IN_FOREGROUND_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+302;


// Start of N transactions // Start of N transactions
int START_BINDER_TRACKING_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 340; int START_BINDER_TRACKING_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 340;
Expand Down
19 changes: 12 additions & 7 deletions core/java/android/widget/Toast.java
Expand Up @@ -18,6 +18,7 @@


import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.StringRes; import android.annotation.StringRes;
import android.app.ActivityManager;
import android.app.INotificationManager; import android.app.INotificationManager;
import android.app.ITransientNotification; import android.app.ITransientNotification;
import android.content.Context; import android.content.Context;
Expand Down Expand Up @@ -436,14 +437,18 @@ public void handleShow(IBinder windowToken) {


ImageView appIcon = (ImageView) mView.findViewById(android.R.id.icon); ImageView appIcon = (ImageView) mView.findViewById(android.R.id.icon);
if (appIcon != null) { if (appIcon != null) {
PackageManager pm = context.getPackageManager(); ActivityManager am =
Drawable icon = null; (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
try { if (!am.isPackageInForeground(packageName)) {
icon = pm.getApplicationIcon(packageName); PackageManager pm = context.getPackageManager();
} catch (PackageManager.NameNotFoundException e) { Drawable icon = null;
// nothing to do try {
icon = pm.getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
// nothing to do
}
appIcon.setImageDrawable(icon);
} }
appIcon.setImageDrawable(icon);
} }
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
// We can resolve the Gravity here by using the Locale for getting // We can resolve the Gravity here by using the Locale for getting
Expand Down
Expand Up @@ -9147,6 +9147,14 @@ public List<RunningTaskInfo> getTasks(int maxNum, int flags) {
return list; return list;
} }


@Override
public boolean isPackageInForeground(String packageName) {
synchronized (this) {
ActivityRecord activity = mStackSupervisor.topRunningActivityLocked();
return activity != null && activity.packageName.equals(packageName);
}
}

/** /**
* Creates a new RecentTaskInfo from a TaskRecord. * Creates a new RecentTaskInfo from a TaskRecord.
*/ */
Expand Down

0 comments on commit 4a1554d

Please sign in to comment.