Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //этот класс спизжен из интернетов
- public class SettableFuture<V> implements Future<V> {
- private final CountDownLatch latch = new CountDownLatch(1);
- private final AtomicBoolean isSet = new AtomicBoolean();
- private V slot;
- @Override
- public boolean cancel(boolean mayInterruptIfRunning) {
- return this.cancel(mayInterruptIfRunning);
- }
- @Override
- public boolean isCancelled() {
- return this.isCancelled();
- }
- @Override
- public boolean isDone() {
- return this.isDone();
- }
- public V get() throws InterruptedException, ExecutionException {
- latch.await();
- return slot;
- }
- @Override
- public V get(long timeout, @NonNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
- latch.await(timeout, unit);
- return slot;
- }
- public void set(V value) {
- if (!isSet.get() && isSet.compareAndSet(false, true)) {
- slot = value;
- latch.countDown();
- }
- }
- }
- public SettableFuture<Bitmap> getImage(int id) {
- SettableFuture<Bitmap> fut = null;
- //imagesCache переменная типа LruChache
- //Проверяем есть ли в кеше изображение
- //Если нет, то
- if (imagesCache.get(id) == null) {
- //Отправляем запрос на передачу изображения
- SessionRegistry.getInstance().get().send(new GetIpPhotoByIdRequest(id));
- //добавляем пустое будущее
- imagesCache.put(id, new SettableFuture<Bitmap>());
- } else {
- //Иначе возвращаем изображение из кеша
- fut = imagesCache.get(id);
- }
- //Возвращаем будущее на битмап.
- //Битмап появится когда придёт пакет с ним,
- //либо не появится если произойдёт таймаут
- return fut;
- }
- public void addImage(final int id, final byte[] image) {
- //Запихиваем изображение в кеш
- SettableFuture<Bitmap> future = imagesCache.get(id);
- Bitmap bitmap = null;
- if (image != null) {
- try {
- bitmap = Glide.with(context)
- .load(image)
- .asBitmap()
- .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
- .get();
- if (bitmap != null) {
- future.set(bitmap);
- } else {
- //вот в этом месте пропадает курсор при отладке
- future.set(defaultImage);
- }
- } catch (InterruptedException | ExecutionException e) {
- FileLog.getInstance().e(ImageReceiver.class.getName(), e);
- future.set(defaultImage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement