Sample authenticator that creates a "DPM-whitelisted" account.

This account will not prevent a test-only DO/PO from being activated.

Test: Manual test
Change-Id: I6407c8c85b4476436db3379752488557d58bc617
diff --git a/samples/DeviceAdminWhitelistedAccount/Android.mk b/samples/DeviceAdminWhitelistedAccount/Android.mk
new file mode 100644
index 0000000..03d618c
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/Android.mk
@@ -0,0 +1,33 @@
+#
+# Copyright (C) 2017 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.
+#
+
+# We build two apps from the same source
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := DeviceAdminWhitelistedAccount
+
+LOCAL_MODULE_TAGS := samples tests
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_PACKAGE)
diff --git a/samples/DeviceAdminWhitelistedAccount/AndroidManifest.xml b/samples/DeviceAdminWhitelistedAccount/AndroidManifest.xml
new file mode 100644
index 0000000..6ce9f89
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/AndroidManifest.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.example.android.app.admin.whitelistedaccount">
+
+    <uses-sdk android:minSdkVersion="25" android:targetSdkVersion="25" />
+
+    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
+    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
+    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
+
+    <application
+        android:label="@string/app_name"
+        android:icon="@mipmap/ic_launcher"
+        >
+
+        <activity
+            android:name="MyMain"
+            android:icon="@mipmap/ic_launcher"
+            android:theme="@android:style/Theme.NoDisplay"
+            >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <service
+            android:name=".MyAuthenticator"
+            >
+            <intent-filter>
+                <action android:name="android.accounts.AccountAuthenticator" />
+            </intent-filter>
+
+            <meta-data android:name="android.accounts.AccountAuthenticator"
+                android:resource="@xml/authenticator" />
+        </service>
+    </application>
+</manifest>
diff --git a/samples/DeviceAdminWhitelistedAccount/README.md b/samples/DeviceAdminWhitelistedAccount/README.md
new file mode 100644
index 0000000..bebb4f5
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/README.md
@@ -0,0 +1,24 @@
+# DeviceAdmin Whitelisted Account
+
+This application creates an account that will *not* prevent test-only DO/PO from being activated.
+
+## Build and install:
+
+```
+croot
+mmma -j development/samples/DeviceAdminWhitelistedAccount
+adb install -r -g $OUT/data/app/DeviceAdminWhitelistedAccount/DeviceAdminWhitelistedAccount.apk
+```
+
+
+## Create a whitelisted account
+
+- Launch the "DA Whitelisted Account" app from the launcher.
+
+## Remove a whitelisted account
+
+- Just uninstall the app. i.e.
+
+```
+adb uninstall com.example.android.app.admin.whitelistedaccount
+```
diff --git a/samples/DeviceAdminWhitelistedAccount/res/mipmap-hdpi/ic_launcher.png b/samples/DeviceAdminWhitelistedAccount/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000..1cb2a39
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/mipmap-hdpi/ic_launcher.png
Binary files differ
diff --git a/samples/DeviceAdminWhitelistedAccount/res/mipmap-mdpi/ic_launcher.png b/samples/DeviceAdminWhitelistedAccount/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000..d29a23d
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/mipmap-mdpi/ic_launcher.png
Binary files differ
diff --git a/samples/DeviceAdminWhitelistedAccount/res/mipmap-xhdpi/ic_launcher.png b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..41ffbcb
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xhdpi/ic_launcher.png
Binary files differ
diff --git a/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxhdpi/ic_launcher.png b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..0fee79d
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxxhdpi/ic_launcher.png b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..fd74afc
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/mipmap-xxxhdpi/ic_launcher.png
Binary files differ
diff --git a/samples/DeviceAdminWhitelistedAccount/res/values/strings.xml b/samples/DeviceAdminWhitelistedAccount/res/values/strings.xml
new file mode 100644
index 0000000..b79588e
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/values/strings.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <string name="app_name">DA Whitelisted Account</string>
+    <string name="account_name">DA Whitelisted Account</string>
+</resources>
diff --git a/samples/DeviceAdminWhitelistedAccount/res/xml/authenticator.xml b/samples/DeviceAdminWhitelistedAccount/res/xml/authenticator.xml
new file mode 100644
index 0000000..7fd89cd
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/res/xml/authenticator.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:accountType="com.example.android.app.admin.whitelistedaccount" />
diff --git a/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyAuthenticator.java b/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyAuthenticator.java
new file mode 100644
index 0000000..db88db3
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyAuthenticator.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2017 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.example.android.app.admin.whitelistedaccount;
+
+import android.accounts.AbstractAccountAuthenticator;
+import android.accounts.Account;
+import android.accounts.AccountAuthenticatorResponse;
+import android.accounts.AccountManager;
+import android.accounts.NetworkErrorException;
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.IBinder;
+
+public class MyAuthenticator extends Service {
+    private static final String TAG = "TestAuthenticator";
+
+    private static final String ACCOUNT_TYPE = "com.example.android.app.admin.whitelistedaccount";
+
+    private static final String ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED =
+            "android.account.DEVICE_OR_PROFILE_OWNER_ALLOWED";
+
+    private static Authenticator sInstance;
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        if (sInstance == null) {
+            sInstance = new Authenticator(getApplicationContext());
+
+        }
+        return sInstance.getIBinder();
+    }
+
+    public static boolean setUpAccount(Context context) {
+        final AccountManager am = AccountManager.get(context);
+        if (am.getAccountsByType(ACCOUNT_TYPE).length > 0) {
+            return false; // Already set up.
+        }
+
+        // Add a new account.
+        final Account account = new Account(
+                context.getResources().getString(R.string.account_name), ACCOUNT_TYPE);
+        am.addAccountExplicitly(account, null, null);
+        return true;
+    }
+
+    public static class Authenticator extends AbstractAccountAuthenticator {
+
+        private final Context mContxet;
+
+        public Authenticator(Context context) {
+            super(context);
+            mContxet = context;
+        }
+
+        @Override
+        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
+                String authTokenType, String[] requiredFeatures, Bundle options)
+                throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
+                String authTokenType, Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
+                Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
+                String authTokenType, Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public String getAuthTokenLabel(String authTokenType) {
+            return "token_label";
+        }
+
+        @Override
+        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
+                String[] features) throws NetworkErrorException {
+
+            boolean hasAll = false;
+
+            if ((features != null) && (features.length == 1)
+                    && ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED.equals(features[0])) {
+                hasAll = true;
+            }
+
+            Bundle result = new Bundle();
+            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, hasAll);
+            return result;
+        }
+    }
+}
diff --git a/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyMain.java b/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyMain.java
new file mode 100644
index 0000000..3f44489
--- /dev/null
+++ b/samples/DeviceAdminWhitelistedAccount/src/com/example/android/app/admin/whitelistedaccount/MyMain.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2017 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.example.android.app.admin.whitelistedaccount;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.widget.Toast;
+
+public class MyMain extends Activity {
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final Context context = getApplicationContext();
+
+        new AsyncTask<Void, Void, Boolean>() {
+            @Override
+            protected Boolean doInBackground(Void... voids) {
+                return MyAuthenticator.setUpAccount(context);
+            }
+
+            @Override
+            protected void onPostExecute(Boolean ret) {
+                Toast.makeText(context,
+                        (ret ? "Account created" : "Account already exists"),
+                        Toast.LENGTH_SHORT).show();
+
+            }
+        }.execute();
+
+        finish();
+    }
+}