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