1 /* 2 * Copyright (C) 2011 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.Intent; 20 import android.content.IntentFilter; 21 import android.net.Uri; 22 import android.os.Environment; 23 import android.test.AndroidTestCase; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.util.concurrent.CountDownLatch; 29 import java.util.concurrent.TimeUnit; 30 31 public class MediaScannerNotificationTest extends AndroidTestCase { 32 33 public void testMediaScannerNotification() throws Exception { 34 ScannerNotificationReceiver startedReceiver = new ScannerNotificationReceiver( 35 Intent.ACTION_MEDIA_SCANNER_STARTED); 36 ScannerNotificationReceiver finishedReceiver = new ScannerNotificationReceiver( 37 Intent.ACTION_MEDIA_SCANNER_FINISHED); 38 39 IntentFilter startedIntentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED); 40 startedIntentFilter.addDataScheme("file"); 41 IntentFilter finshedIntentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED); 42 finshedIntentFilter.addDataScheme("file"); 43 44 mContext.registerReceiver(startedReceiver, startedIntentFilter); 45 mContext.registerReceiver(finishedReceiver, finshedIntentFilter); 46 47 String [] temps = new String[] { "avi", "gif", "jpg", "dat", "mp3", "mp4", "txt" }; 48 String tmpPath = createTempFiles(temps); 49 50 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" 51 + Environment.getExternalStorageDirectory()))); 52 53 startedReceiver.waitForBroadcast(); 54 finishedReceiver.waitForBroadcast(); 55 56 checkTempFiles(tmpPath, temps); 57 58 // add .nomedia file and scan again 59 File noMedia = new File(tmpPath, ".nomedia"); 60 try { 61 noMedia.createNewFile(); 62 } catch (IOException e) { 63 fail("couldn't create .nomedia file"); 64 } 65 startedReceiver.reset(); 66 finishedReceiver.reset(); 67 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" 68 + Environment.getExternalStorageDirectory()))); 69 startedReceiver.waitForBroadcast(); 70 finishedReceiver.waitForBroadcast(); 71 72 checkTempFiles(tmpPath, temps); 73 assertTrue(noMedia.delete()); 74 deleteTempFiles(tmpPath, temps); 75 76 // scan one more time just to clean everything up nicely 77 startedReceiver.reset(); 78 finishedReceiver.reset(); 79 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" 80 + Environment.getExternalStorageDirectory()))); 81 startedReceiver.waitForBroadcast(); 82 finishedReceiver.waitForBroadcast(); 83 84 } 85 86 String createTempFiles(String [] extensions) { 87 String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 88 File tmpDir = new File(externalPath, "" + System.nanoTime()); 89 String tmpPath = tmpDir.getAbsolutePath(); 90 assertFalse(tmpPath + " already exists", tmpDir.exists()); 91 assertTrue("failed to create " + tmpDir, tmpDir.mkdirs()); 92 93 for (int i = 0; i < extensions.length; i++) { 94 File foo = new File(tmpPath, "foobar." + extensions[i]); 95 try { 96 // create a non-empty file 97 foo.createNewFile(); 98 FileOutputStream out = new FileOutputStream(foo); 99 out.write(0x12); 100 out.flush(); 101 out.close(); 102 assertTrue(foo.length() != 0); 103 } catch (IOException e) { 104 fail("Error creating " + foo.getAbsolutePath() + ": " + e); 105 } 106 } 107 return tmpPath; 108 } 109 110 void checkTempFiles(String tmpPath, String [] extensions) { 111 for (int i = 0; i < extensions.length; i++) { 112 File foo = new File(tmpPath, "foobar." + extensions[i]); 113 assertTrue(foo.getAbsolutePath() + " no longer exists or was truncated", 114 foo.length() != 0); 115 } 116 } 117 118 void deleteTempFiles(String tmpPath, String [] extensions) { 119 for (int i = 0; i < extensions.length; i++) { 120 assertTrue(new File(tmpPath, "foobar." + extensions[i]).delete()); 121 } 122 assertTrue(new File(tmpPath).delete()); 123 } 124 } 125