Handle SHOW_REMOTE_BUGREPORT_DIALOG in TvSettings

 Add RemoteBugreportActivity to handle bugreports requested by device admins.

Test: CtsVerifier Device Owner Requesting Bugreport
Bug: 183429341
Change-Id: If8a0b16aad64d5d41ca2225e3043a0713c6bf764
diff --git a/Settings/AndroidManifest.xml b/Settings/AndroidManifest.xml
index df2deb9..4616a33 100644
--- a/Settings/AndroidManifest.xml
+++ b/Settings/AndroidManifest.xml
@@ -1142,6 +1142,16 @@
             </intent-filter>
         </receiver>
 
+        <activity android:name=".RemoteBugreportActivity"
+            android:excludeFromRecents="true"
+            android:exported="true"
+            android:permission="android.permission.DUMP"
+            android:theme="@style/TvSettingsActivityTheme">
+            <intent-filter>
+                <action android:name="android.settings.SHOW_REMOTE_BUGREPORT_DIALOG" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
 
     </application>
 
diff --git a/Settings/res/values/strings.xml b/Settings/res/values/strings.xml
index 0af910a..8b8755f 100644
--- a/Settings/res/values/strings.xml
+++ b/Settings/res/values/strings.xml
@@ -2040,6 +2040,19 @@
 
     <string name="default_admin_support_msg">If you have questions, contact your IT admin</string>
 
+    <!-- Title of dialog shown to ask for user consent for sharing a bugreport that was requested remotely by the IT administrator. -->
+    <string name="share_remote_bugreport_dialog_title">Share bug report?</string>
+    <!-- Message of a dialog shown to ask for user consent for sharing a bugreport that was requested remotely by the IT administrator. -->
+    <string name="share_remote_bugreport_dialog_message_finished">Your IT admin requested a bug report to help troubleshoot this device. Apps and data may be shared.</string>
+    <!-- Message of a dialog shown to ask for user consent for sharing a bugreport that was requested remotely by the IT administrator and it's still being taken. -->
+    <string name="share_remote_bugreport_dialog_message">Your IT admin requested a bug report to help troubleshoot this device. Apps and data may be shared, and your device may temporarily slow down.</string>
+    <!-- Message of a dialog shown to inform that the remote bugreport that was requested remotely by the IT administrator is still being taken and will be shared when finished. -->
+    <string name="sharing_remote_bugreport_dialog_message">This bug report is being shared with your IT admin. Contact them for more details.</string>
+    <!-- Acceptance label of dialog shown to ask for user consent for sharing the remote bugreport. -->
+    <string name="share_remote_bugreport_action">Share</string>
+    <!-- Decline label of dialog shown to ask for user consent for sharing the remote bugreport. -->
+    <string name="decline_remote_bugreport_action">Decline</string>
+
     <!-- Title for Network connection request Dialog [CHAR LIMIT=60] -->
     <string name="network_connection_request_dialog_title">Device to use with <xliff:g id="appName" example="ThirdPartyAppName">%1$s</xliff:g></string>
     <!-- Message for Network connection timeout Dialog [CHAR LIMIT=NONE] -->
diff --git a/Settings/src/com/android/tv/settings/RemoteBugreportActivity.java b/Settings/src/com/android/tv/settings/RemoteBugreportActivity.java
new file mode 100644
index 0000000..377a420
--- /dev/null
+++ b/Settings/src/com/android/tv/settings/RemoteBugreportActivity.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tv.settings;
+
+import android.annotation.Nullable;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.admin.DevicePolicyManager;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.UserHandle;
+import android.util.Log;
+
+/**
+ * UI for the remote bugreport dialog. Shows one of 3 possible dialogs:
+ * <ul>
+ * <li>bugreport is still being taken and can be shared or declined</li>
+ * <li>bugreport has been taken and can be shared or declined</li>
+ * <li>bugreport has already been accepted to be shared, but is still being taken</li>
+ * </ul>
+ */
+public class RemoteBugreportActivity extends Activity {
+
+    private static final String TAG = "RemoteBugreportActivity";
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final int notificationType = getIntent().getIntExtra(
+                DevicePolicyManager.EXTRA_BUGREPORT_NOTIFICATION_TYPE, -1);
+
+        if (notificationType == DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED) {
+            AlertDialog dialog = new AlertDialog.Builder(this)
+                    .setMessage(R.string.sharing_remote_bugreport_dialog_message)
+                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
+                        @Override
+                        public void onDismiss(DialogInterface dialog) {
+                            finish();
+                        }
+                    })
+                    .setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            finish();
+                        }
+                    })
+                    .create();
+            dialog.show();
+        } else if (notificationType == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED
+                || notificationType
+                == DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED) {
+            AlertDialog dialog = new AlertDialog.Builder(this)
+                    .setTitle(R.string.share_remote_bugreport_dialog_title)
+                    .setMessage(notificationType
+                            == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED
+                            ? R.string.share_remote_bugreport_dialog_message
+                            : R.string.share_remote_bugreport_dialog_message_finished)
+                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
+                        @Override
+                        public void onDismiss(DialogInterface dialog) {
+                            finish();
+                        }
+                    })
+                    .setNegativeButton(R.string.decline_remote_bugreport_action,
+                            new DialogInterface.OnClickListener() {
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+                                    Intent intent = new Intent(
+                                            DevicePolicyManager.ACTION_BUGREPORT_SHARING_DECLINED);
+                                    RemoteBugreportActivity.this.sendBroadcastAsUser(intent,
+                                            UserHandle.SYSTEM, android.Manifest.permission.DUMP);
+                                    finish();
+                                }
+                            })
+                    .setPositiveButton(R.string.share_remote_bugreport_action,
+                            new DialogInterface.OnClickListener() {
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+                                    Intent intent = new Intent(
+                                            DevicePolicyManager.ACTION_BUGREPORT_SHARING_ACCEPTED);
+                                    RemoteBugreportActivity.this.sendBroadcastAsUser(intent,
+                                            UserHandle.SYSTEM, android.Manifest.permission.DUMP);
+                                    finish();
+                                }
+                            })
+                    .create();
+            dialog.show();
+        } else {
+            Log.e(TAG, "Incorrect dialog type, no dialog shown. Received: " + notificationType);
+        }
+    }
+}