• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 
17 package com.android.pump.widget;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.drawable.Drawable;
22 import android.net.Uri;
23 import android.util.AttributeSet;
24 
25 import androidx.annotation.AttrRes;
26 import androidx.annotation.DrawableRes;
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.annotation.UiThread;
30 
31 import com.android.pump.util.Globals;
32 import com.android.pump.util.ImageLoader;
33 import com.android.pump.util.Scheme;
34 
35 @UiThread
36 public class UriImageView extends PlaceholderImageView {
37     private Uri mUri;
38 
UriImageView(@onNull Context context)39     public UriImageView(@NonNull Context context) {
40         super(context);
41     }
42 
UriImageView(@onNull Context context, @Nullable AttributeSet attrs)43     public UriImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
UriImageView(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)47     public UriImageView(@NonNull Context context, @Nullable AttributeSet attrs,
48             @AttrRes int defStyleAttr) {
49         super(context, attrs, defStyleAttr);
50     }
51 
52     @Override
setImageResource(@rawableRes int resId)53     public void setImageResource(@DrawableRes int resId) {
54         super.setImageResource(resId);
55         mUri = null;
56     }
57 
58     @Override
setImageDrawable(@ullable Drawable drawable)59     public void setImageDrawable(@Nullable Drawable drawable) {
60         super.setImageDrawable(drawable);
61         mUri = null;
62     }
63 
64     @Override
setImageBitmap(@ullable Bitmap bm)65     public void setImageBitmap(@Nullable Bitmap bm) {
66         super.setImageBitmap(bm);
67         mUri = null;
68     }
69 
70     @Override
setImageURI(@ullable Uri uri)71     public void setImageURI(@Nullable Uri uri) {
72         setImageDrawable(null);
73         if (uri == null) {
74             return;
75         }
76         if (Scheme.isFile(uri) || Scheme.isHttp(uri) || Scheme.isHttps(uri)) {
77             mUri = uri;
78             loadImage();
79         } else {
80             super.setImageURI(uri);
81         }
82     }
83 
loadImage()84     private void loadImage() {
85         ImageLoader imageLoader = Globals.getImageLoader(getContext());
86         imageLoader.loadImage(mUri, (loadedUri, bitmap) -> {
87             if (mUri != null && mUri.equals(loadedUri)) {
88                 setImageBitmap(bitmap);
89                 mUri = loadedUri;
90             }
91         });
92     }
93 }
94