• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 android.holo.cts;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.Bitmap.CompressFormat;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.holo.cts.LayoutAdapter.LayoutInfo;
26 import android.holo.cts.ThemeAdapter.ThemeInfo;
27 import android.os.Environment;
28 import android.util.Log;
29 
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 
34 /**
35  * Collection of utility methods to handle creation and deletion of assets.
36  *
37  * It creates a directory called "cts-holo-assets" on the external storage.
38  * Below that it creates a directory called "reference" to store images
39  * generated by the generator and "failure" to store images that failed to
40  * match when running the test.
41  */
42 class BitmapAssets {
43 
44     private static final String TAG = BitmapAssets.class.getSimpleName();
45 
46     static final int TYPE_REFERENCE = 0;
47 
48     static final int TYPE_FAILED = 1;
49 
50     static final int TYPE_DIFF = 2;
51 
clearDirectory(int type)52     public static boolean clearDirectory(int type) {
53         if (!isExternalStorageReady()) {
54             return false;
55         }
56 
57         File dir = getBitmapDir(type);
58         if (dir.exists()) {
59             File[] files = dir.listFiles();
60             int numFiles = files.length;
61             for (int i = 0; i < numFiles; i++) {
62                 files[i].delete();
63             }
64         }
65         return true;
66     }
67 
isExternalStorageReady()68     private static boolean isExternalStorageReady() {
69         return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
70     }
71 
getBitmapDir(int type)72     public static File getBitmapDir(int type) {
73         String subDir;
74         switch (type) {
75             case TYPE_REFERENCE:
76                 subDir = "reference";
77                 break;
78             case TYPE_FAILED:
79                 subDir = "failed";
80                 break;
81             case TYPE_DIFF:
82                 subDir = "diff";
83                 break;
84             default:
85                 throw new IllegalArgumentException("Bad type: " + type);
86         }
87 
88         File file = new File(Environment.getExternalStorageDirectory(), "cts-holo-assets");
89         return new File(file, subDir);
90     }
91 
getBitmapName(ThemeInfo themeInfo, LayoutInfo layoutInfo)92     public static String getBitmapName(ThemeInfo themeInfo, LayoutInfo layoutInfo) {
93         return themeInfo.getBitmapName() + "_" + layoutInfo.getBitmapName();
94     }
95 
getBitmap(Context context, String bitmapName)96     public static Bitmap getBitmap(Context context, String bitmapName) {
97         Resources resources = context.getResources();
98         int resourceId = resources.getIdentifier(bitmapName, "drawable", context.getPackageName());
99         return ((BitmapDrawable) resources.getDrawable(resourceId)).getBitmap();
100     }
101 
getBitmapPath(String bitmapName, int type)102     public static File getBitmapPath(String bitmapName, int type) {
103         String prefix;
104         switch (type) {
105             case TYPE_REFERENCE:
106                 prefix = "";
107                 break;
108             case TYPE_FAILED:
109                 prefix = "failed_";
110                 break;
111             case TYPE_DIFF:
112                 prefix = "diff_";
113                 break;
114             default:
115                 throw new IllegalArgumentException("Bad type: " + type);
116         }
117 
118         return new File(getBitmapDir(type), prefix + bitmapName + ".png");
119     }
120 
saveBitmap(Bitmap bitmap, String bitmapName, int type)121     public static String saveBitmap(Bitmap bitmap, String bitmapName, int type) throws IOException {
122         if (!isExternalStorageReady()) {
123             Log.i(TAG, "External storage for saving bitmaps is not mounted");
124             return null;
125         }
126 
127         File file = getBitmapPath(bitmapName, type);
128         File dir = file.getParentFile();
129         dir.mkdirs();
130 
131         FileOutputStream stream = new FileOutputStream(file);
132         bitmap.compress(CompressFormat.PNG, 100, stream);
133         stream.close();
134         return file.getAbsolutePath();
135     }
136 }
137