1 /*
2  * Copyright (C) 2015 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.shell;
18 
19 import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
20 import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_FINISHED;
21 import static com.android.shell.BugreportProgressService.getFileExtra;
22 import static com.android.shell.BugreportProgressService.getUri;
23 import static com.android.shell.BugreportReceiver.cleanupOldFiles;
24 
25 import java.io.File;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.net.Uri;
32 import android.os.UserHandle;
33 import android.text.format.DateUtils;
34 
35 /**
36  * Receiver that handles finished remote bugreports, by re-sending
37  * the intent with appended bugreport zip file URI.
38  *
39  * <p> Remote bugreport never contains a screenshot.
40  */
41 public class RemoteBugreportReceiver extends BroadcastReceiver {
42 
43     private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
44 
45     /** Always keep just the last remote bugreport's files around. */
46     private static final int REMOTE_BUGREPORT_FILES_AMOUNT = 3;
47 
48     /** Always keep remote bugreport files created in the last day. */
49     private static final long MIN_KEEP_AGE = DateUtils.DAY_IN_MILLIS;
50 
51     @Override
onReceive(Context context, Intent intent)52     public void onReceive(Context context, Intent intent) {
53         cleanupOldFiles(this, intent, INTENT_REMOTE_BUGREPORT_FINISHED,
54                 REMOTE_BUGREPORT_FILES_AMOUNT, MIN_KEEP_AGE);
55 
56         final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
57         final Uri bugreportUri = getUri(context, bugreportFile);
58         final String bugreportHash = intent.getStringExtra(
59                 DevicePolicyManager.EXTRA_REMOTE_BUGREPORT_HASH);
60 
61         final Intent newIntent = new Intent(DevicePolicyManager.ACTION_REMOTE_BUGREPORT_DISPATCH);
62         newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE);
63         newIntent.putExtra(DevicePolicyManager.EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash);
64         context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM,
65                 android.Manifest.permission.DUMP);
66     }
67 }
68