1 /* 2 * Copyright (C) 2016 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 package com.android.cts.deviceandprofileowner; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.ComponentName; 20 import android.content.Context; 21 22 import junit.framework.Assert; 23 24 import java.util.function.BooleanSupplier; 25 26 public class Utils { 27 private static final long WAIT_SAMPLE_INTERVAL_MILLIS = 200; 28 Utils()29 private Utils() { 30 } 31 removeActiveAdmin(Context context, ComponentName cn)32 public static void removeActiveAdmin(Context context, ComponentName cn) throws Exception { 33 final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 34 if (dpm.isAdminActive(cn)) { 35 dpm.removeActiveAdmin(cn); 36 assertNotActiveAdmin(context, cn); 37 } 38 } 39 assertNotActiveAdmin(Context context, ComponentName cn)40 public static void assertNotActiveAdmin(Context context, ComponentName cn) throws Exception { 41 final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 42 43 for (int i = 0; i < 1000 && dpm.isAdminActive(cn); i++) { 44 Thread.sleep(100); 45 } 46 Assert.assertFalse(dpm.isAdminActive(cn)); 47 } 48 tryWaitForSuccess(BooleanSupplier successCondition, long timeoutMillis)49 public static void tryWaitForSuccess(BooleanSupplier successCondition, long timeoutMillis) 50 throws Exception { 51 long epoch = System.currentTimeMillis(); 52 while (System.currentTimeMillis() - epoch <= timeoutMillis) { 53 Thread.sleep(WAIT_SAMPLE_INTERVAL_MILLIS); 54 if (successCondition.getAsBoolean()) { 55 return; 56 } 57 } 58 } 59 60 } 61