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