blob: 2f099ab6c37995be4ce574cf166f159525b55528 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
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 */
16package com.android.wallpaper.network;
17
18import android.app.Activity;
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.net.Uri;
22import android.util.Log;
23
24import com.android.volley.Request;
25import com.android.volley.RequestQueue;
26import com.android.volley.toolbox.Volley;
27import com.bumptech.glide.Glide;
28import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader;
29import com.bumptech.glide.request.RequestOptions;
30import com.bumptech.glide.request.target.Target;
31
32import java.io.File;
33
34/**
35 * Default implementation of {@link Requester}.
36 */
37public 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
47 public WallpaperRequester(Context context) {
48 mAppContext = context.getApplicationContext();
49 mRequestQueue = Volley.newRequestQueue(context.getApplicationContext());
50 }
51
52 @Override
53 public <T> void addToRequestQueue(Request<T> request) {
54 mRequestQueue.add(request);
55 }
56
57 @Override
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
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
86 public void loadImageBitmap(Uri imageUrl, Target<Bitmap> target) {
87 try {
88 Glide.with(mAppContext)
89 .asBitmap()
90 .load(imageUrl)
91 .apply(RequestOptions.noTransformation())
Santiago Etchebeherebadabbd2018-10-25 12:14:13 -070092 .apply(RequestOptions.option(HttpGlideUrlLoader.TIMEOUT, LONG_TIMEOUT_MS))
Jon Miranda16ea1b12017-12-12 14:52:48 -080093 .into(target);
94 } catch (Exception e) {
95 Log.e(TAG, "Unable to get Bitmap for image with url: " + imageUrl, e);
96 }
97 }
98}