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 { 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 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(), R.drawable.ic_corp_icon); 76 } 77 return new BitmapDrawable(context.getResources(), 78 resizeBitmap(bitmap, maxWidth, maxHeight)); 79 } 80 81 /** 82 * Decodes a bitmap from an input stream. 83 * If the actual dimensions of the bitmap are larger than the desired ones, will try to return a 84 * subsample. 85 * The point of using this method is that the entire image may be too big to fit entirely in 86 * memmory. Since we may not need the entire image anyway, it's better to only decode a 87 * subsample when possible. 88 */ 89 @VisibleForTesting 90 static Bitmap getBitmapPartiallyResized(String filePath, int maxDesiredWidth, 91 int maxDesiredHeight) { 92 BitmapFactory.Options bounds = new BitmapFactory.Options(); 93 // Firstly, let's just get the dimensions of the image. 94 bounds.inJustDecodeBounds = true; 95 BitmapFactory.decodeFile(filePath, bounds); 96 int streamWidth = bounds.outWidth; 97 int streamHeight = bounds.outHeight; 98 int ratio = Math.max(streamWidth / maxDesiredWidth, streamHeight / maxDesiredHeight); 99 if (ratio > 1) { 100 // Decodes a smaller bitmap. Note that this ratio will be rounded down to the nearest 101 // power of 2. The decoded bitmap will not have the expected size, but we'll do another 102 // round of scaling. 103 bounds.inSampleSize = ratio; 104 } 105 bounds.inJustDecodeBounds = false; 106 // Now, decode the actual bitmap 107 return BitmapFactory.decodeFile(filePath, bounds); 108 } 109 110 /* 111 * Returns a new Bitmap with the specified maximum width and height. Does scaling if 112 * necessary. Keeps the ratio of the original image. 113 */ 114 @VisibleForTesting 115 static Bitmap resizeBitmap(Bitmap bitmap, int maxWidth, int maxHeight) { 116 int width = bitmap.getWidth(); 117 int height = bitmap.getHeight(); 118 double ratio = Math.max((double) width / maxWidth, (double) height / maxHeight); 119 // We don't scale up. 120 if (ratio > 1) { 121 width /= ratio; 122 height /= ratio; 123 bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); 124 } 125 return bitmap; 126 } 127 128 public static void cleanUp(Context context) { 129 getOrganisationLogoFile(context).delete(); 130 } 131 132 private static File getOrganisationLogoFile(Context context) { 133 return new File(context.getFilesDir() + File.separator + "organisation_logo"); 134 } 135 } 136