开启固定应用功能后,固定某个应用,连续多次点击返回键后不显示如何退出固定模式的提示。
退出固定模式的提示文字
<string name="screen_pinning_toast" msgid="8177286912533744328">"如需取消固定此应用,请轻触并按住“返回”按钮和“概览”按钮"</string>
- 位置: vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/navigationbar/ScreenPinningNotify.java
/** Show a toast that describes the gesture the user should use to escape pinned mode. */
public void showEscapeToast(boolean isGestureNavEnabled, boolean isRecentsButtonVisible) {
long showToastTime = SystemClock.elapsedRealtime();
if ((showToastTime - mLastShowToastTime) < SHOW_TOAST_MINIMUM_INTERVAL) {
Slog.i(TAG, "Ignore toast since it is requested in very short interval.");
return;
}
if (mLastToast != null) {
mLastToast.cancel();
}
mLastToast = makeAllUserToastAndShow(isGestureNavEnabled
? R.string.screen_pinning_toast_gesture_nav
: isRecentsButtonVisible
? R.string.screen_pinning_toast
: R.string.screen_pinning_toast_recents_invisible);
mLastShowToastTime = showToastTime;
}
private Toast makeAllUserToastAndShow(int resId) {
Toast toast = SysUIToast.makeText(mContext, resId, Toast.LENGTH_LONG);
toast.show();
return toast;
}
从代码中看就是正常的显示toast,也只有一个1s内点击不响应的限制,不是这里的问题。
抓取log分析有看到toast被丢弃的信息
NotificationService: Package com.android.systemui is above allowed toast quota, the following toast was blocked and discarded: TextToastRecord{516da7 1237:com.android.systemui/u0a44 isSystemToast=false token=android.os.BinderProxy@83ec254 text=To unpin this app, touch & hold Back and Overview buttons duration=1}
- 位置:frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
@GuardedBy("mToastQueue")
void showNextToastLocked(boolean lastToastWasTextRecord) {
if (mIsCurrentToastShown) {
return; // Don't show the same toast twice.
}
ToastRecord record = mToastQueue.get(0);
while (record != null) {
int userId = UserHandle.getUserId(record.uid);
boolean rateLimitingEnabled =
!mToastRateLimitingDisabledUids.contains(record.uid);
boolean isWithinQuota =
mToastRateLimiter.isWithinQuota(userId, record.pkg, TOAST_QUOTA_TAG)
|| isExemptFromRateLimiting(record.pkg, userId);
boolean isPackageInForeground = isPackageInForegroundForToast(record.uid);
if (tryShowToast(
record, rateLimitingEnabled, isWithinQuota, isPackageInForeground)) {
scheduleDurationReachedLocked(record, lastToastWasTextRecord);
mIsCurrentToastShown = true;
if (rateLimitingEnabled && !isPackageInForeground) {
mToastRateLimiter.noteEvent(userId, record.pkg, TOAST_QUOTA_TAG);
}
return;
}
int index = mToastQueue.indexOf(record);
if (index >= 0) {
ToastRecord mFailedToast = mToastQueue.remove(index);
mWindowManagerInternal.removeWindowToken(mFailedToast.windowToken, false /* removeWindows */,
mFailedToast.displayId);
scheduleKillTokenTimeout(mFailedToast);
}
record = (mToastQueue.size() > 0) ? mToastQueue.get(0) : null;
}
}
/** Returns true if it successfully showed the toast. */
private boolean tryShowToast(ToastRecord record, boolean rateLimitingEnabled,
boolean isWithinQuota, boolean isPackageInForeground) {
// 如果同时满足速率限制已启用、应用受利率限制并且不是前台应用,相关toast就会被丢弃掉
if (rateLimitingEnabled && !isWithinQuota && !isPackageInForeground) {
reportCompatRateLimitingToastsChange(record.uid);
Slog.w(TAG, "Package " + record.pkg + " is above allowed toast quota, the "
+ "following toast was blocked and discarded: " + record);
return false;
}
if (blockToast(record.uid, record.isSystemToast, record.isAppRendered(),
isPackageInForeground)) {
Slog.w(TAG, "Blocking custom toast from package " + record.pkg
+ " due to package not in the foreground at the time of showing the toast");
return false;
}
return record.show();
}
// 返回true 不受速率限制
private boolean isExemptFromRateLimiting(String pkg, int userId) {
boolean isExemptFromRateLimiting = false;
try {
isExemptFromRateLimiting = mPackageManager.checkPermission(
android.Manifest.permission.UNLIMITED_TOASTS, pkg, userId)
== PackageManager.PERMISSION_GRANTED;
} catch (RemoteException e) {
Slog.e(TAG, "Failed to connect with package manager");
}
return isExemptFromRateLimiting;
}
可以看到如果同时满足速率限制已启用、应用受利率限制并且不是前台应用,相关toast就会被丢弃掉。
其中一个条件有判断应用是否不受速率限制isExemptFromRateLimiting,如果当前应用具有android.Manifest.permission.UNLIMITED_TOASTS权限并且已授权就不会被限制。
最终修改就是给SystemUI添加上UNLIMITED_TOASTS的权限,解决问题。