/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.art.http;

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;

/**
 * A simple model loader for fetching media over http/https using OkHttp.
 */
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {

    private final Call.Factory client;

    public OkHttpUrlLoader(Call.Factory client) {
        this.client = client;
    }

    @Override
    public boolean handles(GlideUrl url) {
        return true;
    }

    @Override
    public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,
                                               Options options) {
        return new LoadData<>(model, new OkHttpStreamFetcher(client, model));
    }

    /**
     * The default factory for {@link OkHttpUrlLoader}s.
     */
    public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
        private static volatile Call.Factory internalClient;
        private Call.Factory client;

        private static Call.Factory getInternalClient() {
            if (internalClient == null) {
                synchronized (Factory.class) {
                    if (internalClient == null) {
                        internalClient = 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(Call.Factory client) {
            this.client = client;
        }

        @Override
        public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
            return new OkHttpUrlLoader(client);
        }

        @Override
        public void teardown() {
            // Do nothing, this instance doesn't own the client.
        }
    }
}

@@ -615,16 +615,60 @@
 .http;%0A%0A
+import android.support.annotation.NonNull;%0A%0A
 import c
@@ -1190,24 +1190,81 @@
 ry client;%0A%0A
+    // Public API.%0A    @SuppressWarnings(%22WeakerAccess%22)%0A
     public O
@@ -1278,16 +1278,25 @@
 lLoader(
+@NonNull 
 Call.Fac
@@ -1388,16 +1388,25 @@
 handles(
+@NonNull 
 GlideUrl
@@ -1502,16 +1502,25 @@
 oadData(
+@NonNull 
 GlideUrl
@@ -1597,16 +1597,25 @@
         
+@NonNull 
 Options 
@@ -1779,24 +1779,81 @@
 %7Ds.%0A     */%0A
+    // Public API.%0A    @SuppressWarnings(%22WeakerAccess%22)%0A
     public s
@@ -2004,16 +2004,22 @@
 private 
+final 
 Call.Fac
@@ -2802,16 +2802,25 @@
 Factory(
+@NonNull 
 Call.Fac
@@ -2871,32 +2871,49 @@
 ent;%0A        %7D%0A%0A
+        @NonNull%0A
         @Overrid

Improve OkHttpUrlLoader