Home | History | Annotate | Download | only in helpers
      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.system.helpers;
     18 
     19 import android.content.Context;
     20 import android.os.Build;
     21 import android.os.RemoteException;
     22 import android.support.test.InstrumentationRegistry;
     23 import android.support.test.uiautomator.UiDevice;
     24 import android.view.WindowManager;
     25 import android.util.DisplayMetrics;
     26 
     27 /**
     28  * Implement common helper methods for device.
     29  */
     30 public class DeviceHelper {
     31 
     32     public static final String PIXEL_XL = "Pixel XL";
     33     public static final String PIXEL = "Pixel";
     34     public static final String RYU = "Pixel C";
     35     // 600dp is the threshold value for 7-inch tablets.
     36     private static final int TABLET_DP_THRESHOLD = 600;
     37     public static final int LONG_TIMEOUT = 2000;
     38     private static DeviceHelper sInstance = null;
     39     private Context mContext = null;
     40     private UiDevice mDevice = null;
     41 
     42     public DeviceHelper() {
     43         mContext = InstrumentationRegistry.getTargetContext();
     44         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
     45     }
     46 
     47     public static DeviceHelper getInstance() {
     48         if (sInstance == null) {
     49             sInstance = new DeviceHelper();
     50         }
     51         return sInstance;
     52     }
     53 
     54     /** Returns true if the device is a tablet */
     55     public boolean isTablet() {
     56         // Get screen density & screen size from window manager
     57         WindowManager wm = (WindowManager) mContext.getSystemService(
     58                 Context.WINDOW_SERVICE);
     59         DisplayMetrics metrics = new DisplayMetrics();
     60         wm.getDefaultDisplay().getMetrics(metrics);
     61         // Determines the smallest screen width DP which is
     62         // calculated as ( pixels * density - independent pixel unit ) / density.
     63         // http://developer.android.com/guide/practices/screens_support.html.
     64         int screenDensity = metrics.densityDpi;
     65         int screenWidth = Math.min(
     66                 metrics.widthPixels, metrics.heightPixels);
     67         int screenHeight = Math.max(
     68                 metrics.widthPixels, metrics.heightPixels);
     69         int smallestScreenWidthDp = (Math.min(screenWidth, screenHeight)
     70                 * DisplayMetrics.DENSITY_DEFAULT) / screenDensity;
     71         return smallestScreenWidthDp >= TABLET_DP_THRESHOLD;
     72     }
     73 
     74     public boolean isNexusExperienceDevice() {
     75         // Get device model
     76         String result = Build.MODEL;
     77         if (result.trim().equalsIgnoreCase(PIXEL) || result.trim().equalsIgnoreCase(PIXEL_XL)) {
     78             return true;
     79         }
     80         return false;
     81     }
     82 
     83     public boolean isRyuDevice() {
     84         return Build.MODEL.trim().equalsIgnoreCase(RYU);
     85     }
     86 
     87     /**
     88      * Device sleep and wake up
     89      * @throws RemoteException, InterruptedException
     90      */
     91     public void sleepAndWakeUpDevice() throws RemoteException, InterruptedException {
     92         mDevice.sleep();
     93         Thread.sleep(LONG_TIMEOUT);
     94         mDevice.wakeUp();
     95     }
     96 }
     97