Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.facade.media;
     18 
     19 import android.app.Service;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.media.MediaScanner;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.os.Environment;
     28 
     29 import com.googlecode.android_scripting.Log;
     30 import com.googlecode.android_scripting.facade.EventFacade;
     31 import com.googlecode.android_scripting.facade.FacadeManager;
     32 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     33 import com.googlecode.android_scripting.rpc.Rpc;
     34 import com.googlecode.android_scripting.rpc.RpcParameter;
     35 
     36 /**
     37  * Expose functionalities of MediaScanner related APIs.
     38  */
     39 public class MediaScannerFacade extends RpcReceiver {
     40 
     41     private final Service mService;
     42     private final MediaScanner mScanService;
     43     private final EventFacade mEventFacade;
     44     private final MediaScannerReceiver mReceiver;
     45 
     46     public MediaScannerFacade(FacadeManager manager) {
     47         super(manager);
     48         mService = manager.getService();
     49         mScanService = new MediaScanner(mService, "external");
     50         mEventFacade = manager.getReceiver(EventFacade.class);
     51         mReceiver = new MediaScannerReceiver();
     52     }
     53 
     54     public class MediaScannerReceiver extends BroadcastReceiver {
     55 
     56         @Override
     57         public void onReceive(Context context, Intent intent) {
     58             String action = intent.getAction();
     59             if(action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
     60                 Log.d("Scan finished, posting event.");
     61                 mEventFacade.postEvent("MediaScanFinished", new Bundle());
     62                 mService.unregisterReceiver(mReceiver);
     63             }
     64         }
     65     }
     66 
     67     @Rpc(description = "Scan external storage for media files.")
     68     public void mediaScanForFiles() {
     69         mService.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
     70                                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
     71         mService.registerReceiver(mReceiver,
     72                                   new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED));
     73     }
     74 
     75     @Rpc(description = "Scan for a media file.")
     76     public void mediaScanForOneFile(@RpcParameter(name = "path") String path) {
     77         mService.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(path)));
     78     }
     79 
     80     @Override
     81     public void shutdown() {
     82     }
     83 }
     84