1 /*
2  * Copyright (C) 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.cts.verifier.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 import android.graphics.Bitmap;
23 import android.graphics.Canvas;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.os.Environment;
27 import android.os.UserHandle;
28 import android.os.Process;
29 import android.util.Log;
30 
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 
35 
36 /**
37  * Activity used to generate sample image for {@link ByodFlowTestActivity} on a reference build.
38  *
39  * <p>Instructions: After Profile owner installed test has passed, run:
40  *  adb shell pm list users
41  *  adb shell am start -a com.android.cts.verifier.managedprovisioning.BYOD_SAMPLE_ICON \
42  *      --user <MANAGED_USER_ID>
43  * The icon can then be copied from /mnt/shell/emulated/<MANAGED_USER_ID>/badged_icon.png.
44  */
45 public class ByodIconSamplerActivity  extends Activity {
46     static final String TAG = "ByodIconSamplerActivity";
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         sampleImage();
52         // This activity has no UI
53         finish();
54     }
55     /**
56      * Writes a badged option of the CTS tests app icon on the sdcard.
57      * For test development only: this should be used to regenerate the asset every time we have
58      * a new badge.
59      */
sampleImage()60     private void sampleImage() {
61         UserHandle userHandle = Process.myUserHandle();
62         Log.d(TAG, "Sampling image for: " + userHandle);
63         Drawable drawable = getPackageManager().getUserBadgedIcon(getAppIcon(), userHandle);
64         Bitmap bitmap = convertToBitmap(drawable);
65         String fileName = Environment.getExternalStorageDirectory().getPath() + "/badged_icon.png";
66         FileOutputStream file = null;
67         try {
68             file = new FileOutputStream(fileName);
69             bitmap.compress(Bitmap.CompressFormat.PNG, 100, file);
70         } catch (FileNotFoundException e) {
71             Log.d(TAG, "sampleImage: FileNotFoundException ", e);
72         } finally {
73             try {
74                 if (file != null) {
75                     file.close();
76                     Log.d(TAG, "Wrote badged icon to file: " + fileName);
77                 }
78             } catch (IOException e) {
79                 e.printStackTrace();
80             }
81         }
82     }
83 
getAppIcon()84     private Drawable getAppIcon() {
85         try {
86             PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
87                     0 /* flags */);
88             if (packageInfo.applicationInfo != null) {
89                 return getResources().getDrawable(packageInfo.applicationInfo.icon);
90             }
91         } catch (NameNotFoundException e) {
92             // Should not happen
93             Log.d(TAG, "getAppIcon: NameNotFoundException", e);
94         }
95         return null;
96     }
97 
convertToBitmap(Drawable icon)98     private static Bitmap convertToBitmap(Drawable icon) {
99         if (icon == null) {
100             return null;
101         }
102         Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
103                 Bitmap.Config.ARGB_8888);
104         Canvas canvas = new Canvas(bitmap);
105         icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
106         icon.draw(canvas);
107         return bitmap;
108     }
109 }
110