summaryrefslogtreecommitdiffstats
path: root/sl4a/Common/src/com/googlecode/android_scripting/facade/media/MediaScannerFacade.java
blob: 636cecd113b86d644ec69b0762b3ffcf3976008c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.googlecode.android_scripting.facade.media;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaScanner;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import com.googlecode.android_scripting.Log;
import com.googlecode.android_scripting.facade.EventFacade;
import com.googlecode.android_scripting.facade.FacadeManager;
import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
import com.googlecode.android_scripting.rpc.Rpc;
import com.googlecode.android_scripting.rpc.RpcParameter;

/**
 * Expose functionalities of MediaScanner related APIs.
 */
public class MediaScannerFacade extends RpcReceiver {

    private final Service mService;
    private final MediaScanner mScanService;
    private final EventFacade mEventFacade;
    private final MediaScannerReceiver mReceiver;

    public MediaScannerFacade(FacadeManager manager) {
        super(manager);
        mService = manager.getService();
        mScanService = new MediaScanner(mService);
        mEventFacade = manager.getReceiver(EventFacade.class);
        mReceiver = new MediaScannerReceiver();
    }

    public class MediaScannerReceiver extends BroadcastReceiver {  

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                Log.d("Scan finished, posting event.");
                mEventFacade.postEvent("MediaScanFinished", new Bundle());
                mService.unregisterReceiver(mReceiver);
            } 
        }
    }

    @Rpc(description = "Scan external storage for media files.")
    public void mediaScanForFiles() {
        mService.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                               Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        mService.registerReceiver(mReceiver,
                                  new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED));
    }

    @Rpc(description = "Scan for a media file.")
    public void mediaScanForOneFile(@RpcParameter(name = "path") String path) {
        mService.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(path)));
    }

    @Override
    public void shutdown() {
    }
}