Add badged icon example to BYOD CTSVerifier test

Bug: 18339619
Change-Id: I5807ad501249a79bd1b9cd2ccf2b9a110b5d5061
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index b3b3cd9..eb5efd9 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -1407,6 +1407,13 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".managedprovisioning.ByodIconSamplerActivity">
+            <intent-filter>
+                <action android:name="com.android.cts.verifier.managedprovisioning.BYOD_SAMPLE_ICON" />
+                <category android:name="android.intent.category.DEFAULT"></category>
+            </intent-filter>
+        </activity>
+
         <activity android:name=".managedprovisioning.CrossProfileTestActivity">
             <intent-filter>
                 <action android:name="com.android.cts.verifier.managedprovisioning.CROSS_PROFILE" />
diff --git a/apps/CtsVerifier/res/drawable/badged_icon.png b/apps/CtsVerifier/res/drawable/badged_icon.png
new file mode 100644
index 0000000..bb748da
--- /dev/null
+++ b/apps/CtsVerifier/res/drawable/badged_icon.png
Binary files differ
diff --git a/apps/CtsVerifier/res/layout/byod_custom_view_badged_icons.xml b/apps/CtsVerifier/res/layout/byod_custom_view_badged_icons.xml
new file mode 100644
index 0000000..1a29f65
--- /dev/null
+++ b/apps/CtsVerifier/res/layout/byod_custom_view_badged_icons.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+    <ScrollView android:id="@+id/scrollView"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingTop="2dip"
+            android:paddingBottom="12dip"
+            android:paddingStart="14dip"
+            android:paddingEnd="10dip"
+            android:overScrollMode="ifContentScrolls">
+        <TextView android:id="@+id/message"
+                style="@style/InstructionsSmallFont"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/provisioning_byod_workapps_visible_instruction" />
+    </ScrollView>
+
+    <ImageView android:id="@+id/sample_icon"
+            android:src="@drawable/badged_icon"
+            android:layout_width="56dip"
+            android:layout_height="56dip"
+            android:layout_gravity="center_horizontal" />
+</LinearLayout>
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index e8cc499..1f9b4da 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -1285,7 +1285,7 @@
         Go to All Apps screen and scroll through it to confirm that:\n
         \n
         - A new set of work apps including CTS Verifier appear in the list.\n
-        - Work badge overlay appears on work app\'s icon.\n
+        - Work badge overlay appears on work app\'s icon (see example icon below).\n
         \n
         Then navigate back to this screen using Recents button.
     </string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
index 6a9de44..b697be8 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodFlowTestActivity.java
@@ -29,10 +29,12 @@
 import android.os.Bundle;
 import android.provider.Settings;
 import android.util.Log;
+import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.View.OnClickListener;
 import android.widget.ArrayAdapter;
+import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -162,9 +164,20 @@
                 R.string.provisioning_byod_admin_visible_instruction,
                 new Intent(Settings.ACTION_SECURITY_SETTINGS));
 
+        /*
+         * To keep the image in this test up to date, use the instructions in
+         * {@link ByodIconSamplerActivity}.
+         */
         mWorkAppVisibleTest = new TestItem(this, R.string.provisioning_byod_workapps_visible,
-                R.string.provisioning_byod_workapps_visible_instruction,
-                new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
+                R.string.provisioning_byod_profile_visible_instruction,
+                new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)) {
+            @Override
+            public View getCustomView() {
+                LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
+                return layoutInflater.inflate(R.layout.byod_custom_view_badged_icons,
+                        null /* root */);
+            }
+        };
 
         Intent intent = new Intent(CrossProfileTestActivity.ACTION_CROSS_PROFILE);
         Intent chooser = Intent.createChooser(intent, getResources().getString(R.string.provisioning_cross_profile_chooser));
@@ -188,25 +201,31 @@
     }
 
     private void showManualTestDialog(final TestItem test) {
-        AlertDialog dialog = new AlertDialog.Builder(this)
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
                 .setIcon(android.R.drawable.ic_dialog_info)
-                .setMessage(test.getManualTestInstruction())
+                .setTitle(R.string.provisioning_byod)
                 .setNeutralButton(R.string.provisioning_byod_go, null)
-                .setPositiveButton(R.string.pass_button_text, new DialogInterface.OnClickListener() {
+                .setPositiveButton(R.string.pass_button_text, new AlertDialog.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         setTestResult(test, TestResult.Passed);
                     }
                 })
-                .setNegativeButton(R.string.fail_button_text, new DialogInterface.OnClickListener() {
+                .setNegativeButton(R.string.fail_button_text, new AlertDialog.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         setTestResult(test, TestResult.Failed);
                     }
-                })
-                .create();
-        dialog.show();
-
+                });
+        View customView = test.getCustomView();
+        if (customView != null) {
+            dialogBuilder.setView(customView);
+        } else {
+            dialogBuilder.setMessage(test.getManualTestInstruction());
+        }
+        AlertDialog dialog = dialogBuilder.show();
+        // Note: Setting the OnClickListener on the Dialog rather than the Builder, prevents the
+        // dialog being dismissed on onClick.
         dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -336,6 +355,10 @@
         public Intent getManualTestIntent() {
             return mManualIntent;
         }
+
+        public View getCustomView() {
+            return null;
+        }
     }
 
     static class TestAdapter extends ArrayAdapter<TestItem> {
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodIconSamplerActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodIconSamplerActivity.java
new file mode 100644
index 0000000..c0579d0
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/ByodIconSamplerActivity.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.verifier.managedprovisioning;
+
+import android.app.Activity;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.os.Environment;
+import android.os.UserHandle;
+import android.os.Process;
+import android.util.Log;
+
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+
+/**
+ * Activity used to generate sample image for {@link ByodFlowTestActivity} on a reference build.
+ *
+ * <p>Instructions: After Profile owner installed test has passed, run:
+ *  adb shell pm list users
+ *  adb shell am start -a com.android.cts.verifier.managedprovisioning.BYOD_SAMPLE_ICON \
+ *      --user <MANAGED_USER_ID>
+ * The icon can then be copied from /mnt/shell/emulated/<MANAGED_USER_ID>/badged_icon.png.
+ */
+public class ByodIconSamplerActivity  extends Activity {
+    static final String TAG = "ByodIconSamplerActivity";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        sampleImage();
+        // This activity has no UI
+        finish();
+    }
+    /**
+     * Writes a badged option of the CTS tests app icon on the sdcard.
+     * For test development only: this should be used to regenerate the asset every time we have
+     * a new badge.
+     */
+    private void sampleImage() {
+        UserHandle userHandle = Process.myUserHandle();
+        Log.d(TAG, "Sampling image for: " + userHandle);
+        Drawable drawable = getPackageManager().getUserBadgedIcon(getAppIcon(), userHandle);
+        Bitmap bitmap = convertToBitmap(drawable);
+        String fileName = Environment.getExternalStorageDirectory().getPath() + "/badged_icon.png";
+        FileOutputStream file = null;
+        try {
+            file = new FileOutputStream(fileName);
+            bitmap.compress(Bitmap.CompressFormat.PNG, 100, file);
+        } catch (FileNotFoundException e) {
+            Log.d(TAG, "sampleImage: FileNotFoundException ", e);
+        } finally {
+            try {
+                if (file != null) {
+                    file.close();
+                    Log.d(TAG, "Wrote badged icon to file: " + fileName);
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private Drawable getAppIcon() {
+        try {
+            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
+                    0 /* flags */);
+            if (packageInfo.applicationInfo != null) {
+                return getResources().getDrawable(packageInfo.applicationInfo.icon);
+            }
+        } catch (NameNotFoundException e) {
+            // Should not happen
+            Log.d(TAG, "getAppIcon: NameNotFoundException", e);
+        }
+        return null;
+    }
+
+    private static Bitmap convertToBitmap(Drawable icon) {
+        if (icon == null) {
+            return null;
+        }
+        Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
+                Bitmap.Config.ARGB_8888);
+        Canvas canvas = new Canvas(bitmap);
+        icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
+        icon.draw(canvas);
+        return bitmap;
+    }
+}