RemoteBugreportReceiver.java revision 226940ed8550c02875a987f7e46699e6003ec1c0
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;
33
34/**
35 * Receiver that handles finished remote bugreports, by re-sending
36 * the intent with appended bugreport zip file URI.
37 *
38 * <p> Remote bugreport never contains a screenshot.
39 */
40public class RemoteBugreportReceiver extends BroadcastReceiver {
41
42    private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
43    private static final String EXTRA_REMOTE_BUGREPORT_HASH =
44            "android.intent.extra.REMOTE_BUGREPORT_HASH";
45
46    /** Always keep just the last remote bugreport zip file */
47    private static final int MIN_KEEP_COUNT = 1;
48
49    @Override
50    public void onReceive(Context context, Intent intent) {
51        cleanupOldFiles(this, intent, INTENT_REMOTE_BUGREPORT_FINISHED, MIN_KEEP_COUNT, 0);
52
53        final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
54        final Uri bugreportUri = getUri(context, bugreportFile);
55        final String bugreportHash = intent.getStringExtra(EXTRA_REMOTE_BUGREPORT_HASH);
56
57        final Intent newIntent = new Intent(INTENT_REMOTE_BUGREPORT_DISPATCH);
58        newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE);
59        newIntent.putExtra(EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash);
60        context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM,
61                android.Manifest.permission.DUMP);
62    }
63}
64