1/* //device/content/providers/media/src/com/android/providers/media/MediaScannerReceiver.java 2** 3** Copyright 2007, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18package com.android.providers.media; 19 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.net.Uri; 24import android.os.Bundle; 25import android.os.Environment; 26import android.util.Log; 27 28public class MediaScannerReceiver extends BroadcastReceiver { 29 private final static String TAG = "MediaScannerReceiver"; 30 31 @Override 32 public void onReceive(Context context, Intent intent) { 33 final String action = intent.getAction(); 34 final Uri uri = intent.getData(); 35 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 36 // Scan both internal and external storage 37 scan(context, MediaProvider.INTERNAL_VOLUME); 38 scan(context, MediaProvider.EXTERNAL_VOLUME); 39 40 } else { 41 if (uri.getScheme().equals("file")) { 42 // handle intents related to external storage 43 String path = uri.getPath(); 44 String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); 45 46 Log.d(TAG, "action: " + action + " path: " + path); 47 if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) { 48 // scan whenever any volume is mounted 49 scan(context, MediaProvider.EXTERNAL_VOLUME); 50 } else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action) && 51 path != null && path.startsWith(externalStoragePath + "/")) { 52 scanFile(context, path); 53 } 54 } 55 } 56 } 57 58 private void scan(Context context, String volume) { 59 Bundle args = new Bundle(); 60 args.putString("volume", volume); 61 context.startService( 62 new Intent(context, MediaScannerService.class).putExtras(args)); 63 } 64 65 private void scanFile(Context context, String path) { 66 Bundle args = new Bundle(); 67 args.putString("filepath", path); 68 context.startService( 69 new Intent(context, MediaScannerService.class).putExtras(args)); 70 } 71} 72