1 /*
2  * Copyright (C) 2019 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.managedprovisioning.common;
17 
18 import static android.app.PendingIntent.FLAG_IMMUTABLE;
19 import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
20 
21 import android.app.Notification;
22 import android.app.NotificationChannel;
23 import android.app.NotificationManager;
24 import android.app.PendingIntent;
25 import android.content.Context;
26 import android.content.Intent;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.managedprovisioning.R;
30 
31 import static com.android.internal.util.Preconditions.checkNotNull;
32 
33 import com.google.android.setupdesign.util.Partner;
34 
35 /**
36  * Helper methods for showing notifications, such as the provisioning reminder and
37  * privacy reminder notifications.
38  */
39 public class NotificationHelper {
40     @VisibleForTesting
41     static final String CHANNEL_ID = "ManagedProvisioning";
42 
43     @VisibleForTesting
44     static final int ENCRYPTION_NOTIFICATION_ID = 1;
45 
46     @VisibleForTesting
47     static final int PRIVACY_REMINDER_NOTIFICATION_ID = 2;
48 
49     private final Context mContext;
50 
NotificationHelper(Context context)51     public NotificationHelper(Context context) {
52         mContext = checkNotNull(context);
53     }
54 
55     /**
56      * Notification asking the user to resume provisioning after encryption has happened.
57      */
showResumeNotification(Intent intent)58     public void showResumeNotification(Intent intent) {
59         final NotificationManager notificationManager =
60                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
61         final NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
62                 mContext.getString(R.string.encrypt), NotificationManager.IMPORTANCE_HIGH);
63         notificationManager.createNotificationChannel(channel);
64 
65         final PendingIntent resumePendingIntent = PendingIntent.getActivity(
66                 mContext, 0, intent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
67         final Notification.Builder notify = new Notification.Builder(mContext)
68                 .setChannelId(CHANNEL_ID)
69                 .setContentIntent(resumePendingIntent)
70                 .setContentTitle(mContext
71                         .getString(R.string.continue_provisioning_notify_title))
72                 .setContentText(mContext.getString(R.string.continue_provisioning_notify_text))
73                 .setSmallIcon(com.android.internal.R.drawable.ic_corp_statusbar_icon)
74                 .setVisibility(Notification.VISIBILITY_PUBLIC)
75                 .setColor(mContext.getResources().getColor(
76                         com.android.internal.R.color.system_notification_accent_color))
77                 .setAutoCancel(true);
78         notificationManager.notify(ENCRYPTION_NOTIFICATION_ID, notify.build());
79     }
80 
showPrivacyReminderNotification(Context context, @NotificationManager.Importance int importance)81     public void showPrivacyReminderNotification(Context context,
82             @NotificationManager.Importance int importance) {
83         final NotificationManager notificationManager =
84                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
85         final NotificationChannel channel = new NotificationChannel(
86                 CHANNEL_ID, mContext.getString(R.string.app_label), importance);
87         notificationManager.createNotificationChannel(channel);
88 
89         final Notification.Builder notify = new Notification.Builder(mContext, CHANNEL_ID)
90                 .setColor(Partner.getColor(context, R.color.setup_notification_bg_color))
91                 .setColorized(true)
92                 .setContentTitle(mContext.getString(
93                         R.string.fully_managed_device_provisioning_privacy_title))
94                 .setContentText(
95                         mContext.getString(R.string.fully_managed_device_provisioning_privacy_body))
96                 .setStyle(new Notification.BigTextStyle().bigText(mContext.getString(
97                         R.string.fully_managed_device_provisioning_privacy_body)))
98                 .setSmallIcon(com.android.internal.R.drawable.ic_corp_statusbar_icon)
99                 .setVisibility(Notification.VISIBILITY_PUBLIC)
100                 .setAutoCancel(true);
101         notificationManager.notify(PRIVACY_REMINDER_NOTIFICATION_ID, notify.build());
102     }
103 }
104