blob: 5133162a1ec9aa6a360aa1ff7002125951d552e3 [file] [log] [blame]
Jeff Sharkey02ffba92013-03-08 16:13:15 -08001/*
2 * Copyright (C) 2013 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
Felipe Lemeb9238b32015-11-24 17:31:47 -080019import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
Felipe Leme69c02922015-11-24 17:48:05 -080020import static com.android.shell.BugreportProgressService.EXTRA_ORIGINAL_INTENT;
21import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED;
Felipe Lemeb9238b32015-11-24 17:31:47 -080022import static com.android.shell.BugreportProgressService.getFileExtra;
Jeff Sharkey02ffba92013-03-08 16:13:15 -080023
Felipe Lemeb9238b32015-11-24 17:31:47 -080024import java.io.File;
25
Jeff Sharkey02ffba92013-03-08 16:13:15 -080026import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
Jeff Sharkey02ffba92013-03-08 16:13:15 -080029import android.os.AsyncTask;
Jeff Sharkeyd9526902013-03-14 14:11:57 -070030import android.os.FileUtils;
Jeff Sharkeyd9526902013-03-14 14:11:57 -070031import android.text.format.DateUtils;
Jeff Sharkey02ffba92013-03-08 16:13:15 -080032
33/**
34 * Receiver that handles finished bugreports, usually by attaching them to an
Felipe Lemeb9238b32015-11-24 17:31:47 -080035 * {@link Intent#ACTION_SEND_MULTIPLE}.
Jeff Sharkey02ffba92013-03-08 16:13:15 -080036 */
37public class BugreportReceiver extends BroadcastReceiver {
Jeff Sharkey02ffba92013-03-08 16:13:15 -080038
39 /**
Jeff Sharkeyd9526902013-03-14 14:11:57 -070040 * Always keep the newest 8 bugreport files; 4 reports and 4 screenshots are
41 * roughly 17MB of disk space.
Jeff Sharkey02ffba92013-03-08 16:13:15 -080042 */
Jeff Sharkeyd9526902013-03-14 14:11:57 -070043 private static final int MIN_KEEP_COUNT = 8;
44
45 /**
46 * Always keep bugreports taken in the last week.
47 */
48 private static final long MIN_KEEP_AGE = DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey02ffba92013-03-08 16:13:15 -080049
50 @Override
51 public void onReceive(Context context, Intent intent) {
Jeff Sharkey02ffba92013-03-08 16:13:15 -080052 // Clean up older bugreports in background
Felipe Lemeb9238b32015-11-24 17:31:47 -080053 cleanupOldFiles(intent);
54
Felipe Leme69c02922015-11-24 17:48:05 -080055 // Delegate intent handling to service.
Felipe Lemeb9238b32015-11-24 17:31:47 -080056 Intent serviceIntent = new Intent(context, BugreportProgressService.class);
Felipe Leme69c02922015-11-24 17:48:05 -080057 serviceIntent.putExtra(EXTRA_ORIGINAL_INTENT, intent);
Felipe Lemeb9238b32015-11-24 17:31:47 -080058 context.startService(serviceIntent);
59 }
60
61 private void cleanupOldFiles(Intent intent) {
Felipe Leme69c02922015-11-24 17:48:05 -080062 if (!INTENT_BUGREPORT_FINISHED.equals(intent.getAction())) {
63 return;
64 }
Felipe Lemeb9238b32015-11-24 17:31:47 -080065 final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
Jeff Sharkey02ffba92013-03-08 16:13:15 -080066 final PendingResult result = goAsync();
67 new AsyncTask<Void, Void, Void>() {
68 @Override
69 protected Void doInBackground(Void... params) {
Jeff Sharkeyd9526902013-03-14 14:11:57 -070070 FileUtils.deleteOlderFiles(
71 bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE);
Jeff Sharkey02ffba92013-03-08 16:13:15 -080072 result.finish();
73 return null;
74 }
75 }.execute();
76 }
Jeff Sharkey02ffba92013-03-08 16:13:15 -080077}