MediaScannerReceiver.java revision 17ad80b32f839ccddac3911799ff732d1ca3a006
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.Bundle;
25import android.os.Environment;
26import android.util.Log;
27
28import java.io.File;
29
30
31public class MediaScannerReceiver extends BroadcastReceiver
32{
33    private final static String TAG = "MediaScannerReceiver";
34
35    @Override
36    public void onReceive(Context context, Intent intent) {
37        String action = intent.getAction();
38        Uri uri = intent.getData();
39        String externalStoragePath = Environment.getExternalStorageDirectory().getPath();
40
41        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
42            // scan internal storage
43            scan(context, MediaProvider.INTERNAL_VOLUME);
44        } else {
45            if (uri.getScheme().equals("file")) {
46                // handle intents related to external storage
47                String path = uri.getPath();
48                if (action.equals(Intent.ACTION_MEDIA_MOUNTED) &&
49                        externalStoragePath.equals(path)) {
50                    scan(context, MediaProvider.EXTERNAL_VOLUME);
51                } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
52                        path != null && path.startsWith(externalStoragePath + "/")) {
53                    scanFile(context, path);
54                }
55            }
56        }
57    }
58
59    private void scan(Context context, String volume) {
60        Bundle args = new Bundle();
61        args.putString("volume", volume);
62        context.startService(
63                new Intent(context, MediaScannerService.class).putExtras(args));
64    }
65
66    private void scanFile(Context context, String path) {
67        Bundle args = new Bundle();
68        args.putString("filepath", path);
69        context.startService(
70                new Intent(context, MediaScannerService.class).putExtras(args));
71    }
72}
73
74
75