1/*
2 * Copyright (C) 2016 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.googlecode.android_scripting.facade.media;
18
19import android.app.Service;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.media.MediaScanner;
25import android.net.Uri;
26import android.os.Bundle;
27import android.os.Environment;
28
29import com.googlecode.android_scripting.Log;
30import com.googlecode.android_scripting.facade.EventFacade;
31import com.googlecode.android_scripting.facade.FacadeManager;
32import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
33import com.googlecode.android_scripting.rpc.Rpc;
34import com.googlecode.android_scripting.rpc.RpcParameter;
35
36/**
37 * Expose functionalities of MediaScanner related APIs.
38 */
39public 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