1 /*
2  * Copyright (C) 2023 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 
17 package com.android.cts.verifier.managedprovisioning;
18 
19 import static android.Manifest.permission.POST_NOTIFICATIONS;
20 
21 import android.Manifest;
22 import android.app.Activity;
23 import android.app.Notification;
24 import android.app.NotificationChannel;
25 import android.app.NotificationManager;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.os.Bundle;
29 
30 import androidx.core.app.ActivityCompat;
31 import androidx.core.content.ContextCompat;
32 
33 import com.android.compatibility.common.util.ApiTest;
34 import com.android.cts.verifier.R;
35 
36 /**
37  * Activity to create a notification.
38  */
39 @ApiTest(apis = {"android.app.admin.DevicePolicyManager#setApplicationExemption"})
40 public class NotificationActivity extends Activity {
41 
42     NotificationManager mNotificationManager;
43 
44     private static final String NOTIFICATION_CHANNEL_ID =
45             "ctsVerifier/Non-DismissibleNotifications";
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.pass_fail_list);
51 
52         mNotificationManager = getSystemService(NotificationManager.class);
53         mNotificationManager.createNotificationChannel(new NotificationChannel(
54                 NOTIFICATION_CHANNEL_ID,
55                 getString(R.string.provisioning_byod_allow_nondismissible_notification_channel),
56                 NotificationManager.IMPORTANCE_DEFAULT));
57     }
58 
59     @Override
onResume()60     protected void onResume() {
61         super.onResume();
62 
63         boolean hasPostNotificationsPermission = ContextCompat.checkSelfPermission(this,
64                 POST_NOTIFICATIONS)
65                 == PackageManager.PERMISSION_GRANTED;
66 
67         if (hasPostNotificationsPermission) {
68             Intent intent = new Intent(this, NotificationActivity.class);
69 
70             Notification builder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
71                     .setContentTitle(getString(
72                             R.string.provisioning_byod_allow_nondismissible_notification_title))
73                     .setContentText(getString(
74                             R.string.provisioning_byod_allow_nondismissible_notification_text))
75                     .setSmallIcon(R.drawable.icon)
76                     .setVisibility(Notification.VISIBILITY_PUBLIC)
77                     .setAutoCancel(true)
78                     .setOngoing(true)
79                     .build();
80             mNotificationManager.notify(1, builder);
81 
82         } else {
83             ActivityCompat.requestPermissions(this,
84                     new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
85         }
86         finish();
87     }
88 }
89