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