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 28import java.io.File; 29import java.io.IOException; 30 31public class MediaScannerReceiver extends BroadcastReceiver { 32 private final static String TAG = "MediaScannerReceiver"; 33 34 @Override 35 public void onReceive(Context context, Intent intent) { 36 final String action = intent.getAction(); 37 final Uri uri = intent.getData(); 38 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 39 // Scan both internal and external storage 40 scan(context, MediaProvider.INTERNAL_VOLUME); 41 scan(context, MediaProvider.EXTERNAL_VOLUME); 42 43 } else { 44 if (uri.getScheme().equals("file")) { 45 // handle intents related to external storage 46 String path = uri.getPath(); 47 String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); 48 String legacyPath = Environment.getLegacyExternalStorageDirectory().getPath(); 49 50 try { 51 path = new File(path).getCanonicalPath(); 52 } catch (IOException e) { 53 Log.e(TAG, "couldn't canonicalize " + path); 54 return; 55 } 56 if (path.startsWith(legacyPath)) { 57 path = externalStoragePath + path.substring(legacyPath.length()); 58 } 59 60 Log.d(TAG, "action: " + action + " path: " + path); 61 if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) { 62 // scan whenever any volume is mounted 63 scan(context, MediaProvider.EXTERNAL_VOLUME); 64 } else if (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action) && 65 path != null && path.startsWith(externalStoragePath + "/")) { 66 scanFile(context, path); 67 } 68 } 69 } 70 } 71 72 private void scan(Context context, String volume) { 73 Bundle args = new Bundle(); 74 args.putString("volume", volume); 75 context.startService( 76 new Intent(context, MediaScannerService.class).putExtras(args)); 77 } 78 79 private void scanFile(Context context, String path) { 80 Bundle args = new Bundle(); 81 args.putString("filepath", path); 82 context.startService( 83 new Intent(context, MediaScannerService.class).putExtras(args)); 84 } 85} 86