Commit 0786d3ba authored by Xi Han's avatar Xi Han Committed by Commit Bot

[WebAPK] Don't set channel ID of a notification via reflection.

In this CL, we use Notification.Builder.recoverBuilder() to create a Builder
from the notification sent by Chrome, and reset the notification's channel
ID when necessary.

Bug: 700228
Change-Id: I34cfa00ee2cb93935053020da487242709fbb36e
Reviewed-on: https://chromium-review.googlesource.com/810984
Commit-Queue: Xi Han <hanxi@chromium.org>
Reviewed-by: default avatarAnita Woodruff <awdf@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522196}
parent 9174633f
......@@ -6,7 +6,7 @@
# (including AndroidManifest.xml) is updated. This version should be incremented
# prior to uploading a new ShellAPK to the WebAPK Minting Server.
# Does not affect Chrome.apk
template_shell_apk_version = 36
template_shell_apk_version = 37
# The ShellAPK version expected by Chrome. Chrome will try to update the WebAPK
# if the WebAPK's ShellAPK version is less than |expected_shell_apk_version|.
......
......@@ -4,11 +4,13 @@
package org.chromium.webapk.shell_apk;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
......@@ -80,10 +82,12 @@ public class WebApkServiceImplWrapper extends IWebApkApi.Stub {
@Override
@SuppressWarnings("NewApi")
public void notifyNotification(String platformTag, int platformID, Notification notification) {
// We always rewrite the notification channel id to the WebAPK's default channel id on O+.
// The WebApkServiceImplWrapper was introduced at the same time when WebAPKs target SDK 26.
// That means, we don't need to check whether the target SDK is less than 26 in a WebAPK
// that has a WebApkServiceImplWrapper class.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
ensureNotificationChannelExists();
if (!setNotificationChannelId(notification)) return;
notification = rebuildNotificationWithChannelId(mContext, notification);
}
delegateNotifyNotification(platformTag, platformID, notification);
}
......@@ -144,22 +148,13 @@ public class WebApkServiceImplWrapper extends IWebApkApi.Stub {
return false;
}
/** Sets the notification channel of the given notification object via reflection. */
private boolean setNotificationChannelId(Notification notification) {
try {
// Now that WebAPKs target SDK 26, we need to set a channel id to display the
// notification properly on O+. We set the channel id via reflection because the
// notification is already built (and therefore supposedly immutable) when received from
// Chrome. This is unavoidable because Notification.Builder is not parcelable.
Field channelId = notification.getClass().getDeclaredField("mChannelId");
channelId.setAccessible(true);
channelId.set(notification, DEFAULT_NOTIFICATION_CHANNEL_ID);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
/** Rebuilds a notification with channel ID from the given notification object. */
@TargetApi(Build.VERSION_CODES.O)
private static Notification rebuildNotificationWithChannelId(
Context context, Notification notification) {
Notification.Builder builder = Notification.Builder.recoverBuilder(context, notification);
builder.setChannelId(DEFAULT_NOTIFICATION_CHANNEL_ID);
return builder.build();
}
/** Calls the delegate's {@link notifyNotification} method via reflection. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment