Android Open Source - EventBus Error Dialog Fragment Factory






From Project

Back to project page EventBus.

License

The source code is released under:

Apache License

If you think the Android project EventBus 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

package de.greenrobot.event.util;
//from   ww  w. j  av a 2  s  .  c  o  m
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;

/**
 * Factory to allow injecting a more complex exception mapping; typically you would subclass one of {@link Honeycomb} or
 * {@link Support}.
 */
public abstract class ErrorDialogFragmentFactory<T> {
    protected final ErrorDialogConfig config;

    protected ErrorDialogFragmentFactory(ErrorDialogConfig config) {
        this.config = config;
    }

    /**
     * Prepares the fragment's arguments and creates the fragment. May be overridden to provide custom error fragments.
     */
    protected T prepareErrorFragment(ThrowableFailureEvent event, boolean finishAfterDialog,
            Bundle argumentsForErrorDialog) {
        if (event.isSuppressErrorUi()) {
            // Show nothing by default
            return null;
        }
        Bundle bundle;
        if (argumentsForErrorDialog != null) {
            bundle = (Bundle) argumentsForErrorDialog.clone();
        } else {
            bundle = new Bundle();
        }

        if (!bundle.containsKey(ErrorDialogManager.KEY_TITLE)) {
            String title = getTitleFor(event, bundle);
            bundle.putString(ErrorDialogManager.KEY_TITLE, title);
        }
        if (!bundle.containsKey(ErrorDialogManager.KEY_MESSAGE)) {
            String message = getMessageFor(event, bundle);
            bundle.putString(ErrorDialogManager.KEY_MESSAGE, message);
        }
        if (!bundle.containsKey(ErrorDialogManager.KEY_FINISH_AFTER_DIALOG)) {
            bundle.putBoolean(ErrorDialogManager.KEY_FINISH_AFTER_DIALOG, finishAfterDialog);
        }
        if (!bundle.containsKey(ErrorDialogManager.KEY_EVENT_TYPE_ON_CLOSE)
                && config.defaultEventTypeOnDialogClosed != null) {
            bundle.putSerializable(ErrorDialogManager.KEY_EVENT_TYPE_ON_CLOSE, config.defaultEventTypeOnDialogClosed);
        }
        if (!bundle.containsKey(ErrorDialogManager.KEY_ICON_ID) && config.defaultDialogIconId != 0) {
            bundle.putInt(ErrorDialogManager.KEY_ICON_ID, config.defaultDialogIconId);
        }
        return createErrorFragment(event, bundle);
    }

    /** Returns either a new Honeycomb+ or a new support library DialogFragment. */
    protected abstract T createErrorFragment(ThrowableFailureEvent event, Bundle arguments);

    /** May be overridden to provide custom error title. */
    protected String getTitleFor(ThrowableFailureEvent event, Bundle arguments) {
        return config.resources.getString(config.defaultTitleId);
    }

    /** May be overridden to provide custom error messages. */
    protected String getMessageFor(ThrowableFailureEvent event, Bundle arguments) {
        int msgResId = config.getMessageIdForThrowable(event.throwable);
        return config.resources.getString(msgResId);
    }

    public static class Support extends ErrorDialogFragmentFactory<Fragment> {

        public Support(ErrorDialogConfig config) {
            super(config);
        }

        protected Fragment createErrorFragment(ThrowableFailureEvent event, Bundle arguments) {
            ErrorDialogFragments.Support errorFragment = new ErrorDialogFragments.Support();
            errorFragment.setArguments(arguments);
            return errorFragment;
        }

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class Honeycomb extends ErrorDialogFragmentFactory<android.app.Fragment> {

        public Honeycomb(ErrorDialogConfig config) {
            super(config);
        }

        protected android.app.Fragment createErrorFragment(ThrowableFailureEvent event, Bundle arguments) {
            ErrorDialogFragments.Honeycomb errorFragment = new ErrorDialogFragments.Honeycomb();
            errorFragment.setArguments(arguments);
            return errorFragment;
        }

    }
}




Java Source Code List

de.greenrobot.event.AsyncPoster.java
de.greenrobot.event.BackgroundPoster.java
de.greenrobot.event.EventBusBuilder.java
de.greenrobot.event.EventBusException.java
de.greenrobot.event.EventBus.java
de.greenrobot.event.HandlerPoster.java
de.greenrobot.event.NoSubscriberEvent.java
de.greenrobot.event.PendingPostQueue.java
de.greenrobot.event.PendingPost.java
de.greenrobot.event.SubscriberExceptionEvent.java
de.greenrobot.event.SubscriberMethodFinder.java
de.greenrobot.event.SubscriberMethod.java
de.greenrobot.event.Subscription.java
de.greenrobot.event.ThreadMode.java
de.greenrobot.event.util.AsyncExecutor.java
de.greenrobot.event.util.ErrorDialogConfig.java
de.greenrobot.event.util.ErrorDialogFragmentFactory.java
de.greenrobot.event.util.ErrorDialogFragments.java
de.greenrobot.event.util.ErrorDialogManager.java
de.greenrobot.event.util.ExceptionToResourceMapping.java
de.greenrobot.event.util.HasExecutionScope.java
de.greenrobot.event.util.ThrowableFailureEvent.java
de.greenrobot.eventperf.TestEvent.java
de.greenrobot.eventperf.TestFinishedEvent.java
de.greenrobot.eventperf.TestParams.java
de.greenrobot.eventperf.TestRunnerActivity.java
de.greenrobot.eventperf.TestRunner.java
de.greenrobot.eventperf.TestSetupActivity.java
de.greenrobot.eventperf.Test.java
de.greenrobot.eventperf.testsubject.PerfTestEventBus.java
de.greenrobot.eventperf.testsubject.PerfTestOtto.java