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