Permalink
Browse files

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

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 Aug 26, 2015
1 parent 81a877f commit 4a1554d222b898dc1886e54b7ae1a218c7531e9d
@@ -1871,6 +1871,23 @@ public int addAppTask(@NonNull Activity activity, @NonNull Intent intent,
}
}
/**
* 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.
*
@@ -679,6 +679,15 @@ public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
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: {
data.enforceInterface(IActivityManager.descriptor);
int maxNum = data.readInt();
@@ -3784,6 +3793,20 @@ public Point getAppTaskThumbnailSize() throws RemoteException {
reply.recycle();
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,
int flags, int userId) throws RemoteException {
Parcel data = Parcel.obtain();
@@ -138,6 +138,7 @@ public int addAppTask(IBinder activityToken, Intent intent,
public List<RunningTaskInfo> getTasks(int maxNum, int flags) throws RemoteException;
public ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
int flags, int userId) throws RemoteException;
public boolean isPackageInForeground(String packageName) throws RemoteException;
public ActivityManager.TaskThumbnail getTaskThumbnail(int taskId) throws RemoteException;
public List<RunningServiceInfo> getServices(int maxNum, int flags) throws RemoteException;
public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState()
@@ -1031,6 +1032,7 @@ private WaitResult(Parcel source) {
= IBinder.FIRST_CALL_TRANSACTION+299;
int SHOW_ASSIST_FROM_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION+300;
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
int START_BINDER_TRACKING_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 340;
@@ -18,6 +18,7 @@
import android.annotation.IntDef;
import android.annotation.StringRes;
import android.app.ActivityManager;
import android.app.INotificationManager;
import android.app.ITransientNotification;
import android.content.Context;
@@ -436,14 +437,18 @@ public void handleShow(IBinder windowToken) {
ImageView appIcon = (ImageView) mView.findViewById(android.R.id.icon);
if (appIcon != null) {
PackageManager pm = context.getPackageManager();
Drawable icon = null;
try {
icon = pm.getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
// nothing to do
ActivityManager am =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (!am.isPackageInForeground(packageName)) {
PackageManager pm = context.getPackageManager();
Drawable icon = null;
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);
// We can resolve the Gravity here by using the Locale for getting
@@ -9147,6 +9147,14 @@ public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
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.
*/

0 comments on commit 4a1554d

Please sign in to comment.