Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.shell; |
| 18 | |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 19 | import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame^] | 20 | import static com.android.shell.BugreportProgressService.EXTRA_ORIGINAL_INTENT; |
| 21 | import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 22 | import static com.android.shell.BugreportProgressService.getFileExtra; |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 23 | |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 24 | import java.io.File; |
| 25 | |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 29 | import android.os.AsyncTask; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 30 | import android.os.FileUtils; |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 31 | import android.text.format.DateUtils; |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Receiver that handles finished bugreports, usually by attaching them to an |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 35 | * {@link Intent#ACTION_SEND_MULTIPLE}. |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class BugreportReceiver extends BroadcastReceiver { |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 40 | * Always keep the newest 8 bugreport files; 4 reports and 4 screenshots are |
| 41 | * roughly 17MB of disk space. |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 42 | */ |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 43 | 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 Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 49 | |
| 50 | @Override |
| 51 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 52 | // Clean up older bugreports in background |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 53 | cleanupOldFiles(intent); |
| 54 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame^] | 55 | // Delegate intent handling to service. |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 56 | Intent serviceIntent = new Intent(context, BugreportProgressService.class); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame^] | 57 | serviceIntent.putExtra(EXTRA_ORIGINAL_INTENT, intent); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 58 | context.startService(serviceIntent); |
| 59 | } |
| 60 | |
| 61 | private void cleanupOldFiles(Intent intent) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame^] | 62 | if (!INTENT_BUGREPORT_FINISHED.equals(intent.getAction())) { |
| 63 | return; |
| 64 | } |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 65 | final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT); |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 66 | final PendingResult result = goAsync(); |
| 67 | new AsyncTask<Void, Void, Void>() { |
| 68 | @Override |
| 69 | protected Void doInBackground(Void... params) { |
Jeff Sharkey | d952690 | 2013-03-14 14:11:57 -0700 | [diff] [blame] | 70 | FileUtils.deleteOlderFiles( |
| 71 | bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE); |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 72 | result.finish(); |
| 73 | return null; |
| 74 | } |
| 75 | }.execute(); |
| 76 | } |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 77 | } |