/*
 * Copyright (C) 2015 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.afwsamples.testdpc.common;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;

import java.util.List;

import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;

/**
 * Common utility functions used for retrieving information about the provisioning state of the
 * device.
 */
public class ProvisioningStateUtil {
    private ProvisioningStateUtil() {}

    /**
     * @return true if the device is not provisioned (setup wizard hasn't completed) and no device
     *         owner is setup
     */
    public static boolean isDeviceUnprovisionedAndNoDeviceOwner(Context context) {
        return !ProvisioningStateUtil.isDeviceProvisioned(context)
                && !ProvisioningStateUtil.isManaged(context);
    }

    /**
     * @returns true if the device is provisioned, and we're past the point where device owner can
     *               be installed
     */
    public static boolean isDeviceProvisioned(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        return Settings.Global.getInt(contentResolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0;
    }

    /**
     * @return true if the device or profile is already owned by TestDPC
     */
    public static boolean isManagedByTestDPC(Context context) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        String packageName = context.getPackageName();

        return devicePolicyManager.isProfileOwnerApp(packageName)
                || devicePolicyManager.isDeviceOwnerApp(packageName);
    }

    /**
     * @return true if the device or profile is already owned
     */
    public static boolean isManaged(Context context) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
                Context.DEVICE_POLICY_SERVICE);

        List<ComponentName> admins = devicePolicyManager.getActiveAdmins();
        if (admins == null) return false;
        for (ComponentName admin : admins) {
            String adminPackageName = admin.getPackageName();
            if (devicePolicyManager.isDeviceOwnerApp(adminPackageName)
                    || devicePolicyManager.isProfileOwnerApp(adminPackageName)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param action Action to be checked
     * @param context Calling activity's context
     * @return true, if provisioning is allowed for corresponding action
     */
    public static boolean isProvisioningAllowed(Context context, String action) {
        // DevicePolicyManager.isProvisioningAllowed is added in N.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            DevicePolicyManager dpm = (DevicePolicyManager) context
                    .getSystemService(Context.DEVICE_POLICY_SERVICE);
            return dpm.isProvisioningAllowed(action);
        }
        if (ACTION_PROVISION_MANAGED_DEVICE.equals(action)) {
            return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                    ? isDeviceUnprovisionedAndNoDeviceOwner(context) : false;
        }
        if (ACTION_PROVISION_MANAGED_PROFILE.equals(action)) {
            return true;
        }
        return false;
    }
}

@@ -3529,66 +3529,79 @@
    /
-/ DevicePolicyManager.isProvisioningAllowed is added in N.
+* TODO: Remove CODENAME check once SDK_INT on device is bumped for N */
 %0A   
@@ -3623,33 +3623,33 @@
 VERSION.SDK_INT 
-%3E
+%3C
 = Build.VERSION_
@@ -3658,215 +3658,76 @@
 DES.
-N) %7B%0A            DevicePolicyManager dpm = (DevicePolicyManager) context%0A                    .getSystemService(Context.DEVICE_POLICY_SERVICE);%0A            return dpm.isProvisioningAllowed(action);%0A        %7D%0A
+M%0A                && (!(%22N%22.equals(Build.VERSION.CODENAME)))) %7B%0A    
     
@@ -3792,24 +3792,28 @@
             
+    
 return (Buil
@@ -3830,17 +3830,17 @@
 SDK_INT 
-%3E
+=
 = Build.
@@ -3856,16 +3856,20 @@
 ODES.M)%0A
+    
         
@@ -3938,34 +3938,42 @@
  false;%0A        
-%7D%0A
+    %7D%0A    
         if (ACTI
@@ -4023,32 +4023,36 @@
 ) %7B%0A            
+    
 return true;%0A   
@@ -4048,32 +4048,36 @@
 n true;%0A        
+    
 %7D%0A        return
@@ -4062,32 +4062,36 @@
       %7D%0A        
+ 
+   
 return false;%0A  
@@ -4084,20 +4084,278 @@
 turn false;%0A
+        %7D%0A        // DevicePolicyManager.isProvisioningAllowed is added in N.%0A        DevicePolicyManager dpm = (DevicePolicyManager) context%0A                .getSystemService(Context.DEVICE_POLICY_SERVICE);%0A        return dpm.isProvisioningAllowed(action);%0A
     %7D%0A%7D%0A

Use codename check until VERSION.SDK_INT is updated.