1 package com.bumptech.glide.load.model; 2 3 import android.content.ContentResolver; 4 import android.content.Context; 5 import android.net.Uri; 6 7 import com.bumptech.glide.load.data.DataFetcher; 8 9 /** 10 * A base ModelLoader for {@link android.net.Uri}s that handles local {@link android.net.Uri}s directly and routes 11 * remote {@link android.net.Uri}s to a wrapped {@link com.bumptech.glide.load.model.ModelLoader} that handles 12 * {@link com.bumptech.glide.load.model.GlideUrl}s. 13 * 14 * @param <T> The type of data that will be retrieved for {@link android.net.Uri}s. 15 */ 16 public abstract class UriLoader<T> implements ModelLoader<Uri, T> { 17 private final Context context; 18 private final ModelLoader<GlideUrl, T> urlLoader; 19 20 public UriLoader(Context context, ModelLoader<GlideUrl, T> urlLoader) { 21 this.context = context; 22 this.urlLoader = urlLoader; 23 } 24 25 @Override 26 public final DataFetcher<T> getResourceFetcher(Uri model, int width, int height) { 27 final String scheme = model.getScheme(); 28 29 DataFetcher<T> result = null; 30 if (isLocalUri(scheme)) { 31 if (AssetUriParser.isAssetUri(model)) { 32 String path = AssetUriParser.toAssetPath(model); 33 result = getAssetPathFetcher(context, path); 34 } else { 35 result = getLocalUriFetcher(context, model); 36 } 37 } else if (urlLoader != null && ("http".equals(scheme) || "https".equals(scheme))) { 38 result = urlLoader.getResourceFetcher(new GlideUrl(model.toString()), width, height); 39 } 40 41 return result; 42 } 43 44 protected abstract DataFetcher<T> getLocalUriFetcher(Context context, Uri uri); 45 46 protected abstract DataFetcher<T> getAssetPathFetcher(Context context, String path); 47 48 private static boolean isLocalUri(String scheme) { 49 return ContentResolver.SCHEME_FILE.equals(scheme) 50 || ContentResolver.SCHEME_CONTENT.equals(scheme) 51 || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme); 52 } 53 } 54