Java Code Examples for com.facebook.datasource.DataSource#close()

The following examples show how to use com.facebook.datasource.DataSource#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrescoControllerImpl.java    From fresco with MIT License 6 votes vote down vote up
@Override
public void onNewResult(
    FrescoState frescoState, DataSource<CloseableReference<CloseableImage>> dataSource) {
  if (dataSource != null && !dataSource.isClosed()) {
    final boolean shouldClose =
        mFrescoContext.getExperiments().closeDatasourceOnNewResult() && dataSource.isFinished();
    final CloseableReference<CloseableImage> result = dataSource.getResult();
    try {
      frescoState.setImageFetched(true);
      if (frescoState.isAttached()) {
        displayResultOrError(frescoState, result, false, dataSource);
      }
    } finally {
      CloseableReference.closeSafely(result);
      if (shouldClose) {
        dataSource.close();
      }
    }
  }
}
 
Example 2
Source File: FrescoStateImpl.java    From fresco with MIT License 6 votes vote down vote up
@Override
@UiThread
public void release() {
  DataSource dataSource;
  if ((dataSource = mMainFetchDatasource) != null) {
    dataSource.close();
  }
  if ((dataSource = mPrefetchDatasource) != null) {
    dataSource.close();
  }

  CloseableReference.closeSafely(mCachedImage);

  if (mFrescoContext.getExperiments().resetState()) {
    mPrefetchDatasource = null;
    mMainFetchDatasource = null;
    mActualImageWrapper = null;
  }
}
 
Example 3
Source File: DraweeSpan.java    From drawee-text-view with Apache License 2.0 6 votes vote down vote up
private void onFailureInternal(String id,
        DataSource<CloseableReference<CloseableImage>> dataSource,
        Throwable throwable, boolean isFinished) {
    if (FLog.isLoggable(Log.WARN)) {
        FLog.w(DraweeSpan.class, id + " load failure", throwable);
    }
    // ignored this result
    if (!getId().equals(id)
            || dataSource != mDataSource
            || !mIsRequestSubmitted) {
        dataSource.close();
        return;
    }
    if (isFinished) {
        mDataSource = null;
        // Set the previously available image if available.
        setDrawableInner(mDrawable);
    }
}
 
Example 4
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 6 votes vote down vote up
@OnBind
static void onBind(
    ComponentContext c,
    final FrescoDrawable2 frescoDrawable,
    @Prop(optional = true) final @Nullable Object callerContext,
    @Prop(optional = true) final @Nullable ImageListener imageListener,
    @CachedValue VitoImageRequest imageRequest,
    @FromPrepare DataSource<Void> prefetchDataSource,
    @FromBoundsDefined Rect viewportDimensions) {
  // We fetch in both mount and bind in case an unbind event triggered a delayed release.
  // We'll only trigger an actual fetch if needed. Most of the time, this will be a no-op.
  FrescoVitoProvider.getController()
      .fetch(frescoDrawable, imageRequest, callerContext, imageListener, viewportDimensions);
  if (prefetchDataSource != null) {
    prefetchDataSource.close();
  }
}
 
Example 5
Source File: AbstractDraweeController.java    From fresco with MIT License 5 votes vote down vote up
private void onProgressUpdateInternal(
    String id, DataSource<T> dataSource, float progress, boolean isFinished) {
  // ignore late callbacks (data source that failed is not the one we expected)
  if (!isExpectedDataSource(id, dataSource)) {
    logMessageAndFailure("ignore_old_datasource @ onProgress", null);
    dataSource.close();
    return;
  }
  if (!isFinished) {
    mSettableDraweeHierarchy.setProgress(progress, false);
  }
}
 
Example 6
Source File: ImageLoaderModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
@ReactMethod
public void abortRequest(final int requestId) {
  DataSource<Void> request = removeRequest(requestId);
  if (request != null) {
    request.close();
  }
}
 
Example 7
Source File: AbstractDraweeController.java    From fresco with MIT License 5 votes vote down vote up
private void onFailureInternal(
    String id, DataSource<T> dataSource, Throwable throwable, boolean isFinished) {
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.beginSection("AbstractDraweeController#onFailureInternal");
  }
  // ignore late callbacks (data source that failed is not the one we expected)
  if (!isExpectedDataSource(id, dataSource)) {
    logMessageAndFailure("ignore_old_datasource @ onFailure", throwable);
    dataSource.close();
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
    return;
  }
  mEventTracker.recordEvent(
      isFinished ? Event.ON_DATASOURCE_FAILURE : Event.ON_DATASOURCE_FAILURE_INT);
  // fail only if the data source is finished
  if (isFinished) {
    logMessageAndFailure("final_failed @ onFailure", throwable);
    mDataSource = null;
    mHasFetchFailed = true;
    // Set the previously available image if available.
    if (mRetainImageOnFailure && mDrawable != null) {
      mSettableDraweeHierarchy.setImage(mDrawable, 1f, true);
    } else if (shouldRetryOnTap()) {
      mSettableDraweeHierarchy.setRetry(throwable);
    } else {
      mSettableDraweeHierarchy.setFailure(throwable);
    }
    reportFailure(throwable, dataSource);
    // IMPORTANT: do not execute any instance-specific code after this point
  } else {
    logMessageAndFailure("intermediate_failed @ onFailure", throwable);
    reportIntermediateFailure(throwable);
    // IMPORTANT: do not execute any instance-specific code after this point
  }
  if (FrescoSystrace.isTracing()) {
    FrescoSystrace.endSection();
  }
}
 
Example 8
Source File: ListDataSource.java    From fresco with MIT License 5 votes vote down vote up
@Override
public boolean close() {
  if (!super.close()) {
    return false;
  }
  for (DataSource<?> dataSource : mDataSources) {
    dataSource.close();
  }
  return true;
}
 
Example 9
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 5 votes vote down vote up
@OnUnmount
static void onUnmount(
    ComponentContext c,
    FrescoDrawable2 frescoDrawable,
    @FromPrepare DataSource<Void> prefetchDataSource) {
  FrescoVitoProvider.getController().release(frescoDrawable);
  if (prefetchDataSource != null) {
    prefetchDataSource.close();
  }
}
 
Example 10
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 5 votes vote down vote up
@OnUnbind
static void onUnbind(
    ComponentContext c,
    FrescoDrawable2 frescoDrawable,
    @FromPrepare DataSource<Void> prefetchDataSource) {
  FrescoVitoProvider.getController().releaseDelayed(frescoDrawable);
  if (prefetchDataSource != null) {
    prefetchDataSource.close();
  }
}
 
Example 11
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 5 votes vote down vote up
@OnMount
static void onMount(
    ComponentContext c,
    final FrescoDrawable2 frescoDrawable,
    @Prop(optional = true) final @Nullable Object callerContext,
    @Prop(optional = true) final @Nullable ImageListener imageListener,
    @CachedValue VitoImageRequest imageRequest,
    @FromPrepare DataSource<Void> prefetchDataSource,
    @FromBoundsDefined Rect viewportDimensions) {
  FrescoVitoProvider.getController()
      .fetch(frescoDrawable, imageRequest, callerContext, imageListener, viewportDimensions);
  if (prefetchDataSource != null) {
    prefetchDataSource.close();
  }
}
 
Example 12
Source File: DraweeSpan.java    From drawee-text-view with Apache License 2.0 5 votes vote down vote up
private void onNewResultInternal(String id,
        DataSource<CloseableReference<CloseableImage>> dataSource,
        CloseableReference<CloseableImage> result,
        boolean isFinished) {
    // ignored this result
    if (!getId().equals(id)
            || dataSource != mDataSource
            || !mIsRequestSubmitted) {
        CloseableReference.closeSafely(result);
        dataSource.close();
        return;
    }
    Drawable drawable;
    try {
        drawable = createDrawable(result);
    } catch (Exception exception) {
        CloseableReference.closeSafely(result);
        onFailureInternal(id, dataSource, exception, isFinished);
        return;
    }
    CloseableReference previousImage = mFetchedImage;
    Drawable previousDrawable = mDrawable;
    mFetchedImage = result;
    try {
        // set the new image
        if (isFinished) {
            mDataSource = null;
            setImage(drawable);
        }
    } finally {
        if (previousDrawable != null && previousDrawable != drawable) {
            releaseDrawable(previousDrawable);
        }
        if (previousImage != null && previousImage != result) {
            CloseableReference.closeSafely(previousImage);
        }
    }
}
 
Example 13
Source File: AbstractDraweeController.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private void onFailureInternal(
    String id,
    DataSource<T> dataSource,
    Throwable throwable,
    boolean isFinished) {
  // ignore late callbacks (data source that failed is not the one we expected)
  if (!isExpectedDataSource(id, dataSource)) {
    logMessageAndFailure("ignore_old_datasource @ onFailure", throwable);
    dataSource.close();
    return;
  }
  mEventTracker.recordEvent(
      isFinished ? Event.ON_DATASOURCE_FAILURE : Event.ON_DATASOURCE_FAILURE_INT);
  // fail only if the data source is finished
  if (isFinished) {
    logMessageAndFailure("final_failed @ onFailure", throwable);
    mDataSource = null;
    mHasFetchFailed = true;
    if (shouldRetryOnTap()) {
      mSettableDraweeHierarchy.setRetry(throwable);
    } else {
      mSettableDraweeHierarchy.setFailure(throwable);
    }
    getControllerListener().onFailure(mId, throwable);
    // IMPORTANT: do not execute any instance-specific code after this point
  } else {
    logMessageAndFailure("intermediate_failed @ onFailure", throwable);
    getControllerListener().onIntermediateImageFailed(mId, throwable);
    // IMPORTANT: do not execute any instance-specific code after this point
  }
}
 
Example 14
Source File: ListDataSource.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean close() {
  if (!super.close()) {
    return false;
  }
  for (DataSource<?> dataSource : mDataSources) {
    dataSource.close();
  }
  return true;
}
 
Example 15
Source File: MyBaseBitmapDataSubscriber.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
@Override
public void onNewResult(DataSource<CloseableReference<CloseableImage>> dataSource) {
    // isFinished() should be checked before calling onNewResultImpl(), otherwise
    // there would be a race condition: the final data source result might be ready before
    // we call isFinished() here, which would lead to the loss of the final result
    // (because of an early dataSource.close() call).
    final boolean shouldClose = dataSource.isFinished();
    try {
        onNewResultImpl(dataSource);
    } finally {
        if (shouldClose) {
           dataSource.close();
        }
    }
}
 
Example 16
Source File: ImageLoaderModule.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public void onHostDestroy() {
  // cancel all requests
  synchronized (mEnqueuedRequestMonitor) {
    for (int i = 0, size = mEnqueuedRequests.size(); i < size; i++) {
      @Nullable DataSource<Void> enqueuedRequest = mEnqueuedRequests.valueAt(i);
      if (enqueuedRequest != null) {
        enqueuedRequest.close();
      }
    }
    mEnqueuedRequests.clear();
  }
}
 
Example 17
Source File: AbstractDraweeController.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private void onNewResultInternal(
    String id,
    DataSource<T> dataSource,
    @Nullable T image,
    boolean isFinished,
    boolean wasImmediate) {
  // ignore late callbacks (data source that returned the new result is not the one we expected)
  if (!isExpectedDataSource(id, dataSource)) {
    logMessageAndImage("ignore_old_datasource @ onNewResult", image);
    releaseImage(image);
    dataSource.close();
    return;
  }
  mEventTracker.recordEvent(
      isFinished ? Event.ON_DATASOURCE_RESULT : Event.ON_DATASOURCE_RESULT_INT);
  // create drawable
  Drawable drawable;
  try {
    drawable = createDrawable(image);
  } catch (Exception exception) {
    logMessageAndImage("drawable_failed @ onNewResult", image);
    releaseImage(image);
    onFailureInternal(id, dataSource, exception, isFinished);
    return;
  }
  T previousImage = mFetchedImage;
  Drawable previousDrawable = mDrawable;
  mFetchedImage = image;
  mDrawable = drawable;
  try {
    // set the new image
    if (isFinished) {
      logMessageAndImage("set_final_result @ onNewResult", image);
      mDataSource = null;
      mSettableDraweeHierarchy.setImage(drawable, wasImmediate, 100);
      getControllerListener().onFinalImageSet(id, getImageInfo(image), getAnimatable());
      // IMPORTANT: do not execute any instance-specific code after this point
    } else {
      logMessageAndImage("set_intermediate_result @ onNewResult", image);
      int progress = getProgress(dataSource, image);
      mSettableDraweeHierarchy.setImage(drawable, wasImmediate, progress);
      getControllerListener().onIntermediateImageSet(id, getImageInfo(image));
      // IMPORTANT: do not execute any instance-specific code after this point
    }
  } finally {
    if (previousDrawable != null && previousDrawable != drawable) {
      releaseDrawable(previousDrawable);
    }
    if (previousImage != null && previousImage != image) {
      logMessageAndImage("release_previous_result @ onNewResult", previousImage);
      releaseImage(previousImage);
    }
  }
}
 
Example 18
Source File: AbstractDraweeController.java    From fresco with MIT License 4 votes vote down vote up
private void onNewResultInternal(
    String id,
    DataSource<T> dataSource,
    @Nullable T image,
    float progress,
    boolean isFinished,
    boolean wasImmediate,
    boolean deliverTempResult) {
  try {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.beginSection("AbstractDraweeController#onNewResultInternal");
    }
    // ignore late callbacks (data source that returned the new result is not the one we expected)
    if (!isExpectedDataSource(id, dataSource)) {
      logMessageAndImage("ignore_old_datasource @ onNewResult", image);
      releaseImage(image);
      dataSource.close();
      return;
    }
    mEventTracker.recordEvent(
        isFinished ? Event.ON_DATASOURCE_RESULT : Event.ON_DATASOURCE_RESULT_INT);
    // create drawable
    Drawable drawable;
    try {
      drawable = createDrawable(image);
    } catch (Exception exception) {
      logMessageAndImage("drawable_failed @ onNewResult", image);
      releaseImage(image);
      onFailureInternal(id, dataSource, exception, isFinished);
      return;
    }
    T previousImage = mFetchedImage;
    Drawable previousDrawable = mDrawable;
    mFetchedImage = image;
    mDrawable = drawable;
    try {
      // set the new image
      if (isFinished) {
        logMessageAndImage("set_final_result @ onNewResult", image);
        mDataSource = null;
        mSettableDraweeHierarchy.setImage(drawable, 1f, wasImmediate);
        reportSuccess(id, image, dataSource);
      } else if (deliverTempResult) {
        logMessageAndImage("set_temporary_result @ onNewResult", image);
        mSettableDraweeHierarchy.setImage(drawable, 1f, wasImmediate);
        reportSuccess(id, image, dataSource);
        // IMPORTANT: do not execute any instance-specific code after this point
      } else {
        logMessageAndImage("set_intermediate_result @ onNewResult", image);
        mSettableDraweeHierarchy.setImage(drawable, progress, wasImmediate);
        reportIntermediateSet(id, image);
        // IMPORTANT: do not execute any instance-specific code after this point
      }
    } finally {
      if (previousDrawable != null && previousDrawable != drawable) {
        releaseDrawable(previousDrawable);
      }
      if (previousImage != null && previousImage != image) {
        logMessageAndImage("release_previous_result @ onNewResult", previousImage);
        releaseImage(previousImage);
      }
    }
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}