Search in sources :

Example 16 with AsyncTask

use of android.os.AsyncTask in project platform_frameworks_base by android.

the class WallpaperCropActivity method setCropViewTileSource.

public void setCropViewTileSource(final BitmapRegionTileSource.BitmapSource bitmapSource, final boolean touchEnabled, final boolean moveToLeft, final Runnable postExecute) {
    final Context context = WallpaperCropActivity.this;
    final View progressView = findViewById(R.id.loading);
    final AsyncTask<Void, Void, Void> loadBitmapTask = new AsyncTask<Void, Void, Void>() {

        protected Void doInBackground(Void... args) {
            if (!isCancelled()) {
                try {
                    bitmapSource.loadInBackground();
                } catch (SecurityException securityException) {
                    if (isDestroyed()) {
                        // Temporarily granted permissions are revoked when the activity
                        // finishes, potentially resulting in a SecurityException here.
                        // Even though {@link #isDestroyed} might also return true in different
                        // situations where the configuration changes, we are fine with
                        // catching these cases here as well.
                        cancel(false);
                    } else {
                        // otherwise it had a different cause and we throw it further
                        throw securityException;
                    }
                }
            }
            return null;
        }

        protected void onPostExecute(Void arg) {
            if (!isCancelled()) {
                progressView.setVisibility(View.INVISIBLE);
                if (bitmapSource.getLoadingState() == BitmapSource.State.LOADED) {
                    mCropView.setTileSource(new BitmapRegionTileSource(context, bitmapSource), null);
                    mCropView.setTouchEnabled(touchEnabled);
                    if (moveToLeft) {
                        mCropView.moveToLeft();
                    }
                }
            }
            if (postExecute != null) {
                postExecute.run();
            }
        }
    };
    // We don't want to show the spinner every time we load an image, because that would be
    // annoying; instead, only start showing the spinner if loading the image has taken
    // longer than 1 sec (ie 1000 ms)
    progressView.postDelayed(new Runnable() {

        public void run() {
            if (loadBitmapTask.getStatus() != AsyncTask.Status.FINISHED) {
                progressView.setVisibility(View.VISIBLE);
            }
        }
    }, 1000);
    loadBitmapTask.execute();
}
Also used : Context(android.content.Context) AsyncTask(android.os.AsyncTask) View(android.view.View) BitmapRegionTileSource(com.android.photos.BitmapRegionTileSource)

Example 17 with AsyncTask

use of android.os.AsyncTask in project platform_frameworks_base by android.

the class LockscreenWallpaper method run.

@Override
public void run() {
    if (mLoader != null) {
        mLoader.cancel(false);
    }
    final int currentUser = mCurrentUserId;
    final UserHandle selectedUser = mSelectedUser;
    mLoader = new AsyncTask<Void, Void, LoaderResult>() {

        @Override
        protected LoaderResult doInBackground(Void... params) {
            return loadBitmap(currentUser, selectedUser);
        }

        @Override
        protected void onPostExecute(LoaderResult result) {
            super.onPostExecute(result);
            if (isCancelled()) {
                return;
            }
            if (result.success) {
                mCached = true;
                mCache = result.bitmap;
                mUpdateMonitor.setHasLockscreenWallpaper(result.bitmap != null);
                mBar.updateMediaMetaData(true, /* metaDataChanged */
                true);
            }
            mLoader = null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : UserHandle(android.os.UserHandle) AsyncTask(android.os.AsyncTask)

Example 18 with AsyncTask

use of android.os.AsyncTask in project platform_frameworks_base by android.

the class ContentProvider method openPipeHelper.

/**
     * A helper function for implementing {@link #openTypedAssetFile}, for
     * creating a data pipe and background thread allowing you to stream
     * generated data back to the client.  This function returns a new
     * ParcelFileDescriptor that should be returned to the caller (the caller
     * is responsible for closing it).
     *
     * @param uri The URI whose data is to be written.
     * @param mimeType The desired type of data to be written.
     * @param opts Options supplied by caller.
     * @param args Your own custom arguments.
     * @param func Interface implementing the function that will actually
     * stream the data.
     * @return Returns a new ParcelFileDescriptor holding the read side of
     * the pipe.  This should be returned to the caller for reading; the caller
     * is responsible for closing it when done.
     */
@NonNull
public <T> ParcelFileDescriptor openPipeHelper(@NonNull final Uri uri, @NonNull final String mimeType, @Nullable final Bundle opts, @Nullable final T args, @NonNull final PipeDataWriter<T> func) throws FileNotFoundException {
    try {
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {

            @Override
            protected Object doInBackground(Object... params) {
                func.writeDataToPipe(fds[1], uri, mimeType, opts, args);
                try {
                    fds[1].close();
                } catch (IOException e) {
                    Log.w(TAG, "Failure closing pipe", e);
                }
                return null;
            }
        };
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[]) null);
        return fds[0];
    } catch (IOException e) {
        throw new FileNotFoundException("failure making pipe");
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) AsyncTask(android.os.AsyncTask) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NonNull(android.annotation.NonNull)

Example 19 with AsyncTask

use of android.os.AsyncTask in project platform_frameworks_base by android.

the class LockPatternChecker method verifyPattern.

/**
     * Verify a pattern asynchronously.
     *
     * @param utils The LockPatternUtils instance to use.
     * @param pattern The pattern to check.
     * @param challenge The challenge to verify against the pattern.
     * @param userId The user to check against the pattern.
     * @param callback The callback to be invoked with the verification result.
     */
public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils, final List<LockPatternView.Cell> pattern, final long challenge, final int userId, final OnVerifyCallback callback) {
    AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {

        private int mThrottleTimeout;

        private List<LockPatternView.Cell> patternCopy;

        @Override
        protected void onPreExecute() {
            // Make a copy of the pattern to prevent race conditions.
            // No need to clone the individual cells because they are immutable.
            patternCopy = new ArrayList(pattern);
        }

        @Override
        protected byte[] doInBackground(Void... args) {
            try {
                return utils.verifyPattern(patternCopy, challenge, userId);
            } catch (RequestThrottledException ex) {
                mThrottleTimeout = ex.getTimeoutMs();
                return null;
            }
        }

        @Override
        protected void onPostExecute(byte[] result) {
            callback.onVerified(result, mThrottleTimeout);
        }
    };
    task.execute();
    return task;
}
Also used : AsyncTask(android.os.AsyncTask) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) RequestThrottledException(com.android.internal.widget.LockPatternUtils.RequestThrottledException)

Example 20 with AsyncTask

use of android.os.AsyncTask in project JamsMusicPlayer by psaravan.

the class EqualizerActivity method buildLoadPresetDialog.

/**
	 * Builds the "Load Preset" dialog. Does not call the show() method, so this 
	 * should be done manually after calling this method.
	 * 
	 * @return A fully built AlertDialog reference.
	 */
private AlertDialog buildLoadPresetDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //Get a cursor with the list of EQ presets.
    final Cursor cursor = mApp.getDBAccessHelper().getAllEQPresets();
    //Set the dialog title.
    builder.setTitle(R.string.load_preset);
    builder.setCursor(cursor, new DialogInterface.OnClickListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onClick(DialogInterface dialog, int which) {
            cursor.moveToPosition(which);
            //Close the dialog.
            dialog.dismiss();
            //Pass on the equalizer values to the appropriate fragment.
            fiftyHertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_50_HZ));
            oneThirtyHertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_130_HZ));
            threeTwentyHertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_320_HZ));
            eightHundredHertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_800_HZ));
            twoKilohertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_2000_HZ));
            fiveKilohertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_5000_HZ));
            twelvePointFiveKilohertzLevel = cursor.getInt(cursor.getColumnIndex(DBAccessHelper.EQ_12500_HZ));
            virtualizerLevel = cursor.getShort(cursor.getColumnIndex(DBAccessHelper.VIRTUALIZER));
            bassBoostLevel = cursor.getShort(cursor.getColumnIndex(DBAccessHelper.BASS_BOOST));
            reverbSetting = cursor.getShort(cursor.getColumnIndex(DBAccessHelper.REVERB));
            //Save the new equalizer settings to the DB.
            @SuppressWarnings({ "rawtypes" }) AsyncTask task = new AsyncTask() {

                @Override
                protected Object doInBackground(Object... arg0) {
                    setEQValuesForSong(mApp.getService().getCurrentSong().getId());
                    return null;
                }

                @Override
                public void onPostExecute(Object result) {
                    super.onPostExecute(result);
                    //Reinitialize the UI elements to apply the new equalizer settings.
                    new AsyncInitSlidersTask().execute();
                }
            };
            task.execute();
            if (cursor != null)
                cursor.close();
        }
    }, DBAccessHelper.PRESET_NAME);
    return builder.create();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) AsyncTask(android.os.AsyncTask) Cursor(android.database.Cursor) Paint(android.graphics.Paint)

Aggregations

AsyncTask (android.os.AsyncTask)395 IOException (java.io.IOException)188 InputStream (java.io.InputStream)159 URL (java.net.URL)159 HttpURLConnection (java.net.HttpURLConnection)158 ExecutionException (java.util.concurrent.ExecutionException)158 Gson (com.google.gson.Gson)155 Message (com.remswork.project.alice.model.support.Message)153 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)102 ArrayList (java.util.ArrayList)93 View (android.view.View)54 Intent (android.content.Intent)52 TextView (android.widget.TextView)52 JSONException (org.json.JSONException)51 JSONArray (org.json.JSONArray)50 DialogInterface (android.content.DialogInterface)40 OutputStream (java.io.OutputStream)37 BufferedWriter (java.io.BufferedWriter)35 OutputStreamWriter (java.io.OutputStreamWriter)35 ImageView (android.widget.ImageView)33