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.base; 18 19 import android.app.Service; 20 import android.graphics.Bitmap; 21 import android.graphics.Bitmap.CompressFormat; 22 import android.os.PowerManager; 23 import android.util.Log; 24 import android.view.KeyEvent; 25 26 import java.io.BufferedOutputStream; 27 28 import io.appium.droiddriver.UiDevice; 29 import io.appium.droiddriver.actions.Action; 30 import io.appium.droiddriver.actions.SingleKeyAction; 31 import io.appium.droiddriver.util.FileUtils; 32 import io.appium.droiddriver.util.Logs; 33 34 /** 35 * Base implementation of {@link UiDevice}. 36 */ 37 public abstract class BaseUiDevice implements UiDevice { 38 // power off may not trigger new events 39 private static final SingleKeyAction POWER_OFF = new SingleKeyAction(KeyEvent.KEYCODE_POWER, 40 0/* metaState */, 0/* timeoutMillis */, false); 41 // power on should always trigger new events 42 private static final SingleKeyAction POWER_ON = new SingleKeyAction(KeyEvent.KEYCODE_POWER, 43 0/* metaState */, 1000L/* timeoutMillis */, false); 44 45 @SuppressWarnings("deprecation") 46 @Override 47 public boolean isScreenOn() { 48 PowerManager pm = 49 (PowerManager) getContext().getInstrumentation().getTargetContext() 50 .getSystemService(Service.POWER_SERVICE); 51 return pm.isScreenOn(); 52 } 53 54 @Override 55 public void wakeUp() { 56 if (!isScreenOn()) { 57 perform(POWER_ON); 58 } 59 } 60 61 @Override 62 public void sleep() { 63 if (isScreenOn()) { 64 perform(POWER_OFF); 65 } 66 } 67 68 @Override 69 public void pressBack() { 70 perform(SingleKeyAction.BACK); 71 } 72 73 @Override 74 public boolean perform(Action action) { 75 return getContext().getDriver().getRootElement().perform(action); 76 } 77 78 @Override 79 public boolean takeScreenshot(String path) { 80 return takeScreenshot(path, Bitmap.CompressFormat.PNG, 0); 81 } 82 83 @Override 84 public boolean takeScreenshot(String path, CompressFormat format, int quality) { 85 Logs.call(this, "takeScreenshot", path, quality); 86 Bitmap screenshot = takeScreenshot(); 87 if (screenshot == null) { 88 return false; 89 } 90 BufferedOutputStream bos = null; 91 try { 92 bos = FileUtils.open(path); 93 screenshot.compress(format, quality, bos); 94 return true; 95 } catch (Exception e) { 96 Logs.log(Log.WARN, e); 97 return false; 98 } finally { 99 if (bos != null) { 100 try { 101 bos.close(); 102 } catch (Exception e) { 103 // ignore 104 } 105 } 106 screenshot.recycle(); 107 } 108 } 109 110 protected abstract Bitmap takeScreenshot(); 111 112 protected abstract DroidDriverContext<?, ?> getContext(); 113 } 114