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