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