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 package com.android.settings;
17 
18 import static android.app.admin.DevicePolicyResources.Strings.Settings.SHARE_REMOTE_BUGREPORT_DIALOG_TITLE;
19 import static android.app.admin.DevicePolicyResources.Strings.Settings.SHARE_REMOTE_BUGREPORT_FINISHED_REQUEST_CONSENT;
20 import static android.app.admin.DevicePolicyResources.Strings.Settings.SHARE_REMOTE_BUGREPORT_NOT_FINISHED_REQUEST_CONSENT;
21 import static android.app.admin.DevicePolicyResources.Strings.Settings.SHARING_REMOTE_BUGREPORT_MESSAGE;
22 
23 import android.app.Activity;
24 import android.app.admin.DevicePolicyManager;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.util.Log;
30 
31 import androidx.annotation.Nullable;
32 import androidx.appcompat.app.AlertDialog;
33 
34 /**
35  * UI for the remote bugreport dialog. Shows one of 3 possible dialogs:
36  * <ul>
37  * <li>bugreport is still being taken and can be shared or declined</li>
38  * <li>bugreport has been taken and can be shared or declined</li>
39  * <li>bugreport has already been accepted to be shared, but is still being taken</li>
40  * </ul>
41  */
42 public class RemoteBugreportActivity extends Activity {
43 
44     private static final String TAG = "RemoteBugreportActivity";
45 
46     @Override
onCreate(@ullable Bundle savedInstanceState)47     protected void onCreate(@Nullable Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49 
50         DevicePolicyManager devicePolicyManager = getSystemService(DevicePolicyManager.class);
51 
52         final int notificationType = getIntent().getIntExtra(
53                 DevicePolicyManager.EXTRA_BUGREPORT_NOTIFICATION_TYPE, -1);
54 
55         if (notificationType == DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED) {
56             AlertDialog dialog = new AlertDialog.Builder(this)
57                     .setMessage(devicePolicyManager.getResources().getString(
58                             SHARING_REMOTE_BUGREPORT_MESSAGE,
59                             () -> getString(R.string.sharing_remote_bugreport_dialog_message)))
60                     .setOnDismissListener(new DialogInterface.OnDismissListener() {
61                         @Override
62                         public void onDismiss(DialogInterface dialog) {
63                             finish();
64                         }
65                     })
66                     .setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
67                         @Override
68                         public void onClick(DialogInterface dialog, int which) {
69                             finish();
70                         }
71                     })
72                     .create();
73             dialog.show();
74         } else if (notificationType == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED
75                 || notificationType
76                         == DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED) {
77 
78             int defaultMessageId =  notificationType
79                     == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED
80                     ? R.string.share_remote_bugreport_dialog_message
81                     : R.string.share_remote_bugreport_dialog_message_finished;
82             String overrideMessageId = notificationType
83                     == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED
84                     ? SHARE_REMOTE_BUGREPORT_NOT_FINISHED_REQUEST_CONSENT
85                     : SHARE_REMOTE_BUGREPORT_FINISHED_REQUEST_CONSENT;
86 
87             AlertDialog dialog = new AlertDialog.Builder(this)
88                     .setTitle(devicePolicyManager.getResources().getString(
89                             SHARE_REMOTE_BUGREPORT_DIALOG_TITLE,
90                             () -> getString(R.string.share_remote_bugreport_dialog_title)))
91                     .setMessage(devicePolicyManager.getResources().getString(overrideMessageId,
92                             () -> getString(defaultMessageId)))
93                     .setOnDismissListener(new DialogInterface.OnDismissListener() {
94                         @Override
95                         public void onDismiss(DialogInterface dialog) {
96                             finish();
97                         }
98                     })
99                     .setNegativeButton(R.string.decline_remote_bugreport_action,
100                             new DialogInterface.OnClickListener() {
101                         @Override
102                         public void onClick(DialogInterface dialog, int which) {
103                             Intent intent = new Intent(
104                                     DevicePolicyManager.ACTION_BUGREPORT_SHARING_DECLINED);
105                             RemoteBugreportActivity.this.sendBroadcastAsUser(intent,
106                                     UserHandle.SYSTEM, android.Manifest.permission.DUMP);
107                             finish();
108                         }
109                     })
110                     .setPositiveButton(R.string.share_remote_bugreport_action,
111                             new DialogInterface.OnClickListener() {
112                         @Override
113                         public void onClick(DialogInterface dialog, int which) {
114                             Intent intent = new Intent(
115                                     DevicePolicyManager.ACTION_BUGREPORT_SHARING_ACCEPTED);
116                             RemoteBugreportActivity.this.sendBroadcastAsUser(intent,
117                                     UserHandle.SYSTEM, android.Manifest.permission.DUMP);
118                             finish();
119                         }
120                     })
121                     .create();
122             dialog.show();
123         } else {
124             Log.e(TAG, "Incorrect dialog type, no dialog shown. Received: " + notificationType);
125         }
126     }
127 }
128