1 /*
2  * Copyright 2014, 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.managedprovisioning.common;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.PorterDuff;
24 import android.graphics.drawable.BitmapDrawable;
25 import android.graphics.drawable.Drawable;
26 import android.net.Uri;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.managedprovisioning.R;
30 
31 import java.io.File;
32 
33 public class LogoUtils {
34     @VisibleForTesting static final int DEFAULT_LOGO_ID = R.drawable.ic_enterprise_blue_24dp;
35 
saveOrganisationLogo(Context context, Uri uri)36     public static void saveOrganisationLogo(Context context, Uri uri) {
37         final File logoFile = getOrganisationLogoFile(context);
38         StoreUtils.copyUriIntoFile(context.getContentResolver(), uri, logoFile);
39     }
40 
41     /**
42      * @param colorTint optional color colorTint to apply to the logo
43      */
getOrganisationLogo(Context context, Integer colorTint)44     public static @NonNull Drawable getOrganisationLogo(Context context, Integer colorTint) {
45         final File logoFile = getOrganisationLogoFile(context);
46         Bitmap bitmap = null;
47         int maxWidth = (int) context.getResources().getDimension(R.dimen.max_logo_width);
48         int maxHeight = (int) context.getResources().getDimension(R.dimen.max_logo_height);
49         if (logoFile.exists()) {
50             bitmap = getBitmapPartiallyResized(logoFile.getPath(), maxWidth, maxHeight);
51             if (bitmap == null) {
52                 ProvisionLogger.loge("Could not get organisation logo from " + logoFile);
53             }
54         }
55 
56         if (bitmap != null) {
57             return new BitmapDrawable(context.getResources(),
58                     resizeBitmap(bitmap, maxWidth, maxHeight));
59         }
60 
61         // fall back to a default logo
62         Drawable organisationLogo = context.getDrawable(DEFAULT_LOGO_ID);
63         if (colorTint != null) {
64             organisationLogo.setColorFilter(colorTint, PorterDuff.Mode.SRC_ATOP);
65         }
66         return organisationLogo;
67     }
68 
69     /**
70      * Decodes a bitmap from an input stream.
71      * If the actual dimensions of the bitmap are larger than the desired ones, will try to return a
72      * subsample.
73      * The point of using this method is that the entire image may be too big to fit entirely in
74      * memmory. Since we may not need the entire image anyway, it's better to only decode a
75      * subsample when possible.
76      */
77     @VisibleForTesting
getBitmapPartiallyResized(String filePath, int maxDesiredWidth, int maxDesiredHeight)78     static Bitmap getBitmapPartiallyResized(String filePath, int maxDesiredWidth,
79             int maxDesiredHeight) {
80         BitmapFactory.Options bounds = new BitmapFactory.Options();
81         // Firstly, let's just get the dimensions of the image.
82         bounds.inJustDecodeBounds = true;
83         BitmapFactory.decodeFile(filePath, bounds);
84         int streamWidth = bounds.outWidth;
85         int streamHeight = bounds.outHeight;
86         int ratio = Math.max(streamWidth / maxDesiredWidth, streamHeight / maxDesiredHeight);
87         if (ratio > 1) {
88             // Decodes a smaller bitmap. Note that this ratio will be rounded down to the nearest
89             // power of 2. The decoded bitmap will not have the expected size, but we'll do another
90             // round of scaling.
91             bounds.inSampleSize = ratio;
92         }
93         bounds.inJustDecodeBounds = false;
94         // Now, decode the actual bitmap
95         return BitmapFactory.decodeFile(filePath, bounds);
96     }
97 
98     /*
99      * Returns a new Bitmap with the specified maximum width and height. Does scaling if
100      * necessary. Keeps the ratio of the original image.
101      */
102     @VisibleForTesting
resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight)103     static Bitmap resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight) {
104         int width = bitmap.getWidth();
105         int height = bitmap.getHeight();
106         double ratio = Math.max((double) width / maxWidth, (double) height / maxHeight);
107         // We don't scale up.
108         if (ratio > 1) {
109             width /= ratio;
110             height /= ratio;
111             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
112         }
113         return bitmap;
114     }
115 
cleanUp(Context context)116     public static void cleanUp(Context context) {
117         getOrganisationLogoFile(context).delete();
118     }
119 
getOrganisationLogoFile(Context context)120     private static File getOrganisationLogoFile(Context context) {
121         return new File(context.getFilesDir() + File.separator + "organisation_logo");
122     }
123 }
124