1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.test.notificationprovider
17 
18 import android.app.Activity
19 import android.app.Notification
20 import android.app.NotificationChannel
21 import android.app.NotificationManager
22 import android.content.Context
23 import android.os.Bundle
24 
25 /**
26  * Used by NotificationManagerTest for testing policy around content uris.
27  */
28 class RichNotificationActivity : Activity() {
29     companion object {
30         const val NOTIFICATION_MANAGER_CHANNEL_ID = "NotificationManagerTest"
31         const val EXTRA_ACTION = "action"
32         const val ACTION_SEND_7 = "send-7"
33         const val ACTION_SEND_8 = "send-8"
34         const val ACTION_CANCEL_7 = "cancel-7"
35         const val ACTION_CANCEL_8 = "cancel-8"
36     }
37 
38     enum class NotificationPreset(val id: Int, val channelId: String) {
39         Preset7(7, NOTIFICATION_MANAGER_CHANNEL_ID),
40         Preset8(8, NOTIFICATION_MANAGER_CHANNEL_ID);
41 
buildnull42         fun build(context: Context): Notification {
43             val extras = Bundle()
44             extras.putString(Notification.EXTRA_BACKGROUND_IMAGE_URI,
45                     "content://com.android.test.notificationprovider.provider/background$id.png")
46             return Notification.Builder(context, NOTIFICATION_MANAGER_CHANNEL_ID)
47                     .setContentTitle("Rich Notification #$id")
48                     .setSmallIcon(android.R.drawable.sym_def_app_icon)
49                     .addExtras(extras)
50                     .build()
51         }
52     }
53 
onCreatenull54     public override fun onCreate(savedInstanceState: Bundle?) {
55         super.onCreate(savedInstanceState)
56         setContentView(R.layout.activity)
57         when (intent?.extras?.getString(EXTRA_ACTION)) {
58             ACTION_SEND_7 -> sendNotification(NotificationPreset.Preset7)
59             ACTION_SEND_8 -> sendNotification(NotificationPreset.Preset8)
60             ACTION_CANCEL_7 -> cancelNotification(NotificationPreset.Preset7)
61             ACTION_CANCEL_8 -> cancelNotification(NotificationPreset.Preset8)
62             else -> NotificationPreset.values().forEach(::cancelNotification)
63         }
64         finish()
65     }
66 
<lambda>null67     private val notificationManager by lazy { getSystemService(NotificationManager::class.java)!! }
68 
sendNotificationnull69     private fun sendNotification(preset: NotificationPreset) {
70         notificationManager.createNotificationChannel(NotificationChannel(preset.channelId,
71                 "${preset.channelId} Notifications", NotificationManager.IMPORTANCE_DEFAULT))
72         notificationManager.notify(preset.id, preset.build(this))
73     }
74 
cancelNotificationnull75     private fun cancelNotification(preset: NotificationPreset) {
76         notificationManager.cancel(preset.id)
77     }
78 }