com.utils.note.rteditor.media.choose.processor.MediaProcessor.java Source code

Java tutorial

Introduction

Here is the source code for com.utils.note.rteditor.media.choose.processor.MediaProcessor.java

Source

/*
 * 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.utils.note.rteditor.media.choose.processor;

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

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 android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;

import com.utils.note.rteditor.api.RTApi;
import com.utils.note.rteditor.api.RTMediaFactory;
import com.utils.note.rteditor.api.media.RTAudio;
import com.utils.note.rteditor.api.media.RTImage;
import com.utils.note.rteditor.api.media.RTVideo;
import com.utils.note.rteditor.utils.RTFileNameUtils;

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 = RTFileNameUtils.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;
    }

}