1 /*
2  * Copyright (C) 2022 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.systemui;
18 
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.UserInfo;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.provider.Settings;
28 
29 import com.android.internal.messages.nano.SystemMessageProto;
30 import com.android.systemui.util.NotificationChannels;
31 
32 import javax.inject.Inject;
33 
34 /**
35  * Posts a persistent notification on entry to guest mode
36  */
37 public final class GuestSessionNotification {
38 
39     private static final String TAG = GuestSessionNotification.class.getSimpleName();
40 
41     private final Context mContext;
42     private final NotificationManager mNotificationManager;
43 
44     @Inject
GuestSessionNotification(Context context, NotificationManager notificationManager)45     public GuestSessionNotification(Context context,
46             NotificationManager notificationManager) {
47         mContext = context;
48         mNotificationManager = notificationManager;
49     }
50 
overrideNotificationAppName(Notification.Builder notificationBuilder)51     private void overrideNotificationAppName(Notification.Builder notificationBuilder) {
52         final Bundle extras = new Bundle();
53         String appName = mContext.getString(R.string.guest_notification_app_name);
54 
55         extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, appName);
56 
57         notificationBuilder.addExtras(extras);
58     }
59 
createPersistentNotification(UserInfo userInfo, boolean isGuestFirstLogin)60     void createPersistentNotification(UserInfo userInfo, boolean isGuestFirstLogin) {
61         if (!userInfo.isGuest()) {
62             // we create a persistent notification only for guests
63             return;
64         }
65         String contentText;
66         if (userInfo.isEphemeral()) {
67             contentText = mContext.getString(R.string.guest_notification_ephemeral);
68         } else if (isGuestFirstLogin) {
69             contentText = mContext.getString(R.string.guest_notification_non_ephemeral);
70         } else {
71             contentText = mContext.getString(
72                             R.string.guest_notification_non_ephemeral_non_first_login);
73         }
74 
75         final Intent guestExitIntent = new Intent(
76                         GuestResetOrExitSessionReceiver.ACTION_GUEST_EXIT);
77         final Intent userSettingsIntent = new Intent(Settings.ACTION_USER_SETTINGS);
78 
79         PendingIntent guestExitPendingIntent =
80                 PendingIntent.getBroadcastAsUser(mContext, 0, guestExitIntent,
81                     PendingIntent.FLAG_IMMUTABLE,
82                     UserHandle.SYSTEM);
83 
84         PendingIntent userSettingsPendingIntent =
85                 PendingIntent.getActivityAsUser(mContext, 0, userSettingsIntent,
86                     PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE,
87                     null,
88                     UserHandle.of(userInfo.id));
89 
90         Notification.Builder builder = new Notification.Builder(mContext,
91                                                                 NotificationChannels.ALERTS)
92                 .setSmallIcon(R.drawable.ic_account_circle)
93                 .setContentTitle(mContext.getString(R.string.guest_notification_session_active))
94                 .setContentText(contentText)
95                 .setPriority(Notification.PRIORITY_DEFAULT)
96                 .setOngoing(true)
97                 .setContentIntent(userSettingsPendingIntent);
98 
99         // we show reset button only if this is a 2nd or later login
100         if (!isGuestFirstLogin) {
101             final Intent guestResetIntent = new Intent(
102                             GuestResetOrExitSessionReceiver.ACTION_GUEST_RESET);
103 
104             PendingIntent guestResetPendingIntent =
105                     PendingIntent.getBroadcastAsUser(mContext, 0, guestResetIntent,
106                         PendingIntent.FLAG_IMMUTABLE,
107                         UserHandle.SYSTEM);
108 
109             builder.addAction(R.drawable.ic_sysbar_home,
110                         mContext.getString(
111                             com.android.settingslib.R.string.guest_reset_guest_confirm_button),
112                         guestResetPendingIntent);
113         }
114         builder.addAction(R.drawable.ic_sysbar_home,
115                         mContext.getString(
116                             com.android.settingslib.R.string.guest_exit_button),
117                         guestExitPendingIntent);
118 
119         overrideNotificationAppName(builder);
120 
121         mNotificationManager.notifyAsUser(null,
122                                          SystemMessageProto.SystemMessage.NOTE_GUEST_SESSION,
123                                          builder.build(),
124                                          UserHandle.of(userInfo.id));
125     }
126 }
127