Commit 5663c4d6 authored by Richard Knoll's avatar Richard Knoll Committed by Chromium LUCI CQ

Open dialer directly on Android S for ClickToCall

Starting Activities from BroadcastReceivers is no longer supported.

Bug: 1169595
Change-Id: I820d888a6adc37b73d0315df9a9b3a598e771d37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2643509
Commit-Queue: Richard Knoll <knollr@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846176}
parent 0797c3b5
......@@ -9,6 +9,7 @@ import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
......@@ -42,10 +43,12 @@ public final class SharingNotificationUtil {
* @param contentText The notification content text.
* @param largeIconId The large notification icon resource id, 0 if not used.
* @param color The color to be used for the notification.
* @param startsActivity Whether the {@code contentIntent} starts an Activity.
*/
public static void showNotification(@SystemNotificationType int type, String group, int id,
PendingIntentProvider contentIntent, String contentTitle, String contentText,
@DrawableRes int smallIconId, @DrawableRes int largeIconId, int color) {
@DrawableRes int smallIconId, @DrawableRes int largeIconId, int color,
boolean startsActivity) {
Context context = ContextUtils.getApplicationContext();
Resources resources = context.getResources();
NotificationWrapperBuilder builder =
......@@ -54,7 +57,6 @@ public final class SharingNotificationUtil {
ChromeChannelDefinitions.ChannelId.SHARING,
/*remoteAppPackageName=*/null,
new NotificationMetadata(type, group, id))
.setContentIntent(contentIntent)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setColor(ApiCompatibilityUtils.getColor(context.getResources(), color))
......@@ -63,6 +65,15 @@ public final class SharingNotificationUtil {
.setSmallIcon(smallIconId)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (startsActivity && Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
// We can't use the NotificationIntentInterceptor to start Activities after Android R.
// Use the unmodified PendingIntent directly instead.
builder.setContentIntent(contentIntent.getPendingIntent());
} else {
builder.setContentIntent(contentIntent);
}
if (largeIconId != 0) {
Bitmap largeIcon = BitmapFactory.decodeResource(resources, largeIconId);
if (largeIcon != null) builder.setLargeIcon(largeIcon);
......
......@@ -37,16 +37,8 @@ public class ClickToCallMessageHandler {
* @param phoneNumber The phone number to show in the dialer.
*/
private static void openDialer(String phoneNumber) {
final Intent dialIntent;
if (!TextUtils.isEmpty(phoneNumber)) {
dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
} else {
dialIntent = new Intent(Intent.ACTION_DIAL);
}
dialIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
ContextUtils.getApplicationContext().startActivity(dialIntent);
ContextUtils.getApplicationContext().startActivity(getDialIntent(phoneNumber));
ClickToCallUma.recordDialerPresent(true);
} catch (ActivityNotFoundException activityNotFound) {
// Notify the user that no dialer app was available.
......@@ -71,7 +63,7 @@ public class ClickToCallMessageHandler {
context.getResources().getString(
R.string.click_to_call_dialer_absent_notification_text),
R.drawable.ic_error_outline_red_24dp, R.drawable.ic_dialer_not_found_red_40dp,
R.color.google_red_600);
R.color.google_red_600, /*startsActivity=*/false);
}
/**
......@@ -109,17 +101,38 @@ public class ClickToCallMessageHandler {
*/
private static void displayNotification(String phoneNumber) {
Context context = ContextUtils.getApplicationContext();
PendingIntentProvider contentIntent = PendingIntentProvider.getBroadcast(context,
/*requestCode=*/0,
new Intent(context, TapReceiver.class).putExtra(EXTRA_PHONE_NUMBER, phoneNumber),
PendingIntent.FLAG_UPDATE_CURRENT);
SharingNotificationUtil.showNotification(
NotificationUmaTracker.SystemNotificationType.CLICK_TO_CALL,
NotificationConstants.GROUP_CLICK_TO_CALL,
NotificationConstants.NOTIFICATION_ID_CLICK_TO_CALL, contentIntent, phoneNumber,
NotificationConstants.NOTIFICATION_ID_CLICK_TO_CALL,
getContentIntentProvider(phoneNumber), phoneNumber,
context.getResources().getString(R.string.click_to_call_notification_text),
R.drawable.ic_devices_16dp, R.drawable.ic_dialer_icon_blue_40dp,
R.color.default_icon_color_blue);
R.color.default_icon_color_blue, /*startsActivity=*/true);
}
private static Intent getDialIntent(String phoneNumber) {
Intent dialIntent = TextUtils.isEmpty(phoneNumber)
? new Intent(Intent.ACTION_DIAL)
: new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
dialIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return dialIntent;
}
private static PendingIntentProvider getContentIntentProvider(String phoneNumber) {
Context context = ContextUtils.getApplicationContext();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
// We can't use the TapReceiver broadcast to start the dialer Activity after Android R.
// Use the dial intent directly instead.
return PendingIntentProvider.getActivity(context, /*requestCode=*/0,
getDialIntent(phoneNumber), PendingIntent.FLAG_UPDATE_CURRENT);
}
return PendingIntentProvider.getBroadcast(context,
/*requestCode=*/0,
new Intent(context, TapReceiver.class).putExtra(EXTRA_PHONE_NUMBER, phoneNumber),
PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
......
......@@ -214,6 +214,6 @@ public class SharedClipboardMessageHandler {
NotificationConstants.NOTIFICATION_ID_SHARED_CLIPBOARD_INCOMING, contentIntent,
notificationTitle, resources.getString(R.string.shared_clipboard_notification_text),
R.drawable.ic_devices_16dp, R.drawable.shared_clipboard_40dp,
R.color.default_icon_color_blue);
R.color.default_icon_color_blue, /*startsActivity=*/false);
}
}
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