com.wit.android.support.database.LoadableAssistant.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.android.support.database.LoadableAssistant.java

Source

/*
 * =================================================================================================
 *                    Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License 
 * you may obtain at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written within this file but as it 
 * is described in the License, the software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.support.database;

import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;

import com.wit.android.support.database.annotation.LoaderId;
import com.wit.android.support.database.util.DatabaseAnnotations;

/**
 * <h3>Class Overview</h3>
 * todo: description
 * <h3>Accepted annotations</h3>
 * <ul>
 * <li>
 * {@link com.wit.android.support.database.annotation.LoaderId @LoaderId} <b>[class]</b>
 * <p>
 * If this annotation is presented, an id provided by this annotation will be set as loader id for
 * an instance of subclass of this LoadableAssistant.
 * </li>
 * </ul>
 *
 * @author Martin Albedinsky
 */
public abstract class LoadableAssistant implements LoaderManager.LoaderCallbacks<Cursor> {

    /**
     * Interface ===================================================================================
     */

    /**
     * Constants ===================================================================================
     */

    /**
     * Log TAG.
     */
    // private static final String TAG = "LoadableAssistant";

    /**
     * Flag indicating whether the output trough log-cat is enabled or not.
     */
    // private static final boolean LOG_ENABLED = true;

    /**
     * Flag indicating whether the debug output trough log-cat is enabled or not.
     */
    // private static final boolean DEBUG_ENABLED = true;

    /**
     * Static members ==============================================================================
     */

    /**
     * Members =====================================================================================
     */

    /**
     * Id of the loader managed by this assistant.
     */
    int mLoaderId = -1;

    /**
     * Constructors ================================================================================
     */

    /**
     * Creates a new instance of LoadableAssistant. If {@link com.wit.android.support.database.annotation.LoaderId @LoaderId}
     * annotation is presented above a subclass of this LoadableAssistant, it will be processed here.
     */
    public LoadableAssistant() {
        this.processClassAnnotations(((Object) this).getClass());
    }

    /**
     * Creates a new instance of LoadableAssistant for loader with the given <var>loaderId</var>.
     *
     * @param loaderId The desired loader id. Can be later changed by {@link #setLoaderId(int)}.
     */
    public LoadableAssistant(int loaderId) {
        this.mLoaderId = loaderId;
    }

    /**
     * Methods =====================================================================================
     */

    /**
     * Public --------------------------------------------------------------------------------------
     */

    /**
     * Same as {@link #startLoader(android.support.v4.app.FragmentActivity, android.os.Bundle)} without
     * <var>params</var>.
     */
    public boolean startLoader(@NonNull FragmentActivity activity) {
        return startLoader(activity, null);
    }

    /**
     * Starts a loader using {@link LoaderManager} obtained from the given <var>activity</var> with
     * the current id and the specified parameters.
     * <p>
     * If the LoaderManager already has a Loader with the id of this assistant, such a loader will be
     * restarted by {@link LoaderManager#restartLoader(int, android.os.Bundle, LoaderManager.LoaderCallbacks)},
     * otherwise will be initiated by {@link LoaderManager#initLoader(int, android.os.Bundle, LoaderManager.LoaderCallbacks)}.
     * <p>
     * <b>Note</b>, that this assistant must have valid (none negative) loader id, otherwise the
     * requested loader can not be started.
     *
     * @param activity Activity used to access LoaderManager instance.
     * @param params   The desired parameters for loader.
     * @return {@code True} if loader was successfully started (initiated/restarted), {@code false}
     * otherwise.
     */
    public boolean startLoader(@NonNull FragmentActivity activity, @Nullable Bundle params) {
        if (mLoaderId >= 0) {
            final LoaderManager loaderManager = activity.getSupportLoaderManager();
            if (loaderManager != null) {
                if (loaderManager.getLoader(mLoaderId) != null) {
                    loaderManager.restartLoader(mLoaderId, params, this);
                } else {
                    loaderManager.initLoader(mLoaderId, params, this);
                }
                return true;
            }
        }
        return false;
    }

    /**
     * Same as {@link #initLoader(android.support.v4.app.FragmentActivity, android.os.Bundle)} without
     * <var>params</var>.
     */
    public boolean initLoader(@NonNull FragmentActivity activity) {
        return initLoader(activity, null);
    }

    /**
     * Wrapped {@link android.support.v4.app.LoaderManager#initLoader(int, android.os.Bundle, android.support.v4.app.LoaderManager.LoaderCallbacks)}
     * called with the current loader id and this assistant as {@code LoaderManager.LoaderCallbacks}.
     */
    public boolean initLoader(@NonNull FragmentActivity activity, @Nullable Bundle params) {
        if (mLoaderId >= 0) {
            final LoaderManager loaderManager = activity.getSupportLoaderManager();
            if (loaderManager != null) {
                loaderManager.initLoader(mLoaderId, params, this);
                return true;
            }
        }
        return false;
    }

    /**
     * Same as {@link #restartLoader(android.support.v4.app.FragmentActivity, android.os.Bundle)}
     * without <var>params</var>.
     */
    public boolean restartLoader(@NonNull FragmentActivity activity) {
        return restartLoader(activity, null);
    }

    /**
     * Wrapped {@link android.support.v4.app.LoaderManager#restartLoader(int, android.os.Bundle, android.support.v4.app.LoaderManager.LoaderCallbacks)}
     * called with the current loader id and this assistant as {@code LoaderManager.LoaderCallbacks}.
     */
    public boolean restartLoader(@NonNull FragmentActivity activity, @Nullable Bundle params) {
        if (mLoaderId >= 0) {
            final LoaderManager loaderManager = activity.getSupportLoaderManager();
            if (loaderManager != null) {
                loaderManager.restartLoader(mLoaderId, params, this);
                return true;
            }
        }
        return false;
    }

    /**
     * Wrapped {@link android.support.v4.app.LoaderManager#getLoader(int)} called with the current
     * loader id.
     */
    @Nullable
    public Loader<Cursor> getLoader(@NonNull FragmentActivity activity) {
        final LoaderManager loaderManager = activity.getSupportLoaderManager();
        return mLoaderId >= 0 && loaderManager != null ? loaderManager.<Cursor>getLoader(mLoaderId) : null;
    }

    /**
     * Wrapped {@link android.support.v4.app.LoaderManager#destroyLoader(int)} called with the current
     * loader id.
     */
    public boolean destroyLoader(@NonNull FragmentActivity activity) {
        if (mLoaderId >= 0) {
            final LoaderManager loaderManager = activity.getSupportLoaderManager();
            if (loaderManager != null) {
                loaderManager.destroyLoader(mLoaderId);
            }
            return true;
        }
        return false;
    }

    /**
     */
    @Override
    public final Loader<Cursor> onCreateLoader(int loaderId, Bundle params) {
        return (mLoaderId == loaderId) ? onCreateLoader(params) : null;
    }

    /**
     */
    @Override
    public final void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (mLoaderId == loader.getId()) {
            if (cursor != null) {
                onLoadFinished(cursor);
            } else {
                onLoadFailed();
            }
        }
    }

    /**
     */
    @Override
    public final void onLoaderReset(Loader<Cursor> loader) {
        if (mLoaderId == loader.getId()) {
            onLoaderReset();
        }
    }

    /**
     * Getters + Setters ---------------------------------------------------------------------------
     */

    /**
     * Sets the loader id for this assistant. As described in {@link #startLoader(android.support.v4.app.FragmentActivity, android.os.Bundle)}
     * method, valid loader id is none negative number.
     *
     * @param loaderId The desired loader id used when starting loader.
     */
    public void setLoaderId(int loaderId) {
        this.mLoaderId = loaderId;
    }

    /**
     * Returns the current loader id of this assistant instance.
     *
     * @return Loader id.
     */
    public int getLoaderId() {
        return mLoaderId;
    }

    /**
     * Protected -----------------------------------------------------------------------------------
     */

    /**
     * Invoked whenever {@link #onCreateLoader(int, android.os.Bundle)} is fired and the specified id
     * is the same id as loader id of this assistant.
     */
    @NonNull
    protected abstract Loader<Cursor> onCreateLoader(@Nullable Bundle params);

    /**
     * Invoked whenever {@link #onLoadFinished(android.support.v4.content.Loader, android.database.Cursor)}
     * is fired and the received Cursor is {@code null}.
     * <p>
     * <b>Note</b>, that this is invoked only in case, when the Loader received in onLoadFinished(Loader, Cursor)
     * had the same id as loader id of this assistant.
     */
    protected void onLoadFailed() {
    }

    /**
     * Invoked whenever {@link #onLoadFinished(android.support.v4.content.Loader, android.database.Cursor)}
     * is fired and the received Cursor is valid (not {@code null}.
     * <p>
     * <b>Note</b>, that this is invoked only in case, when the Loader received in onLoadFinished(Loader, Cursor)
     * had the same id as loader id of this assistant.
     *
     * @param cursor The Cursor received in {@link #onLoadFinished(android.support.v4.content.Loader, android.database.Cursor)}.
     */
    protected abstract void onLoadFinished(@NonNull Cursor cursor);

    /**
     * Invoked whenever {@link #onLoaderReset(android.support.v4.content.Loader)} is fired.
     * <p>
     * <b>Note</b>, that this is invoked only in case, when the Loader received in onLoaderReset(Loader)
     * had the same id as loader id of this assistant.
     */
    protected void onLoaderReset() {
    }

    /**
     * Called to process all annotations of the given <var>classOfAssistant</var>.
     *
     * @param classOfAssistant Class of assistant of which annotations to process.
     */
    private void processClassAnnotations(Class<?> classOfAssistant) {
        // Obtain loader id.
        final LoaderId loaderId = DatabaseAnnotations.obtainAnnotationFrom(classOfAssistant, LoaderId.class);
        if (loaderId != null) {
            this.mLoaderId = loaderId.value();
        }
    }

    /**
     * Private -------------------------------------------------------------------------------------
     */

    /**
     * Inner classes ===============================================================================
     */
}