MediaScannerReceiver.java revision b9a92016487672890435f2c1ced88b9b1da535b7
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.Context; 21import android.content.Intent; 22import android.content.BroadcastReceiver; 23import android.net.Uri; 24import android.os.Environment; 25import android.os.Bundle; 26 27import java.io.File; 28 29 30public class MediaScannerReceiver extends BroadcastReceiver 31{ 32 private final static String TAG = "MediaScannerReceiver"; 33 34 @Override 35 public void onReceive(Context context, Intent intent) { 36 String action = intent.getAction(); 37 Uri uri = intent.getData(); 38 String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); 39 40 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 41 // scan internal storage 42 scan(context, MediaProvider.INTERNAL_VOLUME); 43 } else { 44 if (uri.getScheme().equals("file")) { 45 // handle intents related to external storage 46 String path = uri.getPath(); 47 if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && 48 externalStoragePath.equals(path)) { 49 scan(context, MediaProvider.EXTERNAL_VOLUME); 50 } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) && 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 73 74