blob: 6237ca3e5a901aa725d0d7bb2fdb73f8b9120f38 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2008 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
17package com.android.incallui;
18
19import android.app.Notification;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.drawable.BitmapDrawable;
23import android.graphics.drawable.Drawable;
24import android.net.Uri;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.support.annotation.MainThread;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070026import android.support.annotation.Nullable;
Eric Erfanianccca3152017-02-22 16:32:36 -080027import android.support.annotation.WorkerThread;
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070028import com.android.dialer.common.LogUtil;
29import com.android.dialer.common.concurrent.DialerExecutor;
30import com.android.dialer.common.concurrent.DialerExecutors;
Eric Erfanianccca3152017-02-22 16:32:36 -080031import java.io.IOException;
32import java.io.InputStream;
33
34/** Helper class for loading contacts photo asynchronously. */
35public class ContactsAsyncHelper {
36
37 /** Interface for a WorkerHandler result return. */
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070038 interface OnImageLoadCompleteListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080039
40 /**
41 * Called when the image load is complete. Must be called in main thread.
42 *
43 * @param token Integer passed in {@link ContactsAsyncHelper#startObtainPhotoAsync(int, Context,
44 * Uri, OnImageLoadCompleteListener, Object)}.
45 * @param photo Drawable object obtained by the async load.
46 * @param photoIcon Bitmap object obtained by the async load.
47 * @param cookie Object passed in {@link ContactsAsyncHelper#startObtainPhotoAsync(int, Context,
48 * Uri, OnImageLoadCompleteListener, Object)}. Can be null iff. the original cookie is null.
49 */
50 @MainThread
51 void onImageLoadComplete(int token, Drawable photo, Bitmap photoIcon, Object cookie);
52
53 /** Called when image is loaded to udpate data. Must be called in worker thread. */
54 @WorkerThread
55 void onImageLoaded(int token, Drawable photo, Bitmap photoIcon, Object cookie);
56 }
57
Eric Erfanianccca3152017-02-22 16:32:36 -080058 /**
59 * Starts an asynchronous image load. After finishing the load, {@link
60 * OnImageLoadCompleteListener#onImageLoadComplete(int, Drawable, Bitmap, Object)} will be called.
61 *
62 * @param token Arbitrary integer which will be returned as the first argument of {@link
63 * OnImageLoadCompleteListener#onImageLoadComplete(int, Drawable, Bitmap, Object)}
64 * @param context Context object used to do the time-consuming operation.
65 * @param displayPhotoUri Uri to be used to fetch the photo
66 * @param listener Callback object which will be used when the asynchronous load is done. Can be
67 * null, which means only the asynchronous load is done while there's no way to obtain the
68 * loaded photos.
69 * @param cookie Arbitrary object the caller wants to remember, which will become the fourth
70 * argument of {@link OnImageLoadCompleteListener#onImageLoadComplete(int, Drawable, Bitmap,
71 * Object)}. Can be null, at which the callback will also has null for the argument.
72 */
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070073 static void startObtainPhotoAsync(
Eric Erfanianccca3152017-02-22 16:32:36 -080074 int token,
75 Context context,
76 Uri displayPhotoUri,
77 OnImageLoadCompleteListener listener,
78 Object cookie) {
79 // in case the source caller info is null, the URI will be null as well.
80 // just update using the placeholder image in this case.
81 if (displayPhotoUri == null) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070082 LogUtil.e("ContactsAsyncHelper.startObjectPhotoAsync", "uri is missing");
Eric Erfanianccca3152017-02-22 16:32:36 -080083 return;
84 }
85
86 // Added additional Cookie field in the callee to handle arguments
87 // sent to the callback function.
88
89 // setup arguments
90 WorkerArgs args = new WorkerArgs();
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070091 args.token = token;
Eric Erfanianccca3152017-02-22 16:32:36 -080092 args.cookie = cookie;
93 args.context = context;
94 args.displayPhotoUri = displayPhotoUri;
95 args.listener = listener;
96
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070097 DialerExecutors.createNonUiTaskBuilder(new Worker())
98 .onSuccess(
99 output -> {
100 if (args.listener != null) {
101 LogUtil.d(
102 "ContactsAsyncHelper.startObtainPhotoAsync",
103 "notifying listener: "
104 + args.listener
105 + " image: "
106 + args.displayPhotoUri
107 + " completed");
108 args.listener.onImageLoadComplete(
109 args.token, args.photo, args.photoIcon, args.cookie);
110 }
111 })
112 .build()
113 .executeParallel(args);
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 }
115
116 private static final class WorkerArgs {
117
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700118 public int token;
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 public Context context;
120 public Uri displayPhotoUri;
121 public Drawable photo;
122 public Bitmap photoIcon;
123 public Object cookie;
124 public OnImageLoadCompleteListener listener;
125 }
126
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700127 private static class Worker implements DialerExecutor.Worker<WorkerArgs, Void> {
Eric Erfanianccca3152017-02-22 16:32:36 -0800128
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700129 @Nullable
Eric Erfanianccca3152017-02-22 16:32:36 -0800130 @Override
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700131 public Void doInBackground(WorkerArgs args) throws Throwable {
132 InputStream inputStream = null;
133 try {
134 try {
135 inputStream = args.context.getContentResolver().openInputStream(args.displayPhotoUri);
136 } catch (Exception e) {
137 LogUtil.e(
138 "ContactsAsyncHelper.Worker.doInBackground", "error opening photo input stream", e);
139 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800140
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700141 if (inputStream != null) {
142 args.photo = Drawable.createFromStream(inputStream, args.displayPhotoUri.toString());
143
144 // This assumes Drawable coming from contact database is usually
145 // BitmapDrawable and thus we can have (down)scaled version of it.
146 args.photoIcon = getPhotoIconWhenAppropriate(args.context, args.photo);
147
148 LogUtil.d(
149 "ContactsAsyncHelper.Worker.doInBackground",
150 "loading image, URI: %s",
151 args.displayPhotoUri);
152 } else {
153 args.photo = null;
154 args.photoIcon = null;
155 LogUtil.d(
156 "ContactsAsyncHelper.Worker.doInBackground",
157 "problem with image, URI: %s, using default image.",
158 args.displayPhotoUri);
159 }
160 if (args.listener != null) {
161 args.listener.onImageLoaded(args.token, args.photo, args.photoIcon, args.cookie);
162 }
163 } finally {
164 if (inputStream != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800165 try {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700166 inputStream.close();
167 } catch (IOException e) {
168 LogUtil.e(
169 "ContactsAsyncHelper.Worker.doInBackground", "Unable to close input stream.", e);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 }
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700171 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800172 }
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700173 return null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800174 }
175
176 /**
177 * Returns a Bitmap object suitable for {@link Notification}'s large icon. This might return
178 * null when the given Drawable isn't BitmapDrawable, or if the system fails to create a scaled
179 * Bitmap for the Drawable.
180 */
181 private Bitmap getPhotoIconWhenAppropriate(Context context, Drawable photo) {
182 if (!(photo instanceof BitmapDrawable)) {
183 return null;
184 }
185 int iconSize = context.getResources().getDimensionPixelSize(R.dimen.notification_icon_size);
186 Bitmap orgBitmap = ((BitmapDrawable) photo).getBitmap();
187 int orgWidth = orgBitmap.getWidth();
188 int orgHeight = orgBitmap.getHeight();
189 int longerEdge = orgWidth > orgHeight ? orgWidth : orgHeight;
190 // We want downscaled one only when the original icon is too big.
191 if (longerEdge > iconSize) {
192 float ratio = ((float) longerEdge) / iconSize;
193 int newWidth = (int) (orgWidth / ratio);
194 int newHeight = (int) (orgHeight / ratio);
195 // If the longer edge is much longer than the shorter edge, the latter may
196 // become 0 which will cause a crash.
197 if (newWidth <= 0 || newHeight <= 0) {
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -0700198 LogUtil.w(
199 "ContactsAsyncHelper.Worker.getPhotoIconWhenAppropriate",
200 "Photo icon's width or height become 0.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 return null;
202 }
203
204 // It is sure ratio >= 1.0f in any case and thus the newly created Bitmap
205 // should be smaller than the original.
206 return Bitmap.createScaledBitmap(orgBitmap, newWidth, newHeight, true);
207 } else {
208 return orgBitmap;
209 }
210 }
211 }
212}