Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 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 android.media.cts;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Environment;
     23 
     24 import java.io.File;
     25 import java.util.concurrent.CountDownLatch;
     26 import java.util.concurrent.TimeUnit;
     27 
     28 class ScannerNotificationReceiver extends BroadcastReceiver {
     29 
     30     private static final int TIMEOUT_MS = 4 * 60 * 1000;
     31 
     32     private final String mAction;
     33     private CountDownLatch mLatch = new CountDownLatch(1);
     34 
     35     ScannerNotificationReceiver(String action) {
     36         mAction = action;
     37     }
     38 
     39     @Override
     40     public void onReceive(Context context, Intent intent) {
     41         if (intent.getAction().equals(mAction)) {
     42             mLatch.countDown();
     43         }
     44     }
     45 
     46     public void waitForBroadcast() throws InterruptedException {
     47         if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     48             int numFiles = countFiles(Environment.getExternalStorageDirectory());
     49             MediaScannerTest.fail("Failed to receive broadcast in " + TIMEOUT_MS + "ms for "
     50                     + mAction + " while trying to scan " + numFiles + " files!");
     51         }
     52     }
     53 
     54     void reset() {
     55         mLatch = new CountDownLatch(1);
     56     }
     57 
     58     private int countFiles(File dir) {
     59         int count = 0;
     60         File[] files = dir.listFiles();
     61         if (files != null) {
     62             for (File file : files) {
     63                 if (file.isDirectory()) {
     64                     count += countFiles(file);
     65                 } else {
     66                     count++;
     67                 }
     68             }
     69         }
     70         return count;
     71     }
     72 }
     73