Example usage for android.support.v4.content Loader getClass

List of usage examples for android.support.v4.content Loader getClass

Introduction

In this page you can find the example usage for android.support.v4.content Loader getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:dev.drsoran.moloko.notification.PermanentNotifier.java

@Override
public void onLoadComplete(Loader<Cursor> loader, Cursor data) {
    super.onLoadComplete(loader, data);

    if (loader.getId() == PermanentNotifierTasksLoader.ID) {
        final String filterString = ((AbstractFilterBasedNotificationTasksLoader) loader).getFilterString();

        getHandler().post(new Runnable() {
            @Override/*ww  w  . j  av a  2 s  .c o m*/
            public void run() {
                onFinishedLoadingTasksToNotify(filterString);
            }
        });
    } else {
        throw new IllegalArgumentException(
                String.format("Unexpected Loader completed load. Expected '%s' but was '%s'.",
                        PermanentNotifierTasksLoader.class.getSimpleName(), loader.getClass().getName()));
    }
}

From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java

@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "class comparison with == is done")
private boolean isClash(@Nullable final Loader<InvocationResult<OUTPUT>> loader, final int loaderId,
        @Nonnull final List<? extends INPUT> inputs) {

    if (loader == null) {

        return false;
    }//from   w  ww  .  ja va  2s .  c o  m

    final Logger logger = mLogger;

    if (loader.getClass() != RoutineLoader.class) {

        logger.err("clashing invocation ID [%d]: %s", loaderId, loader.getClass().getCanonicalName());
        throw new InvocationClashException(loaderId);
    }

    final RoutineLoader<INPUT, OUTPUT> routineLoader = (RoutineLoader<INPUT, OUTPUT>) loader;
    final Class<? extends ContextInvocation<INPUT, OUTPUT>> invocationClass = mConstructor.getDeclaringClass();

    if ((new ClassToken<MissingLoaderInvocation<INPUT, OUTPUT>>() {
    }.getRawClass() != invocationClass) && ((routineLoader.getInvocationType() != invocationClass)
            || !Arrays.equals(routineLoader.getInvocationArgs(), mArgs))) {

        logger.wrn("clashing invocation ID [%d]: %s", loaderId,
                routineLoader.getInvocationType().getCanonicalName());
        throw new InvocationClashException(loaderId);
    }

    final ClashResolutionType resolution = mClashResolutionType;

    if (resolution == ClashResolutionType.ABORT_THAT) {

        logger.dbg("restarting existing invocation [%d]", loaderId);
        return true;

    } else if (resolution == ClashResolutionType.ABORT_THIS) {

        logger.dbg("aborting invocation [%d]", loaderId);
        throw new InputClashException(loaderId);

    } else if ((resolution == ClashResolutionType.KEEP_THAT) || routineLoader.areSameInputs(inputs)) {

        logger.dbg("keeping existing invocation [%d]", loaderId);
        return false;

    } else if (resolution == ClashResolutionType.ABORT_THAT_INPUT) {

        logger.dbg("restarting existing invocation [%d]", loaderId);
        return true;

    } else if (resolution == ClashResolutionType.ABORT_THIS_INPUT) {

        logger.dbg("aborting invocation [%d]", loaderId);
        throw new InputClashException(loaderId);
    }

    return true;
}

From source file:com.gh.bmd.jrt.android.v4.core.LoaderInvocation.java

@Override
@SuppressWarnings("unchecked")
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "class comparison with == is done")
public void onCall(@Nonnull final List<? extends INPUT> inputs, @Nonnull final ResultChannel<OUTPUT> result) {

    final Logger logger = mLogger;
    final Object context = mContext.get();

    if (context == null) {

        logger.dbg("avoiding running invocation since context is null");
        return;// w w  w.j  ava2  s  .  c  o  m
    }

    final Context loaderContext;
    final LoaderManager loaderManager;

    if (context instanceof FragmentActivity) {

        final FragmentActivity activity = (FragmentActivity) context;
        loaderContext = activity.getApplicationContext();
        loaderManager = activity.getSupportLoaderManager();
        logger.dbg("running invocation bound to activity: %s", activity);

    } else if (context instanceof Fragment) {

        final Fragment fragment = (Fragment) context;
        loaderContext = fragment.getActivity().getApplicationContext();
        loaderManager = fragment.getLoaderManager();
        logger.dbg("running invocation bound to fragment: %s", fragment);

    } else {

        throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName());
    }

    int loaderId = mLoaderId;

    if (loaderId == ContextRoutineBuilder.AUTO) {

        loaderId = mConstructor.getDeclaringClass().hashCode();

        for (final Object arg : mArgs) {

            loaderId = 31 * loaderId + recursiveHashCode(arg);
        }

        loaderId = 31 * loaderId + inputs.hashCode();
        logger.dbg("generating invocation ID: %d", loaderId);
    }

    final Loader<InvocationResult<OUTPUT>> loader = loaderManager.getLoader(loaderId);
    final boolean isClash = isClash(loader, loaderId, inputs);
    final WeakIdentityHashMap<Object, SparseArray<WeakReference<RoutineLoaderCallbacks<?>>>> callbackMap = sCallbackMap;
    SparseArray<WeakReference<RoutineLoaderCallbacks<?>>> callbackArray = callbackMap.get(context);

    if (callbackArray == null) {

        callbackArray = new SparseArray<WeakReference<RoutineLoaderCallbacks<?>>>();
        callbackMap.put(context, callbackArray);
    }

    final WeakReference<RoutineLoaderCallbacks<?>> callbackReference = callbackArray.get(loaderId);
    RoutineLoaderCallbacks<OUTPUT> callbacks = (callbackReference != null)
            ? (RoutineLoaderCallbacks<OUTPUT>) callbackReference.get()
            : null;

    if ((callbacks == null) || (loader == null) || isClash) {

        final RoutineLoader<INPUT, OUTPUT> routineLoader;

        if (!isClash && (loader != null) && (loader.getClass() == RoutineLoader.class)) {

            routineLoader = (RoutineLoader<INPUT, OUTPUT>) loader;

        } else {

            routineLoader = null;
        }

        final RoutineLoaderCallbacks<OUTPUT> newCallbacks = createCallbacks(loaderContext, loaderManager,
                routineLoader, inputs, loaderId);

        if (callbacks != null) {

            logger.dbg("resetting existing callbacks [%d]", loaderId);
            callbacks.reset();
        }

        callbackArray.put(loaderId, new WeakReference<RoutineLoaderCallbacks<?>>(newCallbacks));
        callbacks = newCallbacks;
    }

    logger.dbg("setting result cache type [%d]: %s", loaderId, mCacheStrategyType);
    callbacks.setCacheStrategy(mCacheStrategyType);

    final OutputChannel<OUTPUT> outputChannel = callbacks.newChannel();

    if (isClash) {

        logger.dbg("restarting loader [%d]", loaderId);
        loaderManager.restartLoader(loaderId, Bundle.EMPTY, callbacks);

    } else {

        logger.dbg("initializing loader [%d]", loaderId);
        loaderManager.initLoader(loaderId, Bundle.EMPTY, callbacks);
    }

    result.pass(outputChannel);
}