content: package allen.town.focus.twitter.utils.glide;
  
  import androidx.annotation.NonNull;
  
  import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
  import com.bumptech.glide.load.Options;
  import com.bumptech.glide.load.model.GlideUrl;
  import com.bumptech.glide.load.model.ModelLoader;
  import com.bumptech.glide.load.model.ModelLoaderFactory;
  import com.bumptech.glide.load.model.MultiModelLoaderFactory;
  
  import java.io.InputStream;
  
  import okhttp3.Call;
  import okhttp3.OkHttpClient;
  
  public class TwitterUrlLoader implements ModelLoader<GlideUrl, InputStream> {
  
      private final Call.Factory client;
  
      // Public API.
      @SuppressWarnings("WeakerAccess")
      public TwitterUrlLoader(@NonNull Call.Factory client) {
          this.client = client;
      }
  
      @Override
      public boolean handles(@NonNull GlideUrl url) {
          return true;
      }
  
      @Override
      public LoadData<InputStream> buildLoadData(
              @NonNull GlideUrl model, int width, int height, @NonNull Options options) {
          return new LoadData<>(model, new TwitterStreamFetcher(client, model));
      }
  
      /** The default factory for {@link OkHttpUrlLoader}s. */
      // Public API.
      @SuppressWarnings("WeakerAccess")
      public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
          private static volatile Call.Factory internalClient;
          private final Call.Factory client;
  
          private static Call.Factory getInternalClient() {
              if (internalClient == null) {
                  synchronized (OkHttpUrlLoader.Factory.class) {
                      if (internalClient == null) {
                          internalClient = (Call.Factory) new OkHttpClient();
                      }
                  }
              }
              return internalClient;
          }
  
          /** Constructor for a new Factory that runs requests using a static singleton client. */
          public Factory() {
              this(getInternalClient());
          }
  
          /**
           * Constructor for a new Factory that runs requests using given client.
           *
           * @param client this is typically an instance of {@code OkHttpClient}.
           */
          public Factory(@NonNull Call.Factory client) {
              this.client = client;
          }
  
          @NonNull
          @Override
          public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
              return new TwitterUrlLoader(client);
          }
  
          @Override
          public void teardown() {
              // Do nothing, this instance doesn't own the client.
          }
      }
  }