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.graphics.Bitmap; 21 import android.os.Environment; 22 import android.util.Log; 23 import android.view.View; 24 25 import androidx.test.platform.app.InstrumentationRegistry; 26 27 import java.io.BufferedReader; 28 import java.io.File; 29 import java.io.FileOutputStream; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.net.HttpURLConnection; 33 import java.net.URL; 34 35 import javax.net.ssl.HttpsURLConnection; 36 37 public class CommonUtils { 38 private static final String TAG = CommonUtils.class.getSimpleName(); 39 takeScreenshot(Activity activity)40 public static void takeScreenshot(Activity activity) { 41 long now = System.currentTimeMillis(); 42 43 try { 44 // image naming and path to include sd card appending name you choose for file 45 String mPath = 46 Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 47 Log.d(TAG, "screenshot path is " + mPath); 48 49 // create bitmap screen capture 50 View v1 = activity.getWindow().getDecorView().getRootView(); 51 v1.setDrawingCacheEnabled(true); 52 Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 53 v1.setDrawingCacheEnabled(false); 54 55 File imageFile = new File(mPath); 56 57 FileOutputStream outputStream = new FileOutputStream(imageFile); 58 int quality = 100; 59 bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 60 outputStream.flush(); 61 outputStream.close(); 62 } catch (Throwable e) { 63 // Several error may come out with file handling or DOM 64 e.printStackTrace(); 65 } 66 } 67 connectToURL(URL url)68 public static boolean connectToURL(URL url) { 69 HttpURLConnection connection = null; 70 try { 71 connection = (HttpsURLConnection) url.openConnection(); 72 connection.setRequestMethod("GET"); 73 connection.setConnectTimeout(8000); 74 connection.setReadTimeout(8000); 75 connection.connect(); 76 if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) { 77 InputStream in = connection.getInputStream(); 78 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 79 StringBuilder response = new StringBuilder(); 80 String line; 81 while (null != (line = reader.readLine())) { 82 response.append(line); 83 } 84 return true; 85 } 86 } catch (Exception e) { 87 Log.d(TAG, e.getMessage()); 88 return false; 89 } finally { 90 if (null != connection) { 91 connection.disconnect(); 92 } 93 } 94 95 return false; 96 } 97 98 /** 99 * Return a resource identifier for the given resource name in Settings app. 100 * 101 * @param name The name of the desired resource. 102 * @return int The associated resource identifier. Returns 0 if no such resource was found. (0 103 * is not a valid resource ID.) 104 */ getResId(String name)105 public static int getResId(String name) { 106 return InstrumentationRegistry.getInstrumentation().getTargetContext().getResources() 107 .getIdentifier(name, "id", Constants.SETTINGS_PACKAGE_NAME); 108 } 109 } 110