1 /*
2  * Copyright 2015, 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;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.net.Uri;
26 import android.provider.MediaStore;
27 import android.view.View;
28 import android.widget.ImageView;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.managedprovisioning.ProvisionLogger;
32 
33 import java.io.InputStream;
34 import java.io.IOException;
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.lang.Math;
38 
39 public class LogoUtils {
saveOrganisationLogo(Context context, Uri uri)40     public static void saveOrganisationLogo(Context context, Uri uri) {
41         final File logoFile = getOrganisationLogoFile(context);
42         try {
43             final InputStream in = context.getContentResolver().openInputStream(uri);
44             final FileOutputStream out = new FileOutputStream(logoFile);
45             final byte buffer[] = new byte[1024];
46             int bytesReadCount;
47             while ((bytesReadCount = in.read(buffer)) != -1) {
48                 out.write(buffer, 0, bytesReadCount);
49             }
50             out.close();
51             ProvisionLogger.logi("Organisation logo from uri " + uri + " has been successfully"
52                     + " copied to " + logoFile);
53         } catch (IOException e) {
54             ProvisionLogger.logi("Could not write organisation logo from " + uri + " to "
55                     + logoFile, e);
56             // If the file was only partly written, delete it.
57             logoFile.delete();
58         }
59     }
60 
getOrganisationLogo(Context context)61     public static Drawable getOrganisationLogo(Context context) {
62         final File logoFile = getOrganisationLogoFile(context);
63         Bitmap bitmap = null;
64         int maxWidth = (int) context.getResources().getDimension(R.dimen.max_logo_width);
65         int maxHeight = (int) context.getResources().getDimension(R.dimen.max_logo_height);
66         if (logoFile.exists()) {
67             bitmap = getBitmapPartiallyResized(logoFile.getPath(), maxWidth, maxHeight);
68             if (bitmap == null) {
69                 ProvisionLogger.loge("Could not get organisation logo from " + logoFile);
70             }
71         }
72         // If the app that started ManagedProvisioning didn't specify a logo or we couldn't get a
73         // logo from the uri they specified, use the default logo.
74         if (bitmap == null) {
75             bitmap = BitmapFactory.decodeResource(context.getResources(),
76                     R.drawable.briefcase_icon);
77         }
78         return new BitmapDrawable(context.getResources(),
79                 resizeBitmap(bitmap, maxWidth, maxHeight));
80     }
81 
82     /**
83      * Decodes a bitmap from an input stream.
84      * If the actual dimensions of the bitmap are larger than the desired ones, will try to return a
85      * subsample.
86      * The point of using this method is that the entire image may be too big to fit entirely in
87      * memmory. Since we may not need the entire image anyway, it's better to only decode a
88      * subsample when possible.
89      */
90     @VisibleForTesting
getBitmapPartiallyResized(String filePath, int maxDesiredWidth, int maxDesiredHeight)91     static Bitmap getBitmapPartiallyResized(String filePath, int maxDesiredWidth,
92             int maxDesiredHeight) {
93         BitmapFactory.Options bounds = new BitmapFactory.Options();
94         // Firstly, let's just get the dimensions of the image.
95         bounds.inJustDecodeBounds = true;
96         BitmapFactory.decodeFile(filePath, bounds);
97         int streamWidth = bounds.outWidth;
98         int streamHeight = bounds.outHeight;
99         int ratio = Math.max(streamWidth / maxDesiredWidth, streamHeight / maxDesiredHeight);
100         if (ratio > 1) {
101             // Decodes a smaller bitmap. Note that this ratio will be rounded down to the nearest
102             // power of 2. The decoded bitmap will not have the expected size, but we'll do another
103             // round of scaling.
104             bounds.inSampleSize = ratio;
105         }
106         bounds.inJustDecodeBounds = false;
107         // Now, decode the actual bitmap
108         return BitmapFactory.decodeFile(filePath, bounds);
109     }
110 
111     /*
112      * Returns a new Bitmap with the specified maximum width and height. Does scaling if
113      * necessary. Keeps the ratio of the original image.
114      */
115     @VisibleForTesting
resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight)116     static Bitmap resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight) {
117         int width = bitmap.getWidth();
118         int height = bitmap.getHeight();
119         double ratio = Math.max((double) width / maxWidth, (double) height / maxHeight);
120         // We don't scale up.
121         if (ratio > 1) {
122             width /= ratio;
123             height /= ratio;
124             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
125         }
126         return bitmap;
127     }
128 
cleanUp(Context context)129     public static void cleanUp(Context context) {
130         getOrganisationLogoFile(context).delete();
131     }
132 
getOrganisationLogoFile(Context context)133     private static File getOrganisationLogoFile(Context context) {
134         return new File(context.getFilesDir() + File.separator + "organisation_logo");
135     }
136 }
137