Commit 880a5ff8 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Commit Bot

[Autofill Assistant] Download image using CachedImageFetcher.

The details image is now downloaded using the CachedImageFetcher
introduced in http://crrev/c/1297639. This allows to safely download the
image and avoid downloading the same one multiple times.

Change-Id: I5ad8e433307b75fafc522fcd379e62c4b3938a9f
Reviewed-on: https://chromium-review.googlesource.com/c/1326005Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Commit-Queue: Jordan Demeulenaere <jdemeulenaere@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606411}
parent 7a397415
...@@ -5,11 +5,9 @@ ...@@ -5,11 +5,9 @@
package org.chromium.chrome.browser.autofill_assistant; package org.chromium.chrome.browser.autofill_assistant;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils; import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable; import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
...@@ -26,19 +24,17 @@ import android.widget.ImageView; ...@@ -26,19 +24,17 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.chromium.base.Promise;
import org.chromium.chrome.autofill_assistant.R; import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile; import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.browser.cached_image_fetcher.CachedImageFetcher;
import org.chromium.chrome.browser.help.HelpAndFeedback; import org.chromium.chrome.browser.help.HelpAndFeedback;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.snackbar.Snackbar; import org.chromium.chrome.browser.snackbar.Snackbar;
import org.chromium.chrome.browser.snackbar.SnackbarManager; import org.chromium.chrome.browser.snackbar.SnackbarManager;
import org.chromium.components.variations.VariationsAssociatedData; import org.chromium.components.variations.VariationsAssociatedData;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
...@@ -452,10 +448,12 @@ class AutofillAssistantUiDelegate { ...@@ -452,10 +448,12 @@ class AutofillAssistantUiDelegate {
if (!url.isEmpty()) { if (!url.isEmpty()) {
// The URL is safe given because it comes from the knowledge graph and is hosted on // The URL is safe given because it comes from the knowledge graph and is hosted on
// Google servers. // Google servers.
downloadImage(url).then(image -> { CachedImageFetcher.getInstance().fetchImage(url, image -> {
mDetailsImage.setImageDrawable(getRoundedImage(image)); if (image != null) {
mDetailsImage.setVisibility(View.VISIBLE); mDetailsImage.setImageDrawable(getRoundedImage(image));
}, ignoredError -> {}); mDetailsImage.setVisibility(View.VISIBLE);
}
});
} else if (!details.isFinal()) { } else if (!details.isFinal()) {
mDetailsImage.setImageDrawable(AppCompatResources.getDrawable( mDetailsImage.setImageDrawable(AppCompatResources.getDrawable(
mActivity, R.drawable.autofill_assistant_default_details)); mActivity, R.drawable.autofill_assistant_default_details));
...@@ -620,39 +618,4 @@ class AutofillAssistantUiDelegate { ...@@ -620,39 +618,4 @@ class AutofillAssistantUiDelegate {
controller.onInitFinished(initOk, checkBox.isChecked()); controller.onInitFinished(initOk, checkBox.isChecked());
mCoordinatorView.removeView(initView); mCoordinatorView.removeView(initView);
} }
private Promise<Bitmap> downloadImage(String url) {
Promise<Bitmap> promise = new Promise<>();
new DownloadImageTask(promise).execute(url);
return promise;
}
private static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private final Promise<Bitmap> mPromise;
private Exception mError = null;
private DownloadImageTask(Promise<Bitmap> promise) {
this.mPromise = promise;
}
@Override
protected Bitmap doInBackground(String... urls) {
try (InputStream in = new URL(urls[0]).openStream()) {
return BitmapFactory.decodeStream(in);
} catch (Exception e) {
mError = e;
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (mError != null) {
mPromise.reject(mError);
return;
}
mPromise.fulfill(bitmap);
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment