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