Michal Karpinski | 226940e | 2015-12-15 18:14:26 +0000 | [diff] [blame] | 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.INTENT_REMOTE_BUGREPORT_DISPATCH; |
| 22 | import static com.android.shell.BugreportProgressService.getFileExtra; |
| 23 | import static com.android.shell.BugreportProgressService.getUri; |
| 24 | import static com.android.shell.BugreportReceiver.cleanupOldFiles; |
| 25 | |
| 26 | import java.io.File; |
| 27 | |
| 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 | |
| 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 | */ |
| 40 | public 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 | } |