Example usage for android.content AsyncTaskLoader AsyncTaskLoader

List of usage examples for android.content AsyncTaskLoader AsyncTaskLoader

Introduction

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

Prototype

public AsyncTaskLoader(Context context) 

Source Link

Usage

From source file:com.annuletconsulting.homecommand.node.AsyncSend.java

@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
    AsyncTaskLoader<String> loader = new AsyncTaskLoader<String>(activity) {
        @Override/*from w  w w  .j  a  v  a 2  s  .  c  om*/
        public String loadInBackground() {
            StringBuffer instr = new StringBuffer();
            try {
                Socket connection = new Socket(ipAddr, port);
                BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
                OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
                osw.write(formatJSON(command.toUpperCase()));
                osw.write(13);
                osw.flush();
                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
                InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
                int c;
                while ((c = isr.read()) != 13)
                    instr.append((char) c);
                isr.close();
                bis.close();
                osw.close();
                bos.close();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return instr.toString();
        }
    };
    return loader;
}

From source file:de.dreier.mytargets.features.statistics.StatisticsActivity.java

@Override
public Loader<List<Pair<Training, Round>>> onCreateLoader(int i, Bundle bundle) {
    final long[] roundIds = getIntent().getLongArrayExtra(ROUND_IDS);
    return new AsyncTaskLoader<List<Pair<Training, Round>>>(this) {
        @Override//www.j  ava2s.  c om
        public List<Pair<Training, Round>> loadInBackground() {
            final List<Round> rounds = Round.getAll(roundIds);
            LongSparseArray<Training> trainingsMap = new LongSparseArray<>();
            Stream.of(rounds).map(round -> round.trainingId).distinct().map(Training::get)
                    .forEach(training -> trainingsMap.append(training.getId(), training));
            return Stream.of(rounds).map(round -> new Pair<>(trainingsMap.get(round.trainingId), round))
                    .collect(Collectors.toList());
        }
    };
}

From source file:org.jboss.aerogear.devnexus2015.ui.fragment.AboutFragment.java

@Override
public Loader<List> onCreateLoader(int id, Bundle args) {
    switch (id) {
    case SPONSOR_LOADER:
        return new AsyncTaskLoader<List>(getActivity()) {
            @Override/*from   ww w.  ja va  2  s  .  c  o m*/
            public List loadInBackground() {
                final List sponsors = new ArrayList(30);
                final InputStream sponsorsStream = getContext().getResources().openRawResource(R.raw.sponsors);
                try {
                    final String sponsorsJson = IOUtils.toString(sponsorsStream);
                    final JsonElement jsonRootElement = new JsonParser().parse(sponsorsJson);
                    final JsonArray sponsorsJsonArray = jsonRootElement.getAsJsonObject()
                            .getAsJsonArray("sponsors");
                    for (int index = 0; index < sponsorsJsonArray.size(); index++) {
                        final JsonElement sponsorObject = sponsorsJsonArray.get(index);
                        final Sponsor sponsor = GsonUtils.GSON.fromJson(sponsorObject, Sponsor.class);
                        sponsors.add(sponsor);
                    }
                } catch (IOException e) {
                    Log.e("LOAD_SPONSORS", e.getMessage(), e);
                }

                return sponsors;
            }
        };

    case OSS_LOADER:
        return new AsyncTaskLoader<List>(getActivity()) {
            @Override
            public List loadInBackground() {
                final List licenses = new ArrayList(30);
                final InputStream licensesStream = getContext().getResources().openRawResource(R.raw.licenses);
                try {
                    final String licensesJson = IOUtils.toString(licensesStream);
                    final JsonElement jsonRootElement = new JsonParser().parse(licensesJson);
                    final JsonArray licensesJsonArray = jsonRootElement.getAsJsonObject()
                            .getAsJsonArray("licenses");
                    for (int index = 0; index < licensesJsonArray.size(); index++) {
                        final JsonElement sponsorObject = licensesJsonArray.get(index);
                        final License license = GsonUtils.GSON.fromJson(sponsorObject, License.class);
                        licenses.add(license);
                    }
                } catch (IOException e) {
                    Log.e("LOAD_LICENSE", e.getMessage(), e);
                }

                return licenses;
            }
        };

    default:
        return null;
    }

}

From source file:it.polimi.spf.app.fragments.profile.ProfileFragment.java

@Override
public Loader<ProfileFieldContainer> onCreateLoader(int id, Bundle args) {
    switch (id) {
    case LOAD_PROFILE_LOADER_ID:
        return new AsyncTaskLoader<ProfileFieldContainer>(getActivity()) {

            @Override//  w  ww .j a  va2  s .  c  o m
            public ProfileFieldContainer loadInBackground() {
                if (mMode == Mode.SELF || mMode == Mode.EDIT) {
                    return SPF.get().getProfileManager().getProfileFieldBulk(mCurrentPersona,
                            ProfilePagerAdapter.DEFAULT_FIELDS);
                } else {
                    SPFRemoteInstance instance = SPF.get().getPeopleManager().getPerson(mPersonIdentifier);
                    if (instance == null) {
                        throw new IllegalStateException(
                                "Person " + mPersonIdentifier + " not found in proximity");
                    } else {
                        String app = getActivity().getCallingPackage();
                        app = app == null ? "it.polimi.spf.app" : app;
                        return instance.getProfileBulk(
                                ProfileField.toIdentifierList(ProfilePagerAdapter.DEFAULT_FIELDS), app);
                    }
                }
            }
        };

    case SAVE_PROFILE_LOADER_ID:
        if (mMode != Mode.EDIT) {
            Log.e(TAG, "SAVE_PROFILE_LOADER initialized in mode " + mMode);
        }

        return new AsyncTaskLoader<ProfileFieldContainer>(getActivity()) {

            @Override
            public ProfileFieldContainer loadInBackground() {
                SPF.get().getProfileManager().setProfileFieldBulk(mContainer, mCurrentPersona);
                return null;
            }
        };

    default:
        throw new IllegalArgumentException("No loader for id " + id);
    }
}

From source file:com.frostwire.android.gui.fragments.BrowsePeerFragment.java

private Loader<Object> createLoaderFiles(final byte fileType) {
    AsyncTaskLoader<Object> loader = new AsyncTaskLoader<Object>(getActivity()) {
        @Override/*  w w  w  .  j  av a 2 s.  com*/
        public Object loadInBackground() {
            try {
                return new Object[] { fileType, peer.browse(fileType) };
            } catch (Throwable e) {
                LOG.error("Error performing finger", e);
            }
            return null;
        }
    };
    loader.forceLoad();
    return loader;
}