Example usage for android.support.v4.content AsyncTaskLoader AsyncTaskLoader

List of usage examples for android.support.v4.content AsyncTaskLoader AsyncTaskLoader

Introduction

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

Prototype

public AsyncTaskLoader(Context context) 

Source Link

Usage

From source file:com.echlabsw.android.drawertab.MainActivity.java

/**
 * Instantiate and return a new Loader for the given ID.
 *///from  ww w.j  ava 2 s. co  m
@Override
public Loader<JSONObject> onCreateLoader(int id, Bundle args) {
    Log.i(TAG, "onCreateLoader");
    return new AsyncTaskLoader<JSONObject>(this) {

        Thread loaderThread;

        @Override
        public JSONObject loadInBackground() {
            try {
                loaderThread = Thread.currentThread();
                for (int i = 0; i < 10; i++) {
                    Log.i(TAG, "[thread: " + Thread.currentThread().getId() + "] loadInBackground: " + i);
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                Log.i(TAG, "Thread " + Thread.currentThread().getId() + " interrupted");
            }
            return null;
        }

        @Override
        public void onCanceled(JSONObject data) {
            Log.i(TAG, "onCanceled");
            if (loaderThread != null) {
                loaderThread.interrupt();
            }
            super.onCanceled(data);
        }

        /**
         * Run in UI Thread
         */
        @Override
        protected void onStopLoading() {
            Log.i(TAG, "onStopLoading");
            if (loaderThread != null) {
                loaderThread.interrupt();
            }
            super.onStopLoading();
        }

        @Override
        protected void onAbandon() {
            Log.i(TAG, "onAbandon");
            super.onAbandon();
        }

        @Override
        protected void onReset() {
            Log.i(TAG, "onReset");
            super.onReset();
        }

        @Override
        protected void onStartLoading() {
            super.onStartLoading();
            showProgressLoading();
        }

    };
}

From source file:net.naonedbus.fragment.CustomListFragment.java

@Override
public Loader<AsyncResult<ListAdapter>> onCreateLoader(final int loaderId, final Bundle bundle) {
    if (DBG)/*from ww  w.  java2  s  .co  m*/
        Log.d(LOG_TAG + "$" + getClass().getSimpleName(), "onCreateLoader");

    final Loader<AsyncResult<ListAdapter>> loader = new AsyncTaskLoader<AsyncResult<ListAdapter>>(
            getActivity()) {
        private AsyncResult<ListAdapter> mResult;

        @Override
        public AsyncResult<ListAdapter> loadInBackground() {
            if (DBG)
                Log.d(LOG_TAG + "$" + getClass().getSimpleName(), "loadInBackground");

            return loadContent(getActivity(), bundle);
        }

        /**
         * Called when there is new data to deliver to the client. The super
         * class will take care of delivering it; the implementation here
         * just adds a little more logic.
         */
        @Override
        public void deliverResult(final AsyncResult<ListAdapter> result) {
            mResult = result;

            if (isStarted()) {
                // If the Loader is currently started, we can immediately
                // deliver its results.
                try {
                    super.deliverResult(result);
                } catch (final NullPointerException e) {

                }
            }
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            if (mResult != null) {
                // If we currently have a result available, deliver it
                // immediately.
                deliverResult(mResult);
            }

            if (takeContentChanged() || mResult == null) {
                // If the data has changed since the last time it was loaded
                // or is not currently available, start a load.
                forceLoad();
            }
        }

    };

    if (getListAdapter() == null || getListAdapter().getCount() == 0)
        showLoader();
    onPreExecute();

    return loader;
}

From source file:it.geosolutions.geocollect.android.core.mission.PendingMissionDetailFragment.java

/**
 * Load the mission and template data either from the database or from the intent
 *///  w  w w. ja  v a  2 s  .  c om
public Loader<Void> onCreateLoader(int id, Bundle args) {
    Log.d(TAG, "onCreateLoader(): id=" + id);
    Loader<Void> loader = new AsyncTaskLoader<Void>(getActivity()) {

        @Override
        public Void loadInBackground() {
            Activity activity = getSherlockActivity();
            Feature myFeature = (Feature) getArguments().getSerializable(ARG_ITEM_FEATURE);
            Mission m = new Mission();
            m.setTemplate(MissionUtils.getDefaultTemplate(activity));
            m.setOrigin(myFeature);

            if (activity instanceof PendingMissionDetailActivity) {
                Log.d(TAG, "Loader: Connecting to Activity database");
                m.db = ((PendingMissionDetailActivity) activity).spatialiteDatabase;
            } else if (activity instanceof PendingMissionListActivity) {
                Log.d(TAG, "Loader: Connecting to Activity database");
                m.db = ((PendingMissionListActivity) activity).spatialiteDatabase;
            } else {
                Log.w(TAG, "Loader: Could not connect to Activity database");
            }

            mission = m;
            return null;
        }
    };
    //TODO create loader;
    loader.forceLoad();
    return loader;
}

From source file:org.rm3l.ddwrt.tiles.admin.nvram.AdminNVRAMTile.java

@Nullable
@Override//from w  w  w  .  java  2  s .com
protected Loader<None> getLoader(int id, Bundle args) {
    return new AsyncTaskLoader<None>(this.mParentFragmentActivity) {

        @Nullable
        @Override
        public None loadInBackground() {

            try {
                Log.d(LOG_TAG,
                        "Init background loader for " + AdminNVRAMTile.class + ": routerInfo=" + mRouter
                                + " / this.mAutoRefreshToggle= " + mAutoRefreshToggle + " / nbRunsLoader="
                                + nbRunsLoader);

                if (nbRunsLoader > 0 && !mAutoRefreshToggle) {
                    //Skip run
                    Log.d(LOG_TAG, "Skip loader run");
                    return (None) new None().setException(new DDWRTTileAutoRefreshNotAllowedException());
                }
                nbRunsLoader++;

                mNvramInfoToDisplay.clear();
                mNvramInfoDefaultSorting.clear();

                NVRAMInfo nvramInfoTmp = null;

                try {
                    nvramInfoTmp = SSHUtils.getNVRamInfoFromRouter(mRouter, mGlobalPreferences);
                } finally {
                    if (nvramInfoTmp != null) {
                        mNvramInfoDefaultSorting.putAll(nvramInfoTmp);
                    }

                    final String[] nvramSize = SSHUtils.getManualProperty(mRouter, mGlobalPreferences,
                            "nvram show 2>&1 1>/dev/null");
                    if (nvramSize != null && nvramSize.length > 0) {
                        final List<String> nvramUsageList = StatusRouterSpaceUsageTile.NVRAM_SIZE_SPLITTER
                                .splitToList(nvramSize[0]);
                        if (nvramUsageList != null && !nvramUsageList.isEmpty()) {
                            final String value = nvramUsageList.get(0);
                            if (value != null) {
                                mNvramInfoDefaultSorting.setProperty(NVRAM_SIZE, value);
                            }
                        }
                    }

                }

                if (mNvramInfoDefaultSorting.isEmpty()) {
                    throw new DDWRTNoDataException("No Data!");
                }

                //Now apply sorting here (as per user-preferences)

                final Properties defaultNVRAMInfo = mNvramInfoDefaultSorting.getData();
                if (mParentFragmentPreferences != null) {
                    final Integer currentSort = sortIds.inverse()
                            .get(mParentFragmentPreferences.getInt(getFormattedPrefKey(SORT), -1));
                    if (currentSort == null || currentSort <= 0) {
                        mNvramInfoToDisplay = new HashMap<>(defaultNVRAMInfo);
                    } else {
                        switch (currentSort) {
                        case R.id.tile_admin_nvram_sort_asc:
                            //asc
                            mNvramInfoToDisplay = new TreeMap<>(COMPARATOR_STRING_CASE_INSENSITIVE);
                            mNvramInfoToDisplay.putAll(defaultNVRAMInfo);
                            break;
                        case R.id.tile_admin_nvram_sort_desc:
                            //desc
                            mNvramInfoToDisplay = new TreeMap<>(COMPARATOR_REVERSE_STRING_CASE_INSENSITIVE);
                            mNvramInfoToDisplay.putAll(defaultNVRAMInfo);
                            break;
                        case R.id.tile_admin_nvram_sort_default:
                        default:
                            mNvramInfoToDisplay = new HashMap<>(defaultNVRAMInfo);
                            break;
                        }
                    }
                } else {
                    mNvramInfoToDisplay = new HashMap<>(defaultNVRAMInfo);
                }

                return new None();
            } catch (@NotNull final Exception e) {
                e.printStackTrace();
                return (None) new None().setException(e);
            }

        }
    };
}