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