MediaScannerReceiver.java revision 702152725052b7b3903ed647cf53f04724886a1b
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
44            // scan external storage if it is mounted
45            String state = Environment.getExternalStorageState();
46            if (Environment.MEDIA_MOUNTED.equals(state) ||
47                    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
48               scan(context, MediaProvider.EXTERNAL_VOLUME);
49            }
50        } else {
51            if (uri.getScheme().equals("file")) {
52                // handle intents related to external storage
53                String path = uri.getPath();
54                if (action.equals(Intent.ACTION_MEDIA_MOUNTED) &&
55                        externalStoragePath.equals(path)) {
56                    scan(context, MediaProvider.EXTERNAL_VOLUME);
57                } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
58                        path != null && path.startsWith(externalStoragePath + "/")) {
59                    scanFile(context, path);
60                }
61            }
62        }
63    }
64
65    private void scan(Context context, String volume) {
66        Bundle args = new Bundle();
67        args.putString("volume", volume);
68        context.startService(
69                new Intent(context, MediaScannerService.class).putExtras(args));
70    }
71
72    private void scanFile(Context context, String path) {
73        Bundle args = new Bundle();
74        args.putString("filepath", path);
75        context.startService(
76                new Intent(context, MediaScannerService.class).putExtras(args));
77    }
78}
79
80
81