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