Search in sources :

Example 1 with AsyncTask

use of android.os.AsyncTask in project SeriesGuide by UweTrottmann.

the class ShortcutUtils method createShortcut.

/**
     * Adds a shortcut from the overview page of the given show to the Home screen.
     *
     * @param showTitle The name of the shortcut.
     * @param posterPath A TVDb show poster path.
     * @param showTvdbId The TVDb ID of the show.
     */
public static void createShortcut(Context localContext, final String showTitle, final String posterPath, final int showTvdbId) {
    // do not pass activity reference to AsyncTask, activity might leak if destroyed
    final Context context = localContext.getApplicationContext();
    AsyncTask<Void, Void, Intent> shortCutTask = new AsyncTask<Void, Void, Intent>() {

        @Override
        protected Intent doInBackground(Void... unused) {
            // Try to get the show poster
            Bitmap posterBitmap = null;
            try {
                final String posterUrl = TvdbImageTools.smallSizeUrl(posterPath);
                if (posterUrl != null) {
                    posterBitmap = Picasso.with(context).load(posterUrl).centerCrop().memoryPolicy(MemoryPolicy.NO_STORE).networkPolicy(NetworkPolicy.NO_STORE).resizeDimen(R.dimen.show_poster_width_default, R.dimen.show_poster_height_default).transform(new RoundedCornerTransformation(posterUrl, 10f)).get();
                }
            } catch (IOException e) {
                Timber.e(e, "Could not load show poster for shortcut %s", posterPath);
                posterBitmap = null;
            }
            // Intent used when the icon is touched
            final Intent shortcutIntent = new Intent(context, OverviewActivity.class);
            shortcutIntent.putExtra(OverviewActivity.EXTRA_INT_SHOW_TVDBID, showTvdbId);
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Intent that actually creates the shortcut
            final Intent intent = new Intent();
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, showTitle);
            if (posterBitmap == null) {
                // Fall back to the app icon
                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
            } else {
                intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, posterBitmap);
            }
            intent.setAction(ACTION_INSTALL_SHORTCUT);
            context.sendBroadcast(intent);
            return null;
        }
    };
    // Do all the above async
    AsyncTaskCompat.executeParallel(shortCutTask);
}
Also used : Context(android.content.Context) Bitmap(android.graphics.Bitmap) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) IOException(java.io.IOException)

Example 2 with AsyncTask

use of android.os.AsyncTask in project Signal-Android by WhisperSystems.

the class ConversationActivity method onRecorderCanceled.

@Override
public void onRecorderCanceled() {
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(50);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    ListenableFuture<Pair<Uri, Long>> future = audioRecorder.stopRecording();
    future.addListener(new ListenableFuture.Listener<Pair<Uri, Long>>() {

        @Override
        public void onSuccess(final Pair<Uri, Long> result) {
            new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    PersistentBlobProvider.getInstance(ConversationActivity.this).delete(result.first);
                    return null;
                }
            }.execute();
        }

        @Override
        public void onFailure(ExecutionException e) {
        }
    });
}
Also used : AsyncTask(android.os.AsyncTask) ListenableFuture(org.thoughtcrime.securesms.util.concurrent.ListenableFuture) Vibrator(android.os.Vibrator) ExecutionException(java.util.concurrent.ExecutionException) Uri(android.net.Uri) Pair(android.util.Pair)

Example 3 with AsyncTask

use of android.os.AsyncTask in project MVCHelper by LuckyJayce.

the class MovieAmountTask2 method execute.

@Override
public RequestHandle execute(final ResponseSender<MovieAmount> sender) throws Exception {
    final AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
                Random random = new Random();
                int commentCount = random.nextInt(100);
                int playCount = random.nextInt(1000) + 10;
                long updateTime = System.currentTimeMillis();
                sender.sendData(new MovieAmount(name, commentCount, playCount, name + " " + DateFormat.format("dd kk:mm:ss", updateTime)));
            } catch (Exception e) {
                e.printStackTrace();
                sender.sendError(e);
            }
            return null;
        }
    };
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        asyncTask.execute();
    }
    return new RequestHandle() {

        @Override
        public void cancle() {
            asyncTask.cancel(true);
        }

        @Override
        public boolean isRunning() {
            return false;
        }
    };
}
Also used : Random(java.util.Random) MovieAmount(com.shizhefei.test.models.enties.MovieAmount) RequestHandle(com.shizhefei.mvc.RequestHandle) AsyncTask(android.os.AsyncTask) IAsyncTask(com.shizhefei.task.IAsyncTask)

Example 4 with AsyncTask

use of android.os.AsyncTask in project android_frameworks_base by ParanoidAndroid.

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.
     */
public <T> ParcelFileDescriptor openPipeHelper(final Uri uri, final String mimeType, final Bundle opts, final T args, 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)

Example 5 with AsyncTask

use of android.os.AsyncTask in project android_frameworks_base by ResurrectionRemix.

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)

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