1 /*
2  * Copyright (C) 2016 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.server.devicepolicy;
18 
19 import android.annotation.IntDef;
20 import android.app.Notification;
21 import android.app.PendingIntent;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.UserHandle;
26 import android.provider.Settings;
27 import android.text.format.DateUtils;
28 
29 import com.android.internal.R;
30 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
31 import com.android.internal.notification.SystemNotificationChannels;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 
36 /**
37  * Utilities class for the remote bugreport operation.
38  */
39 class RemoteBugreportUtils {
40 
41     static final int NOTIFICATION_ID = SystemMessage.NOTE_REMOTE_BUGREPORT;
42 
43     @Retention(RetentionPolicy.SOURCE)
44     @IntDef({
45         DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED,
46         DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED,
47         DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED
48     })
49     @interface RemoteBugreportNotificationType {}
50 
51     static final long REMOTE_BUGREPORT_TIMEOUT_MILLIS = 10 * DateUtils.MINUTE_IN_MILLIS;
52 
53     static final String CTL_STOP = "ctl.stop";
54     static final String REMOTE_BUGREPORT_SERVICE = "bugreportremote";
55 
56     static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
57 
buildNotification(Context context, @RemoteBugreportNotificationType int type)58     static Notification buildNotification(Context context,
59             @RemoteBugreportNotificationType int type) {
60         Intent dialogIntent = new Intent(Settings.ACTION_SHOW_REMOTE_BUGREPORT_DIALOG);
61         dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
62         dialogIntent.putExtra(DevicePolicyManager.EXTRA_BUGREPORT_NOTIFICATION_TYPE, type);
63         PendingIntent pendingDialogIntent = PendingIntent.getActivityAsUser(context, type,
64                 dialogIntent, 0, null, UserHandle.CURRENT);
65 
66         Notification.Builder builder =
67                 new Notification.Builder(context, SystemNotificationChannels.DEVICE_ADMIN)
68                         .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
69                         .setOngoing(true)
70                         .setLocalOnly(true)
71                         .setContentIntent(pendingDialogIntent)
72                         .setColor(context.getColor(
73                                 com.android.internal.R.color.system_notification_accent_color));
74 
75         if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED) {
76             builder.setContentTitle(context.getString(
77                         R.string.sharing_remote_bugreport_notification_title))
78                     .setProgress(0, 0, true);
79         } else if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED) {
80             builder.setContentTitle(context.getString(
81                         R.string.taking_remote_bugreport_notification_title))
82                     .setProgress(0, 0, true);
83         } else if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED) {
84             PendingIntent pendingIntentAccept = PendingIntent.getBroadcast(context, NOTIFICATION_ID,
85                     new Intent(DevicePolicyManager.ACTION_BUGREPORT_SHARING_ACCEPTED),
86                     PendingIntent.FLAG_CANCEL_CURRENT);
87             PendingIntent pendingIntentDecline = PendingIntent.getBroadcast(context,
88                     NOTIFICATION_ID, new Intent(
89                             DevicePolicyManager.ACTION_BUGREPORT_SHARING_DECLINED),
90                     PendingIntent.FLAG_CANCEL_CURRENT);
91             builder.addAction(new Notification.Action.Builder(null /* icon */, context.getString(
92                         R.string.decline_remote_bugreport_action), pendingIntentDecline).build())
93                     .addAction(new Notification.Action.Builder(null /* icon */, context.getString(
94                         R.string.share_remote_bugreport_action), pendingIntentAccept).build())
95                     .setContentTitle(context.getString(
96                         R.string.share_remote_bugreport_notification_title))
97                     .setContentText(context.getString(
98                         R.string.share_remote_bugreport_notification_message_finished))
99                     .setStyle(new Notification.BigTextStyle().bigText(context.getString(
100                         R.string.share_remote_bugreport_notification_message_finished)));
101         }
102 
103         return builder.build();
104     }
105 }
106 
107