1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.wallpaper.network; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.net.Uri; 22 import android.util.Log; 23 24 import com.android.volley.Request; 25 import com.android.volley.RequestQueue; 26 import com.android.volley.toolbox.Volley; 27 import com.bumptech.glide.Glide; 28 import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader; 29 import com.bumptech.glide.request.RequestOptions; 30 import com.bumptech.glide.request.target.Target; 31 32 import java.io.File; 33 34 /** 35 * Default implementation of {@link Requester}. 36 */ 37 public class WallpaperRequester implements Requester { 38 // Network timeout that matches expected outer bound of packet loss duration on slower networks, 39 // i.e., 2g. 40 public static final int LONG_TIMEOUT_MS = 10000; 41 42 private static final String TAG = "WallpaperRequester"; 43 44 private RequestQueue mRequestQueue; 45 private Context mAppContext; 46 WallpaperRequester(Context context)47 public WallpaperRequester(Context context) { 48 mAppContext = context.getApplicationContext(); 49 mRequestQueue = Volley.newRequestQueue(context.getApplicationContext()); 50 } 51 52 @Override addToRequestQueue(Request<T> request)53 public <T> void addToRequestQueue(Request<T> request) { 54 mRequestQueue.add(request); 55 } 56 57 @Override loadImageFile(Uri imageUrl)58 public File loadImageFile(Uri imageUrl) { 59 try { 60 return Glide.with(mAppContext) 61 .downloadOnly() 62 .load(imageUrl) 63 // Apply a longer timeout duration to avoid crashing on networks with long packet loss 64 // durations. 65 .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS)) 66 .submit() 67 .get(); 68 } catch (Exception e) { 69 Log.e(TAG, "Unable to get File for image with url: " + imageUrl); 70 return null; 71 } 72 } 73 74 @Override loadImageFileWithActivity(Activity activity, Uri imageUrl, Target<File> target)75 public void loadImageFileWithActivity(Activity activity, Uri imageUrl, Target<File> target) { 76 Glide.with(activity) 77 .asFile() 78 .load(imageUrl) 79 // Apply a longer timeout duration to avoid crashing on networks with long packet loss 80 // durations. 81 .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS)) 82 .into(target); 83 } 84 85 @Override loadImageBitmap(Uri imageUrl, Target<Bitmap> target)86 public void loadImageBitmap(Uri imageUrl, Target<Bitmap> target) { 87 try { 88 Glide.with(mAppContext) 89 .asBitmap() 90 .load(imageUrl) 91 .apply(RequestOptions.noTransformation()) 92 .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS)) 93 .into(target); 94 } catch (Exception e) { 95 Log.e(TAG, "Unable to get Bitmap for image with url: " + imageUrl, e); 96 } 97 } 98 } 99