List of usage examples for android.os Handler obtainMessage
public final Message obtainMessage(int what, Object obj)
From source file:it.fdev.utils.DrawableManager.java
public void fetchDrawableOnThread(final String urlString, final DrawableManagerListener listener) { if (drawableMap.containsKey(urlString)) { listener.onLoadingComplete(drawableMap.get(urlString)); }// w w w .jav a 2s.c o m final Handler handler = new Handler() { @Override public void handleMessage(Message message) { listener.onLoadingComplete((Drawable) message.obj); } }; final Handler errorHandler = new Handler() { @Override public void handleMessage(Message message) { listener.onLoadingError(); } }; Thread thread = new Thread() { @Override public void run() { //TODO : set imageView to a "pending" image Drawable drawable = fetchDrawable(urlString); if (drawable != null) { Message message = handler.obtainMessage(1, drawable); handler.sendMessage(message); } else { Message message = new Message(); errorHandler.sendMessage(message); } } }; thread.start(); }
From source file:com.binroot.fatpita.BitmapManager2.java
public void fetchBitmapOnThread(final String urlString, final ImageView imageView, final ProgressBar indeterminateProgressBar, final Activity act, final int sample) { SoftReference<Bitmap> ref = mCache.get(urlString); if (ref != null && ref.get() != null) { imageView.setImageBitmap(ref.get()); return;//www . j a va2 s . co m } final Runnable progressBarShow = new Runnable() { public void run() { if (indeterminateProgressBar != null) { imageView.setVisibility(View.GONE); indeterminateProgressBar.setVisibility(View.VISIBLE); } } }; final Runnable progressBarHide = new Runnable() { public void run() { if (indeterminateProgressBar != null) { indeterminateProgressBar.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); } } }; final Handler handler = new Handler() { @Override public void handleMessage(Message message) { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarHide); imageView.setImageBitmap((Bitmap) message.obj); } }; Thread thread = new Thread() { @Override public void run() { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarShow); Bitmap bitmap = fetchBitmap(urlString, sample); Message message = handler.obtainMessage(1, bitmap); handler.sendMessage(message); } }; thread.start(); }
From source file:com.nachiket.titan.LibraryPagerAdapter.java
/** * Set the sort mode for the current adapter. Current adapter must be a * MediaAdapter. Saves this sort mode to preferences and updates the list * associated with the adapter to display the new sort mode. * * @param mode The sort mode. See {@link MediaAdapter#setSortMode(int)} for * details.//w w w . j a v a 2 s. c o m */ public void setSortMode(int mode) { MediaAdapter adapter = (MediaAdapter) mCurrentAdapter; if (mode == adapter.getSortMode()) return; adapter.setSortMode(mode); requestRequery(adapter); // Force a new FastScroller to be created so the scroll sections // are updated. ListView view = mLists[mTabOrder[mCurrentPage]]; view.setFastScrollEnabled(false); enableFastScroll(view); Handler handler = mWorkerHandler; handler.sendMessage(handler.obtainMessage(MSG_SAVE_SORT, adapter)); }
From source file:com.azcltd.android.test.kolesov.DrawableManager.java
public void fetchDrawableOnThread(final String urlString, final ImageView imageView, final boolean isThumbnail, final boolean forceUpdate) { if (drawableMap.containsKey(urlString) && !forceUpdate) { Drawable image = drawableMap.get(urlString); SetDrawableToImageView(image, imageView, isThumbnail); return;//from w w w. j a v a2s . c om } final Handler handler = new Handler() { @Override public void handleMessage(Message message) { DrawableWithFlag obj = (DrawableWithFlag) message.obj; Drawable image = obj.image; boolean flag = obj.isNeedThumbnailed; SetDrawableToImageView(image, imageView, flag); } }; Thread thread = new Thread() { @Override public void run() { //setPriority(3); Drawable drawable = fetchDrawable(urlString, forceUpdate); if (drawable != null) { Message message = handler.obtainMessage(1, new DrawableWithFlag(drawable, isThumbnail)); handler.sendMessage(message); // try{ // //sleep(100); // } // catch(Exception ex) { // int i =0; // } } } }; thread.start(); }
From source file:com.binroot.fatpita.BitmapManager.java
public void fetchBitmapOnThread(final String urlString, final ImageView imageView, final ProgressBar indeterminateProgressBar, final Activity act, final boolean saveToHistory) { SoftReference<Bitmap> ref = mCache.get(urlString); if (ref != null && ref.get() != null) { imageView.setImageBitmap(ref.get()); return;// w w w. jav a2 s. c om } final Runnable progressBarShow = new Runnable() { public void run() { if (indeterminateProgressBar != null) { imageView.setVisibility(View.GONE); indeterminateProgressBar.setVisibility(View.VISIBLE); } } }; final Runnable progressBarHide = new Runnable() { public void run() { if (indeterminateProgressBar != null) { indeterminateProgressBar.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); } } }; final Handler handler = new Handler() { @Override public void handleMessage(Message message) { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarHide); imageView.setImageBitmap((Bitmap) message.obj); } }; Thread thread = new Thread() { @Override public void run() { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarShow); Bitmap bitmap = fetchBitmap(urlString, saveToHistory); Message message = handler.obtainMessage(1, bitmap); handler.sendMessage(message); } }; thread.start(); }
From source file:com.cm.beer.util.ContentManager.java
/** * //from w ww . j a va 2s . c o m * @param urlString * @param imageView */ public void fetchContentOnThread(final String urlString, final Handler handler) { if (mContentCache.containsKey(urlString)) { SoftReference<String> softReference = mContentCache.get(urlString); if ((softReference == null) || (softReference.get() == null)) { mContentCache.remove(urlString); if (Logger.isLogEnabled()) Logger.log("fetchContentOnThread():Soft Reference has been Garbage Collected:" + urlString); } else { String jsonStr = softReference.get(); if (Logger.isLogEnabled()) Logger.log(urlString + "=>" + jsonStr); Message message = handler.obtainMessage(1, jsonStr); handler.sendMessage(message); return; } } Thread thread = new Thread() { @Override public void run() { while (mLockCache.containsKey(urlString)) { if (Logger.isLogEnabled()) Logger.log("URI download request in progress:" + urlString); try { Thread.sleep(1000L); } catch (InterruptedException e) { Log.e(this.getClass().getName(), e.getMessage()); } } String content = fetchContent(urlString); Message message = handler.obtainMessage(1, content); handler.sendMessage(message); } }; thread.start(); }
From source file:org.jinzora.util.DrawableManager.java
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) { if (urlString == null) { if (DBG)/* www .j a v a 2 s . c o m*/ Log.d(TAG, "Null image url."); synchronized (pendingViews) { pendingViews.remove(imageView); } return; } synchronized (pendingViews) { pendingViews.put(imageView, urlString); } if (drawableMap.containsKey(urlString)) { SoftReference<Drawable> reference = drawableMap.get(urlString); Drawable drawable = reference.get(); if (drawable != null) { if (DBG) Log.d(TAG, "Image cached in memory."); imageView.setImageDrawable(drawable); return; } else { if (DBG) Log.d(TAG, "Soft reference cleared."); drawableMap.remove(urlString); } } File localFile = getLocalFile(urlString); if (localFile != null && localFile.exists()) { if (DBG) Log.d(TAG, "Image from disk."); imageView.setImageDrawable(fetchDrawable(urlString)); return; } final Handler handler = new Handler() { @Override public void handleMessage(Message message) { synchronized (pendingViews) { String latest = pendingViews.get(imageView); if (urlString.equals(latest)) { imageView.setImageDrawable((Drawable) message.obj); } } } }; new Thread() { @Override public void run() { Drawable drawable = fetchDrawable(urlString); Message message = handler.obtainMessage(1, drawable); handler.sendMessage(message); } }.start(); }
From source file:mp.teardrop.LibraryPagerAdapter.java
/** * Schedule a query to be run for the given adapter on the worker thread. * * @param adapter The adapter to run the query for. *///from www. j a v a2 s . c om private void postRunQuery(LibraryAdapter adapter) { mRequeryNeeded[adapter != null ? adapter.getMediaType() : MediaUtils.TYPE_UNIFIED] = false; Handler handler = mWorkerHandler; handler.removeMessages(MSG_RUN_QUERY, adapter); handler.sendMessage(handler.obtainMessage(MSG_RUN_QUERY, adapter)); }
From source file:com.scanvine.android.util.SVDownloadManager.java
private void fetchUrlOnThread(final Context context, final String urlString, final Handler handler, final boolean isImage) { if (urlString == null || urlString.trim().length() == 0) return;/*from ww w.java 2 s . c o m*/ Thread thread = new Thread() { @Override public void run() { boolean success = false; File localFile = cacheFor(context, urlString); if (localFile.exists()) success = true; else { Log.i("" + this, "Fetching: " + urlString); success = isImage ? fetchDrawable(context, urlString, localFile) : fetchFile(context, urlString, localFile); } if (success) { Message message = handler.obtainMessage(1, localFile); handler.sendMessage(message); } } }; thread.start(); }
From source file:de.unwesen.web.DownloadCache.java
public void fetch(List<? extends CacheItem> items, Handler handler) { if (null == items || 0 == items.size() || null == handler) { return;/*from w ww . j a va2s . c o m*/ } int added = 0; for (CacheItem item : items) { // Check whether file already exists. String filename = get(item.mDownloadUri); if (null != filename) { item.mFilename = filename; handler.obtainMessage(CACHE_SUCCESS, item).sendToTarget(); continue; } // Otherwise add the item to the fetcher queue. item.mHandler = handler; mFetcherQueue.add(item); ++added; } if (0 != added) { mFetcher.interrupt(); } }