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

org.droidparts.concurrent.task.AsyncTask Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/**
 * Copyright 2013 Alex Yanchenko
 * 
 * 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 org.droidparts.concurrent.task;

import org.droidparts.Injector;
import org.droidparts.util.L;

import android.content.Context;
import android.util.Pair;

public abstract class AsyncTask extends
		android.os.AsyncTask> {

	private final Context ctx;
	private final AsyncTaskResultListener resultListener;

	public AsyncTask(Context ctx) {
		this(ctx, null);
	}

	public AsyncTask(Context ctx, AsyncTaskResultListener resultListener) {
		Injector.inject(ctx, this);
		this.ctx = ctx.getApplicationContext();
		this.resultListener = resultListener;
	}

	public Context getContext() {
		return ctx;
	}

	@Override
	protected final Pair doInBackground(Params... params) {
		Result res = null;
		Exception ex = null;
		try {
			long start = System.currentTimeMillis();
			res = onExecute(params);
			L.i("Executed %s in %d ms.", getClass().getSimpleName(),
					(System.currentTimeMillis() - start));
		} catch (Exception e) {
			L.w(e);
			ex = e;
		}
		return new Pair(res, ex);
	}

	@Override
	protected final void onPostExecute(Pair result) {
		// try-catch to avoid lifecycle-related crashes
		try {
			if (result.first != null) {
				if (resultListener != null) {
					resultListener.onAsyncTaskSuccess(result.first);
				}
				onPostExecuteSuccess(result.first);
			} else {
				if (resultListener != null) {
					resultListener.onAsyncTaskFailure(result.second);
				}
				onPostExecuteFailure(result.second);
			}
		} catch (Throwable t) {
			L.w(t.getMessage());
			L.d(t);
		}
	}

	protected abstract Result onExecute(Params... params) throws Exception;

	protected void onPostExecuteSuccess(Result result) {
	}

	protected void onPostExecuteFailure(Exception exception) {
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy