All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.http.DownloadFileAsyncTask Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
/*
 * Copyright 2013 serso aka se.solovyev
 *
 * 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.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Contact details
 *
 * Email: [email protected]
 * Site:  http://se.solovyev.org
 */

package org.solovyev.android.http;

import android.content.Context;
import org.solovyev.android.async.CommonAsyncTask;
import org.solovyev.common.Converter;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * User: serso
 * Date: 5/29/12
 * Time: 9:50 PM
 */
public class DownloadFileAsyncTask extends CommonAsyncTask> {

	@Nullable
	private OnPostExecute> onPostExecute;

	public DownloadFileAsyncTask(@Nonnull Context context) {
		super(context);
	}

	public DownloadFileAsyncTask(@Nonnull Context context, @Nonnull OnPostExecute> onPostExecute) {
		super(context);
		this.onPostExecute = onPostExecute;
	}

	@Nonnull
	@Override
	protected List doWork(@Nonnull List params) {
		final List result = new ArrayList();
		for (Input param : params) {
			final DownloadFileHttpTransaction downloadFileHttpTransaction = new DownloadFileHttpTransaction(param.getUri(), param.getMethod(), param.getFileConverter());
			try {
				result.add(HttpTransactions.execute(downloadFileHttpTransaction));
			} catch (IOException e) {
				throw new HttpRuntimeIoException(e);
			}
		}

		return result;
	}

	@Override
	protected void onSuccessPostExecute(@Nullable List result) {
		if (onPostExecute != null) {
			assert result != null;
			onPostExecute.onPostExecute(result);
		}
	}

	@Override
	protected void onFailurePostExecute(@Nonnull Exception e) {
		if (e instanceof HttpRuntimeIoException) {
			// no internet connection => ok
		} else {
			defaultOnFailurePostExecute(e);
		}
	}

	public static interface OnPostExecute {
		void onPostExecute(@Nonnull R result);
	}

	public static class Input {

		@Nonnull
		private String uri;

		@Nonnull
		private HttpMethod method;

		@Nonnull
		private Converter fileConverter;

		public Input(@Nonnull String uri, @Nonnull HttpMethod method, @Nonnull Converter fileConverter) {
			this.uri = uri;
			this.method = method;
			this.fileConverter = fileConverter;
		}


		@Nonnull
		public String getUri() {
			return uri;
		}

		@Nonnull
		public HttpMethod getMethod() {
			return method;
		}

		@Nonnull
		public Converter getFileConverter() {
			return (Converter) fileConverter;
		}
	}
}