Home | History | Annotate | Download | only in cts
      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 
     17 package android.view.accessibility.cts;
     18 
     19 import static com.android.compatibility.common.util.TestUtils.waitOn;
     20 
     21 import android.content.ContentResolver;
     22 import android.content.Context;
     23 import android.provider.Settings;
     24 import android.view.accessibility.AccessibilityManager;
     25 
     26 import java.util.function.BooleanSupplier;
     27 
     28 /**
     29  * Utility methods for enabling and disabling the services used in this package
     30  */
     31 public class ServiceControlUtils {
     32 
     33     public static String getEnabledServices(ContentResolver cr) {
     34         return Settings.Secure.getString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
     35     }
     36 
     37     /**
     38      * Wait for a specified condition that will change with a services state change
     39      *
     40      * @param context A valid context
     41      * @param condition The condition to check
     42      * @param timeoutMs The timeout in millis
     43      * @param conditionName The name to include in the assertion. If null, will be given a default.
     44      */
     45     public static void waitForConditionWithServiceStateChange(Context context,
     46             BooleanSupplier condition, long timeoutMs, String conditionName) {
     47         AccessibilityManager manager =
     48                 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
     49         Object lock = new Object();
     50         AccessibilityManager.AccessibilityServicesStateChangeListener listener = (m) -> {
     51             synchronized (lock) {
     52                 lock.notifyAll();
     53             }
     54         };
     55         manager.addAccessibilityServicesStateChangeListener(listener, null);
     56         try {
     57             waitOn(lock, condition, timeoutMs, conditionName);
     58         } finally {
     59             manager.removeAccessibilityServicesStateChangeListener(listener);
     60         }
     61     }
     62 }
     63