1/* 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 19import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT; 20import static com.android.shell.BugreportProgressService.EXTRA_ORIGINAL_INTENT; 21import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED; 22import static com.android.shell.BugreportProgressService.getFileExtra; 23import static com.android.shell.BugreportProgressService.dumpIntent; 24 25import java.io.File; 26 27import android.content.BroadcastReceiver; 28import android.content.Context; 29import android.content.Intent; 30import android.os.AsyncTask; 31import android.os.FileUtils; 32import android.text.format.DateUtils; 33import android.util.Log; 34 35/** 36 * Receiver that handles finished bugreports, usually by attaching them to an 37 * {@link Intent#ACTION_SEND_MULTIPLE}. 38 */ 39public class BugreportReceiver extends BroadcastReceiver { 40 private static final String TAG = "BugreportReceiver"; 41 42 /** 43 * Always keep the newest 8 bugreport files; 4 reports and 4 screenshots are 44 * roughly 17MB of disk space. 45 */ 46 private static final int MIN_KEEP_COUNT = 8; 47 48 /** 49 * Always keep bugreports taken in the last week. 50 */ 51 private static final long MIN_KEEP_AGE = DateUtils.WEEK_IN_MILLIS; 52 53 @Override 54 public void onReceive(Context context, Intent intent) { 55 Log.d(TAG, "onReceive(): " + dumpIntent(intent)); 56 // Clean up older bugreports in background 57 cleanupOldFiles(this, intent, INTENT_BUGREPORT_FINISHED, MIN_KEEP_COUNT, MIN_KEEP_AGE); 58 59 // Delegate intent handling to service. 60 Intent serviceIntent = new Intent(context, BugreportProgressService.class); 61 serviceIntent.putExtra(EXTRA_ORIGINAL_INTENT, intent); 62 context.startService(serviceIntent); 63 } 64 65 static void cleanupOldFiles(BroadcastReceiver br, Intent intent, String expectedAction, 66 final int minCount, final long minAge) { 67 if (!expectedAction.equals(intent.getAction())) { 68 return; 69 } 70 final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT); 71 if (bugreportFile == null || !bugreportFile.exists()) { 72 Log.e(TAG, "Not deleting old files because file " + bugreportFile + " doesn't exist"); 73 return; 74 } 75 final PendingResult result = br.goAsync(); 76 new AsyncTask<Void, Void, Void>() { 77 @Override 78 protected Void doInBackground(Void... params) { 79 try { 80 FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), minCount, minAge); 81 } catch (RuntimeException e) { 82 Log.e(TAG, "RuntimeException deleting old files", e); 83 } 84 result.finish(); 85 return null; 86 } 87 }.execute(); 88 } 89} 90