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.settings.privatespace;
18 
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Flags;
23 import android.os.UserManager;
24 import android.safetycenter.SafetyEvent;
25 import android.safetycenter.SafetySourceData;
26 import android.safetycenter.SafetySourceStatus;
27 import android.util.Log;
28 
29 import com.android.settings.R;
30 import com.android.settings.safetycenter.SafetyCenterManagerWrapper;
31 
32 /** Private Space safety source for the Safety Center */
33 public final class PrivateSpaceSafetySource {
34     public static final String SAFETY_SOURCE_ID = "AndroidPrivateSpace";
35     private static final String TAG = "PrivateSpaceSafetySrc";
36 
PrivateSpaceSafetySource()37     private PrivateSpaceSafetySource() {}
38 
39     /** Sets lock screen safety data for Safety Center. */
setSafetySourceData(Context context, SafetyEvent safetyEvent)40     public static void setSafetySourceData(Context context,
41             SafetyEvent safetyEvent) {
42         if (!SafetyCenterManagerWrapper.get().isEnabled(context)) {
43             Log.i(TAG, "Safety Center disabled");
44             return;
45         }
46 
47         UserManager userManager = context.getSystemService(UserManager.class);
48         PrivateSpaceMaintainer privateSpaceMaintainer =
49                 PrivateSpaceMaintainer.getInstance(context);
50         if (android.multiuser.Flags.enablePrivateSpaceFeatures()
51                 && android.multiuser.Flags.blockPrivateSpaceCreation()) {
52             // Do not add the entry point when
53             // -Private Profile is not present and
54             // -Private Profile cannot be added.
55             if (!privateSpaceMaintainer.isPrivateSpaceEntryPointEnabled()) {
56                 Log.i(TAG, "Private Space not allowed for user " + context.getUser());
57                 return;
58             }
59         }
60 
61         // Check the profile type - we don't want to show this for anything other than primary
62         // user.
63         if (userManager != null && !userManager.isMainUser()) {
64             Log.i(TAG, "setSafetySourceData not main user");
65             return;
66         }
67 
68         if (!Flags.allowPrivateProfile()
69                 || !android.multiuser.Flags.enablePrivateSpaceFeatures()) {
70             // Setting null safetySourceData so that an old entry gets cleared out and this way
71             // provide a response since SC always expects one on rescan.
72             SafetyCenterManagerWrapper.get().setSafetySourceData(
73                     context,
74                     SAFETY_SOURCE_ID,
75                     /* safetySourceData */ null,
76                     safetyEvent
77             );
78             return;
79         }
80 
81         PendingIntent pendingIntent = getPendingIntentForPsDashboard(context);
82 
83         SafetySourceStatus status = new SafetySourceStatus.Builder(
84                 context.getString(R.string.private_space_title),
85                 context.getString(R.string.private_space_summary),
86                 SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED)
87                 .setPendingIntent(pendingIntent).build();
88         SafetySourceData safetySourceData =
89                 new SafetySourceData.Builder().setStatus(status).build();
90 
91         Log.d(TAG, "Setting safety source data");
92         SafetyCenterManagerWrapper.get().setSafetySourceData(
93                 context,
94                 SAFETY_SOURCE_ID,
95                 safetySourceData,
96                 safetyEvent
97         );
98     }
99 
getPendingIntentForPsDashboard(Context context)100     private static PendingIntent getPendingIntentForPsDashboard(Context context) {
101         Intent privateSpaceAuthenticationIntent =
102                 new Intent(context, PrivateSpaceAuthenticationActivity.class)
103                         .setIdentifier(SAFETY_SOURCE_ID);
104 
105         return PendingIntent
106                 .getActivity(
107                         context,
108                         /* requestCode */ 0,
109                         privateSpaceAuthenticationIntent,
110                         PendingIntent.FLAG_IMMUTABLE);
111     }
112 }
113