/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.teleca.jamendo.util.download;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.service.DownloadService;

import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.util.Log;

/**
 * Forces mp3 file scan on a downloaded file and adds it to the Android MusicPlayer's
 * library
 * 
 * @author Lukasz Wisniewski
 */
public class MediaScannerNotifier implements MediaScannerConnectionClient{
	
    private MediaScannerConnection mConnection;
    private DownloadJob mDownloadJob;
    private DownloadService mService;

	public MediaScannerNotifier(DownloadService service, DownloadJob job) {
		mDownloadJob = job;
		mService = service;
		mConnection = new MediaScannerConnection(mService, this);
		mConnection.connect();
	} 

	@Override
	public void onMediaScannerConnected() {
		//String path = mDownloadJob.getDestination();
		String path = DownloadHelper.getAbsolutePath(mDownloadJob.getPlaylistEntry(), mDownloadJob.getDestination());
		String fileName = DownloadHelper.getFileName(mDownloadJob.getPlaylistEntry(), mDownloadJob.getFormat());
		Log.i(JamendoApplication.TAG, "Adding to media library -> "+fileName);
		mConnection.scanFile(path+"/"+fileName, null); 
	}

	@Override
	public void onScanCompleted(String text, Uri uri) {
		Log.i(JamendoApplication.TAG, "Added to media library -> "+uri.toString());
		mConnection.disconnect();
		
		// stop service if there is no downloads left
		if(mService.getQueuedDownloads().size() == 0){
			mService.stopSelf();
		}
	}

}

@@ -1276,16 +1276,68 @@
 Service;
+%0A    static private int mScannedFilesInProgress = 0;
 %0A%0A%09publi
@@ -1929,17 +1929,56 @@
 leName);
-%0A
+%09%09%0A%09%09if(mConnection.isConnected())%0A%09%09%7B%0A%09
 %09%09mConne
@@ -2017,17 +2017,57 @@
 , null);
- 
+%0A%09%09%09mScannedFilesInProgress++;%09%09%09%0A%09%09%7D%0A%09%09%0A
 %0A%09%7D%0A%0A%09@O
@@ -2205,16 +2205,48 @@
 ing());%0A
+%09%09%0A%09%09mScannedFilesInProgress--;%0A
 %09%09mConne
@@ -2366,10 +2366,45 @@
 == 0
-)%7B
+ && mScannedFilesInProgress == 0)%7B%09%09%09
 %0A%09%09%09

Fix for Bug 30 - IllegalStateException in MediaScannerConnection.scanFile()