Android Open Source - android-nfc-lib Generic Task






From Project

Back to project page android-nfc-lib.

License

The source code is released under:

MIT License

If you think the Android project android-nfc-lib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * GenericTask.java/*w  w w  . ja  v  a2s. c  o  m*/
 * NfcLibrary project.
 *
 * Created by : Daneo van Overloop - 17/6/2014.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 AppFoundry. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 */

package be.appfoundry.nfclibrary.tasks;

import android.os.AsyncTask;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import be.appfoundry.nfclibrary.tasks.interfaces.AsyncOperationCallback;
import be.appfoundry.nfclibrary.tasks.interfaces.AsyncUiCallback;
import be.appfoundry.nfclibrary.utilities.interfaces.NfcWriteUtility;
import be.appfoundry.nfclibrary.utilities.sync.NfcWriteUtilityImpl;

/**
 * AsyncTask providing the implementation of a task according to the 2 interfaces in {@link be.appfoundry.nfclibrary.tasks.interfaces}
 * @author Daneo Van Overloop
 * NfcLibrary
 * Created on 18/04/14.
 */
public class GenericTask extends AsyncTask<Void, Boolean, Boolean> {


    private static final String TAG = GenericTask.class.getSimpleName();
    private final NfcWriteUtility mNfcWriteUtility;

    volatile boolean started = false;
    AsyncUiCallback mAsyncUiCallback;
    AsyncOperationCallback mAsyncOperationCallback;
    private Exception error;

    /**
     * @param asyncUiCallback
     *         callback executed on UI thread
     * @param asyncOperationCallback
     *         callback to be executed in the background
     *
     * @throws java.lang.NullPointerException
     *         if (asyncOperationCallback == null)
     */
    public GenericTask(AsyncUiCallback asyncUiCallback, @NotNull AsyncOperationCallback asyncOperationCallback) {
        if (asyncOperationCallback == null) {
            throw new NullPointerException("AsyncOperationCallback cannot be null. It can't be that you create me and expect me to do nothing, right ..?");
        }

        mAsyncUiCallback = asyncUiCallback;
        mAsyncOperationCallback = asyncOperationCallback;
        mNfcWriteUtility = new NfcWriteUtilityImpl();
    }

    /**
     * @param nfcWriteUtility
     *         to be passed to the asyncOperationCallback
     *
     * @throws java.lang.NullPointerException
     *         if (nfcWriteUtility == null)
     * @see #GenericTask(be.appfoundry.nfclibrary.tasks.interfaces.AsyncUiCallback, be.appfoundry.nfclibrary.tasks.interfaces.AsyncOperationCallback)
     */
    public GenericTask(AsyncUiCallback asyncUiCallback, @NotNull AsyncOperationCallback asyncOperationCallback, @NotNull NfcWriteUtility nfcWriteUtility) {
        if (nfcWriteUtility == null) {
            throw new NullPointerException("NfcWriteUtility cannot be null!");
        }

        mAsyncUiCallback = asyncUiCallback;
        mAsyncOperationCallback = asyncOperationCallback;
        mNfcWriteUtility = nfcWriteUtility;

    }

    /**
     * Job to be done in the background.
     * Any error is rerouted to {@link #onPostExecute(Boolean)}, and hence to the callback onError()
     * @param params
     * @return AsyncOperationCallback.performWrite() == true
     */
    @Override
    protected Boolean doInBackground(Void... params) {
        Log.d(TAG, "Writing ..");


        boolean res = false;
        try {
            started = true;
            publishProgress(started);
            if (mAsyncOperationCallback != null) {
                res = mAsyncOperationCallback.performWrite(mNfcWriteUtility);
            } else {
                error = new NullPointerException("OperationCallback is null");
            }

        } catch (Exception e) {
            Log.w(TAG, e);
            mAsyncUiCallback.onError(e);
            error = e;
        }

        // Remove tag from intent in order to prevent writing to a not present tag
        return res;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    /**
     * Fire the callbackWithReturnValue.
     * If the error != null then onError is executed as well.
     * @param result
     */
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (mAsyncOperationCallback != null && mAsyncUiCallback != null) {
            if (error != null) {
                mAsyncUiCallback.onError(error);
            }
            mAsyncUiCallback.callbackWithReturnValue(result);
        }
    }

    @Override
    protected void onProgressUpdate(Boolean... values) {
        super.onProgressUpdate(values);
        if (mAsyncUiCallback != null) {
            mAsyncUiCallback.onProgressUpdate(values);
        }
    }


}




Java Source Code List

be.appfoundry.nfclibrary.activities.NfcActivity.java
be.appfoundry.nfclibrary.constants.NfcPayloadHeader.java
be.appfoundry.nfclibrary.constants.NfcType.java
be.appfoundry.nfclibrary.exceptions.InsufficientCapacityException.java
be.appfoundry.nfclibrary.exceptions.ReadOnlyTagException.java
be.appfoundry.nfclibrary.exceptions.TagNotPresentException.java
be.appfoundry.nfclibrary.exceptions.TagNotWritableException.java
be.appfoundry.nfclibrary.implementation.NfcActivity.java
be.appfoundry.nfclibrary.implementation.TestEmptyFieldsNfcActivity.java
be.appfoundry.nfclibrary.implementation.TestFilledFieldsNfcActivity.java
be.appfoundry.nfclibrary.tasks.GenericTask.java
be.appfoundry.nfclibrary.tasks.interfaces.AsyncOperationCallback.java
be.appfoundry.nfclibrary.tasks.interfaces.AsyncUiCallback.java
be.appfoundry.nfclibrary.utilities.TestUtilities.java
be.appfoundry.nfclibrary.utilities.async.AbstractFailsTestsAsync.java
be.appfoundry.nfclibrary.utilities.async.AbstractNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.GenericTaskTestsAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteBluetoothNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteCallbackNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteEmailNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteGeoLocationNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WritePhoneNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteSmsNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteUriNfcAsync.java
be.appfoundry.nfclibrary.utilities.interfaces.AsyncNfcWriteOperation.java
be.appfoundry.nfclibrary.utilities.interfaces.NdefWrite.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcMessageUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcReadUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcWriteUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.WriteUtility.java
be.appfoundry.nfclibrary.utilities.sync.NdefWriteImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcMessageUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcReadUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcWriteUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.WriteEmailFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteEmailSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteGeolocationFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteGeolocationSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WritePhoneFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WritePhoneSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUriFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUriSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUtilityImpl.java