blob: 1406e8372aa8ec1cdba0ebfa2e8b6d0e862edd38 [file] [log] [blame]
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.security.cts.CVE_2023_21129;
import static org.junit.Assume.assumeNoException;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.os.IBinder;
public class PocService extends Service {
private Resources mResources;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
mResources = getResources();
// Create a bubble intent
PendingIntent bubbleIntent =
PendingIntent.getActivity(
this,
mResources.getInteger(R.integer.requestCode),
new Intent(this, BubbleActivity.class),
PendingIntent.FLAG_MUTABLE /* flags */);
// Create a pending intent for fullscreen intent
PendingIntent pendingIntent =
PendingIntent.getActivity(
this,
mResources.getInteger(R.integer.requestCode),
new Intent(this, PocActivity.class),
PendingIntent.FLAG_IMMUTABLE /* flags */);
// Create icon
Icon icon = createNotificationIcon();
// Create a sharing shortcut
String shortcutId = getString(R.string.shortcutId);
ShortcutInfo shortcut =
new ShortcutInfo.Builder(this, shortcutId)
.setIcon(icon)
.setIntent(new Intent(Intent.ACTION_MAIN))
.setLongLived(true)
.setShortLabel(shortcutId)
.build();
// Create a shortcutManager
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
// Push the shortcut created above to shortcutManager
shortcutManager.pushDynamicShortcut(shortcut);
// Create a notification channel
NotificationChannel notificationChannel =
new NotificationChannel(
getString(R.string.idNotificationChannel),
getString(R.string.nameNotificationChannel),
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(getString(R.string.nameNotificationChannel));
// Create a notificationManager
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
// Create a bubble metadata.
Notification.BubbleMetadata bubbleData =
new Notification.BubbleMetadata.Builder()
.setDesiredHeight(mResources.getInteger(R.integer.bubbleMetaDataheight))
.setIntent(bubbleIntent)
.setAutoExpandBubble(true)
.setSuppressNotification(true)
.setIcon(icon)
.build();
// Set messagingStyle so that conversations is set for notification
Notification.MessagingStyle messagingStyle =
new Notification.MessagingStyle(getString(R.string.personMessagingStyle))
.setConversationTitle(getString(R.string.conversationTitle));
// Create a notification, referencing the sharing shortcut.
Notification.Builder pocNotification =
new Notification.Builder(this, getString(R.string.idNotificationChannel))
.setContentIntent(bubbleIntent)
.setSmallIcon(icon)
.setShortcutId(shortcutId)
.setBubbleMetadata(bubbleData)
.setFullScreenIntent(pendingIntent, true /* high priority */)
.setStyle(messagingStyle);
notificationManager.notify(
getString(R.string.tagNotify),
mResources.getInteger(R.integer.idPocNotification),
pocNotification.build());
} catch (Exception e) {
assumeNoException(e);
}
return super.onStartCommand(intent, flags, startId);
}
Icon createNotificationIcon() throws Exception {
Bitmap testBitmap =
Bitmap.createBitmap(
getResources().getInteger(R.integer.iconWidth),
getResources().getInteger(R.integer.iconHeight),
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(testBitmap);
canvas.drawColor(Color.BLUE);
return Icon.createWithBitmap(testBitmap);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}