RemoteBugreportReceiver.java revision a57f0e6b14754aa39e8454d82b9fa08e3f2b53a5
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
17package com.android.shell;
18
19import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
20import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_FINISHED;
21import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_DISPATCH;
22import static com.android.shell.BugreportProgressService.getFileExtra;
23import static com.android.shell.BugreportProgressService.getUri;
24import static com.android.shell.BugreportReceiver.cleanupOldFiles;
25
26import java.io.File;
27
28import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
31import android.net.Uri;
32import android.os.UserHandle;
33import 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 */
41public class RemoteBugreportReceiver extends BroadcastReceiver {
42
43    private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
44    private static final String EXTRA_REMOTE_BUGREPORT_HASH =
45            "android.intent.extra.REMOTE_BUGREPORT_HASH";
46
47    /** Always keep just the last remote bugreport's files around. */
48    private static final int REMOTE_BUGREPORT_FILES_AMOUNT = 3;
49
50    /** Always keep remote bugreport files created in the last day. */
51    private static final long MIN_KEEP_AGE = DateUtils.DAY_IN_MILLIS;
52
53    @Override
54    public void onReceive(Context context, Intent intent) {
55        cleanupOldFiles(this, intent, INTENT_REMOTE_BUGREPORT_FINISHED,
56                REMOTE_BUGREPORT_FILES_AMOUNT, MIN_KEEP_AGE);
57
58        final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
59        final Uri bugreportUri = getUri(context, bugreportFile);
60        final String bugreportHash = intent.getStringExtra(EXTRA_REMOTE_BUGREPORT_HASH);
61
62        final Intent newIntent = new Intent(INTENT_REMOTE_BUGREPORT_DISPATCH);
63        newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE);
64        newIntent.putExtra(EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash);
65        context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM,
66                android.Manifest.permission.DUMP);
67    }
68}
69