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);
}
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) {
}
});
}
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;
}
};
}
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");
}
}
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;
}
Aggregations