/*
 * Copyright (C) 2015 Emanuel Moecklin
 *
 * 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.onegravity.rteditor.media.choose.processor;

import android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;

import com.onegravity.rteditor.api.RTApi;
import com.onegravity.rteditor.api.RTMediaFactory;
import com.onegravity.rteditor.api.media.RTAudio;
import com.onegravity.rteditor.api.media.RTImage;
import com.onegravity.rteditor.api.media.RTVideo;

import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public abstract class MediaProcessor implements Runnable {

    public interface MediaProcessorListener {
        public void onError(String reason);
    }

    final private MediaProcessorListener mListener;

    final private String mOriginalFile;

    protected final RTMediaFactory<RTImage, RTAudio, RTVideo> mMediaFactory;

    public MediaProcessor(String originalFile, RTMediaFactory<RTImage, RTAudio, RTVideo> mediaFactory, MediaProcessorListener listener) {
        mOriginalFile = originalFile;
        mMediaFactory = mediaFactory;
        mListener = listener;
    }

    @Override
    final public void run() {
        try {
            processMedia();
        } catch (Exception e) {
            if (mListener != null) {
                mListener.onError(e.getMessage());
            }
        }
    }

    protected String getOriginalFile() {
        return mOriginalFile;
    }

    protected abstract void processMedia() throws IOException, Exception;

    protected InputStream getInputStream() throws IOException, Exception {
        InputStream in = null;
        if (mOriginalFile.startsWith("http")) {
            // http download
            in = downloadFile(mOriginalFile);
        } else if (mOriginalFile.startsWith("content://")) {
            // ContentProvider file
            in = processContentProviderMedia(mOriginalFile);
        } else {
            // file system
            in = copyFileToDir(mOriginalFile);
        }

        return in;
    }

    protected String getMimeType() throws IOException, Exception {
        if (mOriginalFile.startsWith("content://")) {
            // ContentProvider file
            ContentResolver resolver = RTApi.getApplicationContext().getContentResolver();
            Uri uri = Uri.parse(mOriginalFile);
            return resolver.getType(uri);
        }

        String extension = FilenameUtils.getExtension(mOriginalFile);
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }

    private InputStream copyFileToDir(String sourceFile) {
        InputStream in = null;
        try {
            File fileFrom = new File(Uri.parse(sourceFile).getPath());
            in = new FileInputStream(fileFrom);
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
        }

        return in;
    }

    private InputStream downloadFile(String sourceFile) {
        HttpClient client = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(sourceFile);

        InputStream in = null;
        try {
            HttpResponse response = client.execute(getRequest);
            in = response.getEntity().getContent();
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
        }

        return in;
    }

    private InputStream processContentProviderMedia(String sourceFile) {
        ContentResolver resolver = RTApi.getApplicationContext().getContentResolver();
        Uri uri = Uri.parse(sourceFile);

        InputStream in = null;
        try {
            in = resolver.openInputStream(uri);
        } catch (IOException ioe) {
            Log.e(getClass().getSimpleName(), ioe.getMessage(), ioe);
        }

        return in;
    }

}
@@ -1090,170 +1090,64 @@
 che.
-http.HttpResponse;%0Aimport org.apache.http.client.HttpClient
+commons.io.IOUtils
 ;%0A
+%0A
 import 
-org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.impl.client.DefaultHttpClient;%0A
+java.io.BufferedInputStream;
 %0Aimp
@@ -1251,16 +1251,72 @@
 tStream;
+%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;
 %0A%0Apublic
@@ -3729,116 +3729,190 @@
     
-HttpClient client = new DefaultHttpClient();%0A        HttpGet getRequest = new HttpGet(sourceFile
+InputStream in = null;%0A        try %7B%0A            URL url = new URL(sourceFile);%0A            final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(
 );%0A
-%0A
         
 Inpu
@@ -3907,38 +3907,82 @@
         
-InputStream in = null;
+    in = new BufferedInputStream(urlConnection.getInputStream()) %7B
 %0A       
@@ -3978,38 +3978,32 @@
 ) %7B%0A        
-try %7B%0A
         
     HttpResp
@@ -3994,114 +3994,163 @@
     
-    HttpResponse response = client.execute(getRequest);%0A            in = response.getEntity().getContent()
+public void close() throws IOException %7B%0A                    super.close();%0A                    urlConnection.disconnect();%0A                %7D%0A            %7D
 ;%0A  
@@ -4237,32 +4237,70 @@
 tMessage(), e);%0A
+            IOUtils.closeQuietly(in);%0A
         %7D%0A%0A     

Use the standard Android HttpURLConnection instead of deprecated Apache classes