Home | History | Annotate | Download | only in uiautomation
      1 /*
      2  * Copyright (C) 2013 DroidDriver committers
      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 io.appium.droiddriver.uiautomation;
     18 
     19 import android.annotation.TargetApi;
     20 import android.app.Instrumentation;
     21 import android.app.UiAutomation;
     22 import android.view.accessibility.AccessibilityNodeInfo;
     23 
     24 import io.appium.droiddriver.base.DroidDriverContext;
     25 import io.appium.droiddriver.exceptions.UnrecoverableException;
     26 
     27 @TargetApi(18)
     28 public class UiAutomationContext extends
     29     DroidDriverContext<AccessibilityNodeInfo, UiAutomationElement> {
     30   private final UiAutomation uiAutomation;
     31 
     32   public UiAutomationContext(Instrumentation instrumentation, UiAutomationDriver driver) {
     33     super(instrumentation, driver);
     34     this.uiAutomation = instrumentation.getUiAutomation();
     35   }
     36 
     37   @Override
     38   public UiAutomationDriver getDriver() {
     39     return (UiAutomationDriver) super.getDriver();
     40   }
     41 
     42   public interface UiAutomationCallable<T> {
     43     T call(UiAutomation uiAutomation);
     44   }
     45 
     46   /**
     47    * Wraps calls to UiAutomation API. Currently supports fail-fast if
     48    * UiAutomation throws IllegalStateException, which occurs when the connection
     49    * to UiAutomation service is lost.
     50    */
     51   public <T> T callUiAutomation(UiAutomationCallable<T> uiAutomationCallable) {
     52     try {
     53       return uiAutomationCallable.call(uiAutomation);
     54     } catch (IllegalStateException e) {
     55       throw new UnrecoverableException(e);
     56     }
     57   }
     58 }
     59