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
17package io.appium.droiddriver.uiautomation;
18
19import android.annotation.TargetApi;
20import android.app.UiAutomation;
21import android.graphics.Bitmap;
22import android.util.Log;
23
24import io.appium.droiddriver.base.BaseUiDevice;
25import io.appium.droiddriver.exceptions.UnrecoverableException;
26import io.appium.droiddriver.uiautomation.UiAutomationContext.UiAutomationCallable;
27import io.appium.droiddriver.util.Logs;
28
29@TargetApi(18)
30class UiAutomationUiDevice extends BaseUiDevice {
31  private final UiAutomationContext context;
32
33  UiAutomationUiDevice(UiAutomationContext context) {
34    this.context = context;
35  }
36
37  @Override
38  protected Bitmap takeScreenshot() {
39    try {
40      return context.callUiAutomation(new UiAutomationCallable<Bitmap>() {
41        @Override
42        public Bitmap call(UiAutomation uiAutomation) {
43          return uiAutomation.takeScreenshot();
44        }
45      });
46    } catch (UnrecoverableException e) {
47      throw e;
48    } catch (Throwable e) {
49      Logs.log(Log.ERROR, e);
50      return null;
51    }
52  }
53
54  @Override
55  protected UiAutomationContext getContext() {
56    return context;
57  }
58}
59