1 /* 2 * Copyright (C) 2023 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 package com.android.cuttlefish.displayhotplughelper; 17 18 import android.app.Activity; 19 import android.content.Intent; 20 import android.hardware.display.DisplayManager; 21 import android.os.Bundle; 22 import android.util.Log; 23 import android.view.Display; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.HashMap; 28 import java.util.Iterator; 29 import java.util.List; 30 31 import org.json.JSONArray; 32 import org.json.JSONException; 33 import org.json.JSONObject; 34 35 /** 36 * Helper application to print display information to logcat in predictable 37 * format for CuttlefishDisplayHotplugTests. 38 */ 39 public class DisplayHotplugHelperApp extends Activity { 40 41 private static final String TAG = "DisplayHotplugHelper"; 42 43 private static final String HELPER_APP_UUID_FLAG = "display_hotplug_uuid"; 44 getDisplayInfo(Display display)45 private JSONObject getDisplayInfo(Display display) throws JSONException { 46 // Cuttlefish displays only have a single mode using the max resolution. 47 final Display.Mode displayMode = display.getMode(); 48 49 JSONObject displayInfo = new JSONObject(); 50 displayInfo.put("id", display.getDisplayId()); 51 displayInfo.put("width", displayMode.getPhysicalWidth()); 52 displayInfo.put("height", displayMode.getPhysicalHeight()); 53 return displayInfo; 54 } 55 logDisplayInfos(String loggingIdentifier)56 private void logDisplayInfos(String loggingIdentifier) { 57 Display[] displays = getSystemService(DisplayManager.class).getDisplays(); 58 try { 59 JSONArray displayInfos = new JSONArray(); 60 for (Display display : displays) { 61 displayInfos.put(getDisplayInfo(display)); 62 } 63 JSONObject displayInfo = new JSONObject(); 64 displayInfo.put("displays", displayInfos); 65 66 Log.e(TAG, loggingIdentifier + " displays: " + displayInfo); 67 } catch (JSONException e) { 68 Log.e(TAG, "Failed to create display info JSON: " + e); 69 } 70 } 71 72 @Override onCreate(Bundle savedInstanceState)73 public void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 Log.e(TAG, "onCreate() called"); 76 String loggingIdentifier = getIntent().getExtras().getString(HELPER_APP_UUID_FLAG); 77 logDisplayInfos(loggingIdentifier); 78 } 79 80 @Override onNewIntent(Intent intent)81 public void onNewIntent(Intent intent) { 82 super.onNewIntent(intent); 83 String loggingIdentifier = intent.getExtras().getString(HELPER_APP_UUID_FLAG); 84 Log.e(TAG, "onNewIntent() called with uuid " + loggingIdentifier); 85 logDisplayInfos(loggingIdentifier); 86 } 87 88 @Override onResume()89 public void onResume() { 90 super.onResume(); 91 Log.e(TAG, "onResume() called"); 92 } 93 } 94