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 
28 import com.bumptech.glide.Glide;
29 import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader;
30 import com.bumptech.glide.request.RequestOptions;
31 import com.bumptech.glide.request.target.Target;
32 
33 import dagger.hilt.android.qualifiers.ApplicationContext;
34 
35 import java.io.File;
36 
37 import javax.inject.Inject;
38 import javax.inject.Singleton;
39 
40 /**
41  * Default implementation of {@link Requester}.
42  */
43 @Singleton
44 public class WallpaperRequester implements Requester {
45     // Network timeout that matches expected outer bound of packet loss duration on slower networks,
46     // i.e., 2g.
47     public static final int LONG_TIMEOUT_MS = 10000;
48 
49     private static final String TAG = "WallpaperRequester";
50 
51     private RequestQueue mRequestQueue;
52     private Context mAppContext;
53 
54     @Inject
WallpaperRequester(@pplicationContext Context context)55     public WallpaperRequester(@ApplicationContext Context context) {
56         mAppContext = context;
57         mRequestQueue = Volley.newRequestQueue(context);
58     }
59 
60     @Override
addToRequestQueue(Request<T> request)61     public <T> void addToRequestQueue(Request<T> request) {
62         mRequestQueue.add(request);
63     }
64 
65     @Override
loadImageFile(Uri imageUrl)66     public File loadImageFile(Uri imageUrl) {
67         try {
68             return Glide.with(mAppContext)
69                     .downloadOnly()
70                     .load(imageUrl)
71                     // Apply a longer timeout duration to avoid crashing on networks with long packet loss
72                     // durations.
73                     .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS))
74                     .submit()
75                     .get();
76         } catch (Exception e) {
77             Log.e(TAG, "Unable to get File for image with url: " + imageUrl);
78             return null;
79         }
80     }
81 
82     @Override
loadImageFileWithActivity(Activity activity, Uri imageUrl, Target<File> target)83     public void loadImageFileWithActivity(Activity activity, Uri imageUrl, Target<File> target) {
84         Glide.with(activity)
85                 .asFile()
86                 .load(imageUrl)
87                 // Apply a longer timeout duration to avoid crashing on networks with long packet loss
88                 // durations.
89                 .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS))
90                 .into(target);
91     }
92 
93     @Override
loadImageBitmap(Uri imageUrl, Target<Bitmap> target)94     public void loadImageBitmap(Uri imageUrl, Target<Bitmap> target) {
95         try {
96             Glide.with(mAppContext)
97                     .asBitmap()
98                     .load(imageUrl)
99                     .apply(RequestOptions.noTransformation())
100                     .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS))
101                     .into(target);
102         } catch (Exception e) {
103             Log.e(TAG, "Unable to get Bitmap for image with url: " + imageUrl, e);
104         }
105     }
106 }
107