Example usage for android.os AsyncTask SERIAL_EXECUTOR

List of usage examples for android.os AsyncTask SERIAL_EXECUTOR

Introduction

In this page you can find the example usage for android.os AsyncTask SERIAL_EXECUTOR.

Prototype

Executor SERIAL_EXECUTOR

To view the source code for android.os AsyncTask SERIAL_EXECUTOR.

Click Source Link

Document

An Executor that executes tasks one at a time in serial order.

Usage

From source file:com.squareup.leakcanary.internal.DisplayLeakActivity.java

void deleteVisibleLeak() {
    final AnalyzedHeap visibleLeak = getVisibleLeak();
    AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
        @Override/*  w w w.  j  a v a 2  s.  co  m*/
        public void run() {
            File heapDumpFile = visibleLeak.heapDump.heapDumpFile;
            File resultFile = visibleLeak.selfFile;
            boolean resultDeleted = resultFile.delete();
            if (!resultDeleted) {
                CanaryLog.d("Could not delete result file %s", resultFile.getPath());
            }
            boolean heapDumpDeleted = heapDumpFile.delete();
            if (!heapDumpDeleted) {
                CanaryLog.d("Could not delete heap dump file %s", heapDumpFile.getPath());
            }
        }
    });
    visibleLeakRefKey = null;
    leaks.remove(visibleLeak);
    updateUi();
}

From source file:org.chromium.chrome.browser.tabmodel.TabPersistentStore.java

/**
 * Creates an instance of a TabPersistentStore.
 * @param modelSelector The {@link TabModelSelector} to restore to and save from.
 * @param tabCreatorManager The {@link TabCreatorManager} to use.
 * @param observer      Notified when the TabPersistentStore has completed tasks.
 * @param mergeTabs     Whether tabs from a second TabModelSelector should be merged into
 *                      into this instance.
 *//*from w  w w.  j  a v  a  2  s . c  om*/
public TabPersistentStore(TabPersistencePolicy policy, TabModelSelector modelSelector,
        TabCreatorManager tabCreatorManager, TabPersistentStoreObserver observer, final boolean mergeTabs) {
    mPersistencePolicy = policy;
    mTabModelSelector = modelSelector;
    mTabCreatorManager = tabCreatorManager;
    mTabsToSave = new ArrayDeque<>();
    mTabsToRestore = new ArrayDeque<>();
    mTabIdsToRestore = new HashSet<>();
    mObserver = observer;
    mPreferences = ContextUtils.getAppSharedPreferences();

    assert isStateFile(policy.getStateFileName()) : "State file name is not valid";
    boolean needsInitialization = mPersistencePolicy.performInitialization(AsyncTask.SERIAL_EXECUTOR);

    if (mPersistencePolicy.isMergeInProgress())
        return;

    Executor executor = needsInitialization ? AsyncTask.SERIAL_EXECUTOR : AsyncTask.THREAD_POOL_EXECUTOR;

    mPrefetchTabListTask = startFetchTabListTask(executor, mPersistencePolicy.getStateFileName());
    startPrefetchActiveTabTask(executor);

    if (mergeTabs) {
        assert mPersistencePolicy.getStateToBeMergedFileName() != null;
        mPrefetchTabListToMergeTask = startFetchTabListTask(executor,
                mPersistencePolicy.getStateToBeMergedFileName());
    }
}

From source file:com.squareup.leakcanary.internal.DisplayLeakActivity.java

void deleteAllLeaks() {
    final LeakDirectoryProvider leakDirectoryProvider = getLeakDirectoryProvider(this);
    AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
        @Override/*from   w  w  w . j  av  a  2 s. c om*/
        public void run() {
            leakDirectoryProvider.clearLeakDirectory();
        }
    });
    leaks = Collections.emptyList();
    updateUi();
}

From source file:com.pk.wallpapermanager.PkWallpaperManager.java

/**
 * Loads wallpapers stored locally asynchronously.<br>
 * Make sure to have set your String Array of Local Wallpapers in 
 * your settings before calling this method.
 * <p>/*w  w  w .ja v a2 s.com*/
 * This will not throw any exceptions but it does not guarantee success either.
 * Use this as a lazy way of loading stuff in the background.
 * 
 * @param parallel   Boolean indicating whether to run serially or in parallel. 
 *                True for parallel, False for serial.
 */
public void fetchLocalWallpapersAsync(boolean parallel) {
    if (localWallpapersTask.getStatus() == AsyncTask.Status.PENDING) {
        // Execute task if it's ready to go!
        localWallpapersTask
                .executeOnExecutor(parallel ? AsyncTask.THREAD_POOL_EXECUTOR : AsyncTask.SERIAL_EXECUTOR);
    } else if (localWallpapersTask.getStatus() == AsyncTask.Status.RUNNING && debugEnabled) {
        // Don't execute if already running
        Log.d(LOG_TAG, "Task is already running...");
    } else if (localWallpapersTask.getStatus() == AsyncTask.Status.FINISHED) {
        // Okay, this is not supposed to happen. Reset and recall.
        if (debugEnabled)
            Log.d(LOG_TAG, "Uh oh, it appears the task has finished without being reset. Resetting task...");

        initLocalWallpapersTask();
        fetchLocalWallpapersAsync(parallel);
    }
}

From source file:eu.power_switch.gui.dialog.AddActionDialog.java

private void updateApartmentList() {
    setPositiveButtonVisibility(false);//w ww  .  j  av a  2  s . co  m

    progressApartment.setVisibility(View.VISIBLE);
    spinner_apartment.setVisibility(View.GONE);

    progressRoom.setVisibility(View.VISIBLE);
    spinner_room.setVisibility(View.GONE);

    progressReceiver.setVisibility(View.VISIBLE);
    spinner_receiver.setVisibility(View.GONE);

    progressButton.setVisibility(View.VISIBLE);
    spinner_button.setVisibility(View.GONE);

    progressScene.setVisibility(View.VISIBLE);
    spinner_scene.setVisibility(View.GONE);

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            apartmentNames.clear();

            try {
                ArrayList<Apartment> availableApartments = (ArrayList<Apartment>) DatabaseHandler
                        .getAllApartments();
                for (Apartment apartment : availableApartments) {
                    apartmentNames.add(apartment.getName());
                }

            } catch (Exception e) {
            }

            Collections.sort(apartmentNames, compareToIgnoreCase);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressApartment.setVisibility(View.GONE);
            spinner_apartment.setVisibility(View.VISIBLE);

            spinner_apartment.setSelection(0);
            apartmentSpinnerArrayAdapter.notifyDataSetChanged();

            currentApartment = getSelectedApartment();
            updateRoomList();
            updateSceneList();
        }
    }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}

From source file:com.pk.wallpapermanager.PkWallpaperManager.java

/**
 * Loads wallpapers stored on your cloud repository asynchronously. It's safe to 
 * call this on the main UI thread./*from   w  ww  .  ja va2  s . co  m*/
 * <p>
 * This will not throw any exceptions but it does not guarantee success either.
 * Use this as a lazy way of loading stuff in the background.
 * 
 * @param parallel   Boolean indicating whether to run serially or in parallel. 
 *                True for parallel, False for serial.
 */
public void fetchCloudWallpapersAsync(boolean parallel) {
    if (cloudWallpapersTask.getStatus() == AsyncTask.Status.PENDING) {
        // Execute task if it's ready to go!
        cloudWallpapersTask
                .executeOnExecutor(parallel ? AsyncTask.THREAD_POOL_EXECUTOR : AsyncTask.SERIAL_EXECUTOR);
    } else if (cloudWallpapersTask.getStatus() == AsyncTask.Status.RUNNING && debugEnabled) {
        // Don't execute if already running
        Log.d(LOG_TAG, "Task is already running...");
    } else if (cloudWallpapersTask.getStatus() == AsyncTask.Status.FINISHED) {
        // Okay, this is not supposed to happen. Reset and recall.
        if (debugEnabled)
            Log.d(LOG_TAG, "Uh oh, it appears the task has finished without being reset. Resetting task...");

        initCloudWallpapersTask();
        fetchCloudWallpapersAsync(parallel);
    }
}

From source file:com.android.ex.chips.RecipientAlternatesAdapter.java

@SuppressLint("NewApi")
private void executePhotoLoadTask(final AsyncTask<Void, Void, Void> photoLoadTask) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    else/*ww  w  .j  a v a2 s. c  o  m*/
        photoLoadTask.execute();
}

From source file:eu.power_switch.gui.dialog.AddActionDialog.java

private void updateSceneList() {
    setPositiveButtonVisibility(false);//from   www.jav a  2  s. c o  m

    progressScene.setVisibility(View.VISIBLE);
    spinner_scene.setVisibility(View.GONE);

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                sceneNames.clear();

                for (Scene scene : currentApartment.getScenes()) {
                    sceneNames.add(scene.getName());
                }

            } catch (Exception e) {
            }

            Collections.sort(sceneNames, compareToIgnoreCase);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressScene.setVisibility(View.GONE);
            spinner_scene.setVisibility(View.VISIBLE);

            spinner_scene.setSelection(0);
            sceneSpinnerArrayAdapter.notifyDataSetChanged();

            updatePositiveButton();
        }
    }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}

From source file:com.inloc.dr.StepService.java

public void logForPost(String post_build) {
    if (current_file == null || records == 0) {
        current_file = new File(mOutputDir.getAbsolutePath(), System.currentTimeMillis() + ".log");
        record_files.add(current_file);//www . ja  va  2s.c o  m
        Log.i(TAG, "Added new record. [" + records + "]");
        if (record_files.size() > 1)
            new DoPost().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }
    try {
        fw = new FileWriter(current_file, true);
        fw.write(post_build + "\n");
        fw.flush();
        fw.close();
        records = (records + 1) % RECORDS_PER_FILE;
    } catch (IOException e) {
        e.printStackTrace();
    }
}