1 /* 2 * Copyright (C) 2021 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 com.android.settings.testutils; 18 19 import android.app.Activity; 20 import android.app.Instrumentation; 21 import android.content.Context; 22 import android.graphics.Bitmap; 23 import android.net.wifi.WifiManager; 24 import android.os.Environment; 25 import android.os.PowerManager; 26 import android.os.StrictMode; 27 import android.os.SystemClock; 28 import android.util.Log; 29 import android.view.View; 30 31 import androidx.test.platform.app.InstrumentationRegistry; 32 33 import java.io.BufferedReader; 34 import java.io.File; 35 import java.io.FileOutputStream; 36 import java.io.InputStream; 37 import java.io.InputStreamReader; 38 import java.net.HttpURLConnection; 39 import java.net.URL; 40 41 import javax.net.ssl.HttpsURLConnection; 42 43 public class CommonUtils { 44 private static final String TAG = CommonUtils.class.getSimpleName(); 45 private static final Instrumentation sInstrumentation = 46 InstrumentationRegistry.getInstrumentation(); 47 private static final WifiManager sWifiManager = 48 (WifiManager) sInstrumentation.getTargetContext().getSystemService( 49 Context.WIFI_SERVICE); 50 private static final PowerManager sPowerManager = 51 (PowerManager) sInstrumentation.getTargetContext().getSystemService( 52 Context.POWER_SERVICE); 53 takeScreenshot(Activity activity)54 public static void takeScreenshot(Activity activity) { 55 long now = System.currentTimeMillis(); 56 57 try { 58 // image naming and path to include sd card appending name you choose for file 59 String mPath = 60 Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 61 Log.d(TAG, "screenshot path is " + mPath); 62 63 // create bitmap screen capture 64 View v1 = activity.getWindow().getDecorView().getRootView(); 65 v1.setDrawingCacheEnabled(true); 66 Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 67 v1.setDrawingCacheEnabled(false); 68 69 File imageFile = new File(mPath); 70 71 FileOutputStream outputStream = new FileOutputStream(imageFile); 72 int quality = 100; 73 bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 74 outputStream.flush(); 75 outputStream.close(); 76 } catch (Throwable e) { 77 // Several error may come out with file handling or DOM 78 e.printStackTrace(); 79 } 80 } 81 connectToURL(URL url)82 public static boolean connectToURL(URL url) { 83 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 84 StrictMode.setThreadPolicy(policy); 85 HttpURLConnection connection = null; 86 try { 87 connection = (HttpsURLConnection) url.openConnection(); 88 connection.setRequestMethod("GET"); 89 connection.setConnectTimeout(8000); 90 connection.setReadTimeout(8000); 91 connection.connect(); 92 if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) { 93 InputStream in = connection.getInputStream(); 94 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 95 StringBuilder response = new StringBuilder(); 96 String line; 97 while (null != (line = reader.readLine())) { 98 response.append(line); 99 } 100 Log.d(TAG, "Connection success! " + response.toString()); 101 return true; 102 } 103 } catch (Exception e) { 104 Log.e(TAG, e.toString()); 105 e.printStackTrace(); 106 return false; 107 } finally { 108 if (null != connection) { 109 connection.disconnect(); 110 } 111 } 112 Log.d(TAG, "End, return false."); 113 return false; 114 } 115 116 /** 117 * Return a resource identifier for the given resource name in Settings app. 118 * 119 * @param name The name of the desired resource. 120 * @return int The associated resource identifier. Returns 0 if no such resource was found. (0 121 * is not a valid resource ID.) 122 */ getResId(String name)123 public static int getResId(String name) { 124 return InstrumentationRegistry.getInstrumentation().getTargetContext().getResources() 125 .getIdentifier(name, "id", Constants.SETTINGS_PACKAGE_NAME); 126 } 127 reopenScreen()128 public static void reopenScreen() { 129 sPowerManager.goToSleep(SystemClock.uptimeMillis()); 130 // According to local test, we need to sleep to wait it fully processed. 131 // 1000 ms is a good value to sleep, otherwise it might cause keyDispatchingTimedOut. 132 try { 133 Thread.sleep(1000); 134 } catch (InterruptedException e) { 135 e.printStackTrace(); 136 } 137 UiUtils.waitUntilCondition(1000, () -> !sPowerManager.isInteractive()); 138 sPowerManager.wakeUp(SystemClock.uptimeMillis()); 139 UiUtils.waitUntilCondition(1000, () -> sPowerManager.isInteractive()); 140 141 // After power on screen, need to unlock and goto home page. 142 sPowerManager.wakeUp(1000, PowerManager.WAKE_REASON_POWER_BUTTON, "Wakeup"); 143 } 144 145 /** 146 * Sets wifi status to given enable / disable via ADB command. 147 */ set_wifi_enabled(boolean enable)148 public static void set_wifi_enabled(boolean enable) { 149 final int timeoutMsec = 10000; 150 Log.d(TAG, "Set wifi status to " + enable); 151 if (sWifiManager.isWifiEnabled() != enable) { 152 AdbUtils.shell("svc wifi " + (enable ? "enable" : "disable")); 153 if (!UiUtils.waitUntilCondition(timeoutMsec, 154 () -> sWifiManager.isWifiEnabled() == enable)) { 155 Log.e(TAG, "Cannot set wifi to " + (enable ? "enabl" : "disable") + ", timeout " 156 + timeoutMsec + " (ms)."); 157 Log.e(TAG, "See logcat for more information."); 158 } 159 Log.d(TAG, "After configuration wifi status = " + sWifiManager.isWifiEnabled()); 160 } else { 161 Log.d(TAG, "Wifi is enable is already " + enable + ", no need to change."); 162 } 163 164 } 165 } 166