Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //этот класс спизжен из интернетов
  2. public class SettableFuture<V> implements Future<V> {
  3.     private final CountDownLatch latch = new CountDownLatch(1);
  4.     private final AtomicBoolean isSet = new AtomicBoolean();
  5.     private V slot;
  6.  
  7.     @Override
  8.     public boolean cancel(boolean mayInterruptIfRunning) {
  9.         return this.cancel(mayInterruptIfRunning);
  10.     }
  11.  
  12.     @Override
  13.     public boolean isCancelled() {
  14.         return this.isCancelled();
  15.     }
  16.  
  17.     @Override
  18.     public boolean isDone() {
  19.         return this.isDone();
  20.     }
  21.  
  22.     public V get() throws InterruptedException, ExecutionException {
  23.         latch.await();
  24.         return slot;
  25.     }
  26.  
  27.     @Override
  28.     public V get(long timeout, @NonNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
  29.         latch.await(timeout, unit);
  30.         return slot;
  31.     }
  32.  
  33.     public void set(V value) {
  34.         if (!isSet.get() && isSet.compareAndSet(false, true)) {
  35.             slot = value;
  36.             latch.countDown();
  37.         }
  38.     }
  39. }    
  40.  
  41. public SettableFuture<Bitmap> getImage(int id) {
  42.         SettableFuture<Bitmap> fut = null;
  43.  
  44.         //imagesCache переменная типа LruChache
  45.         //Проверяем есть ли в кеше изображение
  46.         //Если нет, то
  47.         if (imagesCache.get(id) == null) {
  48.             //Отправляем запрос на передачу изображения
  49.             SessionRegistry.getInstance().get().send(new GetIpPhotoByIdRequest(id));
  50.  
  51.             //добавляем пустое будущее
  52.             imagesCache.put(id, new SettableFuture<Bitmap>());
  53.         } else {
  54.             //Иначе возвращаем изображение из кеша
  55.             fut = imagesCache.get(id);
  56.         }
  57.  
  58.         //Возвращаем будущее на битмап.
  59.         //Битмап появится когда придёт пакет с ним,
  60.         //либо не появится если произойдёт таймаут
  61.         return fut;
  62.     }
  63.  
  64.     public void addImage(final int id, final byte[] image) {
  65.         //Запихиваем изображение в кеш
  66.         SettableFuture<Bitmap> future = imagesCache.get(id);
  67.         Bitmap bitmap = null;
  68.  
  69.         if (image != null) {
  70.             try {
  71.                 bitmap = Glide.with(context)
  72.                         .load(image)
  73.                         .asBitmap()
  74.                         .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
  75.                         .get();
  76.  
  77.                 if (bitmap != null) {
  78.                     future.set(bitmap);
  79.                 } else {
  80.                     //вот в этом месте пропадает курсор при отладке
  81.                     future.set(defaultImage);
  82.                 }
  83.             } catch (InterruptedException | ExecutionException e) {
  84.                 FileLog.getInstance().e(ImageReceiver.class.getName(), e);
  85.                 future.set(defaultImage);
  86.             }
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement