1 /*
2  * Copyright (C) 2020 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.car.admin;
17 
18 import static com.android.car.admin.CarDevicePolicyService.DEBUG;
19 
20 import android.app.Activity;
21 import android.app.Notification;
22 import android.app.NotificationManager;
23 import android.app.PendingIntent;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Slog;
28 import android.widget.Button;
29 
30 import com.android.car.CarLog;
31 import com.android.car.R;
32 import com.android.car.admin.ui.ManagedDeviceTextView;
33 import com.android.internal.annotations.VisibleForTesting;
34 
35 // TODO(b/171603586): STOPSHIP move UI related activities to CarSettings
36 /**
37  * Shows a disclaimer when a new user is added in a device that is managed by a device owner.
38  */
39 public final class NewUserDisclaimerActivity extends Activity {
40 
41     private static final String TAG = CarLog.tagFor(NewUserDisclaimerActivity.class);
42     private static final int NOTIFICATION_ID =
43             NotificationHelper.NEW_USER_DISCLAIMER_NOTIFICATION_ID;
44 
45     private Button mAcceptButton;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         setContentView(R.layout.new_user_disclaimer);
52 
53         mAcceptButton = findViewById(R.id.accept_button);
54         mAcceptButton.setOnClickListener((v) -> accept());
55     }
56 
57     @Override
onResume()58     protected void onResume() {
59         super.onResume();
60 
61         if (DEBUG) Slog.d(TAG, "showing UI");
62 
63         PerUserCarDevicePolicyService.getInstance(this).setShown();
64 
65         // TODO(b/175057848): automotically finish the activity at x ms if the user doesn't ack it
66         // and/or integrate it with UserNoticeService
67     }
68 
69     @VisibleForTesting
getAcceptButton()70     Button getAcceptButton() {
71         return mAcceptButton;
72     }
73 
accept()74     private void accept() {
75         if (DEBUG) Slog.d(TAG, "user accepted");
76 
77         PerUserCarDevicePolicyService.getInstance(this).setAcknowledged();
78         finish();
79     }
80 
showNotification(Context context)81     static void showNotification(Context context) {
82         PendingIntent pendingIntent = getPendingIntent(context, /* extraFlags= */ 0);
83 
84         Notification notification = NotificationHelper
85                 .newNotificationBuilder(context, NotificationManager.IMPORTANCE_DEFAULT)
86                 // TODO(b/175057848): proper icon?
87                 .setSmallIcon(R.drawable.car_ic_mode)
88                 .setContentTitle(context.getString(R.string.new_user_managed_notification_title))
89                 .setContentText(ManagedDeviceTextView.getManagedDeviceText(context))
90                 .setCategory(Notification.CATEGORY_CAR_INFORMATION)
91                 .setContentIntent(pendingIntent)
92                 .setOngoing(true)
93                 .build();
94 
95         if (DEBUG) {
96             Slog.d(TAG, "Showing new managed notification (id " + NOTIFICATION_ID + " on user "
97                     + context.getUserId());
98         }
99         context.getSystemService(NotificationManager.class).notify(NOTIFICATION_ID, notification);
100     }
101 
cancelNotification(Context context)102     static void cancelNotification(Context context) {
103         if (DEBUG) {
104             Slog.d(TAG, "Canceling notification " + NOTIFICATION_ID + " for user "
105                     + context.getUserId());
106         }
107         context.getSystemService(NotificationManager.class).cancel(NOTIFICATION_ID);
108         getPendingIntent(context, PendingIntent.FLAG_UPDATE_CURRENT).cancel();
109     }
110 
111     @VisibleForTesting
getPendingIntent(Context context, int extraFlags)112     static PendingIntent getPendingIntent(Context context, int extraFlags) {
113         return PendingIntent.getActivity(context, NOTIFICATION_ID,
114                 new Intent(context, NewUserDisclaimerActivity.class),
115                 PendingIntent.FLAG_IMMUTABLE | extraFlags);
116     }
117 }
118